/[sudobot]/trunk/src/api/Route.ts
ViewVC logotype

Contents of /trunk/src/api/Route.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 324 - (show annotations)
Mon Jul 29 17:29:32 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 785 byte(s)
feat: improve controller handling
1 import { Request, Response } from "express";
2
3 export default class Route {
4 constructor(public readonly method: string, public readonly path: string, public readonly callback: [Object, string], public middlewareList: Array<any> = []) {
5
6 }
7
8 middleware(...middleware: Object[]) {
9 this.middlewareList = [...this.middlewareList, ...middleware];
10 }
11
12 getCallbackFunction(...args: any[]) {
13 const [controller, method] = this.callback;
14 return (req: Request, res: Response) => {
15 const output = (controller as { [key: string]: Function })[method].call(controller, req, res, ...args);
16
17 if (typeof output === 'object') {
18 return res.json(output);
19 }
20
21 return res.send(output);
22 };
23 }
24 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26