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

70 lines 3.2 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = require("debug"); const lodash_1 = require("lodash"); const actors_1 = require("./actors"); const translators_1 = require("./translators"); const utils_1 = require("./utils"); const debugLog = debug_1.default("unmock:dsl"); class DSL { static translateDSLToOAS(state, schema) { return Object.entries(translators_1.translators).reduce(({ translated, cleaned }, [property, fn]) => { const _a = property, maybeUsed = cleaned[_a], rest = __rest(cleaned, [typeof _a === "symbol" ? _a : _a + ""]); const newTranslation = maybeUsed !== undefined ? fn(cleaned, schema) : undefined; return newTranslation !== undefined ? { translated: newTranslation, cleaned: rest } : { translated, cleaned }; }, { translated: {}, cleaned: state }); } static translateTopLevelToOAS(top, responses) { if (responses === undefined) { return responses; } Object.keys(translators_1.topTranslators) .filter(dslKey => top[dslKey] !== undefined) .map(dslKey => translators_1.topTranslators[dslKey](top[dslKey])) .filter(translation => translation !== undefined) .forEach(translation => utils_1.injectUnmockProperty(responses, translation)); return responses; } static actTopLevelFromOAS(states) { return reduceAndHoistElements(states, (mToS) => reduceAndHoistElements(mToS, actOnSchema)); } } exports.DSL = DSL; DSL.STRICT_MODE = true; const reduceAndHoistElements = (iterable, callable) => Object.keys(iterable) .map(key => (Object.assign({ name: key }, callable(iterable[key])))) .reduce(({ parsed, newState }, o) => ({ parsed: Object.assign(Object.assign({}, parsed), { [o.name]: o.parsed }), newState: Object.assign(Object.assign({}, newState), { [o.name]: o.newState }), }), { parsed: {}, newState: {}, }); const actOnSchema = (schema) => { const newStateAndResult = actors_1.actWithAllActors(schema); const parsed = lodash_1.defaultsDeep({}, ...newStateAndResult.parsed); const newState = newStateAndResult.newState.includes(undefined) ? undefined : lodash_1.defaultsDeep({}, ...newStateAndResult.newState); const maybeProperties = parsed.properties; if (maybeProperties !== undefined && Object.keys(maybeProperties).length === 0) { debugLog(`schema.properties is now empty, removing 'properties' from copied response '${JSON.stringify(schema)}'`); delete parsed.properties; } return { parsed, newState }; }; //# sourceMappingURL=dsl.js.map