Skip to content

Commit 522d6e2

Browse files
committed
feat: add @Render decorator
1 parent a879f96 commit 522d6e2

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/adapter-express/express-utils/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const HTTP_CODE_METADATA = {
1717
path: "inversify-express-utils:path",
1818
};
1919

20+
export const RENDER_METADATA_KEY = Symbol("Render");
21+
2022
export enum PARAMETER_TYPE {
2123
REQUEST,
2224
RESPONSE,

src/adapter-express/express-utils/decorators.ts

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PARAMETER_TYPE,
88
HTTP_VERBS_ENUM,
99
HTTP_CODE_METADATA,
10+
RENDER_METADATA_KEY,
1011
} from "./constants";
1112
import type {
1213
Controller,
@@ -383,6 +384,29 @@ export function params(type: PARAMETER_TYPE, parameterName?: string): ParameterD
383384
};
384385
}
385386

387+
/**
388+
* Render decorator to define the template and default data for a route
389+
* @param template The template to render
390+
* @param defaultData The default data to pass to the template
391+
* @returns
392+
*/
393+
export function Render(template: string, defaultData?: Record<string, unknown>): MethodDecorator {
394+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
395+
return (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor): void => {
396+
Reflect.defineMetadata(RENDER_METADATA_KEY, { template, defaultData }, target, propertyKey);
397+
};
398+
}
399+
400+
export function getRenderMetadata(
401+
target: object,
402+
propertyKey: string | symbol,
403+
): {
404+
template?: string;
405+
defaultData?: Record<string, unknown>;
406+
} {
407+
return Reflect.getMetadata(RENDER_METADATA_KEY, target, propertyKey) || {};
408+
}
409+
386410
/**
387411
* Converts a string value to the specified type.
388412
* @param value The value to convert.

src/adapter-express/express-utils/inversify-express-server.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import type {
4040
RoutingConfig,
4141
} from "./interfaces";
4242
import type { OutgoingHttpHeaders } from "node:http";
43+
import { getRenderMetadata } from "./decorators";
4344

4445
export class InversifyExpressServer {
4546
private _router: Router;
@@ -245,21 +246,26 @@ export class InversifyExpressServer {
245246
controllerName: string,
246247
key: string,
247248
parameterMetadata: Array<ParameterMetadata>,
248-
): express.RequestHandler {
249+
): RequestHandler {
249250
return async (req: Request, res: Response, next: NextFunction): Promise<void> => {
250251
try {
251252
const args = this.extractParameters(req, res, next, parameterMetadata);
252253
const httpContext = this._getHttpContext(req);
253254
httpContext.container.bind<HttpContext>(TYPE.HttpContext).toConstantValue(httpContext);
254255

255256
// invoke controller's action
256-
const value = await (
257-
httpContext.container.getNamed<BaseController>(TYPE.Controller, controllerName)[
258-
key
259-
] as ControllerHandler
260-
)(...args);
261-
262-
if (value instanceof HttpResponseMessage) {
257+
const controller = httpContext.container.getNamed<BaseController>(
258+
TYPE.Controller,
259+
controllerName,
260+
);
261+
const value = await (controller[key] as ControllerHandler)(...args);
262+
263+
const { template, defaultData } = getRenderMetadata(controller, key);
264+
265+
if (template) {
266+
const data = value || defaultData || {};
267+
res.render(template, data as Record<string, unknown>);
268+
} else if (value instanceof HttpResponseMessage) {
263269
await this.handleHttpResponseMessage(value, res);
264270
} else if (instanceOfIHttpActionResult(value)) {
265271
const httpResponseMessage = await value.executeAsync();

0 commit comments

Comments
 (0)