msw 0.49.3 → 1.0.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.
@@ -1,8 +1,8 @@
1
1
  import { IsomorphicResponse, BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
2
- import { I as LifeCycleEventsMap, m as SetupApi, b as RequestHandler, l as SharedOptions } from './SetupApi-f4099ef3.js';
2
+ import { L as LifeCycleEventsMap, S as SetupApi, K as SetupServer, c as RequestHandler, o as SharedOptions } from './glossary-de6278a9.js';
3
3
 
4
4
  type ServerLifecycleEventsMap = LifeCycleEventsMap<IsomorphicResponse>;
5
- declare class SetupServerApi extends SetupApi<ServerLifecycleEventsMap> {
5
+ declare class SetupServerApi extends SetupApi<ServerLifecycleEventsMap> implements SetupServer {
6
6
  protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
7
7
  private resolvedOptions;
8
8
  constructor(interceptors: Array<{
@@ -1,6 +1,7 @@
1
1
  import { Emitter, EventMap } from 'strict-event-emitter';
2
+ import { PartialDeep } from 'type-fest';
3
+ import { RequestInit as RequestInit$1, IsomorphicRequest, IsomorphicResponse } from '@mswjs/interceptors';
2
4
  import { FlatHeadersObject, Headers } from 'headers-polyfill';
3
- import { RequestInit as RequestInit$1, IsomorphicRequest } from '@mswjs/interceptors';
4
5
 
5
6
  type HeadersObject<KeyType extends string = string> = Record<KeyType, string | string[]>;
6
7
  /**
@@ -166,7 +167,7 @@ interface SerializedResponse<BodyType extends DefaultBodyType = any> {
166
167
  }
167
168
  type StartReturnType = Promise<ServiceWorkerRegistration | undefined>;
168
169
  type StopHandler = () => void;
169
- interface SetupWorkerApi {
170
+ interface SetupWorker {
170
171
  /**
171
172
  * Registers and activates the mock Service Worker.
172
173
  * @see {@link https://mswjs.io/docs/api/setup-worker/start `worker.start()`}
@@ -341,4 +342,44 @@ declare abstract class SetupApi<EventsMap extends EventMap> {
341
342
  abstract printHandlers(): void;
342
343
  }
343
344
 
344
- export { AsyncResponseResolverReturnType as A, RequestMode as B, RequestRedirect as C, DefaultContext as D, RequestDestination as E, RequestPriority as F, RequestReferrerPolicy as G, MockedRequestInit as H, LifeCycleEventsMap as I, LifeCycleEventEmitter as L, MockedRequest as M, ResponseTransformer as R, SetupWorkerApi as S, set as a, RequestHandler as b, DefaultBodyType as c, delay as d, ResponseResolver as e, fetch as f, ResponseResolutionContext as g, SerializedResponse as h, RequestHandlerDefaultInfo as i, MockedResponse as j, ResponseLookupResult as k, SharedOptions as l, SetupApi as m, defaultResponse as n, createResponseComposition as o, defaultContext as p, StartOptions as q, response as r, status as s, ResponseResolverReturnType as t, DefaultRequestMultipartBody as u, ResponseComposition as v, ResponseCompositionOptions as w, ResponseFunction as x, DelayMode as y, RequestCache as z };
345
+ type ServerLifecycleEventsMap = LifeCycleEventsMap<IsomorphicResponse>;
346
+ interface SetupServer {
347
+ /**
348
+ * Starts requests interception based on the previously provided request handlers.
349
+ * @see {@link https://mswjs.io/docs/api/setup-server/listen `server.listen()`}
350
+ */
351
+ listen(options?: PartialDeep<SharedOptions>): void;
352
+ /**
353
+ * Stops requests interception by restoring all augmented modules.
354
+ * @see {@link https://mswjs.io/docs/api/setup-server/close `server.close()`}
355
+ */
356
+ close(): void;
357
+ /**
358
+ * Prepends given request handlers to the list of existing handlers.
359
+ * @see {@link https://mswjs.io/docs/api/setup-server/use `server.use()`}
360
+ */
361
+ use(...handlers: RequestHandler[]): void;
362
+ /**
363
+ * Marks all request handlers that respond using `res.once()` as unused.
364
+ * @see {@link https://mswjs.io/docs/api/setup-server/restore-handlers `server.restore-handlers()`}
365
+ */
366
+ restoreHandlers(): void;
367
+ /**
368
+ * Resets request handlers to the initial list given to the `setupServer` call, or to the explicit next request handlers list, if given.
369
+ * @see {@link https://mswjs.io/docs/api/setup-server/reset-handlers `server.reset-handlers()`}
370
+ */
371
+ resetHandlers(...nextHandlers: RequestHandler[]): void;
372
+ /**
373
+ * Returns a readonly list of currently active request handlers.
374
+ * @see {@link https://mswjs.io/docs/api/setup-server/list-handlers `server.listHandlers()`}
375
+ */
376
+ listHandlers(): ReadonlyArray<RequestHandler<RequestHandlerDefaultInfo, MockedRequest<DefaultBodyType>, any, MockedRequest<DefaultBodyType>>>;
377
+ /**
378
+ * Lists all active request handlers.
379
+ * @see {@link https://mswjs.io/docs/api/setup-server/print-handlers `server.print-handlers()`}
380
+ */
381
+ printHandlers(): void;
382
+ events: LifeCycleEventEmitter<ServerLifecycleEventsMap>;
383
+ }
384
+
385
+ export { AsyncResponseResolverReturnType as A, DelayMode as B, RequestCache as C, DefaultContext as D, RequestMode as E, RequestRedirect as F, RequestDestination as G, RequestPriority as H, RequestReferrerPolicy as I, MockedRequestInit as J, SetupServer as K, LifeCycleEventsMap as L, MockedRequest as M, ResponseTransformer as R, SetupApi as S, WorkerLifecycleEventsMap as W, set as a, SetupWorker as b, RequestHandler as c, delay as d, StartOptions as e, fetch as f, StartReturnType as g, DefaultBodyType as h, ResponseResolver as i, ResponseResolutionContext as j, SerializedResponse as k, RequestHandlerDefaultInfo as l, MockedResponse as m, ResponseLookupResult as n, SharedOptions as o, ServerLifecycleEventsMap as p, defaultResponse as q, response as r, status as s, createResponseComposition as t, defaultContext as u, ResponseResolverReturnType as v, DefaultRequestMultipartBody as w, ResponseComposition as x, ResponseCompositionOptions as y, ResponseFunction as z };
package/lib/iife/index.js CHANGED
@@ -350,7 +350,7 @@ var MockServiceWorker = (() => {
350
350
  }(Error);
351
351
  exports.InvariantError = InvariantError;
352
352
  function createInvariantWith(ErrorConstructor) {
353
- var invariant5 = function(predicate, message) {
353
+ var invariant6 = function(predicate, message) {
354
354
  var positionals = [];
355
355
  for (var _i = 2; _i < arguments.length; _i++) {
356
356
  positionals[_i - 2] = arguments[_i];
@@ -363,7 +363,7 @@ var MockServiceWorker = (() => {
363
363
  throw error2;
364
364
  }
365
365
  };
366
- return invariant5;
366
+ return invariant6;
367
367
  }
368
368
  exports.createInvariantWith = createInvariantWith;
369
369
  function polymorphicInvariant(ErrorClass) {
@@ -2278,7 +2278,7 @@ var MockServiceWorker = (() => {
2278
2278
  return InvariantError2;
2279
2279
  }(Error);
2280
2280
  exports.InvariantError = InvariantError;
2281
- function invariant5(predicate, message) {
2281
+ function invariant6(predicate, message) {
2282
2282
  var positionals = [];
2283
2283
  for (var _i = 2; _i < arguments.length; _i++) {
2284
2284
  positionals[_i - 2] = arguments[_i];
@@ -2287,7 +2287,7 @@ var MockServiceWorker = (() => {
2287
2287
  throw new (InvariantError.bind.apply(InvariantError, __spreadArray([void 0, message], positionals)))();
2288
2288
  }
2289
2289
  }
2290
- exports.invariant = invariant5;
2290
+ exports.invariant = invariant6;
2291
2291
  }
2292
2292
  });
2293
2293
 
@@ -8793,6 +8793,7 @@ Invalid value has been removed from localStorage to prevent subsequent failed pa
8793
8793
  RequestHandler: () => RequestHandler,
8794
8794
  RestHandler: () => RestHandler,
8795
8795
  SetupApi: () => SetupApi,
8796
+ SetupWorkerApi: () => SetupWorkerApi,
8796
8797
  cleanUrl: () => cleanUrl,
8797
8798
  compose: () => compose,
8798
8799
  context: () => context_exports,
@@ -9310,8 +9311,35 @@ Invalid value has been removed from localStorage to prevent subsequent failed pa
9310
9311
  };
9311
9312
  };
9312
9313
 
9314
+ // src/utils/internal/checkGlobals.ts
9315
+ var import_outvariant2 = __toESM(require_lib2());
9316
+
9317
+ // src/utils/internal/devUtils.ts
9318
+ var import_outvariant = __toESM(require_lib2());
9319
+ var LIBRARY_PREFIX = "[MSW]";
9320
+ function formatMessage(message, ...positionals) {
9321
+ const interpolatedMessage = (0, import_outvariant.format)(message, ...positionals);
9322
+ return `${LIBRARY_PREFIX} ${interpolatedMessage}`;
9323
+ }
9324
+ function warn(message, ...positionals) {
9325
+ console.warn(formatMessage(message, ...positionals));
9326
+ }
9327
+ function error(message, ...positionals) {
9328
+ console.error(formatMessage(message, ...positionals));
9329
+ }
9330
+ var devUtils = {
9331
+ formatMessage,
9332
+ warn,
9333
+ error
9334
+ };
9335
+
9336
+ // src/utils/internal/checkGlobals.ts
9337
+ function checkGlobals() {
9338
+ (0, import_outvariant2.invariant)(typeof URL !== "undefined", devUtils.formatMessage(`Global "URL" class is not defined. This likely means that you're running MSW in an environment that doesn't support all Node.js standard API (e.g. React Native). If that's the case, please use an appropriate polyfill for the "URL" class, like "react-native-url-polyfill".`));
9339
+ }
9340
+
9313
9341
  // src/setupWorker/setupWorker.ts
9314
- var import_outvariant4 = __toESM(require_lib2());
9342
+ var import_outvariant5 = __toESM(require_lib2());
9315
9343
  var import_is_node_process3 = __toESM(require_lib());
9316
9344
 
9317
9345
  // src/setupWorker/start/createStartHandler.ts
@@ -9339,25 +9367,6 @@ Invalid value has been removed from localStorage to prevent subsequent failed pa
9339
9367
  return new URL(workerUrl, location.href).href;
9340
9368
  }
9341
9369
 
9342
- // src/utils/internal/devUtils.ts
9343
- var import_outvariant = __toESM(require_lib2());
9344
- var LIBRARY_PREFIX = "[MSW]";
9345
- function formatMessage(message, ...positionals) {
9346
- const interpolatedMessage = (0, import_outvariant.format)(message, ...positionals);
9347
- return `${LIBRARY_PREFIX} ${interpolatedMessage}`;
9348
- }
9349
- function warn(message, ...positionals) {
9350
- console.warn(formatMessage(message, ...positionals));
9351
- }
9352
- function error(message, ...positionals) {
9353
- console.error(formatMessage(message, ...positionals));
9354
- }
9355
- var devUtils = {
9356
- formatMessage,
9357
- warn,
9358
- error
9359
- };
9360
-
9361
9370
  // src/setupWorker/start/utils/getWorkerInstance.ts
9362
9371
  var getWorkerInstance = async (url, options = {}, findWorker) => {
9363
9372
  const absoluteWorkerUrl = getAbsoluteWorkerUrl(url);
@@ -9693,7 +9702,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
9693
9702
  }
9694
9703
 
9695
9704
  // node_modules/graphql/jsutils/invariant.mjs
9696
- function invariant(condition, message) {
9705
+ function invariant2(condition, message) {
9697
9706
  const booleanCondition = Boolean(condition);
9698
9707
  if (!booleanCondition) {
9699
9708
  throw new Error(message != null ? message : "Unexpected invariant triggered.");
@@ -9706,7 +9715,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
9706
9715
  let lastLineStart = 0;
9707
9716
  let line = 1;
9708
9717
  for (const match2 of source.body.matchAll(LineRegExp)) {
9709
- typeof match2.index === "number" || invariant(false);
9718
+ typeof match2.index === "number" || invariant2(false);
9710
9719
  if (match2.index >= position) {
9711
9720
  break;
9712
9721
  }
@@ -12199,7 +12208,7 @@ spurious results.`);
12199
12208
  };
12200
12209
 
12201
12210
  // src/context/field.ts
12202
- var import_outvariant2 = __toESM(require_lib2());
12211
+ var import_outvariant3 = __toESM(require_lib2());
12203
12212
  var field = (fieldName, fieldValue) => {
12204
12213
  return (res) => {
12205
12214
  validateFieldName(fieldName);
@@ -12209,10 +12218,10 @@ spurious results.`);
12209
12218
  };
12210
12219
  };
12211
12220
  function validateFieldName(fieldName) {
12212
- (0, import_outvariant2.invariant)(fieldName.trim() !== "", devUtils.formatMessage("Failed to set a custom field on a GraphQL response: field name cannot be empty."));
12213
- (0, import_outvariant2.invariant)(fieldName !== "data", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.data()" instead?', fieldName));
12214
- (0, import_outvariant2.invariant)(fieldName !== "errors", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.errors()" instead?', fieldName));
12215
- (0, import_outvariant2.invariant)(fieldName !== "extensions", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.extensions()" instead?', fieldName));
12221
+ (0, import_outvariant3.invariant)(fieldName.trim() !== "", devUtils.formatMessage("Failed to set a custom field on a GraphQL response: field name cannot be empty."));
12222
+ (0, import_outvariant3.invariant)(fieldName !== "data", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.data()" instead?', fieldName));
12223
+ (0, import_outvariant3.invariant)(fieldName !== "errors", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.errors()" instead?', fieldName));
12224
+ (0, import_outvariant3.invariant)(fieldName !== "extensions", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.extensions()" instead?', fieldName));
12216
12225
  }
12217
12226
 
12218
12227
  // src/utils/internal/tryCatch.ts
@@ -12814,7 +12823,7 @@ If this message still persists after updating, please report an issue: https://g
12814
12823
  }
12815
12824
 
12816
12825
  // src/SetupApi.ts
12817
- var import_outvariant3 = __toESM(require_lib2());
12826
+ var import_outvariant4 = __toESM(require_lib2());
12818
12827
  var import_strict_event_emitter = __toESM(require_lib11());
12819
12828
 
12820
12829
  // src/utils/internal/pipeEvents.ts
@@ -12850,7 +12859,7 @@ If this message still persists after updating, please report an issue: https://g
12850
12859
  }
12851
12860
  validateHandlers(...handlers) {
12852
12861
  for (const handler of handlers) {
12853
- (0, import_outvariant3.invariant)(!Array.isArray(handler), devUtils.formatMessage('Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.'), this.constructor.name);
12862
+ (0, import_outvariant4.invariant)(!Array.isArray(handler), devUtils.formatMessage('Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.'), this.constructor.name);
12854
12863
  }
12855
12864
  }
12856
12865
  dispose() {
@@ -12892,7 +12901,7 @@ If this message still persists after updating, please report an issue: https://g
12892
12901
  super(...handlers);
12893
12902
  this.startHandler = null;
12894
12903
  this.stopHandler = null;
12895
- (0, import_outvariant4.invariant)(!(0, import_is_node_process3.isNodeProcess)(), devUtils.formatMessage("Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead."));
12904
+ (0, import_outvariant5.invariant)(!(0, import_is_node_process3.isNodeProcess)(), devUtils.formatMessage("Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead."));
12896
12905
  this.listeners = [];
12897
12906
  this.context = this.createWorkerContext();
12898
12907
  }
@@ -13039,6 +13048,9 @@ If this message still persists after updating, please report an issue: https://g
13039
13048
  var graphql = __spreadProps(__spreadValues({}, standardGraphQLHandlers), {
13040
13049
  link: createGraphQLLink
13041
13050
  });
13051
+
13052
+ // src/index.ts
13053
+ checkGlobals();
13042
13054
  return __toCommonJS(src_exports);
13043
13055
  })();
13044
13056
  /*!