Skip to content

Commit

Permalink
add support for abort signal in provided dataProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
slax57 committed Jan 26, 2024
1 parent 199735b commit afab2c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
5 changes: 5 additions & 0 deletions packages/ra-data-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ const buildGraphQLProvider = async (
...query,
fetchPolicy: 'network-only',
...getOptions(otherOptions.query, raFetchMethod, resource),
context: {
fetchOptions: {
signal: params?.signal,
},
},
};

return (
Expand Down
64 changes: 36 additions & 28 deletions packages/ra-data-json-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})),

Expand All @@ -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) => {
Expand All @@ -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) =>
Expand Down
14 changes: 10 additions & 4 deletions packages/ra-data-simple-rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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,
})),

Expand All @@ -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) => {
Expand All @@ -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)) {
Expand Down

0 comments on commit afab2c4

Please sign in to comment.