Description
Another idea I’ve been thinking about is either making requests auto-abortable, or providing an easier API than the standard AbortController API when it comes to resubmitting requests. Some ideas:
Option A: implicit params match
Requests that hit the same endpoint with the same options will be automatically aborted. This is “magic” and also will increase the project weight by a significant amount by adding in the equality evaluation (which may also be buggy).
const fetch1 = get('/project/{project_id}', {params:{path:{project_id: 'abc'}}});
const fetch2 = get('/project/{project_id}', {params:{path:{project_id: 'xyz'}}});
const fetch3 = get('/project/{project_id}', {params:{path:{project_id: 'xyz'}}});
await fetch1; // returned 'abc' as normal
await fetch2; // silently aborted; `fetch3` returned because it passed identical options
await fetch3; // returned 'xyz' as normal
Option B: explicit ID
This is less magic, will have almost no impact on library weight, and won’t be buggy.
const fetch1 = get('/project/{project_id}', {params:{path:{project_id: 'abc'}}});
const fetch2 = get('/project/{project_id}', {params:{path:{project_id: 'abc'}}, operationId: 'project_abc'});
const fetch3 = get('/project/{project_id}', {params:{path:{project_id: 'abc'}, operationId: 'project_abc'}});
await fetch1; // returned as normal as there’s no `operationId`
await fetch2; // silently aborted; `fetch3` returned
await fetch3; // returned as normal
Note that in both instances, I think it’s only correct that the silently aborted call be replaced with the call that overrode it. Because if it simply erred, then it would cause the implementor to have to deal with an entirely new layer of typechecking (i.e. rather than simply handling data
or error
, they’d now have to always handle data
, error
, or aborted
). There’d be no easy way for this library to match the abort error shape to the API shape, since the API shapes happen statically whereas the abort error happens at runtime (which has no knowledge of any types).
The “don’t add another typechecking layer” restriction is one I feel strongly about, since this library is meant to reduce complexity and boilerplate and not add to it.