Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http-server): multiple decorations support for controllers #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
112 changes: 64 additions & 48 deletions packages/http-server/src/HttpServerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,60 +95,76 @@ export abstract class HttpServerModule<
);

await mapSeries(controllersReflection, ({ controllerInstance, reflection: controllerReflection }) => {
const controllerDecoratorMetadata: ControllerDecoratorMetadata = controllerReflection.decorators.find(
d => d.module === 'http-server' && d.type === 'controller'
);
const basePath =
controllerDecoratorMetadata?.options?.basePath ?? controllerDecoratorMetadata?.options?.basepath ?? '/';
const controllerDecoratorMetadatas: Array<ControllerDecoratorMetadata> =
controllerReflection.decorators.filter(d => d.module === 'http-server' && d.type === 'controller');

// if the controller is not decorated, a default decoration will be assigned to it
if (controllerDecoratorMetadatas.length === 0) {
controllerDecoratorMetadatas.push({
module: 'http-server',
type: 'controller',
options: { basePath: '/' }
});
}

return mapSeries(controllerReflection.methods, async methodReflection => {
const methodDecoratorMetadatas: Array<MethodDecoratorMetadata> = methodReflection.decorators.filter(
d => d[DecoratorId] === 'http-server.method'
);
const methodName = methodReflection.name;
return mapSeries(controllerDecoratorMetadatas, controllerDecoratorMetadata => {
const basePath =
controllerDecoratorMetadata?.options?.basePath ??
controllerDecoratorMetadata?.options?.basepath ??
'/';

return mapSeries(controllerReflection.methods, async methodReflection => {
const methodDecoratorMetadatas: Array<MethodDecoratorMetadata> = methodReflection.decorators.filter(
d => d[DecoratorId] === 'http-server.method'
);
const methodName = methodReflection.name;

if (!methodDecoratorMetadatas.length) return null;

return mapSeries(methodDecoratorMetadatas, async methodDecoratorMetadata => {
const {
verb,
options: { path }
} = methodDecoratorMetadata;

let fullPath = pathUtils.join(basePath, path);
if (fullPath.length > 1 && fullPath[fullPath.length - 1] === '/') {
fullPath = fullPath.slice(0, -1);
}

if (!methodDecoratorMetadatas.length) return null;
const parametersConfig = await this.createParametersConfigurations({
controllerReflection,
methodReflection
});

return mapSeries(methodDecoratorMetadatas, async methodDecoratorMetadata => {
const {
verb,
options: { path }
} = methodDecoratorMetadata;
const responseStatusCodes = Object.keys(methodDecoratorMetadata.options?.responses ?? {})
.reduce((acc, statusCodeString) => {
const statusCode = Number(statusCodeString);
if (!Number.isNaN(statusCode)) {
acc.push(statusCode);
}
return acc;
}, [])
.sort();

const route: Route<SMG['Request']> = {
path: fullPath,
verb,
parametersConfig,
methodDecoratorMetadata,
methodReflection,
controllerDecoratorMetadata,
controllerReflection,
responseStatusCodes
};

let fullPath = pathUtils.join(basePath, path);
if (fullPath.length > 1 && fullPath[fullPath.length - 1] === '/') {
fullPath = fullPath.slice(0, -1);
}
this.routes.push(route);

const parametersConfig = await this.createParametersConfigurations({
controllerReflection,
methodReflection
return this[verb](
fullPath,
await this.createRequestHandler(controllerInstance, methodName, route)
);
});

const responseStatusCodes = Object.keys(methodDecoratorMetadata.options?.responses ?? {})
.reduce((acc, statusCodeString) => {
const statusCode = Number(statusCodeString);
if (!Number.isNaN(statusCode)) {
acc.push(statusCode);
}
return acc;
}, [])
.sort();

const route: Route<SMG['Request']> = {
path: fullPath,
verb,
parametersConfig,
methodDecoratorMetadata,
methodReflection,
controllerDecoratorMetadata,
controllerReflection,
responseStatusCodes
};

this.routes.push(route);

return this[verb](fullPath, await this.createRequestHandler(controllerInstance, methodName, route));
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/http-server/src/decorators/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function controller(options?: ControllerDecoratorOptions): ClassDecorator
type: 'controller',
options
},
{ allowMultiple: false }
{ allowMultiple: true }
);
}

Expand Down
Loading