msw 2.1.4 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/lib/browser/index.js +25 -12
  2. package/lib/browser/index.js.map +1 -1
  3. package/lib/browser/index.mjs +25 -12
  4. package/lib/browser/index.mjs.map +1 -1
  5. package/lib/core/{GraphQLHandler-rZ7XkeDz.d.mts → GraphQLHandler-AenIUdwE.d.mts} +1 -1
  6. package/lib/core/{GraphQLHandler-eJejgV5u.d.ts → GraphQLHandler-jOzqbxSK.d.ts} +1 -1
  7. package/lib/core/HttpResponse.d.mts +1 -1
  8. package/lib/core/HttpResponse.d.ts +1 -1
  9. package/lib/core/SetupApi.d.mts +1 -1
  10. package/lib/core/SetupApi.d.ts +1 -1
  11. package/lib/core/getResponse.d.mts +1 -1
  12. package/lib/core/getResponse.d.ts +1 -1
  13. package/lib/core/graphql.d.mts +2 -2
  14. package/lib/core/graphql.d.ts +2 -2
  15. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  16. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  17. package/lib/core/handlers/GraphQLHandler.js +2 -2
  18. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  19. package/lib/core/handlers/GraphQLHandler.mjs +2 -2
  20. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  21. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  22. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  23. package/lib/core/handlers/HttpHandler.js +2 -2
  24. package/lib/core/handlers/HttpHandler.js.map +1 -1
  25. package/lib/core/handlers/HttpHandler.mjs +2 -2
  26. package/lib/core/handlers/HttpHandler.mjs.map +1 -1
  27. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  28. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  29. package/lib/core/http.d.mts +1 -1
  30. package/lib/core/http.d.ts +1 -1
  31. package/lib/core/index.d.mts +2 -2
  32. package/lib/core/index.d.ts +2 -2
  33. package/lib/core/passthrough.d.mts +4 -1
  34. package/lib/core/passthrough.d.ts +4 -1
  35. package/lib/core/passthrough.js.map +1 -1
  36. package/lib/core/passthrough.mjs.map +1 -1
  37. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  38. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  39. package/lib/core/utils/executeHandlers.d.mts +1 -1
  40. package/lib/core/utils/executeHandlers.d.ts +1 -1
  41. package/lib/core/utils/handleRequest.d.mts +1 -1
  42. package/lib/core/utils/handleRequest.d.ts +1 -1
  43. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  44. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  45. package/lib/core/utils/internal/parseGraphQLRequest.js +2 -2
  46. package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
  47. package/lib/core/utils/internal/parseGraphQLRequest.mjs +2 -2
  48. package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
  49. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  50. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  51. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  52. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  53. package/lib/core/utils/request/onUnhandledRequest.js +6 -2
  54. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
  55. package/lib/core/utils/request/onUnhandledRequest.mjs +6 -2
  56. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
  57. package/lib/core/utils/request/toPublicUrl.d.mts +7 -0
  58. package/lib/core/utils/request/toPublicUrl.d.ts +7 -0
  59. package/lib/core/utils/request/{getPublicUrlFromRequest.js → toPublicUrl.js} +9 -9
  60. package/lib/core/utils/request/toPublicUrl.js.map +1 -0
  61. package/lib/core/utils/request/toPublicUrl.mjs +11 -0
  62. package/lib/core/utils/request/toPublicUrl.mjs.map +1 -0
  63. package/lib/iife/index.js +37 -21
  64. package/lib/iife/index.js.map +1 -1
  65. package/lib/mockServiceWorker.js +1 -1
  66. package/package.json +5 -5
  67. package/src/browser/setupWorker/glossary.ts +1 -0
  68. package/src/browser/setupWorker/setupWorker.ts +1 -0
  69. package/src/browser/setupWorker/start/createRequestListener.ts +9 -0
  70. package/src/browser/setupWorker/start/createResponseListener.ts +9 -7
  71. package/src/core/handlers/GraphQLHandler.ts +2 -2
  72. package/src/core/handlers/HttpHandler.ts +2 -2
  73. package/src/core/passthrough.ts +4 -2
  74. package/src/core/utils/internal/parseGraphQLRequest.ts +2 -2
  75. package/src/core/utils/request/onUnhandledRequest.ts +14 -2
  76. package/src/core/utils/request/toPublicUrl.test.ts +18 -0
  77. package/src/core/utils/request/toPublicUrl.ts +15 -0
  78. package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +0 -7
  79. package/lib/core/utils/request/getPublicUrlFromRequest.d.ts +0 -7
  80. package/lib/core/utils/request/getPublicUrlFromRequest.js.map +0 -1
  81. package/lib/core/utils/request/getPublicUrlFromRequest.mjs +0 -11
  82. package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +0 -1
  83. package/src/core/utils/request/getPublicUrlFromRequest.test.ts +0 -26
  84. package/src/core/utils/request/getPublicUrlFromRequest.ts +0 -15
  85. package/lib/core/{RequestHandler-CwjkprZE.d.ts → HttpResponse-_514VQ9z.d.ts} +68 -68
  86. package/lib/core/{RequestHandler-MAVTMsma.d.mts → HttpResponse-wcp03c7-.d.mts} +68 -68
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (2.1.4).
5
+ * Mock Service Worker (2.1.6).
6
6
  * @see https://github.com/mswjs/msw
7
7
  * - Please do NOT modify this file.
8
8
  * - Please do NOT serve this file on production.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.1.4",
3
+ "version": "2.1.6",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "./lib/core/index.js",
6
6
  "module": "./lib/core/index.mjs",
@@ -95,7 +95,7 @@
95
95
  "@bundled-es-modules/cookie": "^2.0.0",
96
96
  "@bundled-es-modules/statuses": "^1.0.1",
97
97
  "@mswjs/cookies": "^1.1.0",
98
- "@mswjs/interceptors": "^0.25.14",
98
+ "@mswjs/interceptors": "^0.25.15",
99
99
  "@open-draft/until": "^2.1.0",
100
100
  "@types/cookie": "^0.6.0",
101
101
  "@types/statuses": "^2.0.4",
@@ -189,10 +189,10 @@
189
189
  "check:exports": "node \"./config/scripts/validate-esm.js\"",
190
190
  "test": "pnpm test:unit && pnpm test:node && pnpm test:browser && pnpm test:native",
191
191
  "test:unit": "vitest",
192
- "test:node": "vitest run --config=./test/node/vitest.config.ts",
193
- "test:native": "vitest run --config=./test/native/vitest.config.ts",
192
+ "test:node": "vitest --config=./test/node/vitest.config.ts",
193
+ "test:native": "vitest --config=./test/native/vitest.config.ts",
194
194
  "test:browser": "playwright test -c ./test/browser/playwright.config.ts",
195
- "test:modules:node": "vitest run --config=./test/modules/node/vitest.config.ts",
195
+ "test:modules:node": "vitest --config=./test/modules/node/vitest.config.ts",
196
196
  "test:modules:browser": "playwright test -c ./test/modules/browser/playwright.config.ts",
197
197
  "test:ts": "ts-node test/typings/run.ts",
198
198
  "release": "release publish",
@@ -103,6 +103,7 @@ export interface SetupWorkerInternalContext {
103
103
  worker: ServiceWorker | null
104
104
  registration: ServiceWorkerRegistration | null
105
105
  requestHandlers: Array<RequestHandler>
106
+ requests: Map<string, Request>
106
107
  emitter: Emitter<LifeCycleEventsMap>
107
108
  keepAliveInterval?: number
108
109
  workerChannel: {
@@ -60,6 +60,7 @@ export class SetupWorkerApi
60
60
  worker: null,
61
61
  registration: null,
62
62
  requestHandlers: this.currentHandlers,
63
+ requests: new Map(),
63
64
  emitter: this.emitter,
64
65
  workerChannel: {
65
66
  on: (eventType, callback) => {
@@ -8,6 +8,7 @@ import {
8
8
  WorkerChannel,
9
9
  } from './utils/createMessageChannel'
10
10
  import { parseWorkerRequest } from '../../utils/parseWorkerRequest'
11
+ import { RequestHandler } from '~/core/handlers/RequestHandler'
11
12
  import { handleRequest } from '~/core/utils/handleRequest'
12
13
  import { RequiredDeep } from '~/core/typeUtils'
13
14
  import { devUtils } from '~/core/utils/internal/devUtils'
@@ -30,6 +31,14 @@ export const createRequestListener = (
30
31
  const request = parseWorkerRequest(message.payload)
31
32
  const requestCloneForLogs = request.clone()
32
33
 
34
+ // Make this the first requets clone before the
35
+ // request resolution pipeline even starts.
36
+ // Store the clone in cache so the first matching
37
+ // request handler would skip the cloning phase.
38
+ const requestClone = request.clone()
39
+ RequestHandler.cache.set(request, requestClone)
40
+ context.requests.set(requestId, requestClone)
41
+
33
42
  try {
34
43
  await handleRequest(
35
44
  request,
@@ -1,8 +1,8 @@
1
- import {
1
+ import type {
2
2
  ServiceWorkerIncomingEventsMap,
3
3
  SetupWorkerInternalContext,
4
4
  } from '../glossary'
5
- import { ServiceWorkerMessage } from './utils/createMessageChannel'
5
+ import type { ServiceWorkerMessage } from './utils/createMessageChannel'
6
6
  import { isResponseWithoutBody } from '@mswjs/interceptors'
7
7
 
8
8
  export function createResponseListener(context: SetupWorkerInternalContext) {
@@ -15,6 +15,12 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
15
15
  ) => {
16
16
  const { payload: responseJson } = message
17
17
 
18
+ // Get the Request instance reference stored in the
19
+ // request listener.
20
+ const { requestId } = responseJson
21
+ const request = context.requests.get(requestId)!
22
+ context.requests.delete(requestId)
23
+
18
24
  /**
19
25
  * CORS requests with `mode: "no-cors"` result in "opaque" responses.
20
26
  * That kind of responses cannot be manipulated in JavaScript due
@@ -46,11 +52,7 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
46
52
  responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
47
53
  {
48
54
  response,
49
- /**
50
- * @todo @fixme In this context, we don't know anything about
51
- * the request.
52
- */
53
- request: null as any,
55
+ request,
54
56
  requestId: responseJson.requestId,
55
57
  },
56
58
  )
@@ -17,7 +17,7 @@ import {
17
17
  parseGraphQLRequest,
18
18
  parseDocumentNode,
19
19
  } from '../utils/internal/parseGraphQLRequest'
20
- import { getPublicUrlFromRequest } from '../utils/request/getPublicUrlFromRequest'
20
+ import { toPublicUrl } from '../utils/request/toPublicUrl'
21
21
  import { devUtils } from '../utils/internal/devUtils'
22
22
  import { getAllRequestCookies } from '../utils/request/getRequestCookies'
23
23
 
@@ -200,7 +200,7 @@ export class GraphQLHandler extends RequestHandler<
200
200
  }
201
201
 
202
202
  if (!args.parsedResult.operationName && this.info.operationType !== 'all') {
203
- const publicUrl = getPublicUrlFromRequest(args.request)
203
+ const publicUrl = toPublicUrl(args.request.url)
204
204
 
205
205
  devUtils.warn(`\
206
206
  Failed to intercept a GraphQL request at "${args.request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
@@ -11,7 +11,7 @@ import {
11
11
  Path,
12
12
  PathParams,
13
13
  } from '../utils/matching/matchRequestUrl'
14
- import { getPublicUrlFromRequest } from '../utils/request/getPublicUrlFromRequest'
14
+ import { toPublicUrl } from '../utils/request/toPublicUrl'
15
15
  import { getAllRequestCookies } from '../utils/request/getRequestCookies'
16
16
  import { cleanUrl, getSearchParams } from '../utils/url/cleanUrl'
17
17
  import {
@@ -147,7 +147,7 @@ export class HttpHandler extends RequestHandler<
147
147
  }
148
148
 
149
149
  async log(args: { request: Request; response: Response }) {
150
- const publicUrl = getPublicUrlFromRequest(args.request)
150
+ const publicUrl = toPublicUrl(args.request.url)
151
151
  const loggedRequest = await serializeRequest(args.request)
152
152
  const loggedResponse = await serializeResponse(args.response)
153
153
  const statusColor = getStatusCodeColor(loggedResponse.status)
@@ -1,3 +1,5 @@
1
+ import type { StrictResponse } from './HttpResponse'
2
+
1
3
  /**
2
4
  * Performs the intercepted request as-is.
3
5
  *
@@ -12,12 +14,12 @@
12
14
  *
13
15
  * @see {@link https://mswjs.io/docs/api/passthrough `passthrough()` API reference}
14
16
  */
15
- export function passthrough(): Response {
17
+ export function passthrough(): StrictResponse<any> {
16
18
  return new Response(null, {
17
19
  status: 302,
18
20
  statusText: 'Passthrough',
19
21
  headers: {
20
22
  'x-msw-intention': 'passthrough',
21
23
  },
22
- })
24
+ }) as StrictResponse<any>
23
25
  }
@@ -5,7 +5,7 @@ import type {
5
5
  } from 'graphql'
6
6
  import { parse } from 'graphql'
7
7
  import type { GraphQLVariables } from '../../handlers/GraphQLHandler'
8
- import { getPublicUrlFromRequest } from '../request/getPublicUrlFromRequest'
8
+ import { toPublicUrl } from '../request/toPublicUrl'
9
9
  import { devUtils } from './devUtils'
10
10
  import { jsonParse } from './jsonParse'
11
11
  import { parseMultipartData } from './parseMultipartData'
@@ -184,7 +184,7 @@ export async function parseGraphQLRequest(
184
184
  const parsedResult = parseQuery(query)
185
185
 
186
186
  if (parsedResult instanceof Error) {
187
- const requestPublicUrl = getPublicUrlFromRequest(request)
187
+ const requestPublicUrl = toPublicUrl(request.url)
188
188
 
189
189
  throw new Error(
190
190
  devUtils.formatMessage(
@@ -1,4 +1,4 @@
1
- import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
1
+ import { toPublicUrl } from './toPublicUrl'
2
2
  import { devUtils } from '../internal/devUtils'
3
3
 
4
4
  export interface UnhandledRequestPrint {
@@ -21,7 +21,9 @@ export async function onUnhandledRequest(
21
21
  request: Request,
22
22
  strategy: UnhandledRequestStrategy = 'warn',
23
23
  ): Promise<void> {
24
- const publicUrl = getPublicUrlFromRequest(request)
24
+ const url = new URL(request.url)
25
+ const publicUrl = toPublicUrl(url)
26
+
25
27
  const unhandledRequestMessage = `intercepted a request without a matching request handler:\n\n \u2022 ${request.method} ${publicUrl}\n\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks`
26
28
 
27
29
  function applyStrategy(strategy: UnhandledRequestStrategy) {
@@ -64,5 +66,15 @@ export async function onUnhandledRequest(
64
66
  return
65
67
  }
66
68
 
69
+ /**
70
+ * @note Ignore "file://" requests.
71
+ * Those often are an implementation detail of modern tooling
72
+ * that fetches modules via HTTP. Developers don't issue those
73
+ * requests and so they mustn't be warned about them.
74
+ */
75
+ if (url.protocol === 'file:') {
76
+ return
77
+ }
78
+
67
79
  applyStrategy(strategy)
68
80
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { toPublicUrl } from './toPublicUrl'
5
+
6
+ test('returns an absolute request URL withouth search params', () => {
7
+ expect(toPublicUrl(new URL('https://test.mswjs.io/path'))).toBe(
8
+ 'https://test.mswjs.io/path',
9
+ )
10
+
11
+ expect(toPublicUrl(new URL('http://localhost/path'))).toBe('/path')
12
+
13
+ expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe('/path')
14
+ })
15
+
16
+ it('returns a relative URL given the request to the same origin', () => {
17
+ expect(toPublicUrl('http://localhost/user')).toBe('/user')
18
+ })
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Returns a relative URL if the given request URL is relative
3
+ * to the current origin. Otherwise returns an absolute URL.
4
+ */
5
+ export function toPublicUrl(url: string | URL): string {
6
+ if (typeof location === 'undefined') {
7
+ return url.toString()
8
+ }
9
+
10
+ const urlInstance = url instanceof URL ? url : new URL(url)
11
+
12
+ return urlInstance.origin === location.origin
13
+ ? urlInstance.pathname
14
+ : urlInstance.origin + urlInstance.pathname
15
+ }
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a relative URL if the given request URL is relative to the current origin.
3
- * Otherwise returns an absolute URL.
4
- */
5
- declare function getPublicUrlFromRequest(request: Request): string;
6
-
7
- export { getPublicUrlFromRequest };
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a relative URL if the given request URL is relative to the current origin.
3
- * Otherwise returns an absolute URL.
4
- */
5
- declare function getPublicUrlFromRequest(request: Request): string;
6
-
7
- export { getPublicUrlFromRequest };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../../src/core/utils/request/getPublicUrlFromRequest.ts"],"sourcesContent":["/**\n * Returns a relative URL if the given request URL is relative to the current origin.\n * Otherwise returns an absolute URL.\n */\nexport function getPublicUrlFromRequest(request: Request): string {\n if (typeof location === 'undefined') {\n return request.url\n }\n\n const url = new URL(request.url)\n\n return url.origin === location.origin\n ? url.pathname\n : url.origin + url.pathname\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,wBAAwB,SAA0B;AAChE,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO,QAAQ;AAAA,EACjB;AAEA,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAE/B,SAAO,IAAI,WAAW,SAAS,SAC3B,IAAI,WACJ,IAAI,SAAS,IAAI;AACvB;","names":[]}
@@ -1,11 +0,0 @@
1
- function getPublicUrlFromRequest(request) {
2
- if (typeof location === "undefined") {
3
- return request.url;
4
- }
5
- const url = new URL(request.url);
6
- return url.origin === location.origin ? url.pathname : url.origin + url.pathname;
7
- }
8
- export {
9
- getPublicUrlFromRequest
10
- };
11
- //# sourceMappingURL=getPublicUrlFromRequest.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../../src/core/utils/request/getPublicUrlFromRequest.ts"],"sourcesContent":["/**\n * Returns a relative URL if the given request URL is relative to the current origin.\n * Otherwise returns an absolute URL.\n */\nexport function getPublicUrlFromRequest(request: Request): string {\n if (typeof location === 'undefined') {\n return request.url\n }\n\n const url = new URL(request.url)\n\n return url.origin === location.origin\n ? url.pathname\n : url.origin + url.pathname\n}\n"],"mappings":"AAIO,SAAS,wBAAwB,SAA0B;AAChE,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO,QAAQ;AAAA,EACjB;AAEA,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAE/B,SAAO,IAAI,WAAW,SAAS,SAC3B,IAAI,WACJ,IAAI,SAAS,IAAI;AACvB;","names":[]}
@@ -1,26 +0,0 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
- import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
5
-
6
- test('returns an absolute request URL withouth search params', () => {
7
- expect(
8
- getPublicUrlFromRequest(new Request(new URL('https://test.mswjs.io/path'))),
9
- ).toBe('https://test.mswjs.io/path')
10
-
11
- expect(
12
- getPublicUrlFromRequest(new Request(new URL('http://localhost/path'))),
13
- ).toBe('/path')
14
-
15
- expect(
16
- getPublicUrlFromRequest(
17
- new Request(new URL('http://localhost/path?foo=bar')),
18
- ),
19
- ).toBe('/path')
20
- })
21
-
22
- it('returns a relative URL given the request to the same origin', () => {
23
- expect(getPublicUrlFromRequest(new Request('http://localhost/user'))).toBe(
24
- '/user',
25
- )
26
- })
@@ -1,15 +0,0 @@
1
- /**
2
- * Returns a relative URL if the given request URL is relative to the current origin.
3
- * Otherwise returns an absolute URL.
4
- */
5
- export function getPublicUrlFromRequest(request: Request): string {
6
- if (typeof location === 'undefined') {
7
- return request.url
8
- }
9
-
10
- const url = new URL(request.url)
11
-
12
- return url.origin === location.origin
13
- ? url.pathname
14
- : url.origin + url.pathname
15
- }
@@ -20,74 +20,6 @@ declare const executeHandlers: <Handlers extends RequestHandler<RequestHandlerDe
20
20
  resolutionContext?: ResponseResolutionContext | undefined;
21
21
  }) => Promise<HandlersExecutionResult | null>;
22
22
 
23
- interface HttpResponseInit extends ResponseInit {
24
- type?: ResponseType;
25
- }
26
- declare const bodyType: unique symbol;
27
- interface StrictRequest<BodyType extends DefaultBodyType> extends Request {
28
- json(): Promise<BodyType>;
29
- }
30
- /**
31
- * Opaque `Response` type that supports strict body type.
32
- */
33
- interface StrictResponse<BodyType extends DefaultBodyType> extends Response {
34
- readonly [bodyType]: BodyType;
35
- }
36
- /**
37
- * A drop-in replacement for the standard `Response` class
38
- * to allow additional features, like mocking the response `Set-Cookie` header.
39
- *
40
- * @example
41
- * new HttpResponse('Hello world', { status: 201 })
42
- * HttpResponse.json({ name: 'John' })
43
- * HttpResponse.formData(form)
44
- *
45
- * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}
46
- */
47
- declare class HttpResponse extends Response {
48
- constructor(body?: BodyInit | null, init?: HttpResponseInit);
49
- /**
50
- * Create a `Response` with a `Content-Type: "text/plain"` body.
51
- * @example
52
- * HttpResponse.text('hello world')
53
- * HttpResponse.text('Error', { status: 500 })
54
- */
55
- static text<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
56
- /**
57
- * Create a `Response` with a `Content-Type: "application/json"` body.
58
- * @example
59
- * HttpResponse.json({ firstName: 'John' })
60
- * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })
61
- */
62
- static json<BodyType extends JsonBodyType>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
63
- /**
64
- * Create a `Response` with a `Content-Type: "application/xml"` body.
65
- * @example
66
- * HttpResponse.xml(`<user name="John" />`)
67
- * HttpResponse.xml(`<article id="abc-123" />`, { status: 201 })
68
- */
69
- static xml<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): Response;
70
- /**
71
- * Create a `Response` with an `ArrayBuffer` body.
72
- * @example
73
- * const buffer = new ArrayBuffer(3)
74
- * const view = new Uint8Array(buffer)
75
- * view.set([1, 2, 3])
76
- *
77
- * HttpResponse.arrayBuffer(buffer)
78
- */
79
- static arrayBuffer(body?: ArrayBuffer, init?: HttpResponseInit): Response;
80
- /**
81
- * Create a `Response` with a `FormData` body.
82
- * @example
83
- * const data = new FormData()
84
- * data.set('name', 'Alice')
85
- *
86
- * HttpResponse.formData(data)
87
- */
88
- static formData(body?: FormData, init?: HttpResponseInit): Response;
89
- }
90
-
91
23
  type DefaultRequestMultipartBody = Record<string, string | File | Array<string | File>>;
92
24
  type DefaultBodyType = Record<string, any> | DefaultRequestMultipartBody | string | number | boolean | null | undefined;
93
25
  type JsonBodyType = Record<string, any> | string | number | boolean | null | undefined;
@@ -186,4 +118,72 @@ declare abstract class RequestHandler<HandlerInfo extends RequestHandlerDefaultI
186
118
  private createExecutionResult;
187
119
  }
188
120
 
121
+ interface HttpResponseInit extends ResponseInit {
122
+ type?: ResponseType;
123
+ }
124
+ declare const bodyType: unique symbol;
125
+ interface StrictRequest<BodyType extends DefaultBodyType> extends Request {
126
+ json(): Promise<BodyType>;
127
+ }
128
+ /**
129
+ * Opaque `Response` type that supports strict body type.
130
+ */
131
+ interface StrictResponse<BodyType extends DefaultBodyType> extends Response {
132
+ readonly [bodyType]: BodyType;
133
+ }
134
+ /**
135
+ * A drop-in replacement for the standard `Response` class
136
+ * to allow additional features, like mocking the response `Set-Cookie` header.
137
+ *
138
+ * @example
139
+ * new HttpResponse('Hello world', { status: 201 })
140
+ * HttpResponse.json({ name: 'John' })
141
+ * HttpResponse.formData(form)
142
+ *
143
+ * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}
144
+ */
145
+ declare class HttpResponse extends Response {
146
+ constructor(body?: BodyInit | null, init?: HttpResponseInit);
147
+ /**
148
+ * Create a `Response` with a `Content-Type: "text/plain"` body.
149
+ * @example
150
+ * HttpResponse.text('hello world')
151
+ * HttpResponse.text('Error', { status: 500 })
152
+ */
153
+ static text<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
154
+ /**
155
+ * Create a `Response` with a `Content-Type: "application/json"` body.
156
+ * @example
157
+ * HttpResponse.json({ firstName: 'John' })
158
+ * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })
159
+ */
160
+ static json<BodyType extends JsonBodyType>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
161
+ /**
162
+ * Create a `Response` with a `Content-Type: "application/xml"` body.
163
+ * @example
164
+ * HttpResponse.xml(`<user name="John" />`)
165
+ * HttpResponse.xml(`<article id="abc-123" />`, { status: 201 })
166
+ */
167
+ static xml<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): Response;
168
+ /**
169
+ * Create a `Response` with an `ArrayBuffer` body.
170
+ * @example
171
+ * const buffer = new ArrayBuffer(3)
172
+ * const view = new Uint8Array(buffer)
173
+ * view.set([1, 2, 3])
174
+ *
175
+ * HttpResponse.arrayBuffer(buffer)
176
+ */
177
+ static arrayBuffer(body?: ArrayBuffer, init?: HttpResponseInit): Response;
178
+ /**
179
+ * Create a `Response` with a `FormData` body.
180
+ * @example
181
+ * const data = new FormData()
182
+ * data.set('name', 'Alice')
183
+ *
184
+ * HttpResponse.formData(data)
185
+ */
186
+ static formData(body?: FormData, init?: HttpResponseInit): Response;
187
+ }
188
+
189
189
  export { type AsyncResponseResolverReturnType as A, type DefaultBodyType as D, type HttpResponseInit as H, type JsonBodyType as J, type MaybeAsyncResponseResolverReturnType as M, RequestHandler as R, type StrictRequest as S, type ResponseResolver as a, type ResponseResolverReturnType as b, type RequestHandlerOptions as c, type DefaultRequestMultipartBody as d, type StrictResponse as e, HttpResponse as f, type RequestHandlerDefaultInfo as g, type HandlersExecutionResult as h, type ResponseResolutionContext as i, executeHandlers as j, type RequestHandlerInternalInfo as k, type ResponseResolverInfo as l, type RequestHandlerArgs as m, type RequestHandlerExecutionResult as n };
@@ -20,74 +20,6 @@ declare const executeHandlers: <Handlers extends RequestHandler<RequestHandlerDe
20
20
  resolutionContext?: ResponseResolutionContext | undefined;
21
21
  }) => Promise<HandlersExecutionResult | null>;
22
22
 
23
- interface HttpResponseInit extends ResponseInit {
24
- type?: ResponseType;
25
- }
26
- declare const bodyType: unique symbol;
27
- interface StrictRequest<BodyType extends DefaultBodyType> extends Request {
28
- json(): Promise<BodyType>;
29
- }
30
- /**
31
- * Opaque `Response` type that supports strict body type.
32
- */
33
- interface StrictResponse<BodyType extends DefaultBodyType> extends Response {
34
- readonly [bodyType]: BodyType;
35
- }
36
- /**
37
- * A drop-in replacement for the standard `Response` class
38
- * to allow additional features, like mocking the response `Set-Cookie` header.
39
- *
40
- * @example
41
- * new HttpResponse('Hello world', { status: 201 })
42
- * HttpResponse.json({ name: 'John' })
43
- * HttpResponse.formData(form)
44
- *
45
- * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}
46
- */
47
- declare class HttpResponse extends Response {
48
- constructor(body?: BodyInit | null, init?: HttpResponseInit);
49
- /**
50
- * Create a `Response` with a `Content-Type: "text/plain"` body.
51
- * @example
52
- * HttpResponse.text('hello world')
53
- * HttpResponse.text('Error', { status: 500 })
54
- */
55
- static text<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
56
- /**
57
- * Create a `Response` with a `Content-Type: "application/json"` body.
58
- * @example
59
- * HttpResponse.json({ firstName: 'John' })
60
- * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })
61
- */
62
- static json<BodyType extends JsonBodyType>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
63
- /**
64
- * Create a `Response` with a `Content-Type: "application/xml"` body.
65
- * @example
66
- * HttpResponse.xml(`<user name="John" />`)
67
- * HttpResponse.xml(`<article id="abc-123" />`, { status: 201 })
68
- */
69
- static xml<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): Response;
70
- /**
71
- * Create a `Response` with an `ArrayBuffer` body.
72
- * @example
73
- * const buffer = new ArrayBuffer(3)
74
- * const view = new Uint8Array(buffer)
75
- * view.set([1, 2, 3])
76
- *
77
- * HttpResponse.arrayBuffer(buffer)
78
- */
79
- static arrayBuffer(body?: ArrayBuffer, init?: HttpResponseInit): Response;
80
- /**
81
- * Create a `Response` with a `FormData` body.
82
- * @example
83
- * const data = new FormData()
84
- * data.set('name', 'Alice')
85
- *
86
- * HttpResponse.formData(data)
87
- */
88
- static formData(body?: FormData, init?: HttpResponseInit): Response;
89
- }
90
-
91
23
  type DefaultRequestMultipartBody = Record<string, string | File | Array<string | File>>;
92
24
  type DefaultBodyType = Record<string, any> | DefaultRequestMultipartBody | string | number | boolean | null | undefined;
93
25
  type JsonBodyType = Record<string, any> | string | number | boolean | null | undefined;
@@ -186,4 +118,72 @@ declare abstract class RequestHandler<HandlerInfo extends RequestHandlerDefaultI
186
118
  private createExecutionResult;
187
119
  }
188
120
 
121
+ interface HttpResponseInit extends ResponseInit {
122
+ type?: ResponseType;
123
+ }
124
+ declare const bodyType: unique symbol;
125
+ interface StrictRequest<BodyType extends DefaultBodyType> extends Request {
126
+ json(): Promise<BodyType>;
127
+ }
128
+ /**
129
+ * Opaque `Response` type that supports strict body type.
130
+ */
131
+ interface StrictResponse<BodyType extends DefaultBodyType> extends Response {
132
+ readonly [bodyType]: BodyType;
133
+ }
134
+ /**
135
+ * A drop-in replacement for the standard `Response` class
136
+ * to allow additional features, like mocking the response `Set-Cookie` header.
137
+ *
138
+ * @example
139
+ * new HttpResponse('Hello world', { status: 201 })
140
+ * HttpResponse.json({ name: 'John' })
141
+ * HttpResponse.formData(form)
142
+ *
143
+ * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}
144
+ */
145
+ declare class HttpResponse extends Response {
146
+ constructor(body?: BodyInit | null, init?: HttpResponseInit);
147
+ /**
148
+ * Create a `Response` with a `Content-Type: "text/plain"` body.
149
+ * @example
150
+ * HttpResponse.text('hello world')
151
+ * HttpResponse.text('Error', { status: 500 })
152
+ */
153
+ static text<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
154
+ /**
155
+ * Create a `Response` with a `Content-Type: "application/json"` body.
156
+ * @example
157
+ * HttpResponse.json({ firstName: 'John' })
158
+ * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })
159
+ */
160
+ static json<BodyType extends JsonBodyType>(body?: BodyType | null, init?: HttpResponseInit): StrictResponse<BodyType>;
161
+ /**
162
+ * Create a `Response` with a `Content-Type: "application/xml"` body.
163
+ * @example
164
+ * HttpResponse.xml(`<user name="John" />`)
165
+ * HttpResponse.xml(`<article id="abc-123" />`, { status: 201 })
166
+ */
167
+ static xml<BodyType extends string>(body?: BodyType | null, init?: HttpResponseInit): Response;
168
+ /**
169
+ * Create a `Response` with an `ArrayBuffer` body.
170
+ * @example
171
+ * const buffer = new ArrayBuffer(3)
172
+ * const view = new Uint8Array(buffer)
173
+ * view.set([1, 2, 3])
174
+ *
175
+ * HttpResponse.arrayBuffer(buffer)
176
+ */
177
+ static arrayBuffer(body?: ArrayBuffer, init?: HttpResponseInit): Response;
178
+ /**
179
+ * Create a `Response` with a `FormData` body.
180
+ * @example
181
+ * const data = new FormData()
182
+ * data.set('name', 'Alice')
183
+ *
184
+ * HttpResponse.formData(data)
185
+ */
186
+ static formData(body?: FormData, init?: HttpResponseInit): Response;
187
+ }
188
+
189
189
  export { type AsyncResponseResolverReturnType as A, type DefaultBodyType as D, type HttpResponseInit as H, type JsonBodyType as J, type MaybeAsyncResponseResolverReturnType as M, RequestHandler as R, type StrictRequest as S, type ResponseResolver as a, type ResponseResolverReturnType as b, type RequestHandlerOptions as c, type DefaultRequestMultipartBody as d, type StrictResponse as e, HttpResponse as f, type RequestHandlerDefaultInfo as g, type HandlersExecutionResult as h, type ResponseResolutionContext as i, executeHandlers as j, type RequestHandlerInternalInfo as k, type ResponseResolverInfo as l, type RequestHandlerArgs as m, type RequestHandlerExecutionResult as n };