msw 0.33.1 → 0.35.0
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.
- package/lib/esm/RequestHandler-deps.js +78 -21
- package/lib/esm/graphql-deps.js +34 -13
- package/lib/esm/index.js +526 -125
- package/lib/esm/mockServiceWorker.js +18 -9
- package/lib/esm/rest-deps.js +3 -2
- package/lib/iife/index.js +3 -3
- package/lib/iife/mockServiceWorker.js +18 -9
- package/lib/types/graphql.d.ts +13 -4
- package/lib/types/handlers/GraphQLHandler.d.ts +3 -2
- package/lib/types/node/glossary.d.ts +4 -14
- package/lib/types/setupWorker/glossary.d.ts +4 -14
- package/lib/types/sharedOptions.d.ts +11 -0
- package/lib/types/utils/internal/parseGraphQLRequest.d.ts +2 -1
- package/lib/types/utils/internal/pipeEvents.d.ts +6 -0
- package/lib/types/utils/logging/getStatusCodeColor.d.ts +6 -1
- package/lib/umd/index.js +652 -159
- package/lib/umd/mockServiceWorker.js +18 -9
- package/native/lib/index.js +145 -36
- package/node/lib/index.js +145 -36
- package/package.json +3 -3
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
/* tslint:disable */
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Mock Service Worker (0.
|
|
5
|
+
* Mock Service Worker (0.35.0).
|
|
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.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
const INTEGRITY_CHECKSUM = '
|
|
11
|
+
const INTEGRITY_CHECKSUM = 'f0a916b13c8acc2b526a03a6d26df85f'
|
|
12
12
|
const bypassHeaderName = 'x-msw-bypass'
|
|
13
13
|
const activeClientIds = new Set()
|
|
14
14
|
|
|
@@ -221,13 +221,11 @@ async function getResponse(event, client, requestId) {
|
|
|
221
221
|
|
|
222
222
|
console.error(
|
|
223
223
|
`\
|
|
224
|
-
[MSW]
|
|
224
|
+
[MSW] Uncaught exception in the request handler for "%s %s":
|
|
225
225
|
|
|
226
|
-
${parsedBody.
|
|
227
|
-
(see more detailed error stack trace in the mocked response body)
|
|
226
|
+
${parsedBody.location}
|
|
228
227
|
|
|
229
|
-
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error.
|
|
230
|
-
If you wish to mock an error response, please refer to this guide: https://mswjs.io/docs/recipes/mocking-error-responses\
|
|
228
|
+
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses\
|
|
231
229
|
`,
|
|
232
230
|
request.method,
|
|
233
231
|
request.url,
|
|
@@ -271,11 +269,22 @@ self.addEventListener('fetch', function (event) {
|
|
|
271
269
|
|
|
272
270
|
return event.respondWith(
|
|
273
271
|
handleRequest(event, requestId).catch((error) => {
|
|
272
|
+
if (error.name === 'NetworkError') {
|
|
273
|
+
console.warn(
|
|
274
|
+
'[MSW] Successfully emulated a network error for the "%s %s" request.',
|
|
275
|
+
request.method,
|
|
276
|
+
request.url,
|
|
277
|
+
)
|
|
278
|
+
return
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// At this point, any exception indicates an issue with the original request/response.
|
|
274
282
|
console.error(
|
|
275
|
-
|
|
283
|
+
`\
|
|
284
|
+
[MSW] Caught an exception from the "%s %s" request (%s). This is probably not a problem with Mock Service Worker. There is likely an additional logging output above.`,
|
|
276
285
|
request.method,
|
|
277
286
|
request.url,
|
|
278
|
-
error
|
|
287
|
+
`${error.name}: ${error.message}`,
|
|
279
288
|
)
|
|
280
289
|
}),
|
|
281
290
|
)
|
package/lib/types/graphql.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
+
import { DocumentNode } from 'graphql';
|
|
1
2
|
import { Path } from 'node-match-path';
|
|
2
3
|
import { ResponseResolver } from './handlers/RequestHandler';
|
|
3
4
|
import { GraphQLHandler, GraphQLContext, GraphQLRequest, GraphQLVariables, GraphQLHandlerNameSelector } from './handlers/GraphQLHandler';
|
|
5
|
+
export interface TypedDocumentNode<Result = {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}, Variables = {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}> extends DocumentNode {
|
|
10
|
+
__resultType?: Result;
|
|
11
|
+
__variablesType?: Variables;
|
|
12
|
+
}
|
|
4
13
|
declare const standardGraphQLHandlers: {
|
|
5
14
|
/**
|
|
6
15
|
* Captures any GraphQL operation, regardless of its name, under the current scope.
|
|
@@ -19,7 +28,7 @@ declare const standardGraphQLHandlers: {
|
|
|
19
28
|
* })
|
|
20
29
|
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
|
|
21
30
|
*/
|
|
22
|
-
query: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
31
|
+
query: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector | TypedDocumentNode<Query_1, Variables_1>, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
23
32
|
/**
|
|
24
33
|
* Captures a GraphQL mutation by a given name.
|
|
25
34
|
* @example
|
|
@@ -28,7 +37,7 @@ declare const standardGraphQLHandlers: {
|
|
|
28
37
|
* })
|
|
29
38
|
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
|
|
30
39
|
*/
|
|
31
|
-
mutation: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
40
|
+
mutation: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector | TypedDocumentNode<Query_1, Variables_1>, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
32
41
|
};
|
|
33
42
|
declare function createGraphQLLink(url: Path): typeof standardGraphQLHandlers;
|
|
34
43
|
export declare const graphql: {
|
|
@@ -50,7 +59,7 @@ export declare const graphql: {
|
|
|
50
59
|
* })
|
|
51
60
|
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
|
|
52
61
|
*/
|
|
53
|
-
query: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
62
|
+
query: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector | TypedDocumentNode<Query_1, Variables_1>, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
54
63
|
/**
|
|
55
64
|
* Captures a GraphQL mutation by a given name.
|
|
56
65
|
* @example
|
|
@@ -59,6 +68,6 @@ export declare const graphql: {
|
|
|
59
68
|
* })
|
|
60
69
|
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
|
|
61
70
|
*/
|
|
62
|
-
mutation: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
71
|
+
mutation: <Query_1 extends Record<string, any>, Variables_1 extends GraphQLVariables = GraphQLVariables>(operationName: GraphQLHandlerNameSelector | TypedDocumentNode<Query_1, Variables_1>, resolver: ResponseResolver<GraphQLRequest<Variables_1>, GraphQLContext<Query_1>, any>) => GraphQLHandler<GraphQLRequest<any>>;
|
|
63
72
|
};
|
|
64
73
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OperationTypeNode } from 'graphql';
|
|
1
|
+
import { DocumentNode, OperationTypeNode } from 'graphql';
|
|
2
2
|
import { Path } from 'node-match-path';
|
|
3
3
|
import { SerializedResponse } from '../setupWorker/glossary';
|
|
4
4
|
import { set } from '../context/set';
|
|
@@ -11,7 +11,7 @@ import { cookie } from '../context/cookie';
|
|
|
11
11
|
import { MockedRequest, RequestHandler, ResponseResolver } from './RequestHandler';
|
|
12
12
|
import { ParsedGraphQLRequest, GraphQLMultipartRequestBody } from '../utils/internal/parseGraphQLRequest';
|
|
13
13
|
export declare type ExpectedOperationTypeNode = OperationTypeNode | 'all';
|
|
14
|
-
export declare type GraphQLHandlerNameSelector = RegExp | string;
|
|
14
|
+
export declare type GraphQLHandlerNameSelector = DocumentNode | RegExp | string;
|
|
15
15
|
export declare type GraphQLContext<QueryType> = {
|
|
16
16
|
set: typeof set;
|
|
17
17
|
status: typeof status;
|
|
@@ -35,6 +35,7 @@ export interface GraphQLJsonRequestBody<Variables extends GraphQLVariables> {
|
|
|
35
35
|
export interface GraphQLRequest<Variables extends GraphQLVariables> extends MockedRequest<GraphQLRequestBody<Variables>> {
|
|
36
36
|
variables: Variables;
|
|
37
37
|
}
|
|
38
|
+
export declare function isDocumentNode(value: DocumentNode | any): value is DocumentNode;
|
|
38
39
|
export declare class GraphQLHandler<Request extends GraphQLRequest<any> = GraphQLRequest<any>> extends RequestHandler<GraphQLHandlerInfo, Request, ParsedGraphQLRequest | null, GraphQLRequest<any>> {
|
|
39
40
|
private endpoint;
|
|
40
41
|
constructor(operationType: ExpectedOperationTypeNode, operationName: GraphQLHandlerNameSelector, endpoint: Path, resolver: ResponseResolver<any, any>);
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { PartialDeep } from 'type-fest';
|
|
2
2
|
import { IsomorphicResponse } from '@mswjs/interceptors';
|
|
3
|
-
import {
|
|
4
|
-
import { SharedOptions } from '../sharedOptions';
|
|
5
|
-
export
|
|
6
|
-
'request:start': (request: MockedRequest) => void;
|
|
7
|
-
'request:match': (request: MockedRequest) => void;
|
|
8
|
-
'request:unhandled': (request: MockedRequest) => void;
|
|
9
|
-
'request:end': (request: MockedRequest) => void;
|
|
10
|
-
'response:mocked': (response: IsomorphicResponse, requestId: string) => void;
|
|
11
|
-
'response:bypass': (response: IsomorphicResponse, requestId: string) => void;
|
|
12
|
-
}
|
|
3
|
+
import { RequestHandler } from '../handlers/RequestHandler';
|
|
4
|
+
import { LifeCycleEventEmitter, LifeCycleEventsMap, SharedOptions } from '../sharedOptions';
|
|
5
|
+
export declare type ServerLifecycleEventsMap = LifeCycleEventsMap<IsomorphicResponse>;
|
|
13
6
|
export interface SetupServerApi {
|
|
14
7
|
/**
|
|
15
8
|
* Starts requests interception based on the previously provided request handlers.
|
|
@@ -41,8 +34,5 @@ export interface SetupServerApi {
|
|
|
41
34
|
* @see {@link https://mswjs.io/docs/api/setup-server/print-handlers `server.print-handlers()`}
|
|
42
35
|
*/
|
|
43
36
|
printHandlers(): void;
|
|
44
|
-
|
|
45
|
-
* Attaches a listener to one of the life-cycle events.
|
|
46
|
-
*/
|
|
47
|
-
on<EventType extends keyof ServerLifecycleEventsMap>(eventType: EventType, listener: ServerLifecycleEventsMap[EventType]): void;
|
|
37
|
+
events: LifeCycleEventEmitter<ServerLifecycleEventsMap>;
|
|
48
38
|
}
|
|
@@ -2,9 +2,9 @@ import { Path } from 'node-match-path';
|
|
|
2
2
|
import { PartialDeep } from 'type-fest';
|
|
3
3
|
import { FlatHeadersObject } from 'headers-utils';
|
|
4
4
|
import { StrictEventEmitter } from 'strict-event-emitter';
|
|
5
|
-
import { SharedOptions } from '../sharedOptions';
|
|
5
|
+
import { LifeCycleEventEmitter, LifeCycleEventsMap, SharedOptions } from '../sharedOptions';
|
|
6
6
|
import { ServiceWorkerMessage } from '../utils/createBroadcastChannel';
|
|
7
|
-
import {
|
|
7
|
+
import { RequestHandler } from '../handlers/RequestHandler';
|
|
8
8
|
import { InterceptorApi } from '@mswjs/interceptors';
|
|
9
9
|
export declare type ResolvedPath = Path | URL;
|
|
10
10
|
declare type RequestWithoutMethods = Omit<Request, 'text' | 'body' | 'json' | 'blob' | 'arrayBuffer' | 'formData' | 'clone' | 'signal' | 'isHistoryNavigation' | 'isReloadNavigation'>;
|
|
@@ -45,14 +45,7 @@ export declare type ServiceWorkerOutgoingEventTypes = 'MOCK_ACTIVATE' | 'MOCK_DE
|
|
|
45
45
|
* only as a part of a single `fetch` event handler.
|
|
46
46
|
*/
|
|
47
47
|
export declare type ServiceWorkerFetchEventTypes = 'MOCK_SUCCESS' | 'MOCK_NOT_FOUND' | 'NETWORK_ERROR' | 'INTERNAL_ERROR';
|
|
48
|
-
export
|
|
49
|
-
'request:start': (request: MockedRequest) => void;
|
|
50
|
-
'request:match': (request: MockedRequest) => void;
|
|
51
|
-
'request:unhandled': (request: MockedRequest) => void;
|
|
52
|
-
'request:end': (request: MockedRequest) => void;
|
|
53
|
-
'response:mocked': (response: Response, requestId: string) => void;
|
|
54
|
-
'response:bypass': (response: Response, requestId: string) => void;
|
|
55
|
-
}
|
|
48
|
+
export declare type WorkerLifecycleEventsMap = LifeCycleEventsMap<Response>;
|
|
56
49
|
export interface SetupWorkerInternalContext {
|
|
57
50
|
startOptions?: StartOptions;
|
|
58
51
|
worker: ServiceWorker | null;
|
|
@@ -156,9 +149,6 @@ export interface SetupWorkerApi {
|
|
|
156
149
|
* @see {@link https://mswjs.io/docs/api/setup-worker/print-handlers `worker.printHandlers()`}
|
|
157
150
|
*/
|
|
158
151
|
printHandlers: () => void;
|
|
159
|
-
|
|
160
|
-
* Attaches a listener to one of the life-cycle events.
|
|
161
|
-
*/
|
|
162
|
-
on<EventType extends keyof WorkerLifecycleEventsMap>(eventType: EventType, listener: WorkerLifecycleEventsMap[EventType]): void;
|
|
152
|
+
events: LifeCycleEventEmitter<WorkerLifecycleEventsMap>;
|
|
163
153
|
}
|
|
164
154
|
export {};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { StrictEventEmitter } from 'strict-event-emitter';
|
|
2
|
+
import { MockedRequest } from './handlers/RequestHandler';
|
|
1
3
|
import { UnhandledRequestStrategy } from './utils/request/onUnhandledRequest';
|
|
2
4
|
export interface SharedOptions {
|
|
3
5
|
/**
|
|
@@ -10,3 +12,12 @@ export interface SharedOptions {
|
|
|
10
12
|
*/
|
|
11
13
|
onUnhandledRequest: UnhandledRequestStrategy;
|
|
12
14
|
}
|
|
15
|
+
export interface LifeCycleEventsMap<ResponseType> {
|
|
16
|
+
'request:start': (request: MockedRequest) => void;
|
|
17
|
+
'request:match': (request: MockedRequest) => void;
|
|
18
|
+
'request:unhandled': (request: MockedRequest) => void;
|
|
19
|
+
'request:end': (request: MockedRequest) => void;
|
|
20
|
+
'response:mocked': (response: ResponseType, requestId: string) => void;
|
|
21
|
+
'response:bypass': (response: ResponseType, requestId: string) => void;
|
|
22
|
+
}
|
|
23
|
+
export declare type LifeCycleEventEmitter<ResponseType> = Pick<StrictEventEmitter<ResponseType>, 'on' | 'removeListener' | 'removeAllListeners'>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OperationTypeNode } from 'graphql';
|
|
1
|
+
import { DocumentNode, OperationTypeNode } from 'graphql';
|
|
2
2
|
import { GraphQLVariables } from '../../handlers/GraphQLHandler';
|
|
3
3
|
import { MockedRequest } from '../../handlers/RequestHandler';
|
|
4
4
|
export interface ParsedGraphQLQuery {
|
|
@@ -8,6 +8,7 @@ export interface ParsedGraphQLQuery {
|
|
|
8
8
|
export declare type ParsedGraphQLRequest<VariablesType extends GraphQLVariables = GraphQLVariables> = (ParsedGraphQLQuery & {
|
|
9
9
|
variables?: VariablesType;
|
|
10
10
|
}) | undefined;
|
|
11
|
+
export declare function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery;
|
|
11
12
|
export declare type GraphQLParsedOperationsMap = Record<string, string[]>;
|
|
12
13
|
export declare type GraphQLMultipartRequestBody = {
|
|
13
14
|
operations: string;
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
export declare enum StatusCodeColor {
|
|
2
|
+
Success = "#69AB32",
|
|
3
|
+
Warning = "#F0BB4B",
|
|
4
|
+
Danger = "#E95F5D"
|
|
5
|
+
}
|
|
1
6
|
/**
|
|
2
7
|
* Returns a HEX color for a given response status code number.
|
|
3
8
|
*/
|
|
4
|
-
export declare function getStatusCodeColor(status: number):
|
|
9
|
+
export declare function getStatusCodeColor(status: number): StatusCodeColor;
|