Skip to content

Commit

Permalink
fix(fetch): ensure response url
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 15, 2024
1 parent ee76bae commit 37de1ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/interceptors/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
const response =
decompressedStream === null
? rawResponse
: new FetchResponse(decompressedStream, {
...rawResponse,
url: request.url,
})
: new FetchResponse(decompressedStream, rawResponse)

FetchResponse.setUrl(request.url, response)

/**
* Undici's handling of following redirect responses.
Expand Down
26 changes: 18 additions & 8 deletions src/utils/fetchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ export class FetchResponse extends Response {
return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status)
}

static setUrl(url: string | undefined, response: Response): void {
if (!url) {
return
}

if (response.url != null) {
return
}

Object.defineProperty(response, 'url', {
value: url,
enumerable: true,
configurable: true,
writable: false,
})
}

constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {
const status = init.status ?? 200
const safeStatus = FetchResponse.isConfigurableStatusCode(status)
Expand Down Expand Up @@ -60,13 +77,6 @@ export class FetchResponse extends Response {
}
}

if (init.url) {
Object.defineProperty(this, 'url', {
value: init.url,
enumerable: true,
configurable: true,
writable: false,
})
}
FetchResponse.setUrl(init.url, this)
}
}

0 comments on commit 37de1ed

Please sign in to comment.