@matdata/yasqe 5.16.0 → 5.17.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@matdata/yasqe",
3
3
  "description": "Yet Another SPARQL Query Editor",
4
- "version": "5.16.0",
4
+ "version": "5.17.0",
5
5
  "main": "build/yasqe.min.js",
6
6
  "types": "build/ts/src/index.d.ts",
7
7
  "license": "MIT",
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;
@@ -256,17 +271,22 @@ export async function executeQuery(
256
271
  populatedConfig.url = url.toString();
257
272
  }
258
273
  const request = new Request(populatedConfig.url, fetchOptions);
259
- yasqe.emit("query", request, abortController);
274
+ if (!silent) yasqe.emit("query", request, abortController);
260
275
  const response = await fetch(request);
261
276
 
262
277
  // Await the response content and merge with the `Response` object
278
+ const content = await response.text();
263
279
  const queryResponse = {
264
280
  ok: response.ok,
265
281
  status: response.status,
266
282
  statusText: response.statusText,
267
283
  headers: response.headers,
268
284
  type: response.type,
269
- content: await response.text(),
285
+ content,
286
+ // Compatibility aliases for plugins that expect fetch-like or axios-like response objects.
287
+ data: content,
288
+ json: async () => JSON.parse(content),
289
+ text: async () => content,
270
290
  };
271
291
 
272
292
  if (!response.ok) {
@@ -278,8 +298,10 @@ export async function executeQuery(
278
298
  throw error;
279
299
  }
280
300
 
281
- yasqe.emit("queryResponse", queryResponse, Date.now() - queryStart);
282
- yasqe.emit("queryResults", queryResponse.content, Date.now() - queryStart);
301
+ if (!silent) {
302
+ yasqe.emit("queryResponse", queryResponse, Date.now() - queryStart);
303
+ yasqe.emit("queryResults", queryResponse.content, Date.now() - queryStart);
304
+ }
283
305
  return queryResponse;
284
306
  } catch (e) {
285
307
  if (e instanceof Error && e.message === "Aborted") {
@@ -292,12 +314,12 @@ export async function executeQuery(
292
314
  if (e.message.includes("Failed to fetch") || e.message.includes("NetworkError")) {
293
315
  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
316
  }
295
- yasqe.emit("queryResponse", enhancedError, Date.now() - queryStart);
317
+ if (!silent) yasqe.emit("queryResponse", enhancedError, Date.now() - queryStart);
296
318
  } else {
297
- yasqe.emit("queryResponse", e, Date.now() - queryStart);
319
+ if (!silent) yasqe.emit("queryResponse", e, Date.now() - queryStart);
298
320
  }
299
321
  }
300
- yasqe.emit("error", e);
322
+ if (!silent) yasqe.emit("error", e);
301
323
  throw e;
302
324
  }
303
325
  }