msw 0.24.0 → 0.24.4
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/errors-deps.js +6 -1
- package/lib/esm/fetch-deps.js +25 -9
- package/lib/esm/graphql.js +33 -1
- package/lib/esm/index.js +17 -11
- package/lib/esm/rest-deps.js +61 -0
- package/lib/esm/xml-deps.js +14 -10
- package/lib/iife/index.js +21 -0
- package/lib/iife/mockServiceWorker.js +241 -0
- package/lib/types/context/body.d.ts +4 -2
- package/lib/types/context/cookie.d.ts +2 -3
- package/lib/types/context/data.d.ts +5 -3
- package/lib/types/context/delay.d.ts +4 -3
- package/lib/types/context/errors.d.ts +2 -0
- package/lib/types/context/fetch.d.ts +4 -3
- package/lib/types/context/json.d.ts +6 -3
- package/lib/types/context/set.d.ts +4 -0
- package/lib/types/context/status.d.ts +7 -0
- package/lib/types/context/text.d.ts +4 -3
- package/lib/types/context/xml.d.ts +4 -2
- package/lib/types/graphql.d.ts +67 -11
- package/lib/types/index.d.ts +1 -1
- package/lib/types/node/glossary.d.ts +11 -5
- package/lib/types/node/setupServer.d.ts +5 -0
- package/lib/types/rest.d.ts +87 -157
- package/lib/types/setupWorker/glossary.d.ts +19 -2
- package/lib/types/setupWorker/setupWorker.d.ts +6 -0
- package/lib/umd/index.js +156 -32
- package/native/index.js +30 -24
- package/node/index.js +35 -24
- package/package.json +21 -20
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock Service Worker.
|
|
3
|
+
* @see https://github.com/mswjs/msw
|
|
4
|
+
* - Please do NOT modify this file.
|
|
5
|
+
* - Please do NOT serve this file on production.
|
|
6
|
+
*/
|
|
7
|
+
/* eslint-disable */
|
|
8
|
+
/* tslint:disable */
|
|
9
|
+
|
|
10
|
+
const INTEGRITY_CHECKSUM = '65d33ca82955e1c5928aed19d1bdf3f9'
|
|
11
|
+
const bypassHeaderName = 'x-msw-bypass'
|
|
12
|
+
|
|
13
|
+
let clients = {}
|
|
14
|
+
|
|
15
|
+
self.addEventListener('install', function () {
|
|
16
|
+
return self.skipWaiting()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
self.addEventListener('activate', async function (event) {
|
|
20
|
+
return self.clients.claim()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
self.addEventListener('message', async function (event) {
|
|
24
|
+
const clientId = event.source.id
|
|
25
|
+
const client = await event.currentTarget.clients.get(clientId)
|
|
26
|
+
const allClients = await self.clients.matchAll()
|
|
27
|
+
const allClientIds = allClients.map((client) => client.id)
|
|
28
|
+
|
|
29
|
+
switch (event.data) {
|
|
30
|
+
case 'KEEPALIVE_REQUEST': {
|
|
31
|
+
sendToClient(client, {
|
|
32
|
+
type: 'KEEPALIVE_RESPONSE',
|
|
33
|
+
})
|
|
34
|
+
break
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
case 'INTEGRITY_CHECK_REQUEST': {
|
|
38
|
+
sendToClient(client, {
|
|
39
|
+
type: 'INTEGRITY_CHECK_RESPONSE',
|
|
40
|
+
payload: INTEGRITY_CHECKSUM,
|
|
41
|
+
})
|
|
42
|
+
break
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
case 'MOCK_ACTIVATE': {
|
|
46
|
+
clients = ensureKeys(allClientIds, clients)
|
|
47
|
+
clients[clientId] = true
|
|
48
|
+
|
|
49
|
+
sendToClient(client, {
|
|
50
|
+
type: 'MOCKING_ENABLED',
|
|
51
|
+
payload: true,
|
|
52
|
+
})
|
|
53
|
+
break
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
case 'MOCK_DEACTIVATE': {
|
|
57
|
+
clients = ensureKeys(allClientIds, clients)
|
|
58
|
+
clients[clientId] = false
|
|
59
|
+
break
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
case 'CLIENT_CLOSED': {
|
|
63
|
+
const remainingClients = allClients.filter((client) => {
|
|
64
|
+
return client.id !== clientId
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
// Unregister itself when there are no more clients
|
|
68
|
+
if (remainingClients.length === 0) {
|
|
69
|
+
self.registration.unregister()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
self.addEventListener('fetch', function (event) {
|
|
78
|
+
const { clientId, request } = event
|
|
79
|
+
const requestClone = request.clone()
|
|
80
|
+
const getOriginalResponse = () => fetch(requestClone)
|
|
81
|
+
|
|
82
|
+
// Bypass navigation requests.
|
|
83
|
+
if (request.mode === 'navigate') {
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Bypass mocking if the current client isn't present in the internal clients map
|
|
88
|
+
// (i.e. has the mocking disabled).
|
|
89
|
+
if (!clients[clientId]) {
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Opening the DevTools triggers the "only-if-cached" request
|
|
94
|
+
// that cannot be handled by the worker. Bypass such requests.
|
|
95
|
+
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
event.respondWith(
|
|
100
|
+
new Promise(async (resolve, reject) => {
|
|
101
|
+
const client = await event.target.clients.get(clientId)
|
|
102
|
+
|
|
103
|
+
// Bypass mocking when the request client is not active.
|
|
104
|
+
if (!client) {
|
|
105
|
+
return resolve(getOriginalResponse())
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Bypass requests with the explicit bypass header
|
|
109
|
+
if (requestClone.headers.get(bypassHeaderName) === 'true') {
|
|
110
|
+
const modifiedHeaders = serializeHeaders(requestClone.headers)
|
|
111
|
+
|
|
112
|
+
// Remove the bypass header to comply with the CORS preflight check
|
|
113
|
+
delete modifiedHeaders[bypassHeaderName]
|
|
114
|
+
|
|
115
|
+
const originalRequest = new Request(requestClone, {
|
|
116
|
+
headers: new Headers(modifiedHeaders),
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
return resolve(fetch(originalRequest))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const reqHeaders = serializeHeaders(request.headers)
|
|
123
|
+
const body = await request.text()
|
|
124
|
+
|
|
125
|
+
const rawClientMessage = await sendToClient(client, {
|
|
126
|
+
type: 'REQUEST',
|
|
127
|
+
payload: {
|
|
128
|
+
url: request.url,
|
|
129
|
+
method: request.method,
|
|
130
|
+
headers: reqHeaders,
|
|
131
|
+
cache: request.cache,
|
|
132
|
+
mode: request.mode,
|
|
133
|
+
credentials: request.credentials,
|
|
134
|
+
destination: request.destination,
|
|
135
|
+
integrity: request.integrity,
|
|
136
|
+
redirect: request.redirect,
|
|
137
|
+
referrer: request.referrer,
|
|
138
|
+
referrerPolicy: request.referrerPolicy,
|
|
139
|
+
body,
|
|
140
|
+
bodyUsed: request.bodyUsed,
|
|
141
|
+
keepalive: request.keepalive,
|
|
142
|
+
},
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
const clientMessage = rawClientMessage
|
|
146
|
+
|
|
147
|
+
switch (clientMessage.type) {
|
|
148
|
+
case 'MOCK_SUCCESS': {
|
|
149
|
+
setTimeout(
|
|
150
|
+
resolve.bind(this, createResponse(clientMessage)),
|
|
151
|
+
clientMessage.payload.delay,
|
|
152
|
+
)
|
|
153
|
+
break
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
case 'MOCK_NOT_FOUND': {
|
|
157
|
+
return resolve(getOriginalResponse())
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
case 'NETWORK_ERROR': {
|
|
161
|
+
const { name, message } = clientMessage.payload
|
|
162
|
+
const networkError = new Error(message)
|
|
163
|
+
networkError.name = name
|
|
164
|
+
|
|
165
|
+
// Rejecting a request Promise emulates a network error.
|
|
166
|
+
return reject(networkError)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
case 'INTERNAL_ERROR': {
|
|
170
|
+
const parsedBody = JSON.parse(clientMessage.payload.body)
|
|
171
|
+
|
|
172
|
+
console.error(
|
|
173
|
+
`\
|
|
174
|
+
[MSW] Request handler function for "%s %s" has thrown the following exception:
|
|
175
|
+
|
|
176
|
+
${parsedBody.errorType}: ${parsedBody.message}
|
|
177
|
+
(see more detailed error stack trace in the mocked response body)
|
|
178
|
+
|
|
179
|
+
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error.
|
|
180
|
+
If you wish to mock an error response, please refer to this guide: https://mswjs.io/docs/recipes/mocking-error-responses\
|
|
181
|
+
`,
|
|
182
|
+
request.method,
|
|
183
|
+
request.url,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
return resolve(createResponse(clientMessage))
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}).catch((error) => {
|
|
190
|
+
console.error(
|
|
191
|
+
'[MSW] Failed to mock a "%s" request to "%s": %s',
|
|
192
|
+
request.method,
|
|
193
|
+
request.url,
|
|
194
|
+
error,
|
|
195
|
+
)
|
|
196
|
+
}),
|
|
197
|
+
)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
function serializeHeaders(headers) {
|
|
201
|
+
const reqHeaders = {}
|
|
202
|
+
headers.forEach((value, name) => {
|
|
203
|
+
reqHeaders[name] = reqHeaders[name]
|
|
204
|
+
? [].concat(reqHeaders[name]).concat(value)
|
|
205
|
+
: value
|
|
206
|
+
})
|
|
207
|
+
return reqHeaders
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function sendToClient(client, message) {
|
|
211
|
+
return new Promise((resolve, reject) => {
|
|
212
|
+
const channel = new MessageChannel()
|
|
213
|
+
|
|
214
|
+
channel.port1.onmessage = (event) => {
|
|
215
|
+
if (event.data && event.data.error) {
|
|
216
|
+
reject(event.data.error)
|
|
217
|
+
} else {
|
|
218
|
+
resolve(event.data)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
client.postMessage(JSON.stringify(message), [channel.port2])
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function createResponse(clientMessage) {
|
|
227
|
+
return new Response(clientMessage.payload.body, {
|
|
228
|
+
...clientMessage.payload,
|
|
229
|
+
headers: clientMessage.payload.headers,
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function ensureKeys(keys, obj) {
|
|
234
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
235
|
+
if (keys.includes(key)) {
|
|
236
|
+
acc[key] = obj[key]
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return acc
|
|
240
|
+
}, {})
|
|
241
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
2
|
/**
|
|
3
|
-
* Sets
|
|
3
|
+
* Sets a raw response body. Does not append any `Content-Type` headers.
|
|
4
4
|
* @example
|
|
5
|
-
* res(body('
|
|
5
|
+
* res(ctx.body('Successful response'))
|
|
6
|
+
* res(ctx.body(JSON.stringify({ key: 'value' })))
|
|
7
|
+
* @see {@link https://mswjs.io/docs/api/context/body `ctx.body()`}
|
|
6
8
|
*/
|
|
7
9
|
export declare const body: <BodyType extends string | Blob | ArrayBufferView | ArrayBuffer | ReadableStream<any> | FormData>(value: BodyType) => ResponseTransformer<BodyType>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as cookieUtils from 'cookie';
|
|
2
2
|
import { ResponseTransformer } from '../response';
|
|
3
3
|
/**
|
|
4
|
-
* Sets a given cookie on the response.
|
|
5
|
-
* @example
|
|
6
|
-
* res(cookie('name', 'value'))
|
|
4
|
+
* Sets a given cookie on the mocked response.
|
|
5
|
+
* @example res(ctx.cookie('name', 'value'))
|
|
7
6
|
*/
|
|
8
7
|
export declare const cookie: (name: string, value: string, options?: cookieUtils.CookieSerializeOptions | undefined) => ResponseTransformer;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
|
-
export declare type DataContext<T> = (payload: T) => ResponseTransformer;
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
3
|
+
* Sets a given payload as a GraphQL response body.
|
|
4
|
+
* @example
|
|
5
|
+
* res(ctx.data({ user: { firstName: 'John' }}))
|
|
6
|
+
* @see {@link https://mswjs.io/docs/api/context/data `ctx.data()`}
|
|
5
7
|
*/
|
|
6
|
-
export declare const data:
|
|
8
|
+
export declare const data: <T extends Record<string, any>>(payload: T) => ResponseTransformer;
|
|
@@ -3,9 +3,10 @@ export declare const MIN_SERVER_RESPONSE_TIME = 100;
|
|
|
3
3
|
export declare const MAX_SERVER_RESPONSE_TIME = 400;
|
|
4
4
|
export declare const NODE_SERVER_RESPONSE_TIME = 5;
|
|
5
5
|
/**
|
|
6
|
-
* Delays the
|
|
6
|
+
* Delays the response by the given duration (ms).
|
|
7
7
|
* @example
|
|
8
|
-
* res(delay()) // realistic server response time
|
|
9
|
-
* res(delay(
|
|
8
|
+
* res(ctx.delay()) // realistic server response time
|
|
9
|
+
* res(ctx.delay(1200))
|
|
10
|
+
* @see {@link https://mswjs.io/docs/api/context/delay `ctx.delay()`}
|
|
10
11
|
*/
|
|
11
12
|
export declare const delay: (durationMs?: number | undefined) => ResponseTransformer;
|
|
@@ -2,5 +2,7 @@ import { GraphQLError } from 'graphql';
|
|
|
2
2
|
import { ResponseTransformer } from '../response';
|
|
3
3
|
/**
|
|
4
4
|
* Sets a given list of GraphQL errors on the mocked response.
|
|
5
|
+
* @example res(ctx.errors([{ message: 'Unauthorized' }]))
|
|
6
|
+
* @see {@link https://mswjs.io/docs/api/context/errors}
|
|
5
7
|
*/
|
|
6
8
|
export declare const errors: <ErrorsType extends Partial<GraphQLError>[] | null | undefined>(errorsList: ErrorsType) => ResponseTransformer<string>;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { MockedRequest } from '../utils/handlers/requestHandler';
|
|
2
2
|
export declare const augmentRequestInit: (requestInit: RequestInit) => RequestInit;
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Performs a bypassed request inside a request handler.
|
|
5
|
+
* @example
|
|
6
|
+
* const originalResponse = await ctx.fetch(req)
|
|
7
|
+
* @see {@link https://mswjs.io/docs/api/context/fetch `ctx.fetch()`}
|
|
7
8
|
*/
|
|
8
9
|
export declare const fetch: (input: string | MockedRequest, requestInit?: RequestInit) => any;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
2
|
/**
|
|
3
3
|
* Sets the given value as the JSON body of the response.
|
|
4
|
+
* Appends a `Content-Type: application/json` header on the
|
|
5
|
+
* mocked response.
|
|
4
6
|
* @example
|
|
5
|
-
* res(json(
|
|
6
|
-
* res(json('
|
|
7
|
-
* res(json([1, '2', false, { ok: true }]))
|
|
7
|
+
* res(ctx.json('Some string'))
|
|
8
|
+
* res(ctx.json({ key: 'value' }))
|
|
9
|
+
* res(ctx.json([1, '2', false, { ok: true }]))
|
|
10
|
+
* @see {@link https://mswjs.io/docs/api/context/json `ctx.json()`}
|
|
8
11
|
*/
|
|
9
12
|
export declare const json: <BodyTypeJSON>(body: BodyTypeJSON) => ResponseTransformer<string>;
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
|
+
/**
|
|
3
|
+
* Sets one or multiple response headers.
|
|
4
|
+
* @see {@link https://mswjs.io/docs/api/context/set `ctx.set()`}
|
|
5
|
+
*/
|
|
2
6
|
export declare function set<N extends string | Record<string, string | string[]>>(...args: N extends string ? [N, string] : [N]): ResponseTransformer;
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
|
+
/**
|
|
3
|
+
* Sets a response status code and text.
|
|
4
|
+
* @example
|
|
5
|
+
* res(ctx.status(301))
|
|
6
|
+
* res(ctx.status(400, 'Custom status text'))
|
|
7
|
+
* @see {@link https://mswjs.io/docs/api/context/status `ctx.status()`}
|
|
8
|
+
*/
|
|
2
9
|
export declare const status: (statusCode: number, statusText?: string | undefined) => ResponseTransformer;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
2
|
/**
|
|
3
|
-
* Sets a
|
|
4
|
-
*
|
|
5
|
-
* res(text('
|
|
3
|
+
* Sets a textual response body. Appends a `Content-Type: text/plain`
|
|
4
|
+
* header on the mocked response.
|
|
5
|
+
* @example res(ctx.text('Successful response'))
|
|
6
|
+
* @see {@link https://mswjs.io/docs/api/context/text `ctx.text()`}
|
|
6
7
|
*/
|
|
7
8
|
export declare const text: <BodyType extends string>(body: BodyType) => ResponseTransformer<BodyType>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
2
|
/**
|
|
3
|
-
* Sets
|
|
3
|
+
* Sets an XML response body. Appends a `Content-Type: text/xml` header
|
|
4
|
+
* on the mocked response.
|
|
4
5
|
* @example
|
|
5
|
-
* res(xml('<key>
|
|
6
|
+
* res(ctx.xml('<node key="value">Content</node>'))
|
|
7
|
+
* @see {@link https://mswjs.io/docs/api/context/xml `ctx.xml()`}
|
|
6
8
|
*/
|
|
7
9
|
export declare const xml: <BodyType extends string>(body: BodyType) => ResponseTransformer<BodyType>;
|
package/lib/types/graphql.d.ts
CHANGED
|
@@ -6,23 +6,23 @@ import { set } from './context/set';
|
|
|
6
6
|
import { status } from './context/status';
|
|
7
7
|
import { delay } from './context/delay';
|
|
8
8
|
import { fetch } from './context/fetch';
|
|
9
|
-
import {
|
|
9
|
+
import { data } from './context/data';
|
|
10
10
|
import { errors } from './context/errors';
|
|
11
11
|
declare type GraphQLRequestHandlerSelector = RegExp | string;
|
|
12
12
|
export declare type GraphQLMockedRequest<VariablesType = Record<string, any>> = Omit<MockedRequest, 'body'> & {
|
|
13
13
|
body: (GraphQLRequestPayload<VariablesType> & Record<string, any>) | undefined;
|
|
14
14
|
variables: VariablesType;
|
|
15
15
|
};
|
|
16
|
-
export interface GraphQLMockedContext
|
|
16
|
+
export interface GraphQLMockedContext {
|
|
17
17
|
set: typeof set;
|
|
18
18
|
status: typeof status;
|
|
19
19
|
delay: typeof delay;
|
|
20
20
|
fetch: typeof fetch;
|
|
21
|
-
data:
|
|
21
|
+
data: typeof data;
|
|
22
22
|
errors: typeof errors;
|
|
23
23
|
}
|
|
24
|
-
export declare const graphqlContext: GraphQLMockedContext
|
|
25
|
-
export declare type GraphQLResponseResolver<QueryType, VariablesType> = (req: GraphQLMockedRequest<VariablesType>, res: ResponseComposition
|
|
24
|
+
export declare const graphqlContext: GraphQLMockedContext;
|
|
25
|
+
export declare type GraphQLResponseResolver<QueryType, VariablesType> = (req: GraphQLMockedRequest<VariablesType>, res: ResponseComposition<any>, context: GraphQLMockedContext) => AsyncResponseResolverReturnType<MockedResponse>;
|
|
26
26
|
export interface GraphQLRequestPayload<VariablesType> {
|
|
27
27
|
query: string;
|
|
28
28
|
variables?: VariablesType;
|
|
@@ -33,15 +33,71 @@ export interface GraphQLRequestParsedResult<VariablesType> {
|
|
|
33
33
|
variables: VariablesType | undefined;
|
|
34
34
|
}
|
|
35
35
|
declare const graphqlStandardHandlers: {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Captures any GraphQL operation, regardless of its name, under the current scope.
|
|
38
|
+
* @example
|
|
39
|
+
* graphql.operation((req, res, ctx) => {
|
|
40
|
+
* return res(ctx.data({ name: 'John' }))
|
|
41
|
+
* })
|
|
42
|
+
* @see {@link https://mswjs.io/docs/api/graphql/operation `graphql.operation()`}
|
|
43
|
+
*/
|
|
44
|
+
operation: <QueryType, VariablesType = Record<string, any>>(resolver: GraphQLResponseResolver<QueryType, VariablesType>) => RequestHandler<GraphQLMockedRequest<VariablesType>, GraphQLMockedContext, GraphQLRequestParsedResult<VariablesType>, GraphQLMockedRequest<VariablesType>, any>;
|
|
45
|
+
/**
|
|
46
|
+
* Captures a GraphQL query by a given name.
|
|
47
|
+
* @example
|
|
48
|
+
* graphql.query('GetUser', (req, res, ctx) => {
|
|
49
|
+
* return res(ctx.data({ user: { name: 'John' } }))
|
|
50
|
+
* })
|
|
51
|
+
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
|
|
52
|
+
*/
|
|
53
|
+
query: <QueryType_1, VariablesType_1 = Record<string, any>>(expectedOperationName: GraphQLRequestHandlerSelector, resolver: GraphQLResponseResolver<QueryType_1, VariablesType_1>) => RequestHandler<GraphQLMockedRequest<VariablesType_1>, GraphQLMockedContext, GraphQLRequestParsedResult<VariablesType_1>, GraphQLMockedRequest<VariablesType_1>, any>;
|
|
54
|
+
/**
|
|
55
|
+
* Captures a GraphQL mutation by a given name.
|
|
56
|
+
* @example
|
|
57
|
+
* graphql.mutation('SavePost', (req, res, ctx) => {
|
|
58
|
+
* return res(ctx.data({ post: { id: 'abc-123' } }))
|
|
59
|
+
* })
|
|
60
|
+
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
|
|
61
|
+
*/
|
|
62
|
+
mutation: <QueryType_1, VariablesType_1 = Record<string, any>>(expectedOperationName: GraphQLRequestHandlerSelector, resolver: GraphQLResponseResolver<QueryType_1, VariablesType_1>) => RequestHandler<GraphQLMockedRequest<VariablesType_1>, GraphQLMockedContext, GraphQLRequestParsedResult<VariablesType_1>, GraphQLMockedRequest<VariablesType_1>, any>;
|
|
39
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Creates a GraphQL mocking API scoped to the given endpoint.
|
|
66
|
+
* @param uri Endpoint URL, or path.
|
|
67
|
+
* @example
|
|
68
|
+
* const api = graphql.link('https://api.site.com/graphql)
|
|
69
|
+
* api.query('GetUser', resolver)
|
|
70
|
+
* @see {@link https://mswjs.io/docs/api/graphql/link `graphql.link()`}
|
|
71
|
+
*/
|
|
40
72
|
declare function createGraphQLLink(uri: Mask): typeof graphqlStandardHandlers;
|
|
41
73
|
export declare const graphql: {
|
|
42
74
|
link: typeof createGraphQLLink;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Captures any GraphQL operation, regardless of its name, under the current scope.
|
|
77
|
+
* @example
|
|
78
|
+
* graphql.operation((req, res, ctx) => {
|
|
79
|
+
* return res(ctx.data({ name: 'John' }))
|
|
80
|
+
* })
|
|
81
|
+
* @see {@link https://mswjs.io/docs/api/graphql/operation `graphql.operation()`}
|
|
82
|
+
*/
|
|
83
|
+
operation: <QueryType, VariablesType = Record<string, any>>(resolver: GraphQLResponseResolver<QueryType, VariablesType>) => RequestHandler<GraphQLMockedRequest<VariablesType>, GraphQLMockedContext, GraphQLRequestParsedResult<VariablesType>, GraphQLMockedRequest<VariablesType>, any>;
|
|
84
|
+
/**
|
|
85
|
+
* Captures a GraphQL query by a given name.
|
|
86
|
+
* @example
|
|
87
|
+
* graphql.query('GetUser', (req, res, ctx) => {
|
|
88
|
+
* return res(ctx.data({ user: { name: 'John' } }))
|
|
89
|
+
* })
|
|
90
|
+
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
|
|
91
|
+
*/
|
|
92
|
+
query: <QueryType_1, VariablesType_1 = Record<string, any>>(expectedOperationName: GraphQLRequestHandlerSelector, resolver: GraphQLResponseResolver<QueryType_1, VariablesType_1>) => RequestHandler<GraphQLMockedRequest<VariablesType_1>, GraphQLMockedContext, GraphQLRequestParsedResult<VariablesType_1>, GraphQLMockedRequest<VariablesType_1>, any>;
|
|
93
|
+
/**
|
|
94
|
+
* Captures a GraphQL mutation by a given name.
|
|
95
|
+
* @example
|
|
96
|
+
* graphql.mutation('SavePost', (req, res, ctx) => {
|
|
97
|
+
* return res(ctx.data({ post: { id: 'abc-123' } }))
|
|
98
|
+
* })
|
|
99
|
+
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
|
|
100
|
+
*/
|
|
101
|
+
mutation: <QueryType_1, VariablesType_1 = Record<string, any>>(expectedOperationName: GraphQLRequestHandlerSelector, resolver: GraphQLResponseResolver<QueryType_1, VariablesType_1>) => RequestHandler<GraphQLMockedRequest<VariablesType_1>, GraphQLMockedContext, GraphQLRequestParsedResult<VariablesType_1>, GraphQLMockedRequest<VariablesType_1>, any>;
|
|
46
102
|
};
|
|
47
103
|
export {};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export { SetupWorkerApi } from './setupWorker/glossary';
|
|
|
4
4
|
export { response, defaultResponse, createResponseComposition, MockedResponse, ResponseTransformer, ResponseComposition, ResponseCompositionOptions, ResponseFunction, } from './response';
|
|
5
5
|
export { context };
|
|
6
6
|
export { defaultContext, MockedRequest, RequestHandler, RequestHandlerMetaInfo, RequestParams, RequestQuery, ResponseResolver, ResponseResolverReturnType, AsyncResponseResolverReturnType, } from './utils/handlers/requestHandler';
|
|
7
|
-
export { rest, restContext, RESTMethods, ParsedRestRequest } from './rest';
|
|
7
|
+
export { rest, restContext, RestContext, RESTMethods, ParsedRestRequest, } from './rest';
|
|
8
8
|
export { graphql, graphqlContext, GraphQLMockedRequest, GraphQLMockedContext, GraphQLRequestPayload, GraphQLResponseResolver, GraphQLRequestParsedResult, } from './graphql';
|
|
9
9
|
export { matchRequestUrl } from './utils/matching/matchRequestUrl';
|
|
10
10
|
export { compose } from './utils/internal/compose';
|
|
@@ -2,27 +2,33 @@ import { SharedOptions } from '../sharedOptions';
|
|
|
2
2
|
import { RequestHandlersList } from '../setupWorker/glossary';
|
|
3
3
|
export interface SetupServerApi {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Starts requests interception based on the previously provided request handlers.
|
|
6
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/listen `server.listen()`}
|
|
6
7
|
*/
|
|
7
8
|
listen: (options?: SharedOptions) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Stops requests interception by restoring all augmented modules.
|
|
11
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/close `server.close()`}
|
|
12
|
+
*/
|
|
13
|
+
close: () => void;
|
|
8
14
|
/**
|
|
9
15
|
* Prepends given request handlers to the list of existing handlers.
|
|
16
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/use `server.use()`}
|
|
10
17
|
*/
|
|
11
18
|
use: (...handlers: RequestHandlersList) => void;
|
|
12
19
|
/**
|
|
13
20
|
* Marks all request handlers that respond using `res.once()` as unused.
|
|
21
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/restore-handlers `server.restore-handlers()`}
|
|
14
22
|
*/
|
|
15
23
|
restoreHandlers: () => void;
|
|
16
24
|
/**
|
|
17
25
|
* Resets request handlers to the initial list given to the `setupServer` call, or to the explicit next request handlers list, if given.
|
|
26
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/reset-handlers `server.reset-handlers()`}
|
|
18
27
|
*/
|
|
19
28
|
resetHandlers: (...nextHandlers: RequestHandlersList) => void;
|
|
20
29
|
/**
|
|
21
30
|
* Lists all active request handlers.
|
|
31
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/print-handlers `server.print-handlers()`}
|
|
22
32
|
*/
|
|
23
33
|
printHandlers: () => void;
|
|
24
|
-
/**
|
|
25
|
-
* Stops requests interception by restoring all augmented modules.
|
|
26
|
-
*/
|
|
27
|
-
close: () => void;
|
|
28
34
|
}
|
|
@@ -1 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets up a requests interception in NodeJS with the given request handlers.
|
|
3
|
+
* @param {RequestHandler[]} requestHandlers List of request handlers.
|
|
4
|
+
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer`}
|
|
5
|
+
*/
|
|
1
6
|
export declare const setupServer: (...requestHandlers: import("../setupWorker/glossary").RequestHandlersList) => import("./glossary").SetupServerApi;
|