openapi-metadata
Version:
Auto-Generate OpenAPI specifications from Typescript decorators
1 lines • 2.31 kB
Source Map (JSON)
{"version":3,"file":"metadata.cjs","sources":["../../src/utils/metadata.ts"],"sourcesContent":["import type { Context } from \"../context.js\";\nimport type { TypeValue } from \"../types.js\";\nimport { NoExplicitTypeError } from \"../errors/no-explicit-type.js\";\nimport { ReflectMetadataMissingError } from \"../errors/reflect-metadata-missing.js\";\n\n/**\n * Asserts that `reflect-metadata` exists.\n */\nexport function assertReflectMetadata() {\n if (typeof Reflect !== \"object\" || typeof Reflect.getMetadata !== \"function\") {\n throw new ReflectMetadataMissingError();\n }\n}\n\nexport type MetadataKey = \"design:type\" | \"design:returntype\" | \"design:paramtypes\";\n\nexport type FindTypeOptions = {\n context: Context;\n metadataKey: MetadataKey;\n prototype: Object;\n propertyKey: string;\n};\n\n/**\n * Returns the type inferred from class member.\n */\nexport function findType({ metadataKey, prototype, propertyKey }: FindTypeOptions) {\n assertReflectMetadata();\n const reflectedType: Function | undefined = Reflect.getMetadata(metadataKey, prototype, propertyKey);\n\n if (!reflectedType) {\n throw new NoExplicitTypeError(prototype.constructor.name, propertyKey);\n }\n\n return reflectedType;\n}\n\nconst IS_THUNK_REG = /^.+=>[\\w\\d\\s\\t\\n\\r]*/;\n\n/**\n * Asserts that a value is a thunk value.\n *\n * @example isThunk('hello') === false\n * @example isThunk(() => 'hello') === true\n */\nexport function isThunk(value: any): boolean {\n if (typeof value !== \"function\") {\n return false;\n }\n\n return Boolean(IS_THUNK_REG.exec(value));\n}\n"],"names":["ReflectMetadataMissingError","NoExplicitTypeError"],"mappings":";;;;;AAQO,SAAS,qBAAA,GAAwB;AACtC,EAAA,IAAI,OAAO,OAAA,KAAY,QAAA,IAAY,OAAO,OAAA,CAAQ,gBAAgB,UAAA,EAAY;AAC5E,IAAA,MAAM,IAAIA,kDAAA,EAA4B;AAAA,EACxC;AACF;AAcO,SAAS,QAAA,CAAS,EAAE,WAAA,EAAa,SAAA,EAAW,aAAY,EAAoB;AACjF,EAAA,qBAAA,EAAsB;AACtB,EAAA,MAAM,aAAA,GAAsC,OAAA,CAAQ,WAAA,CAAY,WAAA,EAAa,WAAW,WAAW,CAAA;AAEnG,EAAA,IAAI,CAAC,aAAA,EAAe;AAClB,IAAA,MAAM,IAAIC,kCAAA,CAAoB,SAAA,CAAU,WAAA,CAAY,MAAM,WAAW,CAAA;AAAA,EACvE;AAEA,EAAA,OAAO,aAAA;AACT;AAEA,MAAM,YAAA,GAAe,sBAAA;AAQd,SAAS,QAAQ,KAAA,EAAqB;AAC3C,EAAA,IAAI,OAAO,UAAU,UAAA,EAAY;AAC/B,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,OAAO,OAAA,CAAQ,YAAA,CAAa,IAAA,CAAK,KAAK,CAAC,CAAA;AACzC;;;;;;"}