-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make requests extendable by adding
requestMiddleware
to provi…
…der options (#1822)
- Loading branch information
Showing
6 changed files
with
184 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/account": minor | ||
--- | ||
|
||
Added `requestMiddleware` to `ProviderOptions` as a way to allow the user the modification of each fetch call's request |
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,37 @@ | ||
import { FUEL_NETWORK_URL, Provider, sleep } from 'fuels'; | ||
|
||
async function fetchSomeExternalCredentials() { | ||
return Promise.resolve('credential'); | ||
} | ||
|
||
/** | ||
* @group node | ||
* @group browser | ||
*/ | ||
describe('Provider', () => { | ||
it('can be given options', async () => { | ||
// #region provider-options | ||
await Provider.create(FUEL_NETWORK_URL, { | ||
fetch: async (url: string, requestInit: RequestInit | undefined) => { | ||
// do something | ||
await sleep(100); | ||
return fetch(url, requestInit); | ||
}, | ||
timeout: 2000, | ||
cacheUtxo: 1500, | ||
requestMiddleware: async (request: RequestInit) => { | ||
const credentials = await fetchSomeExternalCredentials(); | ||
request.headers ??= {}; | ||
(request.headers as Record<string, string>).Authorization = credentials; | ||
|
||
return request; | ||
}, | ||
retryOptions: { | ||
maxRetries: 5, | ||
baseDelay: 100, | ||
backoff: 'exponential', | ||
}, | ||
}); | ||
// #endregion provider-options | ||
}); | ||
}); |
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