UNPKG

openapi-metadata

Version:

Auto-Generate OpenAPI specifications from Typescript decorators

103 lines (100 loc) 3.09 kB
import { getEnumValues, getEnumType } from '../utils/enum.mjs'; import { PropertyMetadataStorage } from '../metadata/property.mjs'; import { getSchemaPath } from '../utils/schema.mjs'; import { isThunk } from '../utils/metadata.mjs'; const PrimitiveTypeLoader = async (_context, value) => { if (typeof value === "string") { return { type: value }; } if (value == String) { return { type: "string" }; } if (value == Boolean) { return { type: "boolean" }; } if (value == Number) { return { type: "number" }; } }; const ArrayTypeLoader = async (context, value) => { if (!Array.isArray(value)) { return; } if (value.length <= 0) { context.logger.warn("You tried to specify an array type without any item"); return; } if (value.length > 1) { context.logger.warn( "You tried to specify an array type with multiple items. Please use the 'enum' option if you want to specify an enum." ); return; } const itemsSchema = await loadType(context, { type: value[0] }); if (!itemsSchema) { context.logger.warn("You tried to specify an array type with an item that resolves to undefined."); return; } return { type: "array", items: itemsSchema }; }; const ClassTypeLoader = async (context, value) => { if (typeof value !== "function" || !value.prototype) { return; } const model = value.name; if (context.schemas[model]) { return { $ref: getSchemaPath(model) }; } const schema = { type: "object", properties: {}, required: [] }; const properties = PropertyMetadataStorage.getMetadata(value.prototype); if (!properties) { context.logger.warn(`You tried to use '${model}' as a type but it does not contain any ApiProperty.`); } context.schemas[model] = schema; for (const [key, property] of Object.entries(properties)) { const { required, type, name, enum: e, schema: s, ...metadata } = property; schema.properties[key] = { ...await loadType(context, property), ...metadata }; if (property.required) { schema.required.push(key); } } return { $ref: getSchemaPath(model) }; }; async function loadType(context, options) { if (options.schema) { return options.schema; } if (options.enum) { const enumValues = getEnumValues(options.enum); const enumType = getEnumType(enumValues); return { type: enumType, enum: enumValues }; } if (!options.type) { context.logger.warn("Failed to infer type from property"); return; } const thunk = isThunk(options.type); const value = thunk ? options.type(context) : options.type; for (const loader of [PrimitiveTypeLoader, ArrayTypeLoader, ...context.typeLoaders, ClassTypeLoader]) { const result = await loader(context, value, options.type); if (result) { return result; } } context.logger.warn(`You tried to use '${options.type.toString()}' as a type but no loader supports it ${thunk}`); } export { ArrayTypeLoader, ClassTypeLoader, PrimitiveTypeLoader, loadType }; //# sourceMappingURL=type.mjs.map