-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: experimental customize fetch option #94
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The type of feedback i'm looking for here is How would you use it, what do you mean to achieve and how would your implementation ~roughly look. |
Docs Install
|
panva
force-pushed
the
experimental-custom-fetch
branch
from
January 7, 2024 21:05
b8d96b4
to
31628ec
Compare
Thanks for this. This looks good to me. Below is an example of how I might use this feature: import { discoveryRequest, experimentalCustomFetch } from 'oauth4webapi'
const traceInput = (input: RequestInfo | URL) => {
// ...
}
const traceResponse = (response: Response) => {
// ...
}
const shouldCache = (response: Response): boolean => {
// ...
}
const cachedResponseStillValid = (response: Response): boolean => {
// Not needed for Cloudflare as it's Cache API implementation respects Cache-Control headers
// but may be needed for other environments (browser/deno)
}
const discoveryRequestCache = await caches.open("discovery-request-cache");
const customFetch: typeof fetch = async (input, init) => {
const req = new Request(input, init);
traceInput(req);
const cachedResponse = await discoveryRequestCache.match(req);
if (cachedResponse) {
const stillValid = cachedResponseStillValid(cachedResponse);
if (stillValid) {
traceResponse(cachedResponse);
return cachedResponse;
} else {
discoveryRequestCache.delete(req);
}
}
const response = await fetch(req);
traceResponse(response);
if (shouldCache(response)) {
discoveryRequestCache.put(req, response)
.catch(err => console.log('Caching failed', err))
}
return response;
}
await discoveryRequest(new URL('https://as.example.com'), {
// @ts-expect-error
[experimentalCustomFetch]: customFetch,
}) |
panva
added a commit
that referenced
this pull request
Jan 9, 2024
panva
added a commit
that referenced
this pull request
Jan 9, 2024
panva
added a commit
that referenced
this pull request
Jan 9, 2024
panva
added a commit
that referenced
this pull request
Jan 9, 2024
panva
added a commit
that referenced
this pull request
Jan 10, 2024
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refs #20
Refs #62
Refs #79
Refs #80
This is an experimental feature, it is not subject to semantic versioning rules. Non-backward
compatible changes or removal may occur in any future release.
When configured on an interface that extends HttpRequestOptions, that's every
options
parameter for functions that trigger HTTP Requests, this replaces the use of global fetch.
As a fetch replacement the arguments and expected return are the same as fetch.
In theory any module that claims to be compatible with the Fetch API can be used but your mileage
may vary. No workarounds to allow use of non-conform Responses will be considered.
If you only need to update the Request properties you do not need to use a Fetch API
module, just change what you need and pass it to globalThis.fetch just like this module
would normally do.
Its intended use cases are to enable
(JWKS) endpoints
Known issues:
hardly ever get their typings inline with actual fetch, you should
@ts-expect-error
them.
Example
Using sindresorhus/ky hooks feature for logging outgoing
requests and their responses.
Example
Using nodejs/undici for mocking.