unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
74 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const matcher_1 = require("./matcher");
const state_1 = require("./state/state");
class Service {
constructor(opts) {
this.hasPaths = false;
this.oasSchema = opts.schema;
this.name = opts.name;
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();
}
get schema() {
return this.oasSchema;
}
get hasDefinedPaths() {
return this.hasPaths;
}
match(sreq) {
const maybeOp = this.matcher.matchToOperationObject(sreq);
if (maybeOp === undefined) {
return undefined;
}
const state = this.getState(sreq.method, sreq.path);
return {
operation: maybeOp,
state,
};
}
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,
});
}
getState(method, endpoint) {
method = method.toLowerCase();
endpoint = endpoint.toLowerCase();
const realEndpoint = this.matcher.findEndpoint(endpoint);
if (realEndpoint === undefined) {
return undefined;
}
const schemaEndpoint = realEndpoint.schemaEndpoint;
const operationSchema = this.schema.paths[schemaEndpoint][method];
if (operationSchema === undefined) {
return undefined;
}
return this.state.getState(method, realEndpoint.normalizedEndpoint, operationSchema);
}
}
exports.Service = Service;
//# sourceMappingURL=service.js.map