Skip to content

Commit

Permalink
fix: avoid calling fetch with globalOptions context
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 22, 2021
1 parent 10b4388 commit 8ea2d2b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const retryStatusCodes = new Set([
])

export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
const { fetch, Headers } = globalOptions

function onError (ctx: FetchContext): Promise<FetchResponse<any>> {
// Retry
if (ctx.options.retry !== false) {
Expand Down Expand Up @@ -105,7 +107,7 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
if (ctx.options.body && isPayloadMethod(ctx.options.method)) {
if (isJSONSerializable(ctx.options.body)) {
ctx.options.body = JSON.stringify(ctx.options.body)
ctx.options.headers = new globalOptions.Headers(ctx.options.headers)
ctx.options.headers = new Headers(ctx.options.headers)
if (!ctx.options.headers.has('content-type')) {
ctx.options.headers.set('content-type', 'application/json')
}
Expand All @@ -116,7 +118,7 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
}
}

ctx.response = await globalOptions.fetch(ctx.request, ctx.options as RequestInit).catch(async (error) => {
ctx.response = await fetch(ctx.request, ctx.options as RequestInit).catch(async (error) => {
ctx.error = error
if (ctx.options.onRequestError) {
await ctx.options.onRequestError(ctx as any)
Expand Down

0 comments on commit 8ea2d2b

Please sign in to comment.