UNPKG

fets

Version:

TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience

172 lines (171 loc) 8.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useOpenAPI = void 0; const tslib_1 = require("tslib"); const zod_to_json_schema_1 = require("zod-to-json-schema"); const Response_js_1 = require("../Response.js"); const swagger_ui_html_js_1 = tslib_1.__importDefault(require("../swagger-ui-html.js")); const types_js_1 = require("../zod/types.js"); function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }) { let paths; return { onRouterInit(router) { paths = router.openAPIDocument.paths = router.openAPIDocument.paths || {}; if (oasEndpoint) { router.route({ method: 'GET', path: oasEndpoint, internal: true, handler: () => Response_js_1.Response.json(router.openAPIDocument), }); } if (swaggerUIEndpoint) { router.route({ method: 'GET', path: swaggerUIEndpoint, internal: true, handler: () => new Response_js_1.Response(swagger_ui_html_js_1.default.replace('__SWAGGER_UI_OPTIONS__', JSON.stringify({ spec: router.openAPIDocument, dom_id: '#swagger-ui', displayOperationId: true, tryItOutEnabled: true, requestSnippetsEnabled: true, displayRequestDuration: true, defaultModelRendering: 'model', defaultModelExpandDepth: 3, defaultModelsExpandDepth: 3, ...swaggerUIOpts, })), { headers: { 'Content-Type': 'text/html', }, }), }); } }, onRoute({ method, path, operationId, description, tags, schemas }) { if (schemas) { let pathForOAS = path.replace(/:([^/]+)/g, '{$1}'); if (!pathForOAS.startsWith('/')) { pathForOAS = `/${pathForOAS}`; } const pathObj = (paths[pathForOAS] = paths[pathForOAS] || {}); const lowerCasedMethod = method.toLowerCase(); pathObj[lowerCasedMethod] = pathObj[lowerCasedMethod] || {}; const operation = pathObj[lowerCasedMethod]; operation.operationId = operationId; operation.description = description; operation.tags = tags; if (schemas.responses) { for (const statusCode in schemas.responses) { let responseSchema = schemas.responses[statusCode]; if ((0, types_js_1.isZodSchema)(responseSchema)) { responseSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(responseSchema, { target: 'openApi3', }); } operation.responses = operation.responses || {}; operation.responses[statusCode] = { description: '', content: { 'application/json': { schema: responseSchema, }, }, }; } } else { operation.responses = { default: { description: '', }, }; } if (schemas.request?.headers) { let headersSchema = schemas.request.headers; if ((0, types_js_1.isZodSchema)(headersSchema)) { headersSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(headersSchema, { target: 'openApi3', }); } for (const headerName in headersSchema.properties) { const headerSchema = headersSchema.properties[headerName]; operation.parameters = operation.parameters || []; operation.parameters.push({ name: headerName, in: 'header', required: headersSchema.required?.includes(headerName), schema: headerSchema, }); } } if (schemas.request?.params) { let paramsSchema = schemas.request.params; if ((0, types_js_1.isZodSchema)(paramsSchema)) { paramsSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(paramsSchema, { target: 'openApi3', }); } for (const paramName in paramsSchema.properties) { const paramSchema = paramsSchema.properties[paramName]; operation.parameters = operation.parameters || []; operation.parameters.push({ name: paramName, in: 'path', required: paramsSchema.required?.includes(paramName), schema: paramSchema, }); } } if (schemas.request?.query) { let queriesSchema = schemas.request.query; if ((0, types_js_1.isZodSchema)(queriesSchema)) { queriesSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(queriesSchema, { target: 'openApi3', }); } for (const queryName in queriesSchema.properties) { const querySchema = queriesSchema.properties[queryName]; operation.parameters = operation.parameters || []; operation.parameters.push({ name: queryName, in: 'query', required: queriesSchema.required?.includes(queryName), schema: querySchema, }); } } if (schemas.request?.json) { let requestJsonSchema = schemas.request.json; if ((0, types_js_1.isZodSchema)(requestJsonSchema)) { requestJsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(requestJsonSchema, { target: 'openApi3', }); } const requestBody = (operation.requestBody = (operation.requestBody || {})); requestBody.required = true; const requestBodyContent = (requestBody.content = (requestBody.content || {})); requestBodyContent['application/json'] = { schema: requestJsonSchema, }; } if (schemas.request?.formData) { const requestBody = (operation.requestBody = (operation.requestBody || {})); requestBody.required = true; const requestBodyContent = (requestBody.content = (requestBody.content || {})); let requestFormDataSchema = schemas.request.formData; if ((0, types_js_1.isZodSchema)(requestFormDataSchema)) { requestFormDataSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(requestFormDataSchema, { target: 'openApi3', }); } requestBodyContent['multipart/form-data'] = { schema: requestFormDataSchema, }; } } }, }; } exports.useOpenAPI = useOpenAPI;