openapi-mock-express-middleware
Version:
Generates express mock-servers from OpenAPI specs
52 lines • 2.43 kB
JavaScript
;
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGenerator = exports.handleExamples = void 0;
const json_schema_faker_1 = __importDefault(require("json-schema-faker"));
const lodash_1 = require("lodash");
const defaultOptions = {
optionalsProbability: 0.5,
useExamplesValue: true,
};
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
const handleExamples = (randomizeExamples) => (value) => {
if (typeof value === 'object' && value !== null && Object.keys(value).length) {
return randomizeExamples
? value[Object.keys(value)[Math.floor(Math.random() * Object.keys(value).length)]].value
: value[Object.keys(value)[0]].value;
}
return '';
};
exports.handleExamples = handleExamples;
/* eslint-enable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-empty-function */
const createGenerator = (options = defaultOptions, callback = (() => { })) => {
const { randomizeExamples } = options, jsfOptions = __rest(options, ["randomizeExamples"]);
const applyedOptions = Object.assign(Object.assign({}, defaultOptions), jsfOptions);
const generator = (0, lodash_1.cloneDeep)(json_schema_faker_1.default);
generator.option(applyedOptions);
if (applyedOptions.useExamplesValue) {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
generator.define('example', (value) => value);
generator.define('examples', (0, exports.handleExamples)(!!randomizeExamples));
}
callback(generator);
return generator;
};
exports.createGenerator = createGenerator;
/* eslint-enable @typescript-eslint/no-empty-function */
//# sourceMappingURL=generator.js.map