From afab2c40ae47856d888d0cb113986a0162ee7525 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kaiser Date: Fri, 26 Jan 2024 11:29:15 +0100 Subject: [PATCH] add support for abort signal in provided dataProviders --- packages/ra-data-graphql/src/index.ts | 5 ++ packages/ra-data-json-server/src/index.ts | 64 +++++++++++++---------- packages/ra-data-simple-rest/src/index.ts | 14 +++-- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/packages/ra-data-graphql/src/index.ts b/packages/ra-data-graphql/src/index.ts index 97985d7756b..b20ac8b412c 100644 --- a/packages/ra-data-graphql/src/index.ts +++ b/packages/ra-data-graphql/src/index.ts @@ -185,6 +185,11 @@ const buildGraphQLProvider = async ( ...query, fetchPolicy: 'network-only', ...getOptions(otherOptions.query, raFetchMethod, resource), + context: { + fetchOptions: { + signal: params?.signal, + }, + }, }; return ( diff --git a/packages/ra-data-json-server/src/index.ts b/packages/ra-data-json-server/src/index.ts index 3c88b8ff36e..c8254411620 100644 --- a/packages/ra-data-json-server/src/index.ts +++ b/packages/ra-data-json-server/src/index.ts @@ -46,24 +46,28 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson): DataProvider => ({ }; const url = `${apiUrl}/${resource}?${stringify(query)}`; - return httpClient(url).then(({ headers, json }) => { - if (!headers.has('x-total-count')) { - throw new Error( - 'The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Expose-Headers header?' - ); + return httpClient(url, { signal: params?.signal }).then( + ({ headers, json }) => { + if (!headers.has('x-total-count')) { + throw new Error( + 'The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Expose-Headers header?' + ); + } + return { + data: json, + total: parseInt( + headers.get('x-total-count').split('/').pop(), + 10 + ), + }; } - return { - data: json, - total: parseInt( - headers.get('x-total-count').split('/').pop(), - 10 - ), - }; - }); + ); }, getOne: (resource, params) => - httpClient(`${apiUrl}/${resource}/${params.id}`).then(({ json }) => ({ + httpClient(`${apiUrl}/${resource}/${params.id}`, { + signal: params?.signal, + }).then(({ json }) => ({ data: json, })), @@ -72,7 +76,9 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson): DataProvider => ({ id: params.ids, }; const url = `${apiUrl}/${resource}?${stringify(query)}`; - return httpClient(url).then(({ json }) => ({ data: json })); + return httpClient(url, { signal: params?.signal }).then(({ json }) => ({ + data: json, + })); }, getManyReference: (resource, params) => { @@ -88,20 +94,22 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson): DataProvider => ({ }; const url = `${apiUrl}/${resource}?${stringify(query)}`; - return httpClient(url).then(({ headers, json }) => { - if (!headers.has('x-total-count')) { - throw new Error( - 'The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Expose-Headers header?' - ); + return httpClient(url, { signal: params?.signal }).then( + ({ headers, json }) => { + if (!headers.has('x-total-count')) { + throw new Error( + 'The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Expose-Headers header?' + ); + } + return { + data: json, + total: parseInt( + headers.get('x-total-count').split('/').pop(), + 10 + ), + }; } - return { - data: json, - total: parseInt( - headers.get('x-total-count').split('/').pop(), - 10 - ), - }; - }); + ); }, update: (resource, params) => diff --git a/packages/ra-data-simple-rest/src/index.ts b/packages/ra-data-simple-rest/src/index.ts index 6d337fcafba..28ea325af8f 100644 --- a/packages/ra-data-simple-rest/src/index.ts +++ b/packages/ra-data-simple-rest/src/index.ts @@ -58,8 +58,9 @@ export default ( headers: new Headers({ Range: `${resource}=${rangeStart}-${rangeEnd}`, }), + signal: params?.signal, } - : {}; + : { signal: params?.signal }; return httpClient(url, options).then(({ headers, json }) => { if (!headers.has(countHeader)) { @@ -81,7 +82,9 @@ export default ( }, getOne: (resource, params) => - httpClient(`${apiUrl}/${resource}/${params.id}`).then(({ json }) => ({ + httpClient(`${apiUrl}/${resource}/${params.id}`, { + signal: params?.signal, + }).then(({ json }) => ({ data: json, })), @@ -90,7 +93,9 @@ export default ( filter: JSON.stringify({ id: params.ids }), }; const url = `${apiUrl}/${resource}?${stringify(query)}`; - return httpClient(url).then(({ json }) => ({ data: json })); + return httpClient(url, { signal: params?.signal }).then(({ json }) => ({ + data: json, + })); }, getManyReference: (resource, params) => { @@ -116,8 +121,9 @@ export default ( headers: new Headers({ Range: `${resource}=${rangeStart}-${rangeEnd}`, }), + signal: params?.signal, } - : {}; + : { signal: params?.signal }; return httpClient(url, options).then(({ headers, json }) => { if (!headers.has(countHeader)) {