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

108 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const constants_1 = require("./constants"); const matcher_1 = require("./matcher"); const spy_1 = require("./spy"); const state_1 = require("./state/state"); const util_1 = require("./util"); class ServiceCore { constructor(opts) { this.hasPaths = false; this.oasSchema = opts.schema; this.name = opts.name; this.absPath = opts.absPath || process.cwd(); this.hasPaths = this.schema !== undefined && this.schema.paths !== undefined && Object.keys(this.schema.paths).length > 0; this.matcher = new matcher_1.OASMatcher({ schema: this.schema }); this.state = new state_1.State(); const deref = util_1.derefIfNeeded({ schema: this.schema, absPath: this.absPath }); this.dereferencer = (objToDeref) => deref(objToDeref); this.callTracker = spy_1.createCallTracker(); } static from(baseSchema, { baseUrl, method, endpoint, statusCode, response, name, }) { const mediaType = typeof response === "string" ? "text/*" : "application/json"; const newPath = { [endpoint]: { [method]: { responses: { [statusCode]: { description: "Automatically added", content: { [mediaType]: { schema: response }, }, }, }, }, }, }; const newUrls = [{ url: baseUrl }]; const newPaths = lodash_1.defaultsDeep(newPath, baseSchema.paths); const newServers = lodash_1.unionBy(newUrls.concat(baseSchema.servers || []), e => e.url); const finalSchema = Object.assign(Object.assign({}, baseSchema), { paths: newPaths, servers: newServers }); return new ServiceCore({ schema: finalSchema, name, }); } get schema() { return this.oasSchema; } get hasDefinedPaths() { return this.hasPaths; } track(requestResponsePair) { this.callTracker.track(requestResponsePair); } get spy() { return this.callTracker.spy; } match(sreq) { const maybeOp = this.matcher.matchToOperationObject(sreq); if (maybeOp === undefined) { return undefined; } const state = this.getState(sreq.method, sreq.pathname); return { operation: maybeOp, state, service: this, }; } resetState() { this.state.reset(); } updateState(stateInput) { if (!this.hasDefinedPaths) { throw new Error(`'${this.name}' has no defined paths!`); } const { endpoint } = stateInput; let schemaEndpoint = endpoint; if (endpoint !== constants_1.DEFAULT_STATE_ENDPOINT) { const parsedEndpoint = this.matcher.findEndpoint(endpoint); if (parsedEndpoint === undefined) { throw new Error(`Can't find endpoint '${endpoint}' in '${this.name}'`); } schemaEndpoint = parsedEndpoint.schemaEndpoint; } this.state.update({ stateInput, serviceName: this.name, schemaEndpoint, paths: this.schema.paths, dereferencer: this.dereferencer, }); } getState(method, endpoint) { endpoint = endpoint.toLowerCase(); const realEndpoint = this.matcher.findEndpoint(endpoint); if (realEndpoint === undefined) { return undefined; } return this.state.getState(method, realEndpoint.normalizedEndpoint); } } exports.ServiceCore = ServiceCore; //# sourceMappingURL=serviceCore.js.map