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
package/lib/umd/index.js
CHANGED
|
@@ -70,6 +70,13 @@
|
|
|
70
70
|
"511": "Network Authentication Required"
|
|
71
71
|
};
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Sets a response status code and text.
|
|
75
|
+
* @example
|
|
76
|
+
* res(ctx.status(301))
|
|
77
|
+
* res(ctx.status(400, 'Custom status text'))
|
|
78
|
+
* @see {@link https://mswjs.io/docs/api/context/status `ctx.status()`}
|
|
79
|
+
*/
|
|
73
80
|
const status = (statusCode, statusText) => {
|
|
74
81
|
return (res) => {
|
|
75
82
|
res.status = statusCode;
|
|
@@ -333,6 +340,10 @@
|
|
|
333
340
|
exports.flattenHeadersObject = flattenHeadersObject_1.flattenHeadersObject;
|
|
334
341
|
});
|
|
335
342
|
|
|
343
|
+
/**
|
|
344
|
+
* Sets one or multiple response headers.
|
|
345
|
+
* @see {@link https://mswjs.io/docs/api/context/set `ctx.set()`}
|
|
346
|
+
*/
|
|
336
347
|
function set(...args) {
|
|
337
348
|
return (res) => {
|
|
338
349
|
const [name, value] = args;
|
|
@@ -551,9 +562,8 @@
|
|
|
551
562
|
}
|
|
552
563
|
|
|
553
564
|
/**
|
|
554
|
-
* Sets a given cookie on the response.
|
|
555
|
-
* @example
|
|
556
|
-
* res(cookie('name', 'value'))
|
|
565
|
+
* Sets a given cookie on the mocked response.
|
|
566
|
+
* @example res(ctx.cookie('name', 'value'))
|
|
557
567
|
*/
|
|
558
568
|
const cookie = (name, value, options) => {
|
|
559
569
|
return (res) => {
|
|
@@ -567,9 +577,11 @@
|
|
|
567
577
|
};
|
|
568
578
|
|
|
569
579
|
/**
|
|
570
|
-
* Sets
|
|
580
|
+
* Sets a raw response body. Does not append any `Content-Type` headers.
|
|
571
581
|
* @example
|
|
572
|
-
* res(body('
|
|
582
|
+
* res(ctx.body('Successful response'))
|
|
583
|
+
* res(ctx.body(JSON.stringify({ key: 'value' })))
|
|
584
|
+
* @see {@link https://mswjs.io/docs/api/context/body `ctx.body()`}
|
|
573
585
|
*/
|
|
574
586
|
const body = (value) => {
|
|
575
587
|
return (res) => {
|
|
@@ -620,10 +632,13 @@
|
|
|
620
632
|
|
|
621
633
|
/**
|
|
622
634
|
* Sets the given value as the JSON body of the response.
|
|
635
|
+
* Appends a `Content-Type: application/json` header on the
|
|
636
|
+
* mocked response.
|
|
623
637
|
* @example
|
|
624
|
-
* res(json(
|
|
625
|
-
* res(json('
|
|
626
|
-
* res(json([1, '2', false, { ok: true }]))
|
|
638
|
+
* res(ctx.json('Some string'))
|
|
639
|
+
* res(ctx.json({ key: 'value' }))
|
|
640
|
+
* res(ctx.json([1, '2', false, { ok: true }]))
|
|
641
|
+
* @see {@link https://mswjs.io/docs/api/context/json `ctx.json()`}
|
|
627
642
|
*/
|
|
628
643
|
const json = (body) => {
|
|
629
644
|
return (res) => {
|
|
@@ -634,7 +649,10 @@
|
|
|
634
649
|
};
|
|
635
650
|
|
|
636
651
|
/**
|
|
637
|
-
*
|
|
652
|
+
* Sets a given payload as a GraphQL response body.
|
|
653
|
+
* @example
|
|
654
|
+
* res(ctx.data({ user: { firstName: 'John' }}))
|
|
655
|
+
* @see {@link https://mswjs.io/docs/api/context/data `ctx.data()`}
|
|
638
656
|
*/
|
|
639
657
|
const data = (payload) => {
|
|
640
658
|
return (res) => {
|
|
@@ -671,10 +689,11 @@
|
|
|
671
689
|
MIN_SERVER_RESPONSE_TIME);
|
|
672
690
|
};
|
|
673
691
|
/**
|
|
674
|
-
* Delays the
|
|
692
|
+
* Delays the response by the given duration (ms).
|
|
675
693
|
* @example
|
|
676
|
-
* res(delay()) // realistic server response time
|
|
677
|
-
* res(delay(
|
|
694
|
+
* res(ctx.delay()) // realistic server response time
|
|
695
|
+
* res(ctx.delay(1200))
|
|
696
|
+
* @see {@link https://mswjs.io/docs/api/context/delay `ctx.delay()`}
|
|
678
697
|
*/
|
|
679
698
|
const delay = (durationMs) => {
|
|
680
699
|
return (res) => {
|
|
@@ -685,6 +704,8 @@
|
|
|
685
704
|
|
|
686
705
|
/**
|
|
687
706
|
* Sets a given list of GraphQL errors on the mocked response.
|
|
707
|
+
* @example res(ctx.errors([{ message: 'Unauthorized' }]))
|
|
708
|
+
* @see {@link https://mswjs.io/docs/api/context/errors}
|
|
688
709
|
*/
|
|
689
710
|
const errors = (errorsList) => {
|
|
690
711
|
return (res) => {
|
|
@@ -714,9 +735,10 @@
|
|
|
714
735
|
return requestParameters;
|
|
715
736
|
};
|
|
716
737
|
/**
|
|
717
|
-
*
|
|
718
|
-
*
|
|
719
|
-
*
|
|
738
|
+
* Performs a bypassed request inside a request handler.
|
|
739
|
+
* @example
|
|
740
|
+
* const originalResponse = await ctx.fetch(req)
|
|
741
|
+
* @see {@link https://mswjs.io/docs/api/context/fetch `ctx.fetch()`}
|
|
720
742
|
*/
|
|
721
743
|
const fetch = (input, requestInit = {}) => {
|
|
722
744
|
// Keep the default `window.fetch()` call signature
|
|
@@ -729,9 +751,10 @@
|
|
|
729
751
|
};
|
|
730
752
|
|
|
731
753
|
/**
|
|
732
|
-
* Sets a
|
|
733
|
-
*
|
|
734
|
-
* res(text('
|
|
754
|
+
* Sets a textual response body. Appends a `Content-Type: text/plain`
|
|
755
|
+
* header on the mocked response.
|
|
756
|
+
* @example res(ctx.text('Successful response'))
|
|
757
|
+
* @see {@link https://mswjs.io/docs/api/context/text `ctx.text()`}
|
|
735
758
|
*/
|
|
736
759
|
const text = (body) => {
|
|
737
760
|
return (res) => {
|
|
@@ -742,9 +765,11 @@
|
|
|
742
765
|
};
|
|
743
766
|
|
|
744
767
|
/**
|
|
745
|
-
* Sets
|
|
768
|
+
* Sets an XML response body. Appends a `Content-Type: text/xml` header
|
|
769
|
+
* on the mocked response.
|
|
746
770
|
* @example
|
|
747
|
-
* res(xml('<key>
|
|
771
|
+
* res(ctx.xml('<node key="value">Content</node>'))
|
|
772
|
+
* @see {@link https://mswjs.io/docs/api/context/xml `ctx.xml()`}
|
|
748
773
|
*/
|
|
749
774
|
const xml = (body) => {
|
|
750
775
|
return (res) => {
|
|
@@ -770,18 +795,18 @@
|
|
|
770
795
|
});
|
|
771
796
|
|
|
772
797
|
/*! *****************************************************************************
|
|
773
|
-
Copyright (c) Microsoft Corporation.
|
|
774
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
775
|
-
this file except in compliance with the License. You may obtain a copy of the
|
|
776
|
-
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
798
|
+
Copyright (c) Microsoft Corporation.
|
|
777
799
|
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
781
|
-
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
800
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
801
|
+
purpose with or without fee is hereby granted.
|
|
782
802
|
|
|
783
|
-
|
|
784
|
-
|
|
803
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
804
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
805
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
806
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
807
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
808
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
809
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
785
810
|
***************************************************************************** */
|
|
786
811
|
|
|
787
812
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
@@ -903,7 +928,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
|
|
|
903
928
|
return null;
|
|
904
929
|
}
|
|
905
930
|
// Fallback error message for any other registration errors.
|
|
906
|
-
console.error(`[MSW] Failed to register a Service Worker:\n\
|
|
931
|
+
console.error(`[MSW] Failed to register a Service Worker:\n\n${error.message}`);
|
|
907
932
|
return null;
|
|
908
933
|
}
|
|
909
934
|
return instance;
|
|
@@ -2759,6 +2784,12 @@ If this message still persists after updating, please report an issue: https://g
|
|
|
2759
2784
|
// Declare the list of event handlers on the module's scope
|
|
2760
2785
|
// so it persists between Fash refreshes of the application's code.
|
|
2761
2786
|
let listeners = [];
|
|
2787
|
+
/**
|
|
2788
|
+
* Creates a new mock Service Worker registration
|
|
2789
|
+
* with the given request handlers.
|
|
2790
|
+
* @param {RequestHandler[]} requestHandlers List of request handlers
|
|
2791
|
+
* @see {@link https://mswjs.io/docs/api/setup-worker `setupWorker`}
|
|
2792
|
+
*/
|
|
2762
2793
|
function setupWorker(...requestHandlers) {
|
|
2763
2794
|
requestHandlers.forEach((handler) => {
|
|
2764
2795
|
if (Array.isArray(handler))
|
|
@@ -3093,12 +3124,73 @@ ${queryParams
|
|
|
3093
3124
|
};
|
|
3094
3125
|
};
|
|
3095
3126
|
const rest = {
|
|
3127
|
+
/**
|
|
3128
|
+
* Captures a HEAD request by a given path.
|
|
3129
|
+
* @example
|
|
3130
|
+
* rest.head('/numbers', (req, res, ctx) => {
|
|
3131
|
+
* return res(ctx.status(302))
|
|
3132
|
+
* })
|
|
3133
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3134
|
+
*/
|
|
3096
3135
|
head: createRestHandler(exports.RESTMethods.HEAD),
|
|
3136
|
+
/**
|
|
3137
|
+
* Captures a GET request by a given path.
|
|
3138
|
+
* @example
|
|
3139
|
+
* rest.get('/numbers', (req, res, ctx) => {
|
|
3140
|
+
* return res(ctx.json([1, 2, 3]))
|
|
3141
|
+
* })
|
|
3142
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3143
|
+
*/
|
|
3097
3144
|
get: createRestHandler(exports.RESTMethods.GET),
|
|
3145
|
+
/**
|
|
3146
|
+
* Captures a POST request by a given path.
|
|
3147
|
+
* @example
|
|
3148
|
+
* rest.post('/numbers', (req, res, ctx) => {
|
|
3149
|
+
* return res(ctx.text('success'))
|
|
3150
|
+
* })
|
|
3151
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3152
|
+
*/
|
|
3098
3153
|
post: createRestHandler(exports.RESTMethods.POST),
|
|
3154
|
+
/**
|
|
3155
|
+
* Captures a PUT request by a given path.
|
|
3156
|
+
* @example
|
|
3157
|
+
* rest.put('/numbers', (req, res, ctx) => {
|
|
3158
|
+
* const { numbers } = req.body
|
|
3159
|
+
* return res(ctx.json(numbers))
|
|
3160
|
+
* })
|
|
3161
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3162
|
+
*/
|
|
3099
3163
|
put: createRestHandler(exports.RESTMethods.PUT),
|
|
3164
|
+
/**
|
|
3165
|
+
* Captures a DELETE request by a given path.
|
|
3166
|
+
* @example
|
|
3167
|
+
* rest.delete('/numbers', (req, res, ctx) => {
|
|
3168
|
+
* const index = req.url.searchParams.get('index')
|
|
3169
|
+
* prevNumbers.splice(index, 1)
|
|
3170
|
+
* return res(ctx.json(nextNumbers))
|
|
3171
|
+
* })
|
|
3172
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3173
|
+
*/
|
|
3100
3174
|
delete: createRestHandler(exports.RESTMethods.DELETE),
|
|
3175
|
+
/**
|
|
3176
|
+
* Captures a PATCH request by a given path.
|
|
3177
|
+
* @example
|
|
3178
|
+
* rest.patch('/numbers', (req, res, ctx) => {
|
|
3179
|
+
* const { numbers } = req.body
|
|
3180
|
+
* const nextNumbers = prevNumbers.concat(number)
|
|
3181
|
+
* return res(ctx.json(nextNumbers))
|
|
3182
|
+
* })
|
|
3183
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3184
|
+
*/
|
|
3101
3185
|
patch: createRestHandler(exports.RESTMethods.PATCH),
|
|
3186
|
+
/**
|
|
3187
|
+
* Captures an OPTIONS request by a given path.
|
|
3188
|
+
* @example
|
|
3189
|
+
* rest.options('/numbers', (req, res, ctx) => {
|
|
3190
|
+
* return res(ctx.set('Allow', 'GET,HEAD,POST'))
|
|
3191
|
+
* })
|
|
3192
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
3193
|
+
*/
|
|
3102
3194
|
options: createRestHandler(exports.RESTMethods.OPTIONS),
|
|
3103
3195
|
};
|
|
3104
3196
|
|
|
@@ -6271,7 +6363,7 @@ ${queryParams
|
|
|
6271
6363
|
if (!((_a = req.body) === null || _a === void 0 ? void 0 : _a.query)) {
|
|
6272
6364
|
return null;
|
|
6273
6365
|
}
|
|
6274
|
-
const { query, variables } = req.body;
|
|
6366
|
+
const { query, variables, } = req.body;
|
|
6275
6367
|
const { operationType, operationName } = parseQuery(query, expectedOperationType);
|
|
6276
6368
|
return {
|
|
6277
6369
|
operationType,
|
|
@@ -6339,10 +6431,42 @@ ${queryParams
|
|
|
6339
6431
|
};
|
|
6340
6432
|
};
|
|
6341
6433
|
const graphqlStandardHandlers = {
|
|
6434
|
+
/**
|
|
6435
|
+
* Captures any GraphQL operation, regardless of its name, under the current scope.
|
|
6436
|
+
* @example
|
|
6437
|
+
* graphql.operation((req, res, ctx) => {
|
|
6438
|
+
* return res(ctx.data({ name: 'John' }))
|
|
6439
|
+
* })
|
|
6440
|
+
* @see {@link https://mswjs.io/docs/api/graphql/operation `graphql.operation()`}
|
|
6441
|
+
*/
|
|
6342
6442
|
operation: createGraphQLOperationHandler('*'),
|
|
6443
|
+
/**
|
|
6444
|
+
* Captures a GraphQL query by a given name.
|
|
6445
|
+
* @example
|
|
6446
|
+
* graphql.query('GetUser', (req, res, ctx) => {
|
|
6447
|
+
* return res(ctx.data({ user: { name: 'John' } }))
|
|
6448
|
+
* })
|
|
6449
|
+
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
|
|
6450
|
+
*/
|
|
6343
6451
|
query: createGraphQLScopedHandler('query', '*'),
|
|
6452
|
+
/**
|
|
6453
|
+
* Captures a GraphQL mutation by a given name.
|
|
6454
|
+
* @example
|
|
6455
|
+
* graphql.mutation('SavePost', (req, res, ctx) => {
|
|
6456
|
+
* return res(ctx.data({ post: { id: 'abc-123' } }))
|
|
6457
|
+
* })
|
|
6458
|
+
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
|
|
6459
|
+
*/
|
|
6344
6460
|
mutation: createGraphQLScopedHandler('mutation', '*'),
|
|
6345
6461
|
};
|
|
6462
|
+
/**
|
|
6463
|
+
* Creates a GraphQL mocking API scoped to the given endpoint.
|
|
6464
|
+
* @param uri Endpoint URL, or path.
|
|
6465
|
+
* @example
|
|
6466
|
+
* const api = graphql.link('https://api.site.com/graphql)
|
|
6467
|
+
* api.query('GetUser', resolver)
|
|
6468
|
+
* @see {@link https://mswjs.io/docs/api/graphql/link `graphql.link()`}
|
|
6469
|
+
*/
|
|
6346
6470
|
function createGraphQLLink(uri) {
|
|
6347
6471
|
return {
|
|
6348
6472
|
operation: createGraphQLOperationHandler(uri),
|
package/native/index.js
CHANGED
|
@@ -13,18 +13,18 @@ var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
|
|
|
13
13
|
var tty__default = /*#__PURE__*/_interopDefaultLegacy(tty);
|
|
14
14
|
|
|
15
15
|
/*! *****************************************************************************
|
|
16
|
-
Copyright (c) Microsoft Corporation.
|
|
17
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
18
|
-
this file except in compliance with the License. You may obtain a copy of the
|
|
19
|
-
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
Copyright (c) Microsoft Corporation.
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
24
|
-
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
18
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
19
|
+
purpose with or without fee is hereby granted.
|
|
25
20
|
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
22
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
23
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
24
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
25
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
26
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
27
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
28
28
|
***************************************************************************** */
|
|
29
29
|
|
|
30
30
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
@@ -1554,7 +1554,7 @@ function supportsColor(haveStream, streamIsTTY) {
|
|
|
1554
1554
|
}
|
|
1555
1555
|
|
|
1556
1556
|
if ('CI' in env) {
|
|
1557
|
-
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
|
|
1557
|
+
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
|
|
1558
1558
|
return 1;
|
|
1559
1559
|
}
|
|
1560
1560
|
|
|
@@ -1565,10 +1565,6 @@ function supportsColor(haveStream, streamIsTTY) {
|
|
|
1565
1565
|
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
1566
1566
|
}
|
|
1567
1567
|
|
|
1568
|
-
if ('GITHUB_ACTIONS' in env) {
|
|
1569
|
-
return 1;
|
|
1570
|
-
}
|
|
1571
|
-
|
|
1572
1568
|
if (env.COLORTERM === 'truecolor') {
|
|
1573
1569
|
return 3;
|
|
1574
1570
|
}
|
|
@@ -2315,6 +2311,13 @@ var statuses = {
|
|
|
2315
2311
|
"511": "Network Authentication Required"
|
|
2316
2312
|
};
|
|
2317
2313
|
|
|
2314
|
+
/**
|
|
2315
|
+
* Sets a response status code and text.
|
|
2316
|
+
* @example
|
|
2317
|
+
* res(ctx.status(301))
|
|
2318
|
+
* res(ctx.status(400, 'Custom status text'))
|
|
2319
|
+
* @see {@link https://mswjs.io/docs/api/context/status `ctx.status()`}
|
|
2320
|
+
*/
|
|
2318
2321
|
const status = (statusCode, statusText) => {
|
|
2319
2322
|
return (res) => {
|
|
2320
2323
|
res.status = statusCode;
|
|
@@ -2324,6 +2327,10 @@ const status = (statusCode, statusText) => {
|
|
|
2324
2327
|
};
|
|
2325
2328
|
};
|
|
2326
2329
|
|
|
2330
|
+
/**
|
|
2331
|
+
* Sets one or multiple response headers.
|
|
2332
|
+
* @see {@link https://mswjs.io/docs/api/context/set `ctx.set()`}
|
|
2333
|
+
*/
|
|
2327
2334
|
function set(...args) {
|
|
2328
2335
|
return (res) => {
|
|
2329
2336
|
const [name, value] = args;
|
|
@@ -2367,10 +2374,11 @@ const getRandomServerResponseTime = () => {
|
|
|
2367
2374
|
MIN_SERVER_RESPONSE_TIME);
|
|
2368
2375
|
};
|
|
2369
2376
|
/**
|
|
2370
|
-
* Delays the
|
|
2377
|
+
* Delays the response by the given duration (ms).
|
|
2371
2378
|
* @example
|
|
2372
|
-
* res(delay()) // realistic server response time
|
|
2373
|
-
* res(delay(
|
|
2379
|
+
* res(ctx.delay()) // realistic server response time
|
|
2380
|
+
* res(ctx.delay(1200))
|
|
2381
|
+
* @see {@link https://mswjs.io/docs/api/context/delay `ctx.delay()`}
|
|
2374
2382
|
*/
|
|
2375
2383
|
const delay = (durationMs) => {
|
|
2376
2384
|
return (res) => {
|
|
@@ -2396,9 +2404,10 @@ const createFetchRequestParameters = (input) => {
|
|
|
2396
2404
|
return requestParameters;
|
|
2397
2405
|
};
|
|
2398
2406
|
/**
|
|
2399
|
-
*
|
|
2400
|
-
*
|
|
2401
|
-
*
|
|
2407
|
+
* Performs a bypassed request inside a request handler.
|
|
2408
|
+
* @example
|
|
2409
|
+
* const originalResponse = await ctx.fetch(req)
|
|
2410
|
+
* @see {@link https://mswjs.io/docs/api/context/fetch `ctx.fetch()`}
|
|
2402
2411
|
*/
|
|
2403
2412
|
const fetch = (input, requestInit = {}) => {
|
|
2404
2413
|
// Keep the default `window.fetch()` call signature
|
|
@@ -3979,9 +3988,6 @@ const DEFAULT_LISTEN_OPTIONS = {
|
|
|
3979
3988
|
* Useful to generate identical API using different patches to request issuing modules.
|
|
3980
3989
|
*/
|
|
3981
3990
|
function createSetupServer(...interceptors) {
|
|
3982
|
-
/**
|
|
3983
|
-
* Sets up a server-side requests interception with the given mock definition.
|
|
3984
|
-
*/
|
|
3985
3991
|
return function setupServer(...requestHandlers) {
|
|
3986
3992
|
requestHandlers.forEach((handler) => {
|
|
3987
3993
|
if (Array.isArray(handler))
|
package/node/index.js
CHANGED
|
@@ -15,18 +15,18 @@ var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
|
|
|
15
15
|
var tty__default = /*#__PURE__*/_interopDefaultLegacy(tty);
|
|
16
16
|
|
|
17
17
|
/*! *****************************************************************************
|
|
18
|
-
Copyright (c) Microsoft Corporation.
|
|
19
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
20
|
-
this file except in compliance with the License. You may obtain a copy of the
|
|
21
|
-
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
Copyright (c) Microsoft Corporation.
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
26
|
-
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
20
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
21
|
+
purpose with or without fee is hereby granted.
|
|
27
22
|
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
24
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
25
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
26
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
27
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
28
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
29
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
30
30
|
***************************************************************************** */
|
|
31
31
|
|
|
32
32
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
@@ -1556,7 +1556,7 @@ function supportsColor(haveStream, streamIsTTY) {
|
|
|
1556
1556
|
}
|
|
1557
1557
|
|
|
1558
1558
|
if ('CI' in env) {
|
|
1559
|
-
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
|
|
1559
|
+
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
|
|
1560
1560
|
return 1;
|
|
1561
1561
|
}
|
|
1562
1562
|
|
|
@@ -1567,10 +1567,6 @@ function supportsColor(haveStream, streamIsTTY) {
|
|
|
1567
1567
|
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
1568
1568
|
}
|
|
1569
1569
|
|
|
1570
|
-
if ('GITHUB_ACTIONS' in env) {
|
|
1571
|
-
return 1;
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
1570
|
if (env.COLORTERM === 'truecolor') {
|
|
1575
1571
|
return 3;
|
|
1576
1572
|
}
|
|
@@ -2317,6 +2313,13 @@ var statuses = {
|
|
|
2317
2313
|
"511": "Network Authentication Required"
|
|
2318
2314
|
};
|
|
2319
2315
|
|
|
2316
|
+
/**
|
|
2317
|
+
* Sets a response status code and text.
|
|
2318
|
+
* @example
|
|
2319
|
+
* res(ctx.status(301))
|
|
2320
|
+
* res(ctx.status(400, 'Custom status text'))
|
|
2321
|
+
* @see {@link https://mswjs.io/docs/api/context/status `ctx.status()`}
|
|
2322
|
+
*/
|
|
2320
2323
|
const status = (statusCode, statusText) => {
|
|
2321
2324
|
return (res) => {
|
|
2322
2325
|
res.status = statusCode;
|
|
@@ -2326,6 +2329,10 @@ const status = (statusCode, statusText) => {
|
|
|
2326
2329
|
};
|
|
2327
2330
|
};
|
|
2328
2331
|
|
|
2332
|
+
/**
|
|
2333
|
+
* Sets one or multiple response headers.
|
|
2334
|
+
* @see {@link https://mswjs.io/docs/api/context/set `ctx.set()`}
|
|
2335
|
+
*/
|
|
2329
2336
|
function set(...args) {
|
|
2330
2337
|
return (res) => {
|
|
2331
2338
|
const [name, value] = args;
|
|
@@ -2369,10 +2376,11 @@ const getRandomServerResponseTime = () => {
|
|
|
2369
2376
|
MIN_SERVER_RESPONSE_TIME);
|
|
2370
2377
|
};
|
|
2371
2378
|
/**
|
|
2372
|
-
* Delays the
|
|
2379
|
+
* Delays the response by the given duration (ms).
|
|
2373
2380
|
* @example
|
|
2374
|
-
* res(delay()) // realistic server response time
|
|
2375
|
-
* res(delay(
|
|
2381
|
+
* res(ctx.delay()) // realistic server response time
|
|
2382
|
+
* res(ctx.delay(1200))
|
|
2383
|
+
* @see {@link https://mswjs.io/docs/api/context/delay `ctx.delay()`}
|
|
2376
2384
|
*/
|
|
2377
2385
|
const delay = (durationMs) => {
|
|
2378
2386
|
return (res) => {
|
|
@@ -2398,9 +2406,10 @@ const createFetchRequestParameters = (input) => {
|
|
|
2398
2406
|
return requestParameters;
|
|
2399
2407
|
};
|
|
2400
2408
|
/**
|
|
2401
|
-
*
|
|
2402
|
-
*
|
|
2403
|
-
*
|
|
2409
|
+
* Performs a bypassed request inside a request handler.
|
|
2410
|
+
* @example
|
|
2411
|
+
* const originalResponse = await ctx.fetch(req)
|
|
2412
|
+
* @see {@link https://mswjs.io/docs/api/context/fetch `ctx.fetch()`}
|
|
2404
2413
|
*/
|
|
2405
2414
|
const fetch = (input, requestInit = {}) => {
|
|
2406
2415
|
// Keep the default `window.fetch()` call signature
|
|
@@ -3981,9 +3990,6 @@ const DEFAULT_LISTEN_OPTIONS = {
|
|
|
3981
3990
|
* Useful to generate identical API using different patches to request issuing modules.
|
|
3982
3991
|
*/
|
|
3983
3992
|
function createSetupServer(...interceptors) {
|
|
3984
|
-
/**
|
|
3985
|
-
* Sets up a server-side requests interception with the given mock definition.
|
|
3986
|
-
*/
|
|
3987
3993
|
return function setupServer(...requestHandlers) {
|
|
3988
3994
|
requestHandlers.forEach((handler) => {
|
|
3989
3995
|
if (Array.isArray(handler))
|
|
@@ -4083,6 +4089,11 @@ ${source.bold(meta.header)}
|
|
|
4083
4089
|
};
|
|
4084
4090
|
}
|
|
4085
4091
|
|
|
4092
|
+
/**
|
|
4093
|
+
* Sets up a requests interception in NodeJS with the given request handlers.
|
|
4094
|
+
* @param {RequestHandler[]} requestHandlers List of request handlers.
|
|
4095
|
+
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer`}
|
|
4096
|
+
*/
|
|
4086
4097
|
const setupServer = createSetupServer(ClientRequest.interceptClientRequest, XMLHttpRequest.interceptXMLHttpRequest);
|
|
4087
4098
|
|
|
4088
4099
|
exports.setupServer = setupServer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.4",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.",
|
|
5
5
|
"main": "lib/umd/index.js",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -72,50 +72,51 @@
|
|
|
72
72
|
"node-match-path": "^0.6.0",
|
|
73
73
|
"node-request-interceptor": "^0.5.3",
|
|
74
74
|
"statuses": "^2.0.0",
|
|
75
|
-
"yargs": "^16.
|
|
75
|
+
"yargs": "^16.2.0"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@babel/core": "^7.12.
|
|
79
|
-
"@babel/preset-env": "^7.12.
|
|
78
|
+
"@babel/core": "^7.12.10",
|
|
79
|
+
"@babel/preset-env": "^7.12.11",
|
|
80
80
|
"@rollup/plugin-commonjs": "^16.0.0",
|
|
81
81
|
"@rollup/plugin-inject": "^4.0.2",
|
|
82
82
|
"@rollup/plugin-json": "^4.1.0",
|
|
83
83
|
"@rollup/plugin-node-resolve": "^10.0.0",
|
|
84
84
|
"@rollup/plugin-replace": "^2.3.4",
|
|
85
85
|
"@types/express": "^4.17.9",
|
|
86
|
-
"@types/fs-extra": "^9.0.
|
|
87
|
-
"@types/jest": "^26.0.
|
|
86
|
+
"@types/fs-extra": "^9.0.6",
|
|
87
|
+
"@types/jest": "^26.0.19",
|
|
88
88
|
"@types/json-bigint": "^1.0.0",
|
|
89
|
-
"@types/node": "^14.14.
|
|
89
|
+
"@types/node": "^14.14.16",
|
|
90
90
|
"@types/node-fetch": "^2.5.7",
|
|
91
|
-
"@types/puppeteer": "^5.4.
|
|
91
|
+
"@types/puppeteer": "^5.4.2",
|
|
92
92
|
"@types/webpack-dev-server": "^3.11.1",
|
|
93
|
-
"@typescript-eslint/eslint-plugin": "^4.
|
|
94
|
-
"@typescript-eslint/parser": "^4.
|
|
93
|
+
"@typescript-eslint/eslint-plugin": "^4.11.0",
|
|
94
|
+
"@typescript-eslint/parser": "^4.11.0",
|
|
95
95
|
"apollo-fetch": "^0.7.0",
|
|
96
|
-
"babel-loader": "^8.2.
|
|
96
|
+
"babel-loader": "^8.2.2",
|
|
97
97
|
"babel-minify": "^0.5.1",
|
|
98
|
-
"cross-env": "^7.0.
|
|
99
|
-
"eslint": "^7.
|
|
100
|
-
"eslint-config-prettier": "^
|
|
101
|
-
"eslint-plugin-prettier": "^3.
|
|
98
|
+
"cross-env": "^7.0.3",
|
|
99
|
+
"eslint": "^7.16.0",
|
|
100
|
+
"eslint-config-prettier": "^7.1.0",
|
|
101
|
+
"eslint-plugin-prettier": "^3.3.0",
|
|
102
102
|
"express": "^4.17.1",
|
|
103
103
|
"fs-extra": "^9.0.1",
|
|
104
104
|
"html-webpack-plugin": "^4.5.0",
|
|
105
|
-
"husky": "^4.3.
|
|
105
|
+
"husky": "^4.3.6",
|
|
106
106
|
"ignore-loader": "^0.1.2",
|
|
107
107
|
"jest": "^26.6.3",
|
|
108
108
|
"json-bigint": "^1.0.0",
|
|
109
109
|
"lint-staged": "^10.5.1",
|
|
110
|
-
"prettier": "^2.1
|
|
110
|
+
"prettier": "^2.2.1",
|
|
111
111
|
"puppeteer": "^5.5.0",
|
|
112
112
|
"regenerator-runtime": "^0.13.7",
|
|
113
113
|
"rimraf": "^3.0.2",
|
|
114
|
-
"rollup": "^2.
|
|
114
|
+
"rollup": "^2.35.1",
|
|
115
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
115
116
|
"rollup-plugin-typescript2": "^0.29.0",
|
|
116
117
|
"ts-jest": "^26.4.4",
|
|
117
|
-
"ts-loader": "^8.0.
|
|
118
|
-
"ts-node": "^9.
|
|
118
|
+
"ts-loader": "^8.0.12",
|
|
119
|
+
"ts-node": "^9.1.1",
|
|
119
120
|
"typescript": "^4.0.5",
|
|
120
121
|
"url-loader": "^4.1.1",
|
|
121
122
|
"webpack": "^4.44.1",
|