1 |
import { Request, Response as ExpressResponse } from "express"; |
import { Request, Response as ExpressResponse } from "express"; |
2 |
|
import Controller from "./Controller"; |
3 |
import Response from "./Response"; |
import Response from "./Response"; |
4 |
|
|
5 |
export default class Route { |
export default class Route { |
6 |
constructor(public readonly method: string, public readonly path: string, public readonly callback: [Object, string], public middlewareList: Array<any> = []) { |
constructor(public readonly method: string, public readonly path: string, public readonly callback: [Controller, string], public middlewareList: Array<Function> = []) { |
7 |
|
this.middlewareList = [...this.middlewareList, ...callback[0].globalMiddleware()]; |
8 |
} |
} |
9 |
|
|
10 |
middleware(...middleware: Object[]) { |
middleware(...middleware: Function[]) { |
11 |
this.middlewareList = [...this.middlewareList, ...middleware]; |
this.middlewareList = [...this.middlewareList, ...middleware]; |
12 |
return this; |
return this; |
13 |
} |
} |
15 |
async getCallbackFunction(...args: any[]) { |
async getCallbackFunction(...args: any[]) { |
16 |
const [controller, method] = this.callback; |
const [controller, method] = this.callback; |
17 |
return async (req: Request, res: ExpressResponse) => { |
return async (req: Request, res: ExpressResponse) => { |
18 |
let output = (controller as { [key: string]: Function })[method].call(controller, req, res, ...args); |
let output = ((controller)[method as keyof typeof controller] as any).call(controller, req, res, ...args); |
19 |
|
|
20 |
if (output instanceof Promise) { |
if (output instanceof Promise) { |
21 |
output = await output; |
output = await output; |