@bool-ts/core 1.4.2 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +11 -11
- package/LICENSE +21 -21
- package/__test/afterDispatcher.ts +9 -0
- package/__test/beforeDispatcher.ts +9 -0
- package/__test/controller.ts +66 -66
- package/__test/firstGuard.ts +10 -0
- package/__test/firstMiddleware.ts +9 -0
- package/__test/index.ts +8 -8
- package/__test/interfaces.ts +7 -7
- package/__test/module.ts +25 -16
- package/__test/repository.ts +16 -16
- package/__test/secondGuard.ts +10 -0
- package/__test/secondMiddleware.ts +9 -0
- package/__test/service.ts +20 -20
- package/bun.lockb +0 -0
- package/dist/decorators/arguments.d.ts +12 -6
- package/dist/decorators/arguments.js +39 -26
- package/dist/decorators/controller.d.ts +9 -1
- package/dist/decorators/controller.js +6 -4
- package/dist/decorators/dispatcher.d.ts +7 -0
- package/dist/decorators/dispatcher.js +9 -0
- package/dist/decorators/guard.d.ts +7 -0
- package/dist/decorators/guard.js +9 -0
- package/dist/decorators/http.d.ts +4 -3
- package/dist/decorators/http.js +6 -5
- package/dist/decorators/index.d.ts +13 -3
- package/dist/decorators/index.js +5 -2
- package/dist/decorators/injectable.d.ts +1 -1
- package/dist/decorators/injectable.js +1 -4
- package/dist/decorators/middleware.d.ts +7 -0
- package/dist/decorators/middleware.js +9 -0
- package/dist/decorators/module.d.ts +28 -7
- package/dist/decorators/module.js +48 -1
- package/dist/entities/route.d.ts +5 -5
- package/dist/entities/routerGroup.d.ts +1 -1
- package/dist/hooks/factory.d.ts +3 -3
- package/dist/hooks/factory.js +283 -60
- package/dist/hooks/injector.js +6 -5
- package/dist/interfaces/index.d.ts +5 -2
- package/dist/interfaces/index.js +1 -1
- package/package.json +1 -1
- package/src/decorators/arguments.ts +214 -186
- package/src/decorators/controller.ts +22 -14
- package/src/decorators/dispatcher.ts +19 -0
- package/src/decorators/guard.ts +19 -0
- package/src/decorators/http.ts +81 -81
- package/src/decorators/index.ts +28 -7
- package/src/decorators/inject.ts +13 -13
- package/src/decorators/injectable.ts +8 -11
- package/src/decorators/middleware.ts +19 -0
- package/src/decorators/module.ts +100 -21
- package/src/decorators/zodSchema.ts +20 -20
- package/src/entities/index.ts +3 -3
- package/src/entities/route.ts +328 -328
- package/src/entities/router.ts +35 -35
- package/src/entities/routerGroup.ts +34 -34
- package/src/hooks/factory.ts +656 -332
- package/src/hooks/index.ts +2 -2
- package/src/hooks/injector.ts +43 -43
- package/src/http/clientError.ts +45 -45
- package/src/http/index.ts +63 -63
- package/src/http/serverError.ts +38 -38
- package/src/index.ts +6 -6
- package/src/interfaces/controller.d.ts +1 -0
- package/src/interfaces/dispatcher.d.ts +3 -0
- package/src/interfaces/guard.d.ts +3 -0
- package/src/interfaces/index.ts +5 -3
- package/src/interfaces/middleware.d.ts +3 -0
- package/src/interfaces/module.d.ts +1 -0
- package/test.http +31 -30
- package/tsconfig.json +107 -107
|
@@ -1,186 +1,214 @@
|
|
|
1
|
-
import * as Zod from "zod";
|
|
2
|
-
|
|
3
|
-
export enum EArgumentTypes {
|
|
4
|
-
|
|
5
|
-
body = "BODY",
|
|
6
|
-
params = "PARAMS",
|
|
7
|
-
param = "PARAM",
|
|
8
|
-
query = "QUERY",
|
|
9
|
-
request = "REQUEST"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
1
|
+
import * as Zod from "zod";
|
|
2
|
+
|
|
3
|
+
export enum EArgumentTypes {
|
|
4
|
+
requestHeaders = "REQUEST_HEADERS",
|
|
5
|
+
body = "BODY",
|
|
6
|
+
params = "PARAMS",
|
|
7
|
+
param = "PARAM",
|
|
8
|
+
query = "QUERY",
|
|
9
|
+
request = "REQUEST",
|
|
10
|
+
responseHeaders = "RESPONSE_HEADERS"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type TArgumentsMetadata =
|
|
14
|
+
| {
|
|
15
|
+
index: number;
|
|
16
|
+
type: EArgumentTypes.requestHeaders;
|
|
17
|
+
zodSchema?: Zod.Schema;
|
|
18
|
+
}
|
|
19
|
+
| {
|
|
20
|
+
index: number;
|
|
21
|
+
type: EArgumentTypes.body;
|
|
22
|
+
zodSchema?: Zod.Schema;
|
|
23
|
+
parser?: "arrayBuffer" | "blob" | "formData" | "json" | "text";
|
|
24
|
+
}
|
|
25
|
+
| {
|
|
26
|
+
index: number;
|
|
27
|
+
type: EArgumentTypes.params;
|
|
28
|
+
zodSchema?: Zod.Schema;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
index: number;
|
|
32
|
+
type: EArgumentTypes.param;
|
|
33
|
+
key: string;
|
|
34
|
+
zodSchema?: Zod.Schema;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
index: number;
|
|
38
|
+
type: EArgumentTypes.query;
|
|
39
|
+
zodSchema?: Zod.Schema;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
index: number;
|
|
43
|
+
type: EArgumentTypes.request;
|
|
44
|
+
zodSchema?: Zod.Schema;
|
|
45
|
+
}
|
|
46
|
+
| {
|
|
47
|
+
index: number;
|
|
48
|
+
type: EArgumentTypes.responseHeaders;
|
|
49
|
+
zodSchema?: Zod.Schema;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const argumentsKey = Symbol.for("__bool:arguments__");
|
|
53
|
+
|
|
54
|
+
export const RequestHeaders = (zodSchema?: Zod.Schema) => {
|
|
55
|
+
return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
56
|
+
if (!methodName) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const headersMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
61
|
+
|
|
62
|
+
headersMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
63
|
+
index: parameterIndex,
|
|
64
|
+
type: EArgumentTypes.requestHeaders,
|
|
65
|
+
zodSchema: zodSchema
|
|
66
|
+
} satisfies Extract<
|
|
67
|
+
TArgumentsMetadata,
|
|
68
|
+
{
|
|
69
|
+
type: EArgumentTypes.requestHeaders;
|
|
70
|
+
}
|
|
71
|
+
>;
|
|
72
|
+
|
|
73
|
+
Reflect.defineMetadata(argumentsKey, headersMetadata, target.constructor, methodName);
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const Body = (zodSchema?: Zod.Schema, parser?: "arrayBuffer" | "blob" | "formData" | "json" | "text") => {
|
|
78
|
+
return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
79
|
+
if (!methodName) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const bodyMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
84
|
+
|
|
85
|
+
bodyMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
86
|
+
index: parameterIndex,
|
|
87
|
+
type: EArgumentTypes.body,
|
|
88
|
+
zodSchema: zodSchema,
|
|
89
|
+
parser: parser
|
|
90
|
+
} satisfies Extract<
|
|
91
|
+
TArgumentsMetadata,
|
|
92
|
+
{
|
|
93
|
+
type: EArgumentTypes.body;
|
|
94
|
+
}
|
|
95
|
+
>;
|
|
96
|
+
|
|
97
|
+
Reflect.defineMetadata(argumentsKey, bodyMetadata, target.constructor, methodName);
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const Params =
|
|
102
|
+
(zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
103
|
+
if (!methodName) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const paramsMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
108
|
+
|
|
109
|
+
paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
110
|
+
index: parameterIndex,
|
|
111
|
+
type: EArgumentTypes.params,
|
|
112
|
+
zodSchema: zodSchema
|
|
113
|
+
} satisfies Extract<
|
|
114
|
+
TArgumentsMetadata,
|
|
115
|
+
{
|
|
116
|
+
type: EArgumentTypes.params;
|
|
117
|
+
}
|
|
118
|
+
>;
|
|
119
|
+
|
|
120
|
+
Reflect.defineMetadata(argumentsKey, paramsMetadata, target.constructor, methodName);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const Param = (key: string, zodSchema?: Zod.Schema) => {
|
|
124
|
+
return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
125
|
+
if (!methodName) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const paramsMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
130
|
+
|
|
131
|
+
paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
132
|
+
index: parameterIndex,
|
|
133
|
+
type: EArgumentTypes.param,
|
|
134
|
+
key: key,
|
|
135
|
+
zodSchema: zodSchema
|
|
136
|
+
} satisfies Extract<
|
|
137
|
+
TArgumentsMetadata,
|
|
138
|
+
{
|
|
139
|
+
type: EArgumentTypes.param;
|
|
140
|
+
}
|
|
141
|
+
>;
|
|
142
|
+
|
|
143
|
+
Reflect.defineMetadata(argumentsKey, paramsMetadata, target.constructor, methodName);
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export const Query = (zodSchema?: Zod.Schema) => {
|
|
148
|
+
return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
149
|
+
if (!methodName) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const queryMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
154
|
+
|
|
155
|
+
queryMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
156
|
+
index: parameterIndex,
|
|
157
|
+
type: EArgumentTypes.params,
|
|
158
|
+
zodSchema: zodSchema
|
|
159
|
+
} satisfies Extract<
|
|
160
|
+
TArgumentsMetadata,
|
|
161
|
+
{
|
|
162
|
+
type: EArgumentTypes.params;
|
|
163
|
+
}
|
|
164
|
+
>;
|
|
165
|
+
|
|
166
|
+
Reflect.defineMetadata(argumentsKey, queryMetadata, target.constructor, methodName);
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const Request = (zodSchema?: Zod.Schema) => {
|
|
171
|
+
return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
172
|
+
if (!methodName) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const queryMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
177
|
+
|
|
178
|
+
queryMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
179
|
+
index: parameterIndex,
|
|
180
|
+
type: EArgumentTypes.request,
|
|
181
|
+
zodSchema: zodSchema
|
|
182
|
+
} satisfies Extract<
|
|
183
|
+
TArgumentsMetadata,
|
|
184
|
+
{
|
|
185
|
+
type: EArgumentTypes.request;
|
|
186
|
+
}
|
|
187
|
+
>;
|
|
188
|
+
|
|
189
|
+
Reflect.defineMetadata(argumentsKey, queryMetadata, target.constructor, methodName);
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const ResponseHeaders = (zodSchema?: Zod.Schema) => {
|
|
194
|
+
return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
195
|
+
if (!methodName) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const queryMetadata = Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
200
|
+
|
|
201
|
+
queryMetadata[`argumentIndexes.${parameterIndex}`] = {
|
|
202
|
+
index: parameterIndex,
|
|
203
|
+
type: EArgumentTypes.responseHeaders,
|
|
204
|
+
zodSchema: zodSchema
|
|
205
|
+
} satisfies Extract<
|
|
206
|
+
TArgumentsMetadata,
|
|
207
|
+
{
|
|
208
|
+
type: EArgumentTypes.responseHeaders;
|
|
209
|
+
}
|
|
210
|
+
>;
|
|
211
|
+
|
|
212
|
+
Reflect.defineMetadata(argumentsKey, queryMetadata, target.constructor, methodName);
|
|
213
|
+
};
|
|
214
|
+
};
|
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import type { IController } from "../interfaces/controller";
|
|
2
|
+
import { controllerHttpKey, type THttpMetadata } from "./http";
|
|
3
|
+
|
|
4
|
+
export type TControllerMetadata = Required<{
|
|
5
|
+
prefix: string;
|
|
6
|
+
httpMetadata: THttpMetadata;
|
|
7
|
+
}>;
|
|
8
|
+
|
|
9
|
+
export const controllerKey = Symbol.for("__bool:controller__");
|
|
10
|
+
|
|
11
|
+
export const Controller =
|
|
12
|
+
(prefix: string) =>
|
|
13
|
+
<T extends { new (...args: any[]): IController }>(target: T) => {
|
|
14
|
+
const metadata: TControllerMetadata = {
|
|
15
|
+
prefix: !prefix.startsWith("/") ? `/${prefix}` : prefix,
|
|
16
|
+
httpMetadata: [...(Reflect.getOwnMetadata(controllerHttpKey, target.constructor) || [])]
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
Reflect.defineMetadata(controllerKey, metadata, target);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default Controller;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IDispatcher } from "../interfaces/dispatcher";
|
|
2
|
+
|
|
3
|
+
export type TDispatcherMetadata = undefined;
|
|
4
|
+
|
|
5
|
+
export const dispatcherKey = Symbol.for("__bool:dispatcher__");
|
|
6
|
+
|
|
7
|
+
export const Dispatcher =
|
|
8
|
+
() =>
|
|
9
|
+
<T extends { new (...args: any[]): IDispatcher }>(target: T) => {
|
|
10
|
+
if (!("execute" in target.prototype) || typeof target.prototype.execute !== "function") {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const metadata: TDispatcherMetadata = undefined;
|
|
15
|
+
|
|
16
|
+
Reflect.defineMetadata(dispatcherKey, metadata, target);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default Dispatcher;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IGuard } from "../interfaces";
|
|
2
|
+
|
|
3
|
+
export type TGuardMetadata = undefined;
|
|
4
|
+
|
|
5
|
+
export const guardKey = Symbol.for("__bool:guard__");
|
|
6
|
+
|
|
7
|
+
export const Guard =
|
|
8
|
+
() =>
|
|
9
|
+
<T extends { new (...args: any[]): IGuard }>(target: T) => {
|
|
10
|
+
if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const metadata = undefined;
|
|
15
|
+
|
|
16
|
+
Reflect.defineMetadata(guardKey, metadata, target);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default Guard;
|
package/src/decorators/http.ts
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
export
|
|
2
|
-
path: string;
|
|
3
|
-
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
|
-
methodName: string;
|
|
5
|
-
descriptor: PropertyDescriptor;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
[
|
|
21
|
-
|
|
22
|
-
{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
*
|
|
35
|
-
* @param path
|
|
36
|
-
* @returns
|
|
37
|
-
*/
|
|
38
|
-
export const Get = (path = "/") => defaultDecorator(path, "Get");
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
*
|
|
42
|
-
* @param path
|
|
43
|
-
* @returns
|
|
44
|
-
*/
|
|
45
|
-
export const Post = (path = "/") => defaultDecorator(path, "Post");
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
*
|
|
49
|
-
* @param path
|
|
50
|
-
* @returns
|
|
51
|
-
*/
|
|
52
|
-
export const Put = (path = "/") => defaultDecorator(path, "Put");
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
*
|
|
56
|
-
* @param path
|
|
57
|
-
* @returns
|
|
58
|
-
*/
|
|
59
|
-
export const Patch = (path = "/") => defaultDecorator(path, "Patch");
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
*
|
|
63
|
-
* @param path
|
|
64
|
-
* @returns
|
|
65
|
-
*/
|
|
66
|
-
export const Delete = (path = "/") => defaultDecorator(path, "Delete");
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
*
|
|
70
|
-
* @param path
|
|
71
|
-
* @returns
|
|
72
|
-
*/
|
|
73
|
-
export const Options = (path = "/") => defaultDecorator(path, "Options");
|
|
74
|
-
|
|
75
|
-
export default {
|
|
76
|
-
Get,
|
|
77
|
-
Post,
|
|
78
|
-
Put,
|
|
79
|
-
Patch,
|
|
80
|
-
Delete
|
|
81
|
-
};
|
|
1
|
+
export type TRoute = {
|
|
2
|
+
path: string;
|
|
3
|
+
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
|
+
methodName: string;
|
|
5
|
+
descriptor: PropertyDescriptor;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type THttpMetadata = Array<TRoute>;
|
|
9
|
+
|
|
10
|
+
export const controllerHttpKey = Symbol.for("__bool:controller.http__");
|
|
11
|
+
|
|
12
|
+
const defaultDecorator =
|
|
13
|
+
(path: string, method: "Get" | "Post" | "Put" | "Patch" | "Delete" | "Options") =>
|
|
14
|
+
(target: Object, methodName: string, descriptor: PropertyDescriptor) => {
|
|
15
|
+
if (!(descriptor.value instanceof Function)) {
|
|
16
|
+
throw Error(`${method} decorator only use for class method.`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const metadata: THttpMetadata = [
|
|
20
|
+
...(Reflect.getOwnMetadata(controllerHttpKey, target.constructor) || []),
|
|
21
|
+
{
|
|
22
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
23
|
+
httpMethod: method.toUpperCase(),
|
|
24
|
+
methodName: methodName,
|
|
25
|
+
descriptor: descriptor
|
|
26
|
+
}
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
// Define controller metadata
|
|
30
|
+
Reflect.defineMetadata(controllerHttpKey, metadata, target.constructor);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param path
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
export const Get = (path = "/") => defaultDecorator(path, "Get");
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param path
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
export const Post = (path = "/") => defaultDecorator(path, "Post");
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param path
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
export const Put = (path = "/") => defaultDecorator(path, "Put");
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
* @param path
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
export const Patch = (path = "/") => defaultDecorator(path, "Patch");
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* @param path
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
export const Delete = (path = "/") => defaultDecorator(path, "Delete");
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
*
|
|
70
|
+
* @param path
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
export const Options = (path = "/") => defaultDecorator(path, "Options");
|
|
74
|
+
|
|
75
|
+
export default {
|
|
76
|
+
Get,
|
|
77
|
+
Post,
|
|
78
|
+
Put,
|
|
79
|
+
Patch,
|
|
80
|
+
Delete
|
|
81
|
+
};
|