/[sudobot]/branches/8.x/src/decorators/Action.ts
ViewVC logotype

Annotation of /branches/8.x/src/decorators/Action.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 4277 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /*
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
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 { NextFunction, Request, Response } from "express";
21     import Client from "../core/Client";
22     import { RouteMetadata } from "../types/RouteMetadata";
23     import { AnyFunction } from "../types/Utils";
24    
25     export type Middleware = (client: Client, request: Request, response: Response, next: NextFunction) => unknown;
26    
27     export function Action(method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", uri: string, middleware: Middleware[] = []) {
28     return (
29     originalMethodOrTarget: unknown,
30     contextOrMethodName: string | ClassMethodDecoratorContext,
31     descriptor?: PropertyDescriptor
32     ) => {
33     if (typeof contextOrMethodName === "string") {
34     const metadata: Record<string, RouteMetadata> = Reflect.getMetadata(
35     "action_methods",
36     originalMethodOrTarget as object
37     ) ?? {
38     [contextOrMethodName]: {
39     GET: null,
40     DELETE: null,
41     PATCH: null,
42     POST: null,
43     PUT: null
44     } as RouteMetadata
45     };
46    
47     metadata[contextOrMethodName] ??= {
48     GET: null,
49     DELETE: null,
50     PATCH: null,
51     POST: null,
52     PUT: null
53     } as RouteMetadata;
54    
55     const data = { handler: descriptor!.value, method, path: uri, middleware: middleware as AnyFunction[] };
56    
57     metadata[contextOrMethodName]![method] ??= data;
58     metadata[contextOrMethodName]![method]!.handler ??= data.handler;
59     metadata[contextOrMethodName]![method]!.method ??= data.method;
60    
61     if (metadata[contextOrMethodName]![method]!.middleware?.length) {
62     metadata[contextOrMethodName]![method]!.middleware.push(...data.middleware);
63     } else {
64     metadata[contextOrMethodName]![method]!.middleware = data.middleware;
65     }
66    
67     Reflect.defineMetadata("action_methods", metadata, originalMethodOrTarget as object);
68     } else {
69     const metadata = (contextOrMethodName.metadata?.actionMethods ?? {
70     [contextOrMethodName.name]: {
71     GET: null,
72     DELETE: null,
73     PATCH: null,
74     POST: null,
75     PUT: null
76     } as RouteMetadata
77     }) as Record<string, RouteMetadata>;
78    
79     const key = contextOrMethodName.name as keyof typeof metadata;
80    
81     metadata[key] ??= {
82     GET: null,
83     DELETE: null,
84     PATCH: null,
85     POST: null,
86     PUT: null
87     } as RouteMetadata;
88    
89     const data = {
90     handler: originalMethodOrTarget as AnyFunction,
91     method,
92     path: uri,
93     middleware: middleware as AnyFunction[]
94     };
95    
96     metadata[key]![method] ??= data;
97     metadata[key]![method]!.handler ??= data.handler;
98     metadata[key]![method]!.method ??= data.method;
99    
100     if (metadata[key]![method]!.middleware?.length) {
101     metadata[key]![method]!.middleware.push(...data.middleware);
102     } else {
103     metadata[key]![method]!.middleware = data.middleware;
104     }
105    
106     (contextOrMethodName.metadata as unknown) ??= {};
107     contextOrMethodName.metadata.actionMethods = metadata;
108     return originalMethodOrTarget as void;
109     }
110     };
111     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26