import { OpenAPIV3 as Doc } from 'openapi-types' import type { RequestValidation } from './types.js'; import { normalizeBody } from './types.js'; import type { TSchema } from '@sinclair/typebox'; export type OpenAPIDocumentInput = { openapi?: string, info?: { title: string; version: string; [k: string]: string; }, paths?: Record>; components?: { schemas?: Record securitySchemes?: Record }, security?: Array } export type OpenAPIDocument = { openapi: string, info: { title: string; version: string; [k: string]: string; }, paths: Record>, components: { schemas?: Record securitySchemes?: Record }, security?: Array } /** * @class */ export default class Docs { base: OpenAPIDocument; prefix: string; error: Record; constructor( base: OpenAPIDocumentInput = {}, opts: { prefix?: string; error?: Record; } = {} ) { base.openapi = '3.1.0' if (!base.paths) { base.paths = {}; } if (opts.error) { this.error = opts.error; } else { this.error = {}; } if (!base.components) { base.components = {}; } if (!base.info) { base.info = { title: 'OpenAPI', version: '1.0.0' }; } this.base = base as OpenAPIDocument; this.prefix = opts.prefix || ''; } push( parsed: { method: Doc.HttpMethods path: string; }, schemas: RequestValidation ): void { const path: Array = []; for (let sec of parsed.path.split('/')) { for (const lit of ['.', '-']) { // These chars are used literally and can't be part of a param name const mini: Array = []; for (const s of sec.split(lit)) { if (s[0] === ':') mini.push(`{${s}}`); else mini.push(s); } sec = mini.join(lit); } path.push(sec); } const pathstr = this.prefix + path.join('/'); if (this.base.paths[pathstr] && this.base.paths[pathstr][parsed.method]) throw new Error(`Duplicate Path: ${parsed.method}: ${parsed.path}`); if (!schemas.private) { if (!this.base.paths[pathstr]) this.base.paths[pathstr] = {}; const document: Doc.OperationObject = { summary: schemas.description || 'No Description', deprecated: schemas.deprecated, parameters: [], tags: [schemas.group || 'Default'], responses: {} }; if (schemas.security) document.security = schemas.security as Array; if (schemas.params && schemas.params.type === 'object') { for (const name in (schemas.params.properties || {})) { const param = schemas.params.properties[name]; if (!document.parameters) document.parameters = []; document.parameters.push({ in: 'path', name: `:${name}`, schema: param, required: true, description: param.description || 'No Description' }); } } if (schemas.query && schemas.query.type === 'object') { for (const name in (schemas.query.properties || {})) { const query = schemas.query.properties[name]; if (!document.parameters) document.parameters = []; document.parameters.push({ in: 'query', name, schema: query, required: query.required || (schemas.query.required || []).includes(name), description: query.description || 'No Description' }); } } if (schemas.res) { const response: Doc.ResponseObject = { description: 'Successful Response', content: { 'application/json': { schema: schemas.res } } } document.responses['200'] = response; } else { const response: Doc.ResponseObject = { description: 'Successful Response' } document.responses['200'] = response; } for (const code of Object.keys(this.error)) { document.responses[code] = { description: 'Error Response', content: { 'application/json': { schema: this.error[code] } } } } if (schemas.body) { const normalized = normalizeBody(schemas.body) || {}; const content: Record = {}; for (const ct of Object.keys(normalized)) { const entry = normalized[ct]; const media: Doc.MediaTypeObject = { schema: entry.schema === true ? {} : entry.schema as Doc.SchemaObject }; if (entry.example !== undefined) media.example = entry.example; if (entry.examples !== undefined) media.examples = entry.examples as Record; content[ct] = media; } document.requestBody = { required: schemas.bodyRequired !== false, content }; } this.base.paths[pathstr][parsed.method] = document; } } }