msw 2.12.6 → 2.12.8

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 (62) hide show
  1. package/lib/browser/index.js +641 -925
  2. package/lib/browser/index.js.map +1 -1
  3. package/lib/browser/index.mjs +641 -925
  4. package/lib/browser/index.mjs.map +1 -1
  5. package/lib/core/{HttpResponse-DM21DMt_.d.ts → HttpResponse-CVs3ngx3.d.ts} +12 -2
  6. package/lib/core/{HttpResponse-C86fdZzb.d.mts → HttpResponse-Cw4ELwIN.d.mts} +12 -2
  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 +21 -59
  14. package/lib/core/graphql.d.ts +21 -59
  15. package/lib/core/graphql.js +14 -15
  16. package/lib/core/graphql.js.map +1 -1
  17. package/lib/core/graphql.mjs +14 -15
  18. package/lib/core/graphql.mjs.map +1 -1
  19. package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
  20. package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
  21. package/lib/core/handlers/GraphQLHandler.js +42 -17
  22. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  23. package/lib/core/handlers/GraphQLHandler.mjs +44 -17
  24. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  25. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  26. package/lib/core/handlers/HttpHandler.d.ts +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/index.js.map +1 -1
  34. package/lib/core/index.mjs.map +1 -1
  35. package/lib/core/passthrough.d.mts +1 -1
  36. package/lib/core/passthrough.d.ts +1 -1
  37. package/lib/core/sse.d.mts +1 -1
  38. package/lib/core/sse.d.ts +1 -1
  39. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  40. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  41. package/lib/core/utils/executeHandlers.d.mts +1 -1
  42. package/lib/core/utils/executeHandlers.d.ts +1 -1
  43. package/lib/core/utils/handleRequest.d.mts +1 -1
  44. package/lib/core/utils/handleRequest.d.ts +1 -1
  45. package/lib/core/utils/internal/isHandlerKind.d.mts +1 -1
  46. package/lib/core/utils/internal/isHandlerKind.d.ts +1 -1
  47. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
  48. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +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/ws/handleWebSocketEvent.d.mts +1 -1
  54. package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
  55. package/lib/iife/index.js +977 -1407
  56. package/lib/iife/index.js.map +1 -1
  57. package/lib/mockServiceWorker.js +1 -1
  58. package/package.json +5 -5
  59. package/src/core/graphql.ts +43 -52
  60. package/src/core/handlers/GraphQLHandler.test.ts +1 -1
  61. package/src/core/handlers/GraphQLHandler.ts +73 -25
  62. package/src/core/index.ts +6 -1
@@ -7,7 +7,7 @@
7
7
  * - Please do NOT modify this file.
8
8
  */
9
9
 
10
- const PACKAGE_VERSION = '2.12.6'
10
+ const PACKAGE_VERSION = '2.12.8'
11
11
  const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82'
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.12.6",
3
+ "version": "2.12.8",
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",
@@ -215,7 +215,7 @@
215
215
  "sideEffects": false,
216
216
  "dependencies": {
217
217
  "@inquirer/confirm": "^5.0.0",
218
- "@mswjs/interceptors": "^0.40.0",
218
+ "@mswjs/interceptors": "^0.41.0",
219
219
  "@open-draft/deferred-promise": "^2.2.0",
220
220
  "@types/statuses": "^2.0.6",
221
221
  "cookie": "^1.0.2",
@@ -225,7 +225,7 @@
225
225
  "outvariant": "^1.4.3",
226
226
  "path-to-regexp": "^6.3.0",
227
227
  "picocolors": "^1.1.1",
228
- "rettime": "^0.7.0",
228
+ "rettime": "^0.10.1",
229
229
  "statuses": "^2.0.2",
230
230
  "strict-event-emitter": "^0.5.1",
231
231
  "tough-cookie": "^6.0.0",
@@ -242,7 +242,7 @@
242
242
  "@fastify/websocket": "^11.2.0",
243
243
  "@graphql-typed-document-node/core": "^3.2.0",
244
244
  "@open-draft/test-server": "^0.4.2",
245
- "@ossjs/release": "^0.10.0",
245
+ "@ossjs/release": "^0.10.1",
246
246
  "@playwright/test": "^1.50.1",
247
247
  "@types/express": "^5.0.5",
248
248
  "@types/json-bigint": "^1.0.4",
@@ -285,7 +285,7 @@
285
285
  "vitest-environment-miniflare": "^2.14.4",
286
286
  "webpack": "^5.95.0",
287
287
  "webpack-http-server": "^0.5.0",
288
- "msw": "2.12.6"
288
+ "msw": "2.12.8"
289
289
  },
290
290
  "peerDependencies": {
291
291
  "typescript": ">= 4.8.x"
@@ -1,4 +1,4 @@
1
- import type { DocumentNode, OperationTypeNode } from 'graphql'
1
+ import type { OperationTypeNode } from 'graphql'
2
2
  import {
3
3
  ResponseResolver,
4
4
  RequestHandlerOptions,
@@ -7,32 +7,29 @@ import {
7
7
  GraphQLHandler,
8
8
  GraphQLVariables,
9
9
  GraphQLOperationType,
10
- GraphQLHandlerNameSelector,
11
10
  GraphQLResolverExtras,
12
11
  GraphQLResponseBody,
13
12
  GraphQLQuery,
14
- GraphQLCustomPredicate,
13
+ GraphQLPredicate,
15
14
  } from './handlers/GraphQLHandler'
16
15
  import type { Path } from './utils/matching/matchRequestUrl'
17
16
 
18
- export interface TypedDocumentNode<
19
- Result = { [key: string]: any },
20
- Variables = { [key: string]: any },
21
- > extends DocumentNode {
22
- __apiType?: (variables: Variables) => Result
23
- __resultType?: Result
24
- __variablesType?: Variables
25
- }
26
-
27
17
  export type GraphQLRequestHandler = <
28
18
  Query extends GraphQLQuery = GraphQLQuery,
29
19
  Variables extends GraphQLVariables = GraphQLVariables,
30
20
  >(
31
- predicate:
32
- | GraphQLHandlerNameSelector
33
- | DocumentNode
34
- | TypedDocumentNode<Query, Variables>
35
- | GraphQLCustomPredicate,
21
+ predicate: GraphQLPredicate<Query, Variables>,
22
+ resolver: GraphQLResponseResolver<
23
+ [Query] extends [never] ? GraphQLQuery : Query,
24
+ Variables
25
+ >,
26
+ options?: RequestHandlerOptions,
27
+ ) => GraphQLHandler
28
+
29
+ export type GraphQLOperationHandler = <
30
+ Query extends GraphQLQuery = GraphQLQuery,
31
+ Variables extends GraphQLVariables = GraphQLVariables,
32
+ >(
36
33
  resolver: GraphQLResponseResolver<
37
34
  [Query] extends [never] ? GraphQLQuery : Query,
38
35
  Variables
@@ -58,22 +55,28 @@ function createScopedGraphQLHandler(
58
55
  }
59
56
  }
60
57
 
61
- function createGraphQLOperationHandler(url: Path) {
62
- return <
63
- Query extends GraphQLQuery = GraphQLQuery,
64
- Variables extends GraphQLVariables = GraphQLVariables,
65
- >(
66
- resolver: ResponseResolver<
67
- GraphQLResolverExtras<Variables>,
68
- null,
69
- GraphQLResponseBody<Query>
70
- >,
71
- ) => {
72
- return new GraphQLHandler('all', new RegExp('.*'), url, resolver)
58
+ function createGraphQLOperationHandler(url: Path): GraphQLOperationHandler {
59
+ return (resolver, options) => {
60
+ return new GraphQLHandler('all', new RegExp('.*'), url, resolver, options)
73
61
  }
74
62
  }
75
63
 
76
- const standardGraphQLHandlers = {
64
+ export interface GraphQLLinkHandlers {
65
+ query: GraphQLRequestHandler
66
+ mutation: GraphQLRequestHandler
67
+ operation: GraphQLOperationHandler
68
+ }
69
+
70
+ /**
71
+ * A namespace to intercept and mock GraphQL operations
72
+ *
73
+ * @example
74
+ * graphql.query('GetUser', resolver)
75
+ * graphql.mutation('DeletePost', resolver)
76
+ *
77
+ * @see {@link https://mswjs.io/docs/api/graphql `graphql` API reference}
78
+ */
79
+ export const graphql = {
77
80
  /**
78
81
  * Intercepts a GraphQL query by a given name.
79
82
  *
@@ -110,27 +113,6 @@ const standardGraphQLHandlers = {
110
113
  * @see {@link https://mswjs.io/docs/api/graphql#graphqloperationresolver `graphql.operation()` API reference}
111
114
  */
112
115
  operation: createGraphQLOperationHandler('*'),
113
- }
114
-
115
- function createGraphQLLink(url: Path): typeof standardGraphQLHandlers {
116
- return {
117
- operation: createGraphQLOperationHandler(url),
118
- query: createScopedGraphQLHandler('query' as OperationTypeNode, url),
119
- mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, url),
120
- }
121
- }
122
-
123
- /**
124
- * A namespace to intercept and mock GraphQL operations
125
- *
126
- * @example
127
- * graphql.query('GetUser', resolver)
128
- * graphql.mutation('DeletePost', resolver)
129
- *
130
- * @see {@link https://mswjs.io/docs/api/graphql `graphql` API reference}
131
- */
132
- export const graphql = {
133
- ...standardGraphQLHandlers,
134
116
 
135
117
  /**
136
118
  * Intercepts GraphQL operations scoped by the given URL.
@@ -141,5 +123,14 @@ export const graphql = {
141
123
  *
142
124
  * @see {@link https://mswjs.io/docs/api/graphql#graphqllinkurl `graphql.link()` API reference}
143
125
  */
144
- link: createGraphQLLink,
126
+ link(url: Path): GraphQLLinkHandlers {
127
+ return {
128
+ operation: createGraphQLOperationHandler(url),
129
+ query: createScopedGraphQLHandler('query' as OperationTypeNode, url),
130
+ mutation: createScopedGraphQLHandler(
131
+ 'mutation' as OperationTypeNode,
132
+ url,
133
+ ),
134
+ }
135
+ },
145
136
  }
@@ -139,7 +139,7 @@ describe('info', () => {
139
139
  expect(
140
140
  () => new GraphQLHandler(OperationTypeNode.QUERY, node, '*', resolver),
141
141
  ).toThrow(
142
- 'Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "query", but got "mutation").',
142
+ 'Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "query" but got "mutation").',
143
143
  )
144
144
  })
145
145
  })
@@ -1,4 +1,9 @@
1
- import type { DocumentNode, GraphQLError, OperationTypeNode } from 'graphql'
1
+ import {
2
+ parse,
3
+ type DocumentNode,
4
+ type GraphQLError,
5
+ type OperationTypeNode,
6
+ } from 'graphql'
2
7
  import {
3
8
  DefaultBodyType,
4
9
  RequestHandler,
@@ -16,10 +21,21 @@ import {
16
21
  GraphQLMultipartRequestBody,
17
22
  parseGraphQLRequest,
18
23
  parseDocumentNode,
24
+ ParsedGraphQLQuery,
19
25
  } from '../utils/internal/parseGraphQLRequest'
20
26
  import { toPublicUrl } from '../utils/request/toPublicUrl'
21
27
  import { devUtils } from '../utils/internal/devUtils'
22
28
  import { getAllRequestCookies } from '../utils/request/getRequestCookies'
29
+ import { invariant } from 'outvariant'
30
+
31
+ export interface DocumentTypeDecoration<
32
+ Result = { [key: string]: any },
33
+ Variables = { [key: string]: any },
34
+ > {
35
+ __apiType?: (variables: Variables) => Result
36
+ __resultType?: Result
37
+ __variablesType?: Variables
38
+ }
23
39
 
24
40
  export type GraphQLOperationType = OperationTypeNode | 'all'
25
41
  export type GraphQLHandlerNameSelector = DocumentNode | RegExp | string
@@ -88,8 +104,9 @@ export type GraphQLCustomPredicate = (args: {
88
104
 
89
105
  export type GraphQLCustomPredicateResult = boolean | { matches: boolean }
90
106
 
91
- export type GraphQLPredicate =
107
+ export type GraphQLPredicate<Query = any, Variables = any> =
92
108
  | GraphQLHandlerNameSelector
109
+ | DocumentTypeDecoration<Query, Variables>
93
110
  | GraphQLCustomPredicate
94
111
 
95
112
  export function isDocumentNode(
@@ -102,6 +119,12 @@ export function isDocumentNode(
102
119
  return typeof value === 'object' && 'kind' in value && 'definitions' in value
103
120
  }
104
121
 
122
+ function isDocumentTypeDecoration(
123
+ value: any,
124
+ ): value is DocumentTypeDecoration<any, any> {
125
+ return value instanceof String
126
+ }
127
+
105
128
  export class GraphQLHandler extends RequestHandler<
106
129
  GraphQLHandlerInfo,
107
130
  GraphQLRequestParsedResult,
@@ -114,6 +137,45 @@ export class GraphQLHandler extends RequestHandler<
114
137
  ParsedGraphQLRequest<GraphQLVariables>
115
138
  >()
116
139
 
140
+ static #parseOperationName(
141
+ predicate: GraphQLPredicate,
142
+ operationType: GraphQLOperationType,
143
+ ): GraphQLHandlerInfo['operationName'] {
144
+ const getOperationName = (node: ParsedGraphQLQuery): string => {
145
+ invariant(
146
+ node.operationType === operationType,
147
+ 'Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "%s" but got "%s").',
148
+ operationType,
149
+ node.operationType,
150
+ )
151
+
152
+ invariant(
153
+ node.operationName,
154
+ 'Failed to create a GraphQL handler: provided a DocumentNode without operation name',
155
+ )
156
+
157
+ return node.operationName
158
+ }
159
+
160
+ if (isDocumentNode(predicate)) {
161
+ return getOperationName(parseDocumentNode(predicate))
162
+ }
163
+
164
+ if (isDocumentTypeDecoration(predicate)) {
165
+ const documentNode = parse(predicate.toString())
166
+
167
+ invariant(
168
+ isDocumentNode(documentNode),
169
+ 'Failed to create a GraphQL handler: given TypedDocumentString (%s) does not produce a valid DocumentNode',
170
+ predicate,
171
+ )
172
+
173
+ return getOperationName(parseDocumentNode(documentNode))
174
+ }
175
+
176
+ return predicate
177
+ }
178
+
117
179
  constructor(
118
180
  operationType: GraphQLOperationType,
119
181
  predicate: GraphQLPredicate,
@@ -121,30 +183,13 @@ export class GraphQLHandler extends RequestHandler<
121
183
  resolver: ResponseResolver<GraphQLResolverExtras<any>, any, any>,
122
184
  options?: RequestHandlerOptions,
123
185
  ) {
124
- let resolvedOperationName = predicate
125
-
126
- if (isDocumentNode(resolvedOperationName)) {
127
- const parsedNode = parseDocumentNode(resolvedOperationName)
128
-
129
- if (parsedNode.operationType !== operationType) {
130
- throw new Error(
131
- `Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "${operationType}", but got "${parsedNode.operationType}").`,
132
- )
133
- }
134
-
135
- if (!parsedNode.operationName) {
136
- throw new Error(
137
- `Failed to create a GraphQL handler: provided a DocumentNode with no operation name.`,
138
- )
139
- }
140
-
141
- resolvedOperationName = parsedNode.operationName
142
- }
186
+ const operationName = GraphQLHandler.#parseOperationName(
187
+ predicate,
188
+ operationType,
189
+ )
143
190
 
144
191
  const displayOperationName =
145
- typeof resolvedOperationName === 'function'
146
- ? '[custom predicate]'
147
- : resolvedOperationName
192
+ typeof operationName === 'function' ? '[custom predicate]' : operationName
148
193
 
149
194
  const header =
150
195
  operationType === 'all'
@@ -155,7 +200,10 @@ export class GraphQLHandler extends RequestHandler<
155
200
  info: {
156
201
  header,
157
202
  operationType,
158
- operationName: resolvedOperationName,
203
+ operationName: GraphQLHandler.#parseOperationName(
204
+ predicate,
205
+ operationType,
206
+ ),
159
207
  },
160
208
  resolver,
161
209
  options,
package/src/core/index.ts CHANGED
@@ -73,7 +73,12 @@ export type {
73
73
  GraphQLOperationType,
74
74
  GraphQLCustomPredicate,
75
75
  } from './handlers/GraphQLHandler'
76
- export type { GraphQLRequestHandler, GraphQLResponseResolver } from './graphql'
76
+ export type {
77
+ GraphQLRequestHandler,
78
+ GraphQLOperationHandler,
79
+ GraphQLResponseResolver,
80
+ GraphQLLinkHandlers,
81
+ } from './graphql'
77
82
 
78
83
  export type { WebSocketData, WebSocketEventListener } from './ws'
79
84