@@ -174,6 +174,13 @@ export function getAjaxConfig(
174174export interface ExecuteQueryOptions {
175175 customQuery ?: string ;
176176 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 ;
177184}
178185
179186export async function executeQuery (
@@ -182,13 +189,21 @@ export async function executeQuery(
182189 options ?: ExecuteQueryOptions ,
183190) : Promise < any > {
184191 const queryStart = Date . now ( ) ;
192+ const silent = ! ! options ?. silent ;
185193 try {
186- yasqe . emit ( "queryBefore" , yasqe , config ) ;
194+ if ( ! silent ) yasqe . emit ( "queryBefore" , yasqe , config ) ;
187195 const populatedConfig = getAjaxConfig ( yasqe , config ) ;
188196 if ( ! populatedConfig ) {
189197 return ; // Nothing to query
190198 }
191199 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+ }
192207
193208 // Use custom accept header if provided, otherwise use the default
194209 const acceptHeader = options ?. customAccept || populatedConfig . accept ;
@@ -256,17 +271,22 @@ export async function executeQuery(
256271 populatedConfig . url = url . toString ( ) ;
257272 }
258273 const request = new Request ( populatedConfig . url , fetchOptions ) ;
259- yasqe . emit ( "query" , request , abortController ) ;
274+ if ( ! silent ) yasqe . emit ( "query" , request , abortController ) ;
260275 const response = await fetch ( request ) ;
261276
262277 // Await the response content and merge with the `Response` object
278+ const content = await response . text ( ) ;
263279 const queryResponse = {
264280 ok : response . ok ,
265281 status : response . status ,
266282 statusText : response . statusText ,
267283 headers : response . headers ,
268284 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 ,
270290 } ;
271291
272292 if ( ! response . ok ) {
@@ -278,8 +298,10 @@ export async function executeQuery(
278298 throw error ;
279299 }
280300
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+ }
283305 return queryResponse ;
284306 } catch ( e ) {
285307 if ( e instanceof Error && e . message === "Aborted" ) {
@@ -292,12 +314,12 @@ export async function executeQuery(
292314 if ( e . message . includes ( "Failed to fetch" ) || e . message . includes ( "NetworkError" ) ) {
293315 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.).` ;
294316 }
295- yasqe . emit ( "queryResponse" , enhancedError , Date . now ( ) - queryStart ) ;
317+ if ( ! silent ) yasqe . emit ( "queryResponse" , enhancedError , Date . now ( ) - queryStart ) ;
296318 } else {
297- yasqe . emit ( "queryResponse" , e , Date . now ( ) - queryStart ) ;
319+ if ( ! silent ) yasqe . emit ( "queryResponse" , e , Date . now ( ) - queryStart ) ;
298320 }
299321 }
300- yasqe . emit ( "error" , e ) ;
322+ if ( ! silent ) yasqe . emit ( "error" , e ) ;
301323 throw e ;
302324 }
303325}
0 commit comments