|
1 | 1 | import { Type } from '@nestjs/common'; |
2 | 2 | import { SchemaObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface'; |
3 | | -import { DECORATORS } from '..'; |
| 3 | +import { DECORATORS } from '../constants'; |
4 | 4 | import { OperationObjectFactory } from '../services'; |
5 | 5 |
|
6 | 6 | const operationObjectFactory = new OperationObjectFactory(); |
7 | 7 |
|
8 | | -export const exploreAsyncapiOperationMetadata = ( |
9 | | - schemas: Record<string, SchemaObject>, |
10 | | - _schemaRefsStack: [], |
11 | | - instance: object, |
12 | | - prototype: Type<unknown>, |
13 | | - method: object, |
14 | | -) => { |
15 | | - const pubObject = exploreAsyncapiPubMetadata( |
16 | | - schemas, |
17 | | - instance, |
18 | | - prototype, |
19 | | - method, |
20 | | - ); |
21 | | - const subObject = exploreAsyncapiSubMetadata( |
22 | | - schemas, |
23 | | - instance, |
24 | | - prototype, |
25 | | - method, |
26 | | - ); |
| 8 | +enum AsyncapiMetadataType { |
| 9 | + pub = 'pub', |
| 10 | + sub = 'sub', |
| 11 | +} |
27 | 12 |
|
28 | | - return { ...pubObject, ...subObject }; |
| 13 | +const typeDecoratorsMap = { |
| 14 | + [AsyncapiMetadataType.pub]: DECORATORS.AsyncapiPub, |
| 15 | + [AsyncapiMetadataType.sub]: DECORATORS.AsyncapiSub, |
29 | 16 | }; |
30 | 17 |
|
31 | | -export const exploreAsyncapiPubMetadata = ( |
| 18 | +export const exploreAsyncapiMetadata = ( |
| 19 | + type: AsyncapiMetadataType, |
32 | 20 | schemas: Record<string, SchemaObject>, |
33 | 21 | _instance: object, |
34 | 22 | _prototype: Type<unknown>, |
35 | 23 | method: object, |
36 | 24 | ) => { |
37 | | - const metadata = Reflect.getMetadata(DECORATORS.ASYNCAPI_PUB, method); |
| 25 | + const metadata = Reflect.getMetadata(typeDecoratorsMap[type], method); |
38 | 26 |
|
39 | 27 | if (!metadata) { |
40 | 28 | return; |
41 | 29 | } |
42 | 30 |
|
43 | 31 | return metadata.map((option) => ({ |
44 | 32 | channel: option.channel, |
45 | | - pub: { |
| 33 | + [type]: { |
46 | 34 | ...option, |
47 | 35 | ...operationObjectFactory.create(option, ['application/json'], schemas), |
48 | 36 | channel: undefined, |
49 | 37 | }, |
50 | 38 | })); |
51 | 39 | }; |
52 | | -export const exploreAsyncapiSubMetadata = ( |
| 40 | + |
| 41 | +export function exploreAsyncapiPubMetadata( |
53 | 42 | schemas: Record<string, SchemaObject>, |
54 | 43 | _instance: object, |
55 | 44 | _prototype: Type<unknown>, |
56 | 45 | method: object, |
57 | | -) => { |
58 | | - const metadata = Reflect.getMetadata(DECORATORS.ASYNCAPI_SUB, method); |
59 | | - |
60 | | - if (!metadata) { |
61 | | - return; |
62 | | - } |
| 46 | +) { |
| 47 | + return exploreAsyncapiMetadata( |
| 48 | + AsyncapiMetadataType.pub, |
| 49 | + schemas, |
| 50 | + _instance, |
| 51 | + _prototype, |
| 52 | + method, |
| 53 | + ); |
| 54 | +} |
63 | 55 |
|
64 | | - return metadata.map((option) => ({ |
65 | | - channel: option.channel, |
66 | | - sub: { |
67 | | - ...option, |
68 | | - ...operationObjectFactory.create(option, ['application/json'], schemas), |
69 | | - channel: undefined, |
70 | | - }, |
71 | | - })); |
72 | | -}; |
| 56 | +export function exploreAsyncapiSubMetadata( |
| 57 | + schemas: Record<string, SchemaObject>, |
| 58 | + _instance: object, |
| 59 | + _prototype: Type<unknown>, |
| 60 | + method: object, |
| 61 | +) { |
| 62 | + return exploreAsyncapiMetadata( |
| 63 | + AsyncapiMetadataType.sub, |
| 64 | + schemas, |
| 65 | + _instance, |
| 66 | + _prototype, |
| 67 | + method, |
| 68 | + ); |
| 69 | +} |
0 commit comments