UNPKG

@sleeksky/alt-swagger

Version:

A fluent, programmatic API for generating OpenAPI 3.0 specifications with TypeScript support. Define REST API endpoints, schemas, parameters, and security using a simple, chainable syntax.

144 lines 4.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.toSwaggerSchema = toSwaggerSchema; exports.pathParameters = pathParameters; exports.pathClean = pathClean; exports.toParameter = toParameter; /** Author: Yusuf Bhabhrawala */ const _ = __importStar(require("lodash")); // @ts-ignore const alt_schema_1 = require("@sleeksky/alt-schema"); const TYPES = { i: "integer", s: "string", b: "boolean", n: "number", o: "object", a: "array" }; const RX_BRACE = /\{([^\}]+)\}/g; const RX_COLON = /\:([^\/]+)/g; function toSwaggerSchema(str) { function traverse(schema, obj) { if (_.isArray(obj)) { schema.type = "array"; schema.items = {}; traverse(schema.items, obj[0]); } else if (_.isObject(obj)) { schema.type = "object"; schema.properties = {}; _.forOwn(obj, (v, k) => { schema.properties[k] = {}; traverse(schema.properties[k], v); }); } else { let [optional, type, def] = obj.split(":"); const isOptional = optional === '?'; if (isOptional) { type = type || 'string'; } if (['string', 'number', 'boolean', 'integer', 'array', 'object'].indexOf(type) < 0) type = "string"; schema.type = type; if (def !== undefined && def !== '') { if (type === 'number' || type === 'integer') { schema.example = parseFloat(def); } else if (type === 'boolean') { schema.example = ['true', '1'].indexOf(def) > -1; } else { schema.example = def; } } if (!isOptional) schema.required = true; } } try { const obj = (0, alt_schema_1.typeShape)(str); const schema = {}; traverse(schema, obj); return schema; } catch (error) { throw new Error(`Malformed schema: ${str}`); } } function pathParameters(str) { let params = []; let matches = str.match(RX_BRACE); if (!matches) matches = str.match(RX_COLON); if (matches) { params = matches.map(m => { let [p, def] = m.replace(/^[\{\:]/, "").replace(/[\}]$/, "").split(":"); if (!def) def = ""; return { in: "path", name: p, schema: { type: "string", example: def }, required: true }; }); } return params; } function pathClean(path) { if (path.match(RX_BRACE)) return path.replace(RX_BRACE, m => `${m.split(/(\:.+)?\}/)[0]}}`); return path; } // header:?token function toParameter(inType, str) { if (!_.isString(str)) return str; const cleanStr = str.replace(/ /g, ""); let [name, type, def] = cleanStr.split(":"); let required = true; if (type && type.match(/^\?/)) { type = type.replace(/^\?/, ""); required = false; } if (type && TYPES[type]) type = TYPES[type]; else type = "string"; const schema = { type }; if (def) schema.example = def; return { name, in: inType, required, schema }; } module.exports = { toSwaggerSchema, pathParameters, pathClean, toParameter }; //# sourceMappingURL=utils.js.map