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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 347 by rakin, Mon Jul 29 17:29:42 2024 UTC revision 348 by rakin, Mon Jul 29 17:29:43 2024 UTC
# Line 1  Line 1 
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      }      }
# Line 14  export default class Route { Line 15  export default class Route {
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;

Legend:
Removed from v.347  
changed lines
  Added in v.348

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26