1 |
rakin |
324 |
import { Request, Response } from "express"; |
2 |
|
|
|
3 |
rakin |
323 |
export default class Route { |
4 |
rakin |
324 |
constructor(public readonly method: string, public readonly path: string, public readonly callback: [Object, string], public middlewareList: Array<any> = []) { |
5 |
rakin |
323 |
|
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 |
rakin |
324 |
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 |
rakin |
323 |
} |
24 |
|
|
} |