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

Annotation of /branches/5.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: 1498 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import { log } from "console";
2     import type Controller from "../api/Controller";
3     import { RouteMetadata } from "../types/RouteMetadata";
4    
5     export function Action(method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", uri: string, middleware: Function[] = []) {
6     return (target: Controller, propertyKey: string, descriptor: PropertyDescriptor) => {
7     const metadata: Record<string, RouteMetadata> = Reflect.getMetadata("action_methods", target) ?? {
8     [propertyKey]: {
9     GET: null,
10     DELETE: null,
11     PATCH: null,
12     POST: null,
13     PUT: null
14     } as RouteMetadata
15     };
16    
17     metadata[propertyKey] ??= {
18     GET: null,
19     DELETE: null,
20     PATCH: null,
21     POST: null,
22     PUT: null
23     } as RouteMetadata;
24    
25     const data = { handler: descriptor.value, method, path: uri, middleware };
26    
27     metadata[propertyKey]![method] ??= data;
28     metadata[propertyKey]![method]!.handler ??= data.handler;
29     metadata[propertyKey]![method]!.method ??= data.method;
30    
31     if (metadata[propertyKey]![method]!.middleware?.length) {
32     metadata[propertyKey]![method]!.middleware.push(...data.middleware);
33     } else {
34     metadata[propertyKey]![method]!.middleware = data.middleware;
35     }
36    
37     Reflect.defineMetadata("action_methods", metadata, target);
38     log("Found controller function: ", propertyKey);
39     };
40     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26