Skip to content
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

fix: support non-configurable responses #2360

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"@bundled-es-modules/statuses": "^1.0.1",
"@bundled-es-modules/tough-cookie": "^0.1.6",
"@inquirer/confirm": "^5.0.0",
"@mswjs/interceptors": "^0.36.5",
"@mswjs/interceptors": "^0.37.0",
"@open-draft/deferred-promise": "^2.2.0",
"@open-draft/until": "^2.1.0",
"@types/cookie": "^0.6.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 14 additions & 19 deletions src/browser/setupWorker/start/createResponseListener.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FetchResponse } from '@mswjs/interceptors'
import type {
ServiceWorkerIncomingEventsMap,
SetupWorkerInternalContext,
} from '../glossary'
import type { ServiceWorkerMessage } from './utils/createMessageChannel'
import { isResponseWithoutBody } from '@mswjs/interceptors'

export function createResponseListener(context: SetupWorkerInternalContext) {
return (
Expand Down Expand Up @@ -35,32 +35,27 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
const response =
responseJson.status === 0
? Response.error()
: new Response(
: new FetchResponse(
/**
* Responses may be streams here, but when we create a response object
* with null-body status codes, like 204, 205, 304 Response will
* throw when passed a non-null body, so ensure it's null here
* for those codes
*/
isResponseWithoutBody(responseJson.status)
? null
: responseJson.body,
responseJson,
FetchResponse.isResponseWithBody(responseJson.status)
? responseJson.body
: null,
{
...responseJson,
/**
* Set response URL if it's not set already.
* @see https://github.com/mswjs/msw/issues/2030
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url
*/
url: request.url,
},
)

/**
* Set response URL if it's not set already.
* @see https://github.com/mswjs/msw/issues/2030
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url
*/
if (!response.url) {
Object.defineProperty(response, 'url', {
value: request.url,
enumerable: true,
writable: false,
})
}

context.emitter.emit(
responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
{
Expand Down
Loading