Skip to content

Commit 3242c1c

Browse files
committed
chore: refactoring & better lib incapsulation
1 parent 3e21bba commit 3242c1c

30 files changed

+100
-146
lines changed

.eslintrc.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
"@typescript-eslint/explicit-function-return-type": "off",
2828
"@typescript-eslint/explicit-module-boundary-types": "off",
2929
"@typescript-eslint/no-explicit-any": "off",
30-
"@typescript-eslint/no-use-before-define": "off",
31-
"@typescript-eslint/no-unused-vars": 1,
30+
"@typescript-eslint/no-use-before-define": "warn",
31+
"@typescript-eslint/no-unused-vars": [
32+
"error", {
33+
"args": "none"
34+
}],
3235
"@typescript-eslint/ban-types": "off",
3336
"@typescript-eslint/no-empty-interface": "off",
3437
"max-len": [

lib/asyncapi-document.builder.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Logger } from '@nestjs/common';
21
import {
32
ExternalDocumentationObject,
43
SecuritySchemeObject,
@@ -12,8 +11,6 @@ import {
1211
} from './interfaces';
1312

1413
export class AsyncApiDocumentBuilder {
15-
private readonly logger = new Logger(AsyncApiDocumentBuilder.name);
16-
1714
private readonly buildDocumentBase = (): Omit<
1815
AsyncAPIObject,
1916
'channels'

lib/asyncapi.module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
import { SwaggerDocumentOptions } from '@nestjs/swagger';
77
import { validatePath } from '@nestjs/swagger/dist/utils/validate-path.util';
88
import jsyaml from 'js-yaml';
9-
import { AsyncapiScanner } from './asyncapi.scanner';
109
import { AsyncApiTemplateOptions } from './interfaces';
1110
import { AsyncapiGenerator } from './services';
11+
import { AsyncapiScanner } from './services/asyncapi.scanner';
1212
import { AsyncAPIObject } from './index';
1313

1414
export interface AsyncApiDocumentOptions extends SwaggerDocumentOptions {}
@@ -41,9 +41,9 @@ export class AsyncApiModule {
4141
templateOptions?: AsyncApiTemplateOptions,
4242
) {
4343
const generator = new AsyncapiGenerator(templateOptions);
44-
return await generator.generate(contract).catch((e) => {
45-
this.logger.error(e);
46-
throw e;
44+
return await generator.generate(contract).catch((err) => {
45+
this.logger.error(err);
46+
throw err;
4747
});
4848
}
4949

lib/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const DECORATORS_PREFIX = 'asyncapi';
2+
23
export const DECORATORS = {
3-
ASYNCAPI_SERVICE: `${DECORATORS_PREFIX}/service`,
4-
ASYNCAPI_PUB: `${DECORATORS_PREFIX}/pub`,
5-
ASYNCAPI_SUB: `${DECORATORS_PREFIX}/sub`,
4+
AsyncapiService: `${DECORATORS_PREFIX}/service`,
5+
AsyncapiPub: `${DECORATORS_PREFIX}/pub`,
6+
AsyncapiSub: `${DECORATORS_PREFIX}/sub`,
67
};

lib/decorators/asyncapi-operation.decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export interface AsyncOperationOptions
2626
export function AsyncApiPub(
2727
...options: AsyncOperationOptions[]
2828
): MethodDecorator & ClassDecorator {
29-
return createMixedDecorator(DECORATORS.ASYNCAPI_PUB, options);
29+
return createMixedDecorator(DECORATORS.AsyncapiPub, options);
3030
}
3131

3232
export function AsyncApiSub(
3333
...options: AsyncOperationOptions[]
3434
): MethodDecorator & ClassDecorator {
35-
return createMixedDecorator(DECORATORS.ASYNCAPI_SUB, options);
35+
return createMixedDecorator(DECORATORS.AsyncapiSub, options);
3636
}

lib/decorators/asyncapi-service.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export interface AsyncApiServiceOptions {
1515
}
1616

1717
export function AsyncApiService(options?: AsyncApiServiceOptions) {
18-
return createMixedDecorator(DECORATORS.ASYNCAPI_SERVICE, options);
18+
return createMixedDecorator(DECORATORS.AsyncapiService, options);
1919
}
Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,69 @@
11
import { Type } from '@nestjs/common';
22
import { SchemaObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
3-
import { DECORATORS } from '..';
3+
import { DECORATORS } from '../constants';
44
import { OperationObjectFactory } from '../services';
55

66
const operationObjectFactory = new OperationObjectFactory();
77

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+
}
2712

28-
return { ...pubObject, ...subObject };
13+
const typeDecoratorsMap = {
14+
[AsyncapiMetadataType.pub]: DECORATORS.AsyncapiPub,
15+
[AsyncapiMetadataType.sub]: DECORATORS.AsyncapiSub,
2916
};
3017

31-
export const exploreAsyncapiPubMetadata = (
18+
export const exploreAsyncapiMetadata = (
19+
type: AsyncapiMetadataType,
3220
schemas: Record<string, SchemaObject>,
3321
_instance: object,
3422
_prototype: Type<unknown>,
3523
method: object,
3624
) => {
37-
const metadata = Reflect.getMetadata(DECORATORS.ASYNCAPI_PUB, method);
25+
const metadata = Reflect.getMetadata(typeDecoratorsMap[type], method);
3826

3927
if (!metadata) {
4028
return;
4129
}
4230

4331
return metadata.map((option) => ({
4432
channel: option.channel,
45-
pub: {
33+
[type]: {
4634
...option,
4735
...operationObjectFactory.create(option, ['application/json'], schemas),
4836
channel: undefined,
4937
},
5038
}));
5139
};
52-
export const exploreAsyncapiSubMetadata = (
40+
41+
export function exploreAsyncapiPubMetadata(
5342
schemas: Record<string, SchemaObject>,
5443
_instance: object,
5544
_prototype: Type<unknown>,
5645
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+
}
6355

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+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Type } from '@nestjs/common';
2-
import { DECORATORS } from '..';
2+
import { DECORATORS } from '../constants';
33

44
export const exploreAsyncapiServiceMetadata = (metatype: Type<unknown>) => {
5-
return Reflect.getMetadata(DECORATORS.ASYNCAPI_SERVICE, metatype);
5+
return Reflect.getMetadata(DECORATORS.AsyncapiService, metatype);
66
};

lib/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ export * from './asyncapi-document.builder';
22
export * from './interfaces';
33
export * from './binding-interfaces';
44
export * from './asyncapi.module';
5-
export * from './asyncapi.explorer';
65
export * from './decorators';
7-
export * from './constants';

lib/interfaces/asyncapi-common.interfaces.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ export interface AsyncAPIObject {
2828
defaultContentType?: string;
2929
}
3030

31-
export interface AsyncAPIObject {
32-
asyncapi: string;
33-
id?: string;
34-
info: InfoObject;
35-
servers?: Record<string, AsyncServerObject>;
36-
channels: AsyncChannelsObject;
37-
components?: AsyncComponentsObject;
38-
tags?: AsyncTagObject[];
39-
externalDocs?: ExternalDocumentationObject;
40-
defaultContentType?: string;
41-
}
42-
4331
export type AsyncChannelsObject = Record<string, AsyncChannelObject>;
4432
export interface AsyncChannelObject {
4533
description?: string;

0 commit comments

Comments
 (0)