unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
82 lines • 3.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ajv_1 = __importDefault(require("ajv"));
const dsl_1 = require("../dsl");
const interfaces_1 = require("../interfaces");
const ajv = new ajv_1.default({ unknownFormats: ["int32", "int64"] });
exports.default = (state) => ({
isEmpty: state === undefined || Object.keys(state).length === 0,
top: dsl_1.getTopLevelDSL(state || {}),
gen: (schema) => exports.spreadStateFromService(schema, dsl_1.filterTopLevelDSL(state || {})),
});
exports.textMW = (state, dsl) => ({
isEmpty: typeof state !== "string" || state.length === 0,
top: dsl_1.getTopLevelDSL((dsl || {})),
gen: (schema) => generateTextResponse(schema, state),
});
const generateTextResponse = (schema, state) => {
if (state === undefined || schema === undefined || schema.type !== "string") {
return {};
}
return Object.assign({}, schema, { const: state });
};
exports.spreadStateFromService = (serviceSchema, statePath) => {
let matches = {};
for (const key of Object.keys(statePath)) {
const scm = serviceSchema[key];
const stateValue = statePath[key];
if (scm === undefined) {
if (hasNestedItems(serviceSchema)) {
const translated = dsl_1.DSL.translateDSLToOAS(statePath, serviceSchema);
const spread = Object.assign({}, oneLevelOfIndirectNestedness(serviceSchema, statePath), translated);
if (Object.keys(spread).length === 0) {
spread[key] = null;
}
matches = Object.assign({}, matches, spread);
}
}
else if (scm !== undefined) {
if (isConcreteValue(stateValue)) {
const spread = {
[key]: interfaces_1.isSchema(scm) && ajv.validate(scm, stateValue)
? Object.assign({}, scm, { const: stateValue }) : null,
};
matches = Object.assign({}, matches, spread);
}
else if (hasNestedItems(scm) || isNonEmptyObject(scm)) {
const translated = dsl_1.DSL.translateDSLToOAS(stateValue, scm);
const spread = {
[key]: Object.assign({}, exports.spreadStateFromService(scm, stateValue), translated),
};
matches = Object.assign({}, matches, oneLevelOfIndirectNestedness(scm, statePath, spread));
}
else {
matches[key] = null;
}
}
else {
throw new Error(`${statePath[key]} (object '${JSON.stringify(statePath)}' with key '${key}') is not an object, string, number or boolean!`);
}
}
return matches;
};
const NESTED_SCHEMA_ITEMS = ["properties", "items", "additionalProperties"];
const hasNestedItems = (obj) => NESTED_SCHEMA_ITEMS.some((key) => obj[key] !== undefined);
const isConcreteValue = (obj) => ["string", "number", "boolean"].includes(typeof obj);
const isNonEmptyObject = (obj) => typeof obj === "object" && Object.keys(obj).length > 0;
const oneLevelOfIndirectNestedness = (schema, path, internalObj = {}) => {
for (const key of NESTED_SCHEMA_ITEMS) {
if (schema[key] !== undefined) {
const maybeContents = exports.spreadStateFromService(schema[key], path);
if (maybeContents !== undefined &&
Object.keys(maybeContents).length > 0) {
internalObj[key] = maybeContents;
}
}
}
return internalObj;
};
//# sourceMappingURL=middleware.js.map