UNPKG

lyrebird

Version:

A wrapper for writing more reusable and cleaner mocks using mswjs.io.

178 lines (173 loc) 5.43 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); var __export = (target, all) => { __markAsModule(target); for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __reExport = (target, module2, desc) => { if (module2 && typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && key !== "default") __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); } return target; }; var __toModule = (module2) => { return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); }; // src/index.ts __export(exports, { HandlerCollection: () => HandlerCollection, MockServer: () => MockServer, RestHandler: () => RestHandler }); // src/RestHandler.ts var import_msw = __toModule(require("msw")); var RestHandler = class { constructor() { this.method = import_msw.RESTMethods.GET; this.responseStatusCode = 200; this.name = null; } on(method, url) { this.method = typeof method === "string" ? import_msw.RESTMethods[method] : method; this.url = url; return this; } onGet(url) { return this.on(import_msw.RESTMethods.GET, url); } onPost(url) { return this.on(import_msw.RESTMethods.POST, url); } onPatch(url) { return this.on(import_msw.RESTMethods.PATCH, url); } onPut(url) { return this.on(import_msw.RESTMethods.PUT, url); } onOptions(url) { return this.on(import_msw.RESTMethods.OPTIONS, url); } onHead(url) { return this.on(import_msw.RESTMethods.HEAD, url); } onDelete(url) { return this.on(import_msw.RESTMethods.DELETE, url); } withParams(params) { this.params = params; return this; } withPayload(payload) { this.requestPayload = payload; return this; } reply(status, body) { this.responseStatusCode = status; this.responseBody = body; return this; } resolve(resolver) { this.resolver = resolver; return this; } as(name) { this.name = name; return this; } run() { if (!this.url) throw new Error("No url provided"); return new import_msw.RestHandler(this.method, this.url, (request, response, context) => { if (this.resolver) { return this.resolver({ request, response, context }); } if (this.params && paramsMismatched(this.params, request.url.searchParams)) { return response.networkError("Params mismatch."); } if (payloadMismatched(this.requestPayload, request.body)) { return response.networkError("Payload mismatch."); } return response(context.status(this.responseStatusCode), context.json(this.responseBody)); }); } }; function paramsMismatched(expected, actual) { for (const property in expected) { if (expected[property] !== actual.get(property)) { return true; } } return false; } function payloadMismatched(expected, actual) { return expected && JSON.stringify(expected) !== JSON.stringify(actual); } // src/MockServer.ts var MockServer = class { constructor(server, options) { this.server = server; if (options) { this.collection = options.collection; } } use(...handlers) { if (Array.isArray(handlers)) { handlers.forEach((handler) => { if (handler instanceof RestHandler) { return this.enableHandlerInstance(handler); } this.enableHandlerFromCollection(handler); }); } } enable(...handlers) { return this.use(...handlers); } enableHandlerInstance(handler) { return this.server.use(handler.run()); } enableHandlerFromCollection(handlerName) { if (!this.collection) { throw new Error(` Lyrebird: Unable to find handler \`${handlerName}\` because there isn't a HandlerCollection associated with the current MockServer instance. Please consider creating a HandlerCollection or using an inline handler instead.`); } this.server.use(this.collection.find(handlerName).run()); } }; // src/HandlerCollection.ts var HandlerCollection = class { constructor() { this.collection = new Map(); } collect(...handlers) { handlers.forEach((handler) => { const key = handler.name; if (!key) { throw new Error('\nLyrebird: Each handler should contain a name to be stored in the collection. \nFor future reference, name your handler using the `as("...")` method'); } this.collection.set(key, handler); }); } find(name) { const handler = this.collection.get(name); if (!handler) { throw new Error(` Lyrebird: No Handler found with the given name \`${name}\` in the collection.`); } return handler; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { HandlerCollection, MockServer, RestHandler });