UNPKG

unmock-core

Version:

[![npm](https://img.shields.io/npm/v/unmock-core.svg)][npmjs] [![CircleCI](https://circleci.com/gh/unmock/unmock-js.svg?style=svg)](https://circleci.com/gh/unmock/unmock-js) [![codecov](https://codecov.io/gh/unmock/unmock-js/branch/dev/graph/badge.svg)](h

54 lines 1.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const interfaces_1 = require("./interfaces"); const middleware_1 = __importDefault(require("./state/middleware")); class ServiceStore { constructor(services) { this.serviceMapping = {}; services.forEach(service => { this.serviceMapping[service.name] = service; }); } match(sreq) { for (const service of Object.values(this.serviceMapping)) { const matchResponse = service.match(sreq); if (matchResponse !== undefined) { return matchResponse; } } return undefined; } resetState(serviceName) { if (serviceName === undefined) { for (const service of Object.values(this.serviceMapping)) { service.resetState(); } } else if (this.serviceMapping[serviceName] !== undefined) { this.serviceMapping[serviceName].resetState(); } } saveState({ serviceName, method, endpoint, state, }) { if (this.serviceMapping[serviceName] === undefined || !interfaces_1.isExtendedRESTMethod(method)) { throw new Error(`Can't find specification for service named '${interfaces_1.isExtendedRESTMethod(method) ? serviceName : method}'!`); } let stateGen; if (!interfaces_1.isStateInputGenerator(state)) { stateGen = middleware_1.default(state); } else { stateGen = state; } this.serviceMapping[serviceName].updateState({ endpoint, method, newState: stateGen, }); } } exports.ServiceStore = ServiceStore; //# sourceMappingURL=serviceStore.js.map