/[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 323 by rakin, Mon Jul 29 17:29:32 2024 UTC revision 393 by rakin, Mon Jul 29 17:29:59 2024 UTC
# Line 1  Line 1 
1  export default class Route {  /**
2      constructor(public readonly method: string, public readonly path: string, public readonly callback: [Object, string], public middlewareList: Array<Object> = []) {  * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
5    *
6    * SudoBot is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Affero General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10    *
11    * SudoBot is distributed in the hope that it will be useful, but
12    * WITHOUT ANY WARRANTY; without even the implied warranty of
13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    * GNU Affero General Public License for more details.
15    *
16    * You should have received a copy of the GNU Affero General Public License
17    * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18    */
19    
20    import { Request, Response as ExpressResponse } from "express";
21    import Controller from "./Controller";
22    import Response from "./Response";
23    
24    export default class Route {
25        constructor(public readonly method: string, public readonly path: string, public readonly callback: [Controller, string], public middlewareList: Array<Function> = []) {
26            this.middlewareList = [...this.middlewareList, ...callback[0].globalMiddleware()];
27      }      }
28    
29      middleware(...middleware: Object[]) {      middleware(...middleware: Function[]) {
30          this.middlewareList = [...this.middlewareList, ...middleware];          this.middlewareList = [...this.middlewareList, ...middleware];
31            return this;
32      }      }
33    
34      getCallbackFunction(...args: any[]) {      async getCallbackFunction(...args: any[]) {
35          const [controller, method] = this.callback;          const [controller, method] = this.callback;
36          return () => (controller as { [key: string]: Function })[method].call(controller, ...args);          return async (req: Request, res: ExpressResponse) => {
37                let output = ((controller)[method as keyof typeof controller] as any).call(controller, req, res, ...args);
38    
39                if (output instanceof Promise) {
40                    output = await output;
41                }
42    
43                if (output instanceof Response) {
44                    res.status(output.status);
45    
46                    for (const header in output.headers) {
47                        res.setHeader(header, output.headers[header]);
48                    }
49    
50                    if (typeof output.body === 'object') {
51                        res.json(output.body);
52                    }
53                    else {
54                        res.send(output.body);
55                    }
56                }
57                else {
58                    if (typeof output === 'object') {
59                        return res.json(output);
60                    }
61        
62                    return res.send(output);
63                }
64            };
65      }      }
66  }  }

Legend:
Removed from v.323  
changed lines
  Added in v.393

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26