unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
141 lines • 5.63 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 constants_1 = require("../constants");
const interfaces_1 = require("../interfaces");
const debugLog = debug_1.default("unmock:state:utils");
exports.filterStatesByOperation = (states, operation) => {
const opResponses = operation.responses;
const statusCodes = Object.keys(opResponses);
const mediaTypes = Object.keys(opResponses).reduce((types, code) => {
const response = opResponses[code];
return Object.assign(types, response === undefined ||
interfaces_1.isReference(response) ||
response.content === undefined
? { [code]: [] }
: { [code]: Object.keys(response.content) });
}, {});
const filtered = states.reduce((stateAcc, state) => {
const relCodesInState = Object.keys(state).filter((code) => statusCodes.includes(code));
if (relCodesInState.length === 0) {
return stateAcc;
}
stateAcc.push(filterByMediaType(relCodesInState, state, mediaTypes));
return stateAcc;
}, []);
return spreadNestedCodeToMedia(filtered);
};
const spreadNestedCodeToMedia = (nested) => {
const spreaded = {};
for (const state of nested) {
for (const code of Object.keys(state)) {
const resp = state[code];
for (const mediaType of Object.keys(state[code])) {
const content = resp[mediaType];
if (content !== undefined) {
const spreadSchema = Object.assign({}, (spreaded[code] || {})[mediaType], content);
spreaded[code] = Object.assign({}, spreaded[code], { [mediaType]: spreadSchema });
}
}
}
}
return spreaded;
};
const filterByMediaType = (statusCodes, stateObj, allowedMediaTypes) => {
const stateCodeToMedia = {};
for (const code of statusCodes) {
const codeSchema = stateObj[code];
const validMediaTypes = Object.keys(codeSchema).filter((mediaType) => allowedMediaTypes[code].includes(mediaType));
if (validMediaTypes.length === 0) {
continue;
}
stateCodeToMedia[code] = validMediaTypes.reduce((filteredCodeToMedia, mediaType) => Object.assign(filteredCodeToMedia, {
[mediaType]: codeSchema[mediaType],
}), {});
}
return stateCodeToMedia;
};
exports.getOperations = ({ stateInput, schemaEndpoint, serviceName, paths, }) => {
const { method, endpoint } = stateInput;
const isDefMethod = method === constants_1.DEFAULT_STATE_HTTP_METHOD;
const errPref = "Can't find ";
const errSuff = ` in '${serviceName}'!`;
const err = (msg) => ({
operations: [],
error: errPref + msg + errSuff,
});
if (endpoint !== constants_1.DEFAULT_STATE_ENDPOINT) {
debugLog(`Fetching operations for specific endpoint '${endpoint}'...`);
const pathItem = paths[schemaEndpoint];
if (pathItem === undefined) {
return err(`endpoint '${endpoint}'`);
}
if (isDefMethod) {
debugLog(`Fetching operations for any REST method...`);
const ops = getOperationsFromPathItem(pathItem, endpoint);
return ops === undefined
? err(`any operations under '${endpoint}'`)
: { operations: ops };
}
debugLog(`Fetching operations for REST method '${method}'...`);
const op = pathItem[method];
return op === undefined
? err(`response for '${method} ${endpoint}'`)
: {
operations: [
{
endpoint,
method: method,
operation: op,
},
],
};
}
debugLog(`Fetching operations for any endpoint...`);
const operations = getOperationsByMethod(method, paths);
return operations === undefined
? err(`any endpoints with ` +
`${isDefMethod ? "operations" : `method '${method}'`}`)
: { operations };
};
const getOperationsByMethod = (method, paths) => {
const anyMethod = method === constants_1.DEFAULT_STATE_HTTP_METHOD;
const filterFn = anyMethod
? (pathItemKey) => interfaces_1.isRESTMethod(pathItemKey)
: (pathItemKey) => pathItemKey === method;
const operations = Object.keys(paths).reduce((ops, path) => {
const pathItem = paths[path];
const pathOperations = Object.keys(pathItem)
.filter((maybeOperationKey) => filterFn(maybeOperationKey))
.map((operationKey) => ({
endpoint: path,
method: operationKey,
operation: pathItem[operationKey],
}));
return ops.concat(pathOperations);
}, []);
if (operations.length === 0) {
return undefined;
}
return operations;
};
const getOperationsFromPathItem = (pathItem, endpoint) => {
const operations = [];
for (const key of Object.keys(pathItem)) {
if (interfaces_1.isRESTMethod(key)) {
const method = key;
const operation = pathItem[key];
if (operation !== undefined) {
operations.push({ endpoint, method, operation });
}
}
}
if (operations.length === 0) {
return undefined;
}
return operations;
};
//# sourceMappingURL=utils.js.map