Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions lib/decorators/asyncapi-operation-for-meta-key.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { createMethodDecorator } from '@nestjs/swagger/dist/decorators/helpers';
import {
AsyncApiOperationHeaders,
AsyncApiOperationOptions,
AsyncMessageObject,
AsyncOperationObject,
} from '../interface';
import { OneAsyncApiMessage } from '../interface/asyncapi-message.interface';

function makeHeaders(headers?: AsyncApiOperationHeaders) {
return headers
? {
type: 'object',
properties: Object.entries(headers)
.map(([key, value]) => ({
[key]: {
type: 'string',
...value,
},
}))
.reduce((acc, j) => ({ ...acc, ...j }), {}),
}
: undefined;
}

function makeMessage(
message: OneAsyncApiMessage,
defaultName: string,
): AsyncMessageObject {
return {
...message,
name: message.name || defaultName,
payload: {
type: message.payload,
},
headers: makeHeaders(message.headers),
};
}

export function AsyncApiOperationForMetaKey(
metaKey: string,
options: AsyncApiOperationOptions[],
): MethodDecorator {
return (target, propertyKey: string | symbol, descriptor) => {
const methodName = `${target.constructor.name}#${String(propertyKey)}`;

const transformedOptions: AsyncOperationObject[] = options.map((i) => {
const message = Array.isArray(i.message)
? {
oneOf: i.message.map((i, index) =>
makeMessage(i, `${methodName}#${index}`),
),
}
: makeMessage(i.message, methodName);

const transformedOptionInstance = {
...i,
message,
};

return transformedOptionInstance;
});

return createMethodDecorator(metaKey, transformedOptions)(
target,
propertyKey,
descriptor,
);
};
}
66 changes: 3 additions & 63 deletions lib/decorators/asyncapi-operation.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,9 @@
import { createMethodDecorator } from '@nestjs/swagger/dist/decorators/helpers';
import { DECORATORS } from '../asyncapi.constants';
import {
AsyncApiOperationHeaders,
AsyncApiOperationOptions,
AsyncMessageObject,
AsyncOperationObject,
} from '../interface';
import { OneAsyncApiMessage } from '../interface/asyncapi-message.interface';

function makeHeaders(headers?: AsyncApiOperationHeaders) {
return headers
? {
type: 'object',
properties: Object.entries(headers)
.map(([key, value]) => ({
[key]: {
type: 'string',
...value,
},
}))
.reduce((acc, j) => ({ ...acc, ...j }), {}),
}
: undefined;
}

function makeMessage(
message: OneAsyncApiMessage,
defaultName: string,
): AsyncMessageObject {
return {
...message,
name: message.name || defaultName,
payload: {
type: message.payload,
},
headers: makeHeaders(message.headers),
};
}
import { AsyncApiOperationOptions } from '../interface';
import { AsyncApiOperationForMetaKey } from './asyncapi-operation-for-meta-key.decorator';

export function AsyncApiOperation(
...options: AsyncApiOperationOptions[]
): MethodDecorator {
return (target, propertyKey: string | symbol, descriptor) => {
const methodName = `${target.constructor.name}#${String(propertyKey)}`;

const transformedOptions: AsyncOperationObject[] = options.map((i) => {
const message = Array.isArray(i.message)
? {
oneOf: i.message.map((i, index) =>
makeMessage(i, `${methodName}#${index}`),
),
}
: makeMessage(i.message, methodName);

const transformedOptionInstance = {
...i,
message,
};

return transformedOptionInstance;
});

return createMethodDecorator(
DECORATORS.AsyncApiOperation,
transformedOptions,
)(target, propertyKey, descriptor);
};
return AsyncApiOperationForMetaKey(DECORATORS.AsyncApiOperation, options);
}
5 changes: 3 additions & 2 deletions lib/decorators/asyncapi-pub.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DECORATORS } from '../asyncapi.constants';
import {
AsyncApiOperationOptions,
AsyncApiSpecificOperationOptions,
} from '../interface';
import { AsyncApiOperation } from './asyncapi-operation.decorator';
import { AsyncApiOperationForMetaKey } from './asyncapi-operation-for-meta-key.decorator';

export function AsyncApiPub(
...specificOperationOptions: AsyncApiSpecificOperationOptions[]
Expand All @@ -13,5 +14,5 @@ export function AsyncApiPub(
type: 'pub',
}),
);
return AsyncApiOperation(...options);
return AsyncApiOperationForMetaKey(DECORATORS.AsyncApiPub, options);
}
5 changes: 3 additions & 2 deletions lib/decorators/asyncapi-sub.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DECORATORS } from '../asyncapi.constants';
import {
AsyncApiOperationOptions,
AsyncApiSpecificOperationOptions,
} from '../interface';
import { AsyncApiOperation } from './asyncapi-operation.decorator';
import { AsyncApiOperationForMetaKey } from './asyncapi-operation-for-meta-key.decorator';

export function AsyncApiSub(
...specificOperationOptions: AsyncApiSpecificOperationOptions[]
Expand All @@ -13,5 +14,5 @@ export function AsyncApiSub(
type: 'sub',
}),
);
return AsyncApiOperation(...options);
return AsyncApiOperationForMetaKey(DECORATORS.AsyncApiSub, options);
}
14 changes: 5 additions & 9 deletions lib/explorers/asyncapi-operation.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ export const exploreAsyncApiOperationMetadata = (
_prototype: Type<unknown>,
method: object,
) => {
const metadata: AsyncApiOperationOptionsRaw[] = Reflect.getMetadata(
DECORATORS.AsyncApiOperation,
method,
);
const metadataOperations: AsyncApiOperationOptionsRaw[] = Reflect.getMetadata(DECORATORS.AsyncApiOperation, method);
const metadataSubs: AsyncApiOperationOptionsRaw[] = Reflect.getMetadata(DECORATORS.AsyncApiSub, method);
const metadataPubs: AsyncApiOperationOptionsRaw[] = Reflect.getMetadata(DECORATORS.AsyncApiPub, method);
const metadataCombined = [...metadataOperations || [], ...metadataSubs || [], ...metadataPubs || []]

if (!metadata) {
return;
}

return metadata.map((option: AsyncApiOperationOptionsRaw) => {
return metadataCombined.map((option: AsyncApiOperationOptionsRaw) => {
const { channel, type } = option;

const methodTypeData = {
Expand Down
4 changes: 2 additions & 2 deletions misc/references/ref.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion misc/references/ref.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion misc/references/ref.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions sample/felines/felines.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ export class FelinesController {
payload: CreateFelineDto,
},
})
@AsyncApiSub({
channel: EventPatternsMS.createFeline,
message: {
payload: FelineRto,
},
})
@EventPattern(EventPatternsMS.createFeline)
async createFeline(createFelineDto: CreateFelineDto) {
const feline = await this.felinesService.create(createFelineDto);
this.logger.debug(`feline created:\n${JSON.stringify(feline)}`);
this.publishCreatedFeline(feline);
}

@AsyncApiSub({
channel: EventPatternsMS.createFeline,
message: {
payload: FelineRto,
},
})
publishCreatedFeline(feline: Feline) {
const felineRto = new FelineRto(feline);
return this.client.emit(EventPatternsMS.journal, felineRto);
Expand Down