UNPKG

redis-smq-rest-api

Version:
181 lines 7.89 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateOpenApiDocument = generateOpenApiDocument; exports.saveOpenApiDocument = saveOpenApiDocument; const promises_1 = require("fs/promises"); const path_1 = require("path"); const constants_js_1 = require("../../config/constants.js"); const index_js_1 = require("../controller/types/index.js"); const schema_generator_js_1 = require("../validator/schema-generator.js"); const adaptor_js_1 = require("./adaptor.js"); function toOpenAPISchema(schema) { return __awaiter(this, void 0, void 0, function* () { const { default: toOpenApiSchema } = yield Promise.resolve().then(() => __importStar(require('@openapi-contrib/json-schema-to-openapi-schema'))); return toOpenApiSchema(schema); }); } function getRequestParameters(schema, payloadSource) { return __awaiter(this, void 0, void 0, function* () { var _a; if (schema.$ref) { const def = (schema.definitions || {})[schema.$ref]; if (typeof def !== 'object') throw new Error(`Schema ${schema.$ref} not found.`); schema = def; } if (schema.definitions) delete schema.definitions; if (payloadSource === index_js_1.EControllerRequestPayload.PATH || payloadSource === index_js_1.EControllerRequestPayload.QUERY) { const parameters = []; for (const property in schema.properties) { const prop = schema.properties[property]; if (typeof prop !== 'boolean') { const { description = '' } = prop, propSchema = __rest(prop, ["description"]); const param = { name: property, in: payloadSource === index_js_1.EControllerRequestPayload.PATH ? 'path' : 'query', required: ((_a = schema.required) === null || _a === void 0 ? void 0 : _a.includes(property)) || false, schema: yield toOpenAPISchema(propSchema), description, }; parameters.push(param); } } return parameters; } return []; }); } function getRequestBody(schema_1, payloadSource_1) { return __awaiter(this, arguments, void 0, function* (schema, payloadSource, contentType = 'application/json') { if (payloadSource === index_js_1.EControllerRequestPayload.BODY && schema.properties && Object.keys(schema.properties).length) { if (schema.definitions) delete schema.definitions; return { required: true, content: { [contentType]: { schema: yield toOpenAPISchema(schema) }, }, }; } return null; }); } function getResponses(schemaMap) { return __awaiter(this, void 0, void 0, function* () { const responses = {}; for (const [status, schema] of schemaMap) { if (schema.definitions) delete schema.definitions; responses[status] = { description: '', content: { 'application/json': { schema: yield toOpenAPISchema(schema), }, }, }; } return responses; }); } function buildOpenApiDocument(routes_1) { return __awaiter(this, arguments, void 0, function* (routes, basePath = '/') { const spec = { openapi: '3.0.0', info: { title: 'RedisSMQ HTTP API specification', version: '8.0.0', }, components: { schemas: {} }, servers: [ { url: basePath, }, ], paths: {}, }; for (const route of routes) { const { path, method, description, tags } = route; const operation = { responses: {}, description, tags, }; operation.parameters = []; for (const [payloadSource, schema] of route.requestParamsSchemas) { operation.parameters.push(...(yield getRequestParameters(schema, payloadSource))); const requestBody = yield getRequestBody(schema, payloadSource); if (requestBody) operation.requestBody = requestBody; } if (!operation.parameters.length) delete operation.parameters; operation.responses = yield getResponses(route.response); spec.paths[path] = Object.assign(Object.assign({}, spec.paths[path]), { [method]: operation }); } return spec; }); } function generateOpenApiDocument(routingMap_1) { return __awaiter(this, arguments, void 0, function* (routingMap, basePath = '/') { const schema = (0, schema_generator_js_1.SchemaGenerator)(); const openApiRoutes = yield (0, adaptor_js_1.getOpenApiRoutes)(routingMap, schema); return buildOpenApiDocument(openApiRoutes, basePath); }); } function saveOpenApiDocument(spec, dir) { return __awaiter(this, void 0, void 0, function* () { const { openApiDocumentFilename } = constants_js_1.constants; const openApiDocumentPath = (0, path_1.resolve)(dir, openApiDocumentFilename); yield (0, promises_1.writeFile)(openApiDocumentPath, JSON.stringify(spec)); return openApiDocumentPath; }); } //# sourceMappingURL=builder.js.map