unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
88 lines • 3.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const minimatch_1 = __importDefault(require("minimatch"));
const constants_1 = require("../constants");
const dsl_1 = require("../dsl");
const utils_1 = require("./utils");
const validator_1 = require("./validator");
const debugLog = debug_1.default("unmock:state");
class State {
constructor() {
this.state = {};
}
update(stateUpdate) {
const { stateInput } = stateUpdate;
const { endpoint, method, newState } = stateInput;
debugLog(`Fetching operations for '${method} ${endpoint}'...`);
const ops = utils_1.getOperations(stateUpdate);
if (ops.error !== undefined) {
debugLog(`Couldn't find any matching operations: ${ops.error}`);
throw new Error(ops.error);
}
if (newState.isEmpty) {
return;
}
debugLog(`Found follow operations: ${ops.operations}`);
let errorMsg;
const opsResult = ops.operations.some((op) => {
const stateResponses = validator_1.getValidStatesForOperationWithState(op.operation, newState);
if (stateResponses.error === undefined) {
debugLog(`Matched successfully for ${op.operation.operationId}`);
const augmentedResponses = dsl_1.DSL.translateTopLevelToOAS(newState.top, stateResponses.responses);
this.updateStateInternal(endpoint, method, augmentedResponses);
return true;
}
debugLog(`Couldn't match for ${op.operation.operationId} - received error ${stateResponses.error}`);
errorMsg = stateResponses.error;
return false;
});
if (opsResult === false) {
throw new Error(errorMsg);
}
}
getState(method, endpoint, operation) {
debugLog(`Filtering all saved states that match '${endpoint}'...`);
const matchingEndpointKeys = Object.keys(this.state).filter((sKey) => minimatch_1.default(endpoint, sKey, { nocase: true }));
if (matchingEndpointKeys.length === 0) {
debugLog(`No states match '${endpoint}'`);
return undefined;
}
matchingEndpointKeys.sort((a, b) => {
const nA = a.split("*");
const nB = b.split("*");
return nA > nB || (nA === nB && a.indexOf("*") < b.indexOf("*")) ? 1 : -1;
});
debugLog(`Found following endpoints as matches for '${matchingEndpointKeys}: ${JSON.stringify(matchingEndpointKeys)}`);
const states = [];
for (const key of matchingEndpointKeys) {
const methodToStatus = this.state[key];
if (methodToStatus[constants_1.DEFAULT_STATE_HTTP_METHOD] !== undefined) {
states.push(methodToStatus[constants_1.DEFAULT_STATE_HTTP_METHOD]);
}
if (methodToStatus[method] !== undefined) {
states.push(methodToStatus[method]);
}
}
const filteredStates = utils_1.filterStatesByOperation(states, operation);
const parsedTopLevel = dsl_1.DSL.actTopLevelFromOAS(filteredStates);
return Object.keys(parsedTopLevel).length > 0 ? parsedTopLevel : undefined;
}
reset() {
this.state = {};
}
updateStateInternal(endpoint, method, responses) {
if (this.state[endpoint] === undefined) {
this.state[endpoint] = {};
}
if (this.state[endpoint][method] === undefined) {
this.state[endpoint][method] = {};
}
this.state[endpoint][method] = Object.assign({}, this.state[endpoint][method], responses);
}
}
exports.State = State;
//# sourceMappingURL=state.js.map