@matdata/yasqe 5.16.0 → 5.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/ts/src/sparql.d.ts +2 -0
- package/build/ts/src/sparql.js +29 -10
- package/build/ts/src/sparql.js.map +1 -1
- package/build/yasqe.min.css.map +1 -1
- package/build/yasqe.min.js +44 -44
- package/build/yasqe.min.js.map +3 -3
- package/package.json +1 -1
- package/src/sparql.ts +33 -10
package/package.json
CHANGED
package/src/sparql.ts
CHANGED
|
@@ -174,6 +174,13 @@ export function getAjaxConfig(
|
|
|
174
174
|
export interface ExecuteQueryOptions {
|
|
175
175
|
customQuery?: string;
|
|
176
176
|
customAccept?: string;
|
|
177
|
+
/** Optional external abort signal, useful for plugin-driven background fetches. */
|
|
178
|
+
signal?: AbortSignal;
|
|
179
|
+
/**
|
|
180
|
+
* Execute without emitting Yasqe query lifecycle events.
|
|
181
|
+
* Useful for background/plugin-driven queries that should not update main UI state.
|
|
182
|
+
*/
|
|
183
|
+
silent?: boolean;
|
|
177
184
|
}
|
|
178
185
|
|
|
179
186
|
export async function executeQuery(
|
|
@@ -182,13 +189,21 @@ export async function executeQuery(
|
|
|
182
189
|
options?: ExecuteQueryOptions,
|
|
183
190
|
): Promise<any> {
|
|
184
191
|
const queryStart = Date.now();
|
|
192
|
+
const silent = !!options?.silent;
|
|
185
193
|
try {
|
|
186
|
-
yasqe.emit("queryBefore", yasqe, config);
|
|
194
|
+
if (!silent) yasqe.emit("queryBefore", yasqe, config);
|
|
187
195
|
const populatedConfig = getAjaxConfig(yasqe, config);
|
|
188
196
|
if (!populatedConfig) {
|
|
189
197
|
return; // Nothing to query
|
|
190
198
|
}
|
|
191
199
|
const abortController = new AbortController();
|
|
200
|
+
if (options?.signal) {
|
|
201
|
+
if (options.signal.aborted) {
|
|
202
|
+
abortController.abort();
|
|
203
|
+
} else {
|
|
204
|
+
options.signal.addEventListener("abort", () => abortController.abort(), { once: true });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
192
207
|
|
|
193
208
|
// Use custom accept header if provided, otherwise use the default
|
|
194
209
|
const acceptHeader = options?.customAccept || populatedConfig.accept;
|
|
@@ -236,8 +251,9 @@ export async function executeQuery(
|
|
|
236
251
|
|
|
237
252
|
// Use custom query if provided, otherwise use the args from config
|
|
238
253
|
if (options?.customQuery) {
|
|
239
|
-
|
|
240
|
-
|
|
254
|
+
// A custom query is always a SPARQL read query (CONSTRUCT/DESCRIBE/SELECT/ASK),
|
|
255
|
+
// never an update — always use the "query" parameter regardless of editor mode.
|
|
256
|
+
searchParams.append("query", options.customQuery);
|
|
241
257
|
|
|
242
258
|
// Add other args except the query/update parameter
|
|
243
259
|
appendArgsToParams(populatedConfig.args, ["query", "update"]);
|
|
@@ -256,17 +272,22 @@ export async function executeQuery(
|
|
|
256
272
|
populatedConfig.url = url.toString();
|
|
257
273
|
}
|
|
258
274
|
const request = new Request(populatedConfig.url, fetchOptions);
|
|
259
|
-
yasqe.emit("query", request, abortController);
|
|
275
|
+
if (!silent) yasqe.emit("query", request, abortController);
|
|
260
276
|
const response = await fetch(request);
|
|
261
277
|
|
|
262
278
|
// Await the response content and merge with the `Response` object
|
|
279
|
+
const content = await response.text();
|
|
263
280
|
const queryResponse = {
|
|
264
281
|
ok: response.ok,
|
|
265
282
|
status: response.status,
|
|
266
283
|
statusText: response.statusText,
|
|
267
284
|
headers: response.headers,
|
|
268
285
|
type: response.type,
|
|
269
|
-
content
|
|
286
|
+
content,
|
|
287
|
+
// Compatibility aliases for plugins that expect fetch-like or axios-like response objects.
|
|
288
|
+
data: content,
|
|
289
|
+
json: async () => JSON.parse(content),
|
|
290
|
+
text: async () => content,
|
|
270
291
|
};
|
|
271
292
|
|
|
272
293
|
if (!response.ok) {
|
|
@@ -278,8 +299,10 @@ export async function executeQuery(
|
|
|
278
299
|
throw error;
|
|
279
300
|
}
|
|
280
301
|
|
|
281
|
-
|
|
282
|
-
|
|
302
|
+
if (!silent) {
|
|
303
|
+
yasqe.emit("queryResponse", queryResponse, Date.now() - queryStart);
|
|
304
|
+
yasqe.emit("queryResults", queryResponse.content, Date.now() - queryStart);
|
|
305
|
+
}
|
|
283
306
|
return queryResponse;
|
|
284
307
|
} catch (e) {
|
|
285
308
|
if (e instanceof Error && e.message === "Aborted") {
|
|
@@ -292,12 +315,12 @@ export async function executeQuery(
|
|
|
292
315
|
if (e.message.includes("Failed to fetch") || e.message.includes("NetworkError")) {
|
|
293
316
|
enhancedError.message = `${e.message}. The server may have returned an error response (check browser dev tools), but CORS headers are preventing JavaScript from accessing it. Ensure the endpoint returns proper CORS headers even for error responses (Access-Control-Allow-Origin, etc.).`;
|
|
294
317
|
}
|
|
295
|
-
yasqe.emit("queryResponse", enhancedError, Date.now() - queryStart);
|
|
318
|
+
if (!silent) yasqe.emit("queryResponse", enhancedError, Date.now() - queryStart);
|
|
296
319
|
} else {
|
|
297
|
-
yasqe.emit("queryResponse", e, Date.now() - queryStart);
|
|
320
|
+
if (!silent) yasqe.emit("queryResponse", e, Date.now() - queryStart);
|
|
298
321
|
}
|
|
299
322
|
}
|
|
300
|
-
yasqe.emit("error", e);
|
|
323
|
+
if (!silent) yasqe.emit("error", e);
|
|
301
324
|
throw e;
|
|
302
325
|
}
|
|
303
326
|
}
|