Skip to content

Commit

Permalink
chore: use async import instead of require
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 6, 2023
1 parent d74c391 commit 8d4db08
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
1 change: 0 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'reflect-metadata';
export * from './decorators';
export * from './document-builder';
export * from './interfaces';
export * from './plugin';
export * from './swagger-module';
export * from './type-helpers';
export * from './utils';
5 changes: 0 additions & 5 deletions lib/interfaces/swagger-document-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,4 @@ export interface SwaggerDocumentOptions {
* @default () => controllerKey_methodKey
*/
operationIdFactory?: (controllerKey: string, methodKey: string) => string;

/**
* Extra static metadata to be loaded into the specification
*/
metadata?: Record<string, any>;
}
14 changes: 8 additions & 6 deletions lib/plugin/metadata-loader.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { METADATA_FACTORY_NAME } from './plugin-constants';

export class MetadataLoader {
load(metadata: Record<string, any>) {
async load(metadata: Record<string, any>) {
const pkgMetadata = metadata['@nestjs/swagger'];
if (!pkgMetadata) {
return;
}
const { models, controllers } = pkgMetadata;
if (models) {
this.applyMetadata(models);
await this.applyMetadata(models);
}
if (controllers) {
this.applyMetadata(controllers);
await this.applyMetadata(controllers);
}
}

private applyMetadata(meta: Record<string, any>) {
meta.forEach(([fileRef, fileMeta]) => {
Object.keys(fileMeta).forEach((key) => {
private async applyMetadata(meta: Record<string, any>) {
const loadPromises = meta.map(async ([fileImport, fileMeta]) => {
const fileRef = await fileImport;
Object.keys(fileMeta).map((key) => {
const clsRef = fileRef[key];
clsRef[METADATA_FACTORY_NAME] = () => fileMeta[key];
});
});
await Promise.all(loadPromises);
}
}
5 changes: 3 additions & 2 deletions lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
const metadataWithImports = [];
Object.keys(this._collectedMetadata).forEach((filePath) => {
const metadata = this._collectedMetadata[filePath];
const path = filePath.replace(/\.[jt]s$/, '');
const importExpr = ts.factory.createCallExpression(
ts.factory.createIdentifier('require'),
ts.factory.createToken(ts.SyntaxKind.ImportKeyword) as ts.Expression,
undefined,
[ts.factory.createStringLiteral(filePath)]
[ts.factory.createStringLiteral(path)]
);
metadataWithImports.push([importExpr, metadata]);
});
Expand Down

0 comments on commit 8d4db08

Please sign in to comment.