Skip to content

Commit

Permalink
Add support for fetch cancellation with abort signal
Browse files Browse the repository at this point in the history
  • Loading branch information
kueda committed May 23, 2024
1 parent 2467869 commit 8380051
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 10 additions & 5 deletions build/inaturalistjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ var iNaturalistAPI = /*#__PURE__*/function () {
headers: headers,
body: JSON.stringify(_objectSpread(_objectSpread({}, params), {}, {
fields: fieldsObject
}))
})),
signal: options.signal
});
} else {
fetch = localFetch(urlWithQueryParams, {
headers: headers
headers: headers,
signal: options.signal
});
}
return fetch.then(iNaturalistAPI.thenText).then(iNaturalistAPI.thenJson).then(iNaturalistAPI.thenWrap);
Expand Down Expand Up @@ -191,11 +193,13 @@ var iNaturalistAPI = /*#__PURE__*/function () {
headers: headers,
body: JSON.stringify(_objectSpread(_objectSpread({}, remainingParams), {}, {
fields: fieldsObject
}))
})),
signal: options.signal
});
} else {
fetch = localFetch(urlWithQueryParams, {
headers: headers
headers: headers,
signal: options.signal
});
}
return fetch.then(iNaturalistAPI.thenText).then(iNaturalistAPI.thenJson).then(iNaturalistAPI.thenWrap);
Expand Down Expand Up @@ -265,7 +269,8 @@ var iNaturalistAPI = /*#__PURE__*/function () {
var fetchOpts = {
method: options.method || "post",
credentials: options.same_origin ? "same-origin" : undefined,
headers: headers
headers: headers,
signal: options.signal
};
if (options.method !== "head") {
fetchOpts.body = body;
Expand Down
13 changes: 8 additions & 5 deletions lib/inaturalist_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ const iNaturalistAPI = class iNaturalistAPI {
fetch = localFetch( baseURL, {
method: "post",
headers,
body: JSON.stringify( { ...params, fields: fieldsObject } )
body: JSON.stringify( { ...params, fields: fieldsObject } ),
signal: options.signal
} );
} else {
fetch = localFetch( urlWithQueryParams, { headers } );
fetch = localFetch( urlWithQueryParams, { headers, signal: options.signal } );
}
return fetch
.then( iNaturalistAPI.thenText )
Expand Down Expand Up @@ -111,10 +112,11 @@ const iNaturalistAPI = class iNaturalistAPI {
fetch = localFetch( baseURL, {
method: "post",
headers,
body: JSON.stringify( { ...remainingParams, fields: fieldsObject } )
body: JSON.stringify( { ...remainingParams, fields: fieldsObject } ),
signal: options.signal
} );
} else {
fetch = localFetch( urlWithQueryParams, { headers } );
fetch = localFetch( urlWithQueryParams, { headers, signal: options.signal } );
}
return fetch
.then( iNaturalistAPI.thenText )
Expand Down Expand Up @@ -184,7 +186,8 @@ const iNaturalistAPI = class iNaturalistAPI {
const fetchOpts = {
method: ( options.method || "post" ),
credentials: ( options.same_origin ? "same-origin" : undefined ),
headers
headers,
signal: options.signal
};
if ( options.method !== "head" ) {
fetchOpts.body = body;
Expand Down

0 comments on commit 8380051

Please sign in to comment.