UNPKG

openapi-ts-request

Version:

Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas

46 lines (45 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.objectify = objectify; exports.get = get; exports.normalizeArray = normalizeArray; exports.inferSchema = inferSchema; exports.getRandomInt = getRandomInt; const lodash_1 = require("lodash"); const util_1 = require("../generator/util"); function objectify(thing) { if (!(0, lodash_1.isObject)(thing)) return {}; return thing; } function get(openAPI, path) { var _a, _b; const refPaths = path.split('/'); const schema = (_b = (_a = openAPI.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[refPaths[refPaths.length - 1]]; if (!schema) { return null; } return schema; } function normalizeArray(arr) { if ((0, lodash_1.isArray)(arr)) return arr; return [arr]; } function isParameterObject(thing) { return (thing === null || thing === void 0 ? void 0 : thing.schema) !== undefined; } function inferSchema(thing) { if (isParameterObject(thing)) { return thing.schema; } if ((0, util_1.isSchemaObject)(thing)) { return Object.assign(Object.assign({}, thing), { type: 'object' }); } return thing; } function getRandomInt(min, max) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); }