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

58 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const interfaces_1 = require("../interfaces"); const constants_1 = require("./constants"); const interfaces_2 = require("./interfaces"); const transformers_1 = require("./state/transformers"); class Service { constructor(core) { this.core = core; this.state = new Proxy(this.updateDefaultState.bind(this), StateHandler(this.core)); this.spy = core.spy; } reset() { this.state.reset(); this.spy.resetHistory(); } updateDefaultState(stateOrEndpoint, maybeState, method = constants_1.DEFAULT_STATE_HTTP_METHOD) { const { state, endpoint } = maybeState === undefined ? { state: stateOrEndpoint, endpoint: constants_1.DEFAULT_STATE_ENDPOINT } : { state: maybeState, endpoint: stateOrEndpoint }; const newState = interfaces_2.isStateInputGenerator(state) ? state : typeof state === "string" ? transformers_1.textResponse(state) : typeof state === "function" ? transformers_1.functionResponse(state) : transformers_1.objResponse(state); this.core.updateState({ endpoint, method, newState }); return new Proxy(this.updateDefaultState.bind(this), StateHandler(this.core)); } } exports.Service = Service; const AfterResetHandler = { get: (_, _2) => { throw new Error("Don't use fluent API after reseting a state, it makes it harder to track the state changes."); }, apply: (_, _2, _3) => { throw new Error("Don't use fluent API after reseting a state!"); }, }; const StateHandler = (service) => ({ get: (stateUpdateFn, resetOrRestMethod) => { if (interfaces_1.isRESTMethod(resetOrRestMethod)) { return (endpoint = constants_1.DEFAULT_STATE_ENDPOINT, state) => { stateUpdateFn(endpoint, state, resetOrRestMethod); return new Proxy(stateUpdateFn, StateHandler(service)); }; } if (resetOrRestMethod === "reset") { return () => { service.resetState(); return new Proxy({}, AfterResetHandler); }; } throw new Error(`Unsure what to do with '${resetOrRestMethod}' when setting a state for ${service.name}`); }, }); //# sourceMappingURL=service.js.map