UNPKG

openapi-tsk

Version:
163 lines (162 loc) 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SecuritySchemesDescriber = exports.RefTypeDescriber = exports.TypeDescriber = void 0; const types_1 = require("./types"); const SecuritySchemesStore_1 = require("./SecuritySchemesStore"); const MetadataClass_1 = require("./MetadataClass"); const SchemasStore_1 = require("./SchemasStore"); require("reflect-metadata"); const ZodToOpenAPI_1 = require("./ZodToOpenAPI"); class TypeDescriber { constructor(obj) { this.type = obj.type; this.properties = obj.props; const props = {}; Object.entries(obj.props).forEach(([key, value]) => { props[key] = value; }); this.schema = { name: obj.name, type: obj.type, required: [], properties: {}, }; if (this.type === types_1.PropTypeEnum.PRIMITIVE) { this.type = this.properties.primitive; return; } if (!Object.keys(props).length) return; const schemaType = {}; Object.keys(props).forEach((key) => { var _a, _b; if (props[key].type) { schemaType[key] = { type: props[key].type, nullable: (_a = props[key].nullable) !== null && _a !== void 0 ? _a : false, }; if (props[key].format) schemaType[key].format = props[key].format; if (props[key].minimum) schemaType[key].minimum = props[key].minimum; if (props[key].maximum) schemaType[key].maximum = props[key].maximum; if (props[key].required) (_b = this.schema.required) === null || _b === void 0 ? void 0 : _b.push(key); } else if (props[key].$ref) { schemaType[key] = { $ref: props[key].$ref, }; } }); this.schema.properties = schemaType; SchemasStore_1.SchemasStore.add(this.schema.name, { type: this.schema.type, properties: this.schema.properties, required: this.schema.required, }); Object.entries(TypeDescriber.referenceSchemas).forEach(([key, value]) => { SchemasStore_1.SchemasStore.add(key, value); }); } static describePrimitive(primitive, format) { return format ? { primitive, format } : { primitive }; } static describeUrlParam(param) { if (!param.required) param.required = true; if (!param.allowEmptyValue) param.allowEmptyValue = false; if (!param.deprecated) param.deprecated = false; return param; } static describeProps(input) { const props = {}; const instance = new MetadataClass_1.MetadataClass(input); const keys = Object.keys(instance); keys.forEach((key) => { const metadata = MetadataClass_1.MetadataClass.getPropMetadata(instance, key); props[key] = metadata; }); return props; } static describeReference(name, input) { this.referenceSchemas[name] = { type: types_1.PropTypeEnum.OBJECT, properties: this.describeProps(input), }; return { $ref: "#/components/schemas/".concat(name) }; } static describeArrayReference(name, input) { this.referenceSchemas[name] = { type: types_1.PropTypeEnum.ARRAY, properties: this.describeProps(input), }; return { $ref: "#/components/schemas/".concat(name) }; } static describeZodObject(name, zodObject, type = types_1.PropTypeEnum.OBJECT) { const openApiSchema = ZodToOpenAPI_1.ZodToOpenAPI.transform(zodObject); return new TypeDescriber({ name, type, props: openApiSchema.properties, }); } } exports.TypeDescriber = TypeDescriber; TypeDescriber.referenceSchemas = {}; class RefTypeDescriber { constructor(obj) { this.type = obj.type; this.schema = { name: obj.name, definition: {}, }; if (this.type === types_1.PropTypeEnum.ARRAY) { this.schema = { name: obj.name, definition: { type: types_1.PropTypeEnum.ARRAY, items: { $ref: "#/components/schemas/".concat(obj.name) }, }, }; } else { this.schema = { name: obj.name, definition: { $ref: "#/components/schemas/".concat(obj.name) }, }; } } } exports.RefTypeDescriber = RefTypeDescriber; class SecuritySchemesDescriber { constructor(key, securitySchemes) { this[key] = securitySchemes; SecuritySchemesStore_1.SecuritySchemesStore.add(key, securitySchemes); } static defaultHttpBearer() { return { type: "http", description: "Bearer token", scheme: "bearer", bearerFormat: "JWT", }; } static defaultHttpApiKey(apiKeyName, into) { return { type: "apiKey", description: "Api key", name: apiKeyName, in: into, }; } } exports.SecuritySchemesDescriber = SecuritySchemesDescriber; SecuritySchemesDescriber.HTTP = "http"; SecuritySchemesDescriber.API_KEY = "apiKey"; SecuritySchemesDescriber.OAUTH2 = "oauth2"; SecuritySchemesDescriber.OPEN_ID_CONNECT = "openIdConnect"; SecuritySchemesDescriber.MUTUAL_TLS = "mutualTLS";