msw 2.10.0 → 2.10.2

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 (68) hide show
  1. package/lib/browser/index.js +8 -8
  2. package/lib/browser/index.js.map +1 -1
  3. package/lib/browser/index.mjs +8 -8
  4. package/lib/browser/index.mjs.map +1 -1
  5. package/lib/core/{HttpResponse-DzeJL_i8.d.ts → HttpResponse-CCdkF1fJ.d.ts} +103 -2
  6. package/lib/core/{HttpResponse-BOGtXZ-B.d.mts → HttpResponse-I457nh8V.d.mts} +103 -2
  7. package/lib/core/HttpResponse.d.mts +3 -1
  8. package/lib/core/HttpResponse.d.ts +3 -1
  9. package/lib/core/HttpResponse.js +5 -1
  10. package/lib/core/HttpResponse.js.map +1 -1
  11. package/lib/core/HttpResponse.mjs +5 -1
  12. package/lib/core/HttpResponse.mjs.map +1 -1
  13. package/lib/core/SetupApi.d.mts +3 -2
  14. package/lib/core/SetupApi.d.ts +3 -2
  15. package/lib/core/getResponse.d.mts +3 -1
  16. package/lib/core/getResponse.d.ts +3 -1
  17. package/lib/core/graphql.d.mts +1 -2
  18. package/lib/core/graphql.d.ts +1 -2
  19. package/lib/core/handlers/GraphQLHandler.d.mts +1 -2
  20. package/lib/core/handlers/GraphQLHandler.d.ts +1 -2
  21. package/lib/core/handlers/HttpHandler.d.mts +2 -1
  22. package/lib/core/handlers/HttpHandler.d.ts +2 -1
  23. package/lib/core/handlers/RequestHandler.d.mts +3 -1
  24. package/lib/core/handlers/RequestHandler.d.ts +3 -1
  25. package/lib/core/handlers/RequestHandler.js.map +1 -1
  26. package/lib/core/handlers/RequestHandler.mjs.map +1 -1
  27. package/lib/core/handlers/WebSocketHandler.d.mts +6 -3
  28. package/lib/core/handlers/WebSocketHandler.d.ts +6 -3
  29. package/lib/core/handlers/WebSocketHandler.js.map +1 -1
  30. package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
  31. package/lib/core/http.d.mts +2 -1
  32. package/lib/core/http.d.ts +2 -1
  33. package/lib/core/index.d.mts +1 -2
  34. package/lib/core/index.d.ts +1 -2
  35. package/lib/core/passthrough.d.mts +3 -1
  36. package/lib/core/passthrough.d.ts +3 -1
  37. package/lib/core/utils/HttpResponse/decorators.d.mts +3 -1
  38. package/lib/core/utils/HttpResponse/decorators.d.ts +3 -1
  39. package/lib/core/utils/executeHandlers.d.mts +3 -1
  40. package/lib/core/utils/executeHandlers.d.ts +3 -1
  41. package/lib/core/utils/handleRequest.d.mts +3 -1
  42. package/lib/core/utils/handleRequest.d.ts +3 -1
  43. package/lib/core/utils/internal/isHandlerKind.d.mts +3 -2
  44. package/lib/core/utils/internal/isHandlerKind.d.ts +3 -2
  45. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -2
  46. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -2
  47. package/lib/core/utils/internal/parseMultipartData.d.mts +3 -1
  48. package/lib/core/utils/internal/parseMultipartData.d.ts +3 -1
  49. package/lib/core/utils/internal/requestHandlerUtils.d.mts +3 -1
  50. package/lib/core/utils/internal/requestHandlerUtils.d.ts +3 -1
  51. package/lib/core/ws/WebSocketClientManager.d.mts +4 -2
  52. package/lib/core/ws/WebSocketClientManager.d.ts +4 -2
  53. package/lib/core/ws/WebSocketClientManager.js +10 -0
  54. package/lib/core/ws/WebSocketClientManager.js.map +1 -1
  55. package/lib/core/ws/WebSocketClientManager.mjs +10 -0
  56. package/lib/core/ws/WebSocketClientManager.mjs.map +1 -1
  57. package/lib/core/ws/handleWebSocketEvent.d.mts +3 -2
  58. package/lib/core/ws/handleWebSocketEvent.d.ts +3 -2
  59. package/lib/iife/index.js +23 -9
  60. package/lib/iife/index.js.map +1 -1
  61. package/lib/mockServiceWorker.js +1 -1
  62. package/package.json +3 -3
  63. package/src/core/HttpResponse.ts +8 -1
  64. package/src/core/handlers/RequestHandler.ts +17 -2
  65. package/src/core/handlers/WebSocketHandler.ts +12 -3
  66. package/src/core/ws/WebSocketClientManager.ts +33 -3
  67. package/lib/core/GraphQLHandler-DOXAygvT.d.mts +0 -100
  68. package/lib/core/GraphQLHandler-noP9MRWa.d.ts +0 -100
@@ -7,7 +7,7 @@
7
7
  * - Please do NOT modify this file.
8
8
  */
9
9
 
10
- const PACKAGE_VERSION = '2.10.0'
10
+ const PACKAGE_VERSION = '2.10.2'
11
11
  const INTEGRITY_CHECKSUM = 'f5825c521429caf22a4dd13b66e243af'
12
12
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
13
13
  const activeClientIds = new Set()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.10.0",
3
+ "version": "2.10.2",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "type": "commonjs",
6
6
  "main": "./lib/core/index.js",
@@ -218,7 +218,7 @@
218
218
  "@bundled-es-modules/statuses": "^1.0.1",
219
219
  "@bundled-es-modules/tough-cookie": "^0.1.6",
220
220
  "@inquirer/confirm": "^5.0.0",
221
- "@mswjs/interceptors": "^0.38.7",
221
+ "@mswjs/interceptors": "^0.39.1",
222
222
  "@open-draft/deferred-promise": "^2.2.0",
223
223
  "@open-draft/until": "^2.1.0",
224
224
  "@types/cookie": "^0.6.0",
@@ -280,7 +280,7 @@
280
280
  "vitest-environment-miniflare": "^2.14.4",
281
281
  "webpack": "^5.95.0",
282
282
  "webpack-http-server": "^0.5.0",
283
- "msw": "2.10.0"
283
+ "msw": "2.10.2"
284
284
  },
285
285
  "peerDependencies": {
286
286
  "typescript": ">= 4.8.x"
@@ -10,7 +10,10 @@ export interface HttpResponseInit extends ResponseInit {
10
10
  type?: ResponseType
11
11
  }
12
12
 
13
- const bodyType: unique symbol = Symbol('bodyType')
13
+ export const bodyType: unique symbol = Symbol('bodyType')
14
+ export type DefaultUnsafeFetchResponse = Response & {
15
+ [bodyType]?: never
16
+ }
14
17
 
15
18
  export interface StrictRequest<BodyType extends JsonBodyType> extends Request {
16
19
  json(): Promise<BodyType>
@@ -46,6 +49,10 @@ export class HttpResponse<
46
49
  decorateResponse(this, responseInit)
47
50
  }
48
51
 
52
+ static error(): HttpResponse<any> {
53
+ return super.error() as HttpResponse<any>
54
+ }
55
+
49
56
  /**
50
57
  * Create a `Response` with a `Content-Type: "text/plain"` body.
51
58
  * @example
@@ -6,8 +6,13 @@ import {
6
6
  } from '../utils/internal/isIterable'
7
7
  import type { ResponseResolutionContext } from '../utils/executeHandlers'
8
8
  import type { MaybePromise } from '../typeUtils'
9
- import type { HttpResponse, StrictRequest } from '..//HttpResponse'
9
+ import {
10
+ StrictRequest,
11
+ HttpResponse,
12
+ DefaultUnsafeFetchResponse,
13
+ } from '../HttpResponse'
10
14
  import type { HandlerKind } from './common'
15
+ import type { GraphQLRequestBody } from './GraphQLHandler'
11
16
 
12
17
  export type DefaultRequestMultipartBody = Record<
13
18
  string,
@@ -42,9 +47,19 @@ export interface RequestHandlerInternalInfo {
42
47
  export type ResponseResolverReturnType<
43
48
  ResponseBodyType extends DefaultBodyType = undefined,
44
49
  > =
50
+ // If ResponseBodyType is a union and one of the types is `undefined`,
51
+ // allow plain Response as the type.
45
52
  | ([ResponseBodyType] extends [undefined]
46
53
  ? Response
47
- : HttpResponse<ResponseBodyType>)
54
+ : /**
55
+ * Treat GraphQL response body type as a special case.
56
+ * For esome reason, making the default HttpResponse<T> | DefaultUnsafeFetchResponse
57
+ * union breaks the body type inference for HTTP requests.
58
+ * @see https://github.com/mswjs/msw/issues/2130
59
+ */
60
+ ResponseBodyType extends GraphQLRequestBody<any>
61
+ ? HttpResponse<ResponseBodyType> | DefaultUnsafeFetchResponse
62
+ : HttpResponse<ResponseBodyType>)
48
63
  | undefined
49
64
  | void
50
65
 
@@ -1,6 +1,10 @@
1
1
  import { Emitter } from 'strict-event-emitter'
2
2
  import { createRequestId } from '@mswjs/interceptors'
3
- import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
3
+ import type {
4
+ WebSocketClientConnectionProtocol,
5
+ WebSocketConnectionData,
6
+ WebSocketServerConnectionProtocol,
7
+ } from '@mswjs/interceptors/WebSocket'
4
8
  import {
5
9
  type Match,
6
10
  type Path,
@@ -18,7 +22,10 @@ export type WebSocketHandlerEventMap = {
18
22
  connection: [args: WebSocketHandlerConnection]
19
23
  }
20
24
 
21
- export interface WebSocketHandlerConnection extends WebSocketConnectionData {
25
+ export interface WebSocketHandlerConnection {
26
+ client: WebSocketClientConnectionProtocol
27
+ server: WebSocketServerConnectionProtocol
28
+ info: WebSocketConnectionData['info']
22
29
  params: PathParams
23
30
  }
24
31
 
@@ -67,7 +74,9 @@ export class WebSocketHandler {
67
74
  return args.parsedResult.match.matches
68
75
  }
69
76
 
70
- public async run(connection: WebSocketConnectionData): Promise<boolean> {
77
+ public async run(
78
+ connection: Omit<WebSocketHandlerConnection, 'params'>,
79
+ ): Promise<boolean> {
71
80
  const parsedResult = this.parse({
72
81
  url: connection.client.url,
73
82
  })
@@ -1,7 +1,7 @@
1
1
  import type {
2
2
  WebSocketData,
3
- WebSocketClientConnection,
4
3
  WebSocketClientConnectionProtocol,
4
+ WebSocketClientEventMap,
5
5
  } from '@mswjs/interceptors/WebSocket'
6
6
  import { WebSocketClientStore } from './WebSocketClientStore'
7
7
  import { WebSocketMemoryClientStore } from './WebSocketMemoryClientStore'
@@ -105,7 +105,9 @@ export class WebSocketClientManager {
105
105
  this.channel.postMessage({ type: 'db:update' })
106
106
  }
107
107
 
108
- private async addClient(client: WebSocketClientConnection): Promise<void> {
108
+ private async addClient(
109
+ client: WebSocketClientConnectionProtocol,
110
+ ): Promise<void> {
109
111
  await this.store.add(client)
110
112
  // Sync the in-memory clients in this runtime with the
111
113
  // updated database. This pulls in all the stored clients.
@@ -119,7 +121,9 @@ export class WebSocketClientManager {
119
121
  * connection object because `addConnection()` is called only
120
122
  * for the opened connections in the same runtime.
121
123
  */
122
- public async addConnection(client: WebSocketClientConnection): Promise<void> {
124
+ public async addConnection(
125
+ client: WebSocketClientConnectionProtocol,
126
+ ): Promise<void> {
123
127
  // Store this client in the map of clients created in this runtime.
124
128
  // This way, the manager can distinguish between this runtime clients
125
129
  // and extraneous runtime clients when synchronizing clients storage.
@@ -208,4 +212,30 @@ export class WebSocketRemoteClientConnection
208
212
  },
209
213
  } as WebSocketBroadcastChannelMessage)
210
214
  }
215
+
216
+ addEventListener<EventType extends keyof WebSocketClientEventMap>(
217
+ _type: EventType,
218
+ _listener: (
219
+ this: WebSocket,
220
+ event: WebSocketClientEventMap[EventType],
221
+ ) => void,
222
+ _options?: AddEventListenerOptions | boolean,
223
+ ): void {
224
+ throw new Error(
225
+ 'WebSocketRemoteClientConnection.addEventListener is not supported',
226
+ )
227
+ }
228
+
229
+ removeEventListener<EventType extends keyof WebSocketClientEventMap>(
230
+ _event: EventType,
231
+ _listener: (
232
+ this: WebSocket,
233
+ event: WebSocketClientEventMap[EventType],
234
+ ) => void,
235
+ _options?: EventListenerOptions | boolean,
236
+ ): void {
237
+ throw new Error(
238
+ 'WebSocketRemoteClientConnection.removeEventListener is not supported',
239
+ )
240
+ }
211
241
  }
@@ -1,100 +0,0 @@
1
- import { OperationTypeNode, DocumentNode, GraphQLError } from 'graphql';
2
- import { D as DefaultBodyType, R as RequestHandler, g as RequestHandlerDefaultInfo, a as ResponseResolver, c as RequestHandlerOptions } from './HttpResponse-BOGtXZ-B.mjs';
3
- import { Match, Path } from './utils/matching/matchRequestUrl.mjs';
4
-
5
- interface ParsedGraphQLQuery {
6
- operationType: OperationTypeNode;
7
- operationName?: string;
8
- }
9
- type ParsedGraphQLRequest<VariablesType extends GraphQLVariables = GraphQLVariables> = (ParsedGraphQLQuery & {
10
- query: string;
11
- variables?: VariablesType;
12
- }) | undefined;
13
- declare function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery;
14
- type GraphQLParsedOperationsMap = Record<string, string[]>;
15
- type GraphQLMultipartRequestBody = {
16
- operations: string;
17
- map?: string;
18
- } & {
19
- [fileName: string]: File;
20
- };
21
- /**
22
- * Determines if a given request can be considered a GraphQL request.
23
- * Does not parse the query and does not guarantee its validity.
24
- */
25
- declare function parseGraphQLRequest(request: Request): Promise<ParsedGraphQLRequest>;
26
-
27
- type ExpectedOperationTypeNode = OperationTypeNode | 'all';
28
- type GraphQLHandlerNameSelector = DocumentNode | RegExp | string;
29
- type GraphQLQuery = Record<string, any> | null;
30
- type GraphQLVariables = Record<string, any>;
31
- interface GraphQLHandlerInfo extends RequestHandlerDefaultInfo {
32
- operationType: ExpectedOperationTypeNode;
33
- operationName: GraphQLHandlerNameSelector;
34
- }
35
- type GraphQLRequestParsedResult = {
36
- match: Match;
37
- cookies: Record<string, string>;
38
- } & (ParsedGraphQLRequest<GraphQLVariables>
39
- /**
40
- * An empty version of the ParsedGraphQLRequest
41
- * which simplifies the return type of the resolver
42
- * when the request is to a non-matching endpoint
43
- */
44
- | {
45
- operationType?: undefined;
46
- operationName?: undefined;
47
- query?: undefined;
48
- variables?: undefined;
49
- });
50
- type GraphQLResolverExtras<Variables extends GraphQLVariables> = {
51
- query: string;
52
- operationName: string;
53
- variables: Variables;
54
- cookies: Record<string, string>;
55
- };
56
- type GraphQLRequestBody<VariablesType extends GraphQLVariables> = GraphQLJsonRequestBody<VariablesType> | GraphQLMultipartRequestBody | Record<string, any> | undefined;
57
- interface GraphQLJsonRequestBody<Variables extends GraphQLVariables> {
58
- query: string;
59
- variables?: Variables;
60
- }
61
- type GraphQLResponseBody<BodyType extends DefaultBodyType> = {
62
- data?: BodyType | null;
63
- errors?: readonly Partial<GraphQLError>[] | null;
64
- extensions?: Record<string, any>;
65
- } | null | undefined;
66
- declare function isDocumentNode(value: DocumentNode | any): value is DocumentNode;
67
- declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo, GraphQLRequestParsedResult, GraphQLResolverExtras<any>> {
68
- private endpoint;
69
- static parsedRequestCache: WeakMap<Request, ParsedGraphQLRequest<GraphQLVariables>>;
70
- constructor(operationType: ExpectedOperationTypeNode, operationName: GraphQLHandlerNameSelector, endpoint: Path, resolver: ResponseResolver<GraphQLResolverExtras<any>, any, any>, options?: RequestHandlerOptions);
71
- /**
72
- * Parses the request body, once per request, cached across all
73
- * GraphQL handlers. This is done to avoid multiple parsing of the
74
- * request body, which each requires a clone of the request.
75
- */
76
- parseGraphQLRequestOrGetFromCache(request: Request): Promise<ParsedGraphQLRequest<GraphQLVariables>>;
77
- parse(args: {
78
- request: Request;
79
- }): Promise<GraphQLRequestParsedResult>;
80
- predicate(args: {
81
- request: Request;
82
- parsedResult: GraphQLRequestParsedResult;
83
- }): boolean;
84
- protected extendResolverArgs(args: {
85
- request: Request;
86
- parsedResult: GraphQLRequestParsedResult;
87
- }): {
88
- query: string;
89
- operationName: string;
90
- variables: GraphQLVariables;
91
- cookies: Record<string, string>;
92
- };
93
- log(args: {
94
- request: Request;
95
- response: Response;
96
- parsedResult: GraphQLRequestParsedResult;
97
- }): Promise<void>;
98
- }
99
-
100
- export { type ExpectedOperationTypeNode as E, GraphQLHandler as G, type ParsedGraphQLRequest as P, type GraphQLQuery as a, type GraphQLVariables as b, type GraphQLRequestBody as c, type GraphQLJsonRequestBody as d, type GraphQLHandlerNameSelector as e, type GraphQLResolverExtras as f, type GraphQLResponseBody as g, type ParsedGraphQLQuery as h, type GraphQLParsedOperationsMap as i, type GraphQLMultipartRequestBody as j, parseGraphQLRequest as k, type GraphQLHandlerInfo as l, type GraphQLRequestParsedResult as m, isDocumentNode as n, parseDocumentNode as p };
@@ -1,100 +0,0 @@
1
- import { OperationTypeNode, DocumentNode, GraphQLError } from 'graphql';
2
- import { D as DefaultBodyType, R as RequestHandler, g as RequestHandlerDefaultInfo, a as ResponseResolver, c as RequestHandlerOptions } from './HttpResponse-DzeJL_i8.js';
3
- import { Match, Path } from './utils/matching/matchRequestUrl.js';
4
-
5
- interface ParsedGraphQLQuery {
6
- operationType: OperationTypeNode;
7
- operationName?: string;
8
- }
9
- type ParsedGraphQLRequest<VariablesType extends GraphQLVariables = GraphQLVariables> = (ParsedGraphQLQuery & {
10
- query: string;
11
- variables?: VariablesType;
12
- }) | undefined;
13
- declare function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery;
14
- type GraphQLParsedOperationsMap = Record<string, string[]>;
15
- type GraphQLMultipartRequestBody = {
16
- operations: string;
17
- map?: string;
18
- } & {
19
- [fileName: string]: File;
20
- };
21
- /**
22
- * Determines if a given request can be considered a GraphQL request.
23
- * Does not parse the query and does not guarantee its validity.
24
- */
25
- declare function parseGraphQLRequest(request: Request): Promise<ParsedGraphQLRequest>;
26
-
27
- type ExpectedOperationTypeNode = OperationTypeNode | 'all';
28
- type GraphQLHandlerNameSelector = DocumentNode | RegExp | string;
29
- type GraphQLQuery = Record<string, any> | null;
30
- type GraphQLVariables = Record<string, any>;
31
- interface GraphQLHandlerInfo extends RequestHandlerDefaultInfo {
32
- operationType: ExpectedOperationTypeNode;
33
- operationName: GraphQLHandlerNameSelector;
34
- }
35
- type GraphQLRequestParsedResult = {
36
- match: Match;
37
- cookies: Record<string, string>;
38
- } & (ParsedGraphQLRequest<GraphQLVariables>
39
- /**
40
- * An empty version of the ParsedGraphQLRequest
41
- * which simplifies the return type of the resolver
42
- * when the request is to a non-matching endpoint
43
- */
44
- | {
45
- operationType?: undefined;
46
- operationName?: undefined;
47
- query?: undefined;
48
- variables?: undefined;
49
- });
50
- type GraphQLResolverExtras<Variables extends GraphQLVariables> = {
51
- query: string;
52
- operationName: string;
53
- variables: Variables;
54
- cookies: Record<string, string>;
55
- };
56
- type GraphQLRequestBody<VariablesType extends GraphQLVariables> = GraphQLJsonRequestBody<VariablesType> | GraphQLMultipartRequestBody | Record<string, any> | undefined;
57
- interface GraphQLJsonRequestBody<Variables extends GraphQLVariables> {
58
- query: string;
59
- variables?: Variables;
60
- }
61
- type GraphQLResponseBody<BodyType extends DefaultBodyType> = {
62
- data?: BodyType | null;
63
- errors?: readonly Partial<GraphQLError>[] | null;
64
- extensions?: Record<string, any>;
65
- } | null | undefined;
66
- declare function isDocumentNode(value: DocumentNode | any): value is DocumentNode;
67
- declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo, GraphQLRequestParsedResult, GraphQLResolverExtras<any>> {
68
- private endpoint;
69
- static parsedRequestCache: WeakMap<Request, ParsedGraphQLRequest<GraphQLVariables>>;
70
- constructor(operationType: ExpectedOperationTypeNode, operationName: GraphQLHandlerNameSelector, endpoint: Path, resolver: ResponseResolver<GraphQLResolverExtras<any>, any, any>, options?: RequestHandlerOptions);
71
- /**
72
- * Parses the request body, once per request, cached across all
73
- * GraphQL handlers. This is done to avoid multiple parsing of the
74
- * request body, which each requires a clone of the request.
75
- */
76
- parseGraphQLRequestOrGetFromCache(request: Request): Promise<ParsedGraphQLRequest<GraphQLVariables>>;
77
- parse(args: {
78
- request: Request;
79
- }): Promise<GraphQLRequestParsedResult>;
80
- predicate(args: {
81
- request: Request;
82
- parsedResult: GraphQLRequestParsedResult;
83
- }): boolean;
84
- protected extendResolverArgs(args: {
85
- request: Request;
86
- parsedResult: GraphQLRequestParsedResult;
87
- }): {
88
- query: string;
89
- operationName: string;
90
- variables: GraphQLVariables;
91
- cookies: Record<string, string>;
92
- };
93
- log(args: {
94
- request: Request;
95
- response: Response;
96
- parsedResult: GraphQLRequestParsedResult;
97
- }): Promise<void>;
98
- }
99
-
100
- export { type ExpectedOperationTypeNode as E, GraphQLHandler as G, type ParsedGraphQLRequest as P, type GraphQLQuery as a, type GraphQLVariables as b, type GraphQLRequestBody as c, type GraphQLJsonRequestBody as d, type GraphQLHandlerNameSelector as e, type GraphQLResolverExtras as f, type GraphQLResponseBody as g, type ParsedGraphQLQuery as h, type GraphQLParsedOperationsMap as i, type GraphQLMultipartRequestBody as j, parseGraphQLRequest as k, type GraphQLHandlerInfo as l, type GraphQLRequestParsedResult as m, isDocumentNode as n, parseDocumentNode as p };