1 | import { HttpVerb } from "./http-verb";
|
2 |
|
3 | export interface LynxRouteBody {
|
4 | name: string;
|
5 | schema: any;
|
6 | }
|
7 |
|
8 | export interface LynxRouteMetadata {
|
9 | type: HttpVerb;
|
10 | path: string;
|
11 | method: any;
|
12 | body?: LynxRouteBody;
|
13 | isAPI: boolean;
|
14 | isMultipartForm: boolean;
|
15 | verifiers: { fun: Function; isAsync: boolean }[];
|
16 | isDisabledOn?: Function;
|
17 | name?: string;
|
18 | }
|
19 |
|
20 | export interface LynxControllerMetadata {
|
21 | controllerPath: string;
|
22 | routes: LynxRouteMetadata[];
|
23 | }
|
24 |
|
25 | let routes: LynxRouteMetadata[] = [];
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export function Route(path: string) {
|
32 | return (target: any) => {
|
33 | target.controllerPath = path;
|
34 | target.routes = routes;
|
35 | routes = [];
|
36 | };
|
37 | }
|
38 |
|
39 | function setTarget(_: any, type: HttpVerb, path: string, method: any) {
|
40 | routes.push({
|
41 | type: type,
|
42 | path: path,
|
43 | method: method,
|
44 | body: undefined,
|
45 | isAPI: false,
|
46 | isMultipartForm: false,
|
47 | verifiers: [],
|
48 | isDisabledOn: undefined
|
49 | });
|
50 | }
|
51 |
|
52 | function setBody(_: any, name: string, schema: any) {
|
53 | if (!routes) return;
|
54 | routes[routes.length - 1].body = {
|
55 | name: name,
|
56 | schema: schema
|
57 | };
|
58 | }
|
59 |
|
60 | function setApi(_: any) {
|
61 | if (!routes) return;
|
62 | routes[routes.length - 1].isAPI = true;
|
63 | }
|
64 |
|
65 | function setMultipartForm(_: any) {
|
66 | if (!routes) return;
|
67 | routes[routes.length - 1].isMultipartForm = true;
|
68 | }
|
69 |
|
70 | function setName(name: string) {
|
71 | if (!routes) return;
|
72 | routes[routes.length - 1].name = name;
|
73 | }
|
74 |
|
75 | function setVerify(_: any, func: Function) {
|
76 | if (!routes) return;
|
77 | routes[routes.length - 1].verifiers.push({ fun: func, isAsync: false });
|
78 | }
|
79 |
|
80 | function setAsyncVerify(_: any, func: Function) {
|
81 | if (!routes) return;
|
82 | routes[routes.length - 1].verifiers.push({ fun: func, isAsync: true });
|
83 | }
|
84 |
|
85 | function setIsDisabledOn(_:any, func: Function) {
|
86 | if (!routes) return;
|
87 | routes[routes.length - 1].isDisabledOn = func;
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | export function GET(path: string) {
|
95 | return (target: any, method: any, _: any) => {
|
96 | setTarget(target, HttpVerb.GET, path, method);
|
97 | };
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | export function POST(path: string) {
|
105 | return (target: any, method: any, _: any) => {
|
106 | setTarget(target, HttpVerb.POST, path, method);
|
107 | };
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | export function PUT(path: string) {
|
115 | return (target: any, method: any, _: any) => {
|
116 | setTarget(target, HttpVerb.PUT, path, method);
|
117 | };
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export function DELETE(path: string) {
|
125 | return (target: any, method: any, _: any) => {
|
126 | setTarget(target, HttpVerb.DELETE, path, method);
|
127 | };
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | export function PATCH(path: string) {
|
135 | return (target: any, method: any, _: any) => {
|
136 | setTarget(target, HttpVerb.PATCH, path, method);
|
137 | };
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | export function Body(name: string, schema: any) {
|
147 | return (target: any, _: any, __: any) => {
|
148 | setBody(target, name, schema);
|
149 | };
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | export function API() {
|
158 | return (target: any, _: any, __: any) => {
|
159 | setApi(target);
|
160 | };
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | export function MultipartForm() {
|
167 | return (target: any, _: any, __: any) => {
|
168 | setMultipartForm(target);
|
169 | };
|
170 | }
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | export function Name(name: string) {
|
178 | return (_: any, __: any, ___: any) => {
|
179 | setName(name);
|
180 | };
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | export function Verify(func: Function) {
|
192 | return (target: any, _: any, __: any) => {
|
193 | setVerify(target, func);
|
194 | };
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | export function AsyncVerify(func: Function) {
|
206 | return (target: any, _: any, __: any) => {
|
207 | setAsyncVerify(target, func);
|
208 | };
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | export function IsDisabledOn(func: Function) {
|
218 | return (target: any, _: any, __: any) => {
|
219 | setIsDisabledOn(target, func);
|
220 | };
|
221 | }
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | export function Middleware(path: string) {
|
228 | return (target: any) => {
|
229 | target.middlewarePath = path;
|
230 | };
|
231 | }
|