-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental customize fetch option
closes #94
- Loading branch information
Showing
14 changed files
with
393 additions
and
60 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Variable: experimentalCustomFetch | ||
|
||
[💗 Help the project](https://github.com/sponsors/panva) | ||
|
||
• `Const` **experimentalCustomFetch**: typeof [`experimentalCustomFetch`](experimentalCustomFetch.md) | ||
|
||
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](../interfaces/HttpRequestOptions.md), 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: | ||
|
||
- Request/Response tracing and logging | ||
- Custom caching strategies for responses of Authorization Server Metadata and JSON Web Key Set | ||
(JWKS) endpoints | ||
- Changing the Request properties like headers, body, credentials, mode before it is passed | ||
to fetch | ||
|
||
Known caveats: | ||
|
||
- Expect Type-related issues when passing the inputs through to fetch-like modules, they hardly | ||
ever get their typings inline with actual fetch, you should `@ts-expect-error` them. | ||
- Returning self-constructed Response instances prohibits AS-signalled DPoP Nonce caching. | ||
|
||
**`Example`** | ||
|
||
Using [sindresorhus/ky](https://github.com/sindresorhus/ky) hooks feature for logging outgoing | ||
requests and their responses. | ||
|
||
```js | ||
import ky from 'ky' | ||
import { experimentalCustomFetch } from 'oauth4webapi' | ||
|
||
// example use | ||
await discoveryRequest(new URL('https://as.example.com'), { | ||
[experimentalCustomFetch]: (...args) => | ||
ky(args[0], { | ||
...args[1], | ||
hooks: { | ||
beforeRequest: [ | ||
(request) => { | ||
logRequest(request) | ||
}, | ||
], | ||
beforeRetry: [ | ||
({ request, error, retryCount }) => { | ||
logRetry(request, error, retryCount) | ||
}, | ||
], | ||
afterResponse: [ | ||
(request, _, response) => { | ||
logResponse(request, response) | ||
}, | ||
], | ||
}, | ||
}), | ||
}) | ||
``` | ||
|
||
**`Example`** | ||
|
||
Using [nodejs/undici](https://github.com/nodejs/undici) for mocking. | ||
|
||
```js | ||
import * as undici from 'undici' | ||
import { discoveryRequest, experimentalCustomFetch } from 'oauth4webapi' | ||
|
||
const mockAgent = new undici.MockAgent() | ||
mockAgent.disableNetConnect() | ||
undici.setGlobalDispatcher(mockAgent) | ||
|
||
// continue as per undici documentation | ||
// https://github.com/nodejs/undici/blob/v6.2.1/docs/api/MockAgent.md#example---basic-mocked-request | ||
|
||
// example use | ||
await discoveryRequest(new URL('https://as.example.com'), { | ||
// @ts-expect-error | ||
[experimentalCustomFetch]: undici.fetch, | ||
}) | ||
``` |
Oops, something went wrong.