UNPKG

openapi-metadata

Version:

Auto-Generate OpenAPI specifications from Typescript decorators

1 lines 3.2 kB
{"version":3,"file":"api-property.cjs","sources":["../../src/decorators/api-property.ts"],"sourcesContent":["import type { Context } from \"../context.js\";\nimport { SymbolKeysNotSupportedError } from \"../errors/symbol-keys-not-supported.js\";\nimport { type PropertyMetadata, PropertyMetadataStorage } from \"../metadata/property.js\";\nimport { findType } from \"../utils/metadata.js\";\n\nexport type ApiPropertyOptions = Partial<PropertyMetadata>;\n\n/**\n * Configures this class member as a property of the schema.\n * Can be applied to properties, getters and methods.\n *\n * @see https://swagger.io/specification/#schema-object\n */\nexport function ApiProperty(options?: ApiPropertyOptions): PropertyDecorator;\nexport function ApiProperty(options?: ApiPropertyOptions): MethodDecorator;\nexport function ApiProperty(options?: ApiPropertyOptions): PropertyDecorator | MethodDecorator {\n return (prototype, propertyKey, descriptor) => {\n const isMethod = Boolean(descriptor?.value);\n\n if (typeof propertyKey === \"symbol\") {\n throw new SymbolKeysNotSupportedError();\n }\n\n const metadata = {\n name: options?.name ?? propertyKey,\n required: true,\n ...options,\n } as PropertyMetadata;\n\n if (!metadata.type && !metadata.schema && !metadata.enum) {\n (metadata as any).type = (context: Context) =>\n findType({\n context,\n metadataKey: isMethod ? \"design:returntype\" : \"design:type\",\n prototype,\n propertyKey,\n });\n }\n\n PropertyMetadataStorage.mergeMetadata(prototype, {\n [metadata.name]: metadata as PropertyMetadata,\n });\n };\n}\n\n/**\n * Configures this class member as an optional property of the schema.\n * Can be applied to properties, getters and methods.\n *\n * @see https://swagger.io/specification/#schema-object\n */\nexport function ApiPropertyOptional(options?: Omit<ApiPropertyOptions, \"required\">): PropertyDecorator;\nexport function ApiPropertyOptional(options?: Omit<ApiPropertyOptions, \"required\">): MethodDecorator;\nexport function ApiPropertyOptional(\n options?: Omit<ApiPropertyOptions, \"required\">,\n): PropertyDecorator | MethodDecorator {\n return ApiProperty({\n ...options,\n required: false,\n });\n}\n"],"names":["SymbolKeysNotSupportedError","metadata","findType","PropertyMetadataStorage"],"mappings":";;;;;;AAeO,SAAS,YAAY,OAAA,EAAmE;AAC7F,EAAA,OAAO,CAAC,SAAA,EAAW,WAAA,EAAa,UAAA,KAAe;AAC7C,IAAA,MAAM,QAAA,GAAW,OAAA,CAAQ,UAAA,EAAY,KAAK,CAAA;AAE1C,IAAA,IAAI,OAAO,gBAAgB,QAAA,EAAU;AACnC,MAAA,MAAM,IAAIA,kDAAA,EAA4B;AAAA,IACxC;AAEA,IAAA,MAAMC,UAAA,GAAW;AAAA,MACf,IAAA,EAAM,SAAS,IAAA,IAAQ,WAAA;AAAA,MACvB,QAAA,EAAU,IAAA;AAAA,MACV,GAAG;AAAA,KACL;AAEA,IAAA,IAAI,CAACA,WAAS,IAAA,IAAQ,CAACA,WAAS,MAAA,IAAU,CAACA,WAAS,IAAA,EAAM;AACxD,MAACA,UAAA,CAAiB,IAAA,GAAO,CAAC,OAAA,KACxBC,iBAAA,CAAS;AAAA,QAEP,WAAA,EAAa,WAAW,mBAAA,GAAsB,aAAA;AAAA,QAC9C,SAAA;AAAA,QACA;AAAA,OACD,CAAA;AAAA,IACL;AAEA,IAAAC,gCAAA,CAAwB,cAAc,SAAA,EAAW;AAAA,MAC/C,CAACF,UAAA,CAAS,IAAI,GAAGA;AAAA,KAClB,CAAA;AAAA,EACH,CAAA;AACF;AAUO,SAAS,oBACd,OAAA,EACqC;AACrC,EAAA,OAAO,WAAA,CAAY;AAAA,IACjB,GAAG,OAAA;AAAA,IACH,QAAA,EAAU;AAAA,GACX,CAAA;AACH;;;;;"}