UNPKG

openapi-zod-typed-express

Version:

Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types

131 lines (130 loc) 5.86 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertUrlsMethodsSchemaToOpenAPI = exports.generateOpenAPIPath = void 0; var zod_1 = require("zod"); var utils_1 = require("./utils"); var generateOpenAPIPath = function (schemas) { var _a, _b, _c, _d, _e, _f, _g, _h; var materializedZodSchemas = { path: ((_a = schemas.pathSchema) === null || _a === void 0 ? void 0 : _a.shape) ? (0, utils_1.mapEntries)(function (_a) { var _b = __read(_a, 2), k = _b[0], v = _b[1]; return [k, v]; }, (_b = schemas.pathSchema) === null || _b === void 0 ? void 0 : _b.shape) : {}, query: ((_c = schemas.querySchema) === null || _c === void 0 ? void 0 : _c.shape) ? (0, utils_1.mapEntries)(function (_a) { var _b = __read(_a, 2), k = _b[0], v = _b[1]; return [k, v]; }, (_d = schemas.querySchema) === null || _d === void 0 ? void 0 : _d.shape) : {}, headers: ((_e = schemas.headersSchema) === null || _e === void 0 ? void 0 : _e.shape) ? (0, utils_1.mapEntries)(function (_a) { var _b = __read(_a, 2), k = _b[0], v = _b[1]; return [k, v]; }, (_f = schemas.headersSchema) === null || _f === void 0 ? void 0 : _f.shape) : {}, body: ((_g = schemas.bodySchema) === null || _g === void 0 ? void 0 : _g.shape) ? schemas.bodySchema : undefined, returns: ((_h = schemas.returnsSchema) === null || _h === void 0 ? void 0 : _h.shape) ? schemas.returnsSchema : undefined, }; var endpointSchema = __assign(__assign({ parameters: __spreadArray(__spreadArray(__spreadArray([], __read(Object.entries(materializedZodSchemas.path).map(function (_a) { var _b; var _c = __read(_a, 2), k = _c[0], v = _c[1]; return ({ in: 'path', name: k, required: ((_b = v.def) === null || _b === void 0 ? void 0 : _b.type) !== 'optional', schema: (0, zod_1.toJSONSchema)(v, { io: 'input' }), }); })), false), __read(Object.entries(materializedZodSchemas.query).map(function (_a) { var _b; var _c = __read(_a, 2), k = _c[0], v = _c[1]; return ({ in: 'query', name: k, required: ((_b = v.def) === null || _b === void 0 ? void 0 : _b.type) !== 'optional', schema: (0, zod_1.toJSONSchema)(v, { io: 'input' }), }); })), false), __read(Object.entries(materializedZodSchemas.headers).map(function (_a) { var _b; var _c = __read(_a, 2), k = _c[0], v = _c[1]; return ({ in: 'header', name: k, required: ((_b = v.def) === null || _b === void 0 ? void 0 : _b.type) !== 'optional', schema: (0, zod_1.toJSONSchema)(v, { io: 'input' }), }); })), false).filter(Boolean) }, (materializedZodSchemas.body ? { requestBody: { required: true, content: { 'application/json': { schema: (0, zod_1.toJSONSchema)(materializedZodSchemas.body, { io: 'input' }), }, }, }, } : {})), { responses: { 200: __assign({ description: '200 response' }, ((0, utils_1.isObject)(schemas.returnsSchema) ? { content: { 'application/json': { schema: (0, zod_1.toJSONSchema)(schemas.returnsSchema, { io: 'input', }), }, }, } : {})), } }); return endpointSchema; }; exports.generateOpenAPIPath = generateOpenAPIPath; var colonUrlVariableReplaceWithBrackets = function (url) { return url.replaceAll(/:(\w+)/g, '{$1}'); }; var convertUrlsMethodsSchemaToOpenAPI = function (obj) { return (0, utils_1.mapEntries)(function (_a) { var _b = __read(_a, 2), url = _b[0], methods = _b[1]; return [ colonUrlVariableReplaceWithBrackets(url), (0, utils_1.mapEntries)(function (_a) { var _b = __read(_a, 2), method = _b[0], schema = _b[1]; return [ method, (0, exports.generateOpenAPIPath)(schema), ]; }, methods), ]; }, obj); }; exports.convertUrlsMethodsSchemaToOpenAPI = convertUrlsMethodsSchemaToOpenAPI;