UNPKG

msw

Version:

Seamless REST/GraphQL API mocking library for browser and Node.js.

92 lines 2.8 kB
import { invariant } from "outvariant"; import { Emitter } from "strict-event-emitter"; import { devUtils } from './utils/internal/devUtils.mjs'; import { pipeEvents } from './utils/internal/pipeEvents.mjs'; import { toReadonlyArray } from './utils/internal/toReadonlyArray.mjs'; import { Disposable } from './utils/internal/Disposable.mjs'; class HandlersController { } class InMemoryHandlersController { constructor(initialHandlers) { this.initialHandlers = initialHandlers; this.handlers = [...initialHandlers]; } handlers; prepend(runtimeHandles) { this.handlers.unshift(...runtimeHandles); } reset(nextHandlers) { this.handlers = nextHandlers.length > 0 ? [...nextHandlers] : [...this.initialHandlers]; } currentHandlers() { return this.handlers; } } class SetupApi extends Disposable { handlersController; emitter; publicEmitter; events; constructor(...initialHandlers) { super(); invariant( this.validateHandlers(initialHandlers), devUtils.formatMessage( `Failed to apply given request handlers: invalid input. Did you forget to spread the request handlers Array?` ) ); this.handlersController = new InMemoryHandlersController(initialHandlers); this.emitter = new Emitter(); this.publicEmitter = new Emitter(); pipeEvents(this.emitter, this.publicEmitter); this.events = this.createLifeCycleEvents(); this.subscriptions.push(() => { this.emitter.removeAllListeners(); this.publicEmitter.removeAllListeners(); }); } validateHandlers(handlers) { return handlers.every((handler) => !Array.isArray(handler)); } use(...runtimeHandlers) { invariant( this.validateHandlers(runtimeHandlers), devUtils.formatMessage( `Failed to call "use()" with the given request handlers: invalid input. Did you forget to spread the array of request handlers?` ) ); this.handlersController.prepend(runtimeHandlers); } restoreHandlers() { this.handlersController.currentHandlers().forEach((handler) => { if ("isUsed" in handler) { handler.isUsed = false; } }); } resetHandlers(...nextHandlers) { this.handlersController.reset(nextHandlers); } listHandlers() { return toReadonlyArray(this.handlersController.currentHandlers()); } createLifeCycleEvents() { return { on: (...args) => { return this.publicEmitter.on(...args); }, removeListener: (...args) => { return this.publicEmitter.removeListener(...args); }, removeAllListeners: (...args) => { return this.publicEmitter.removeAllListeners(...args); } }; } } export { HandlersController, InMemoryHandlersController, SetupApi }; //# sourceMappingURL=SetupApi.mjs.map