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

106 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = require("debug"); const interfaces_1 = require("../../interfaces"); const constants_1 = require("../constants"); const interfaces_2 = require("../dsl/interfaces"); const debugLog = debug_1.default("unmock:state:utils"); 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(`getOperations: Fetching operations for specific endpoint '${endpoint}'...`); const pathItem = paths[schemaEndpoint]; if (pathItem === undefined) { return err(`endpoint '${endpoint}'`); } if (isDefMethod) { debugLog(`getOperations: Fetching operations for any REST method...`); const ops = getOperationsFromPathItem(pathItem, endpoint); return ops === undefined ? err(`any operations under '${endpoint}'`) : { operations: ops }; } debugLog(`getOperations: 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) => { debugLog(`getOperationsByMethod: Extracting all ${method} operations from ${JSON.stringify(paths)}`); const anyMethod = method === constants_1.DEFAULT_STATE_HTTP_METHOD; const filterFn = anyMethod ? (pathMethod) => interfaces_1.isRESTMethod(pathMethod) : (pathMethod) => pathMethod === 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) { debugLog(`getOperationsByMethod: No matching operations found`); return undefined; } return operations; }; const getOperationsFromPathItem = (pathItem, endpoint) => { debugLog(`getOperationsFromPathItem: Extracting all operations from ${JSON.stringify(pathItem)}`); const operations = Object.keys(pathItem) .filter(key => interfaces_1.isRESTMethod(key) && pathItem[key] !== undefined) .map(key => ({ endpoint, method: key, operation: pathItem[key], })); if (operations.length === 0) { debugLog(`getOperationsFromPathItem: no operations found`); return undefined; } return operations; }; const stringHasDSLKeys = (input) => interfaces_2.DSLKeys.some((key) => input.includes(key)); const chooseBestMatchingError = (firstError, secondError) => { if (secondError === undefined) { return firstError; } const firstHasDSL = stringHasDSLKeys(firstError.msg); const secondHasDSL = stringHasDSLKeys(secondError.msg); return firstHasDSL && !secondHasDSL ? firstError : secondHasDSL && !firstHasDSL ? secondError : firstError.nestedLevel < secondError.nestedLevel ? secondError : firstError; }; exports.chooseErrorFromList = (errList) => errList.reduce((e, c) => (c === undefined ? e : chooseBestMatchingError(c, e)), undefined); exports.convertEndpointToWildcard = (endpoint) => endpoint.replace(/\{[^}]*?\}/g, "*"); //# sourceMappingURL=utils.js.map