@bool-ts/core 1.9.0 → 1.9.2
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/__test/_constants.ts +1 -0
- package/__test/container.ts +28 -0
- package/__test/controller.ts +1 -1
- package/__test/{module.ts → firstModule.ts} +4 -6
- package/__test/index.ts +2 -4
- package/__test/secondModule.ts +29 -0
- package/__test/tsconfig.json +1 -1
- package/__test/webSocket.ts +1 -1
- package/dist/decorators/arguments.d.ts +6 -1
- package/dist/decorators/container.d.ts +29 -0
- package/dist/decorators/http.d.ts +2 -0
- package/dist/decorators/index.d.ts +4 -2
- package/dist/decorators/module.d.ts +6 -2
- package/dist/decorators/webSocket.d.ts +2 -0
- package/dist/entities/httpRoute.d.ts +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/interfaces/context.d.ts +8 -2
- package/dist/interfaces/index.d.ts +1 -1
- package/dist/keys/index.d.ts +3 -0
- package/dist/producers/context.d.ts +13 -0
- package/dist/{hooks → producers}/factory.d.ts +5 -2
- package/dist/{hooks → producers}/injector.d.ts +7 -1
- package/package.json +3 -3
- package/src/decorators/arguments.ts +126 -77
- package/src/decorators/container.ts +94 -0
- package/src/decorators/controller.ts +1 -1
- package/src/decorators/http.ts +9 -2
- package/src/decorators/index.ts +4 -1
- package/src/decorators/module.ts +27 -4
- package/src/decorators/webSocket.ts +6 -2
- package/src/entities/httpRoute.ts +2 -0
- package/src/index.ts +1 -1
- package/src/interfaces/context.ts +9 -2
- package/src/interfaces/index.ts +1 -1
- package/src/keys/index.ts +3 -0
- package/src/producers/context.ts +63 -0
- package/src/producers/factory.ts +2013 -0
- package/src/{hooks → producers}/injector.ts +41 -6
- package/src/hooks/factory.ts +0 -1701
- /package/dist/{hooks → producers}/index.d.ts +0 -0
- /package/src/{hooks → producers}/index.ts +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bool-ts/core",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
"@bool-ts/date-time": "^1.0.0",
|
|
22
22
|
"qs": "^6.14.0",
|
|
23
23
|
"reflect-metadata": "^0.2.2",
|
|
24
|
-
"zod": "^3.24.
|
|
24
|
+
"zod": "^3.24.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/bun": "latest",
|
|
28
28
|
"@types/qs": "^6.9.18",
|
|
29
|
-
"typescript": "^5.
|
|
29
|
+
"typescript": "^5.8.2"
|
|
30
30
|
},
|
|
31
31
|
"private": "false"
|
|
32
32
|
}
|
|
@@ -2,6 +2,7 @@ import * as Zod from "zod";
|
|
|
2
2
|
import {
|
|
3
3
|
argumentsKey,
|
|
4
4
|
contextArgsKey,
|
|
5
|
+
httpServerArgsKey,
|
|
5
6
|
paramArgsKey,
|
|
6
7
|
paramsArgsKey,
|
|
7
8
|
queryArgsKey,
|
|
@@ -64,17 +65,25 @@ export type TArgumentsMetadata =
|
|
|
64
65
|
| {
|
|
65
66
|
index: number;
|
|
66
67
|
type: typeof routeModelArgsKey;
|
|
68
|
+
}
|
|
69
|
+
| {
|
|
70
|
+
index: number;
|
|
71
|
+
type: typeof httpServerArgsKey;
|
|
67
72
|
};
|
|
68
73
|
|
|
74
|
+
export type TArgumentsMetadataCollection = Record<`argumentIndexes.${number}`, TArgumentsMetadata>;
|
|
75
|
+
|
|
69
76
|
export const RequestHeaders =
|
|
70
|
-
(schema?: Zod.Schema) =>
|
|
77
|
+
(schema?: Zod.Schema) =>
|
|
78
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
71
79
|
if (!methodName) {
|
|
72
80
|
return;
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
const
|
|
83
|
+
const metadata: TArgumentsMetadataCollection =
|
|
84
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
76
85
|
|
|
77
|
-
|
|
86
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
78
87
|
index: parameterIndex,
|
|
79
88
|
type: requestHeadersArgsKey,
|
|
80
89
|
zodSchema: schema
|
|
@@ -85,18 +94,20 @@ export const RequestHeaders =
|
|
|
85
94
|
}
|
|
86
95
|
>;
|
|
87
96
|
|
|
88
|
-
Reflect.defineMetadata(argumentsKey,
|
|
97
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
89
98
|
};
|
|
90
99
|
|
|
91
100
|
export const RequestHeader =
|
|
92
|
-
(key: string, schema?: Zod.Schema) =>
|
|
101
|
+
(key: string, schema?: Zod.Schema) =>
|
|
102
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
93
103
|
if (!methodName) {
|
|
94
104
|
return;
|
|
95
105
|
}
|
|
96
106
|
|
|
97
|
-
const
|
|
107
|
+
const metadata: TArgumentsMetadataCollection =
|
|
108
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
98
109
|
|
|
99
|
-
|
|
110
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
100
111
|
index: parameterIndex,
|
|
101
112
|
type: requestHeaderArgsKey,
|
|
102
113
|
key: key,
|
|
@@ -108,7 +119,7 @@ export const RequestHeader =
|
|
|
108
119
|
}
|
|
109
120
|
>;
|
|
110
121
|
|
|
111
|
-
Reflect.defineMetadata(argumentsKey,
|
|
122
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
112
123
|
};
|
|
113
124
|
|
|
114
125
|
export const RequestBody =
|
|
@@ -118,9 +129,10 @@ export const RequestBody =
|
|
|
118
129
|
return;
|
|
119
130
|
}
|
|
120
131
|
|
|
121
|
-
const
|
|
132
|
+
const metadata: TArgumentsMetadataCollection =
|
|
133
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
122
134
|
|
|
123
|
-
|
|
135
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
124
136
|
index: parameterIndex,
|
|
125
137
|
type: requestBodyArgsKey,
|
|
126
138
|
zodSchema: schema,
|
|
@@ -132,18 +144,20 @@ export const RequestBody =
|
|
|
132
144
|
}
|
|
133
145
|
>;
|
|
134
146
|
|
|
135
|
-
Reflect.defineMetadata(argumentsKey,
|
|
147
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
136
148
|
};
|
|
137
149
|
|
|
138
150
|
export const Params =
|
|
139
|
-
(schema?: Zod.Schema) =>
|
|
151
|
+
(schema?: Zod.Schema) =>
|
|
152
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
140
153
|
if (!methodName) {
|
|
141
154
|
return;
|
|
142
155
|
}
|
|
143
156
|
|
|
144
|
-
const
|
|
157
|
+
const metadata: TArgumentsMetadataCollection =
|
|
158
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
145
159
|
|
|
146
|
-
|
|
160
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
147
161
|
index: parameterIndex,
|
|
148
162
|
type: paramsArgsKey,
|
|
149
163
|
zodSchema: schema
|
|
@@ -154,18 +168,20 @@ export const Params =
|
|
|
154
168
|
}
|
|
155
169
|
>;
|
|
156
170
|
|
|
157
|
-
Reflect.defineMetadata(argumentsKey,
|
|
171
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
158
172
|
};
|
|
159
173
|
|
|
160
174
|
export const Param =
|
|
161
|
-
(key: string, schema?: Zod.Schema) =>
|
|
175
|
+
(key: string, schema?: Zod.Schema) =>
|
|
176
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
162
177
|
if (!methodName) {
|
|
163
178
|
return;
|
|
164
179
|
}
|
|
165
180
|
|
|
166
|
-
const
|
|
181
|
+
const metadata: TArgumentsMetadataCollection =
|
|
182
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
167
183
|
|
|
168
|
-
|
|
184
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
169
185
|
index: parameterIndex,
|
|
170
186
|
type: paramArgsKey,
|
|
171
187
|
key: key,
|
|
@@ -177,18 +193,20 @@ export const Param =
|
|
|
177
193
|
}
|
|
178
194
|
>;
|
|
179
195
|
|
|
180
|
-
Reflect.defineMetadata(argumentsKey,
|
|
196
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
181
197
|
};
|
|
182
198
|
|
|
183
199
|
export const Query =
|
|
184
|
-
(schema?: Zod.Schema) =>
|
|
200
|
+
(schema?: Zod.Schema) =>
|
|
201
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
185
202
|
if (!methodName) {
|
|
186
203
|
return;
|
|
187
204
|
}
|
|
188
205
|
|
|
189
|
-
const
|
|
206
|
+
const metadata: TArgumentsMetadataCollection =
|
|
207
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
190
208
|
|
|
191
|
-
|
|
209
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
192
210
|
index: parameterIndex,
|
|
193
211
|
type: queryArgsKey,
|
|
194
212
|
zodSchema: schema
|
|
@@ -199,18 +217,20 @@ export const Query =
|
|
|
199
217
|
}
|
|
200
218
|
>;
|
|
201
219
|
|
|
202
|
-
Reflect.defineMetadata(argumentsKey,
|
|
220
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
203
221
|
};
|
|
204
222
|
|
|
205
223
|
export const Request =
|
|
206
|
-
(schema?: Zod.Schema) =>
|
|
224
|
+
(schema?: Zod.Schema) =>
|
|
225
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
207
226
|
if (!methodName) {
|
|
208
227
|
return;
|
|
209
228
|
}
|
|
210
229
|
|
|
211
|
-
const
|
|
230
|
+
const metadata: TArgumentsMetadataCollection =
|
|
231
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
212
232
|
|
|
213
|
-
|
|
233
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
214
234
|
index: parameterIndex,
|
|
215
235
|
type: requestArgsKey,
|
|
216
236
|
zodSchema: schema
|
|
@@ -221,66 +241,95 @@ export const Request =
|
|
|
221
241
|
}
|
|
222
242
|
>;
|
|
223
243
|
|
|
224
|
-
Reflect.defineMetadata(argumentsKey,
|
|
244
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
225
245
|
};
|
|
226
246
|
|
|
227
|
-
export const ResponseHeaders =
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
247
|
+
export const ResponseHeaders =
|
|
248
|
+
() => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
249
|
+
if (!methodName) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const metadata: TArgumentsMetadataCollection =
|
|
254
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
231
255
|
|
|
232
|
-
|
|
256
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
257
|
+
index: parameterIndex,
|
|
258
|
+
type: responseHeadersArgsKey
|
|
259
|
+
} satisfies Extract<
|
|
260
|
+
TArgumentsMetadata,
|
|
261
|
+
{
|
|
262
|
+
type: typeof responseHeadersArgsKey;
|
|
263
|
+
}
|
|
264
|
+
>;
|
|
265
|
+
|
|
266
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
267
|
+
};
|
|
233
268
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
{
|
|
240
|
-
type: typeof responseHeadersArgsKey;
|
|
269
|
+
export const Context =
|
|
270
|
+
(key?: symbol) =>
|
|
271
|
+
(target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
272
|
+
if (!methodName) {
|
|
273
|
+
return;
|
|
241
274
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
275
|
+
|
|
276
|
+
const metadata: TArgumentsMetadataCollection =
|
|
277
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
278
|
+
|
|
279
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
280
|
+
index: parameterIndex,
|
|
281
|
+
type: contextArgsKey,
|
|
282
|
+
key: key
|
|
283
|
+
} satisfies Extract<
|
|
284
|
+
TArgumentsMetadata,
|
|
285
|
+
{
|
|
286
|
+
type: typeof contextArgsKey;
|
|
287
|
+
}
|
|
288
|
+
>;
|
|
289
|
+
|
|
290
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
export const RouteModel =
|
|
294
|
+
() => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
295
|
+
if (!methodName) {
|
|
296
|
+
return;
|
|
262
297
|
}
|
|
263
|
-
>;
|
|
264
298
|
|
|
265
|
-
|
|
266
|
-
};
|
|
299
|
+
const metadata: TArgumentsMetadataCollection =
|
|
300
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
267
301
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
302
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
303
|
+
index: parameterIndex,
|
|
304
|
+
type: routeModelArgsKey
|
|
305
|
+
} satisfies Extract<
|
|
306
|
+
TArgumentsMetadata,
|
|
307
|
+
{
|
|
308
|
+
type: typeof routeModelArgsKey;
|
|
309
|
+
}
|
|
310
|
+
>;
|
|
272
311
|
|
|
273
|
-
|
|
312
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
313
|
+
};
|
|
274
314
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
TArgumentsMetadata,
|
|
280
|
-
{
|
|
281
|
-
type: typeof routeModelArgsKey;
|
|
315
|
+
export const HttpServer =
|
|
316
|
+
() => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
|
|
317
|
+
if (!methodName) {
|
|
318
|
+
return;
|
|
282
319
|
}
|
|
283
|
-
>;
|
|
284
320
|
|
|
285
|
-
|
|
286
|
-
};
|
|
321
|
+
const metadata: TArgumentsMetadataCollection =
|
|
322
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
323
|
+
|
|
324
|
+
metadata[`argumentIndexes.${parameterIndex}`] = {
|
|
325
|
+
index: parameterIndex,
|
|
326
|
+
type: httpServerArgsKey
|
|
327
|
+
} satisfies Extract<
|
|
328
|
+
TArgumentsMetadata,
|
|
329
|
+
{
|
|
330
|
+
type: typeof httpServerArgsKey;
|
|
331
|
+
}
|
|
332
|
+
>;
|
|
333
|
+
|
|
334
|
+
Reflect.defineMetadata(argumentsKey, metadata, target.constructor, methodName);
|
|
335
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { IModule } from "../interfaces";
|
|
2
|
+
|
|
3
|
+
import { containerKey, guardKey, injectableKey, middlewareKey, moduleKey } from "../keys";
|
|
4
|
+
|
|
5
|
+
type TInstances = Array<new (...args: any[]) => any>;
|
|
6
|
+
type TLoaders<TConfig extends {} = {}> = Record<
|
|
7
|
+
string | symbol,
|
|
8
|
+
(args: { config: TConfig }) => [string | symbol, any] | Promise<[string | symbol, any]>
|
|
9
|
+
>;
|
|
10
|
+
export type TContainerConfig<TConfig> =
|
|
11
|
+
| TConfig
|
|
12
|
+
| (() => TConfig | Promise<TConfig>)
|
|
13
|
+
| Readonly<{
|
|
14
|
+
key: symbol;
|
|
15
|
+
value: TConfig | (() => TConfig | Promise<TConfig>);
|
|
16
|
+
}>;
|
|
17
|
+
|
|
18
|
+
export type TContainerOptions<TConfig extends {} = {}> =
|
|
19
|
+
| Partial<{
|
|
20
|
+
config: TContainerConfig<TConfig>;
|
|
21
|
+
modules: TInstances;
|
|
22
|
+
dependencies: TInstances;
|
|
23
|
+
loaders: TLoaders<TConfig>;
|
|
24
|
+
middlewares: TInstances;
|
|
25
|
+
guards: TInstances;
|
|
26
|
+
}>
|
|
27
|
+
| undefined;
|
|
28
|
+
|
|
29
|
+
export type TContainerMetadata<TConfig extends {} = {}> =
|
|
30
|
+
| Partial<{
|
|
31
|
+
modules: TInstances;
|
|
32
|
+
config: TContainerConfig<TConfig>;
|
|
33
|
+
dependencies: TInstances;
|
|
34
|
+
loaders: TLoaders<TConfig>;
|
|
35
|
+
middlewares: TInstances;
|
|
36
|
+
guards: TInstances;
|
|
37
|
+
}>
|
|
38
|
+
| undefined;
|
|
39
|
+
|
|
40
|
+
export const Container =
|
|
41
|
+
<TConfig extends {} = {}>(args?: TContainerOptions<TConfig>) =>
|
|
42
|
+
<T extends { new (...args: any[]): IModule }>(target: T) => {
|
|
43
|
+
const { modules, middlewares, guards, dependencies } = args || {};
|
|
44
|
+
|
|
45
|
+
if (Reflect.hasOwnMetadata(moduleKey, target)) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Conflict detected! ${target.name} class cannot be both a Module and a Container.`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (modules) {
|
|
52
|
+
for (let i = 0; i < modules.length; i++) {
|
|
53
|
+
if (!Reflect.getOwnMetadataKeys(modules[i]).includes(moduleKey)) {
|
|
54
|
+
throw Error(`${modules[i].name} is not a module.`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (middlewares) {
|
|
60
|
+
for (let i = 0; i < middlewares.length; i++) {
|
|
61
|
+
if (!Reflect.getOwnMetadataKeys(middlewares[i]).includes(middlewareKey)) {
|
|
62
|
+
throw Error(`${middlewares[i].name} is not a middleware.`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (middlewares) {
|
|
68
|
+
for (let i = 0; i < middlewares.length; i++) {
|
|
69
|
+
if (!Reflect.getOwnMetadataKeys(middlewares[i]).includes(middlewareKey)) {
|
|
70
|
+
throw Error(`${middlewares[i].name} is not a middleware.`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (guards) {
|
|
76
|
+
for (let i = 0; i < guards.length; i++) {
|
|
77
|
+
if (!Reflect.getOwnMetadataKeys(guards[i]).includes(guardKey)) {
|
|
78
|
+
throw Error(`${guards[i].name} is not a guard.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (dependencies) {
|
|
84
|
+
for (let i = 0; i < dependencies.length; i++) {
|
|
85
|
+
if (!Reflect.getOwnMetadataKeys(dependencies[i]).includes(injectableKey)) {
|
|
86
|
+
throw Error(`${dependencies[i].name} is not an injectable.`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
Reflect.defineMetadata(containerKey, args, target);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export default Container;
|
|
@@ -13,7 +13,7 @@ export const Controller =
|
|
|
13
13
|
<T extends { new (...args: any[]): IController }>(target: T) => {
|
|
14
14
|
const metadata: TControllerMetadata = {
|
|
15
15
|
prefix: !prefix?.startsWith("/") ? `/${prefix || ""}` : prefix,
|
|
16
|
-
httpMetadata: [...(Reflect.getOwnMetadata(controllerHttpKey, target
|
|
16
|
+
httpMetadata: [...(Reflect.getOwnMetadata(controllerHttpKey, target) || [])]
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
Reflect.defineMetadata(controllerKey, metadata, target);
|
package/src/decorators/http.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TArgumentsMetadataCollection } from "./arguments";
|
|
2
|
+
|
|
3
|
+
import { argumentsKey, controllerHttpKey } from "../keys";
|
|
2
4
|
|
|
3
5
|
export type TRoute = {
|
|
4
6
|
path: string;
|
|
5
7
|
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
6
8
|
methodName: string;
|
|
7
9
|
descriptor: TypedPropertyDescriptor<any>;
|
|
10
|
+
argumentsMetadata: TArgumentsMetadataCollection;
|
|
8
11
|
};
|
|
9
12
|
|
|
10
13
|
export type THttpMetadata = Array<TRoute>;
|
|
@@ -16,13 +19,17 @@ const defaultDecorator =
|
|
|
16
19
|
throw Error(`${method} decorator only use for class method.`);
|
|
17
20
|
}
|
|
18
21
|
|
|
22
|
+
const argumentsMetadata: TArgumentsMetadataCollection =
|
|
23
|
+
Reflect.getOwnMetadata(argumentsKey, target.constructor, methodName) || {};
|
|
24
|
+
|
|
19
25
|
const metadata: THttpMetadata = [
|
|
20
26
|
...(Reflect.getOwnMetadata(controllerHttpKey, target.constructor) || []),
|
|
21
27
|
{
|
|
22
28
|
path: !path.startsWith("/") ? `/${path}` : path,
|
|
23
29
|
httpMethod: method.toUpperCase(),
|
|
24
30
|
methodName: methodName,
|
|
25
|
-
descriptor: descriptor
|
|
31
|
+
descriptor: descriptor,
|
|
32
|
+
argumentsMetadata: argumentsMetadata
|
|
26
33
|
}
|
|
27
34
|
];
|
|
28
35
|
|
package/src/decorators/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export {
|
|
2
2
|
Context,
|
|
3
|
+
HttpServer,
|
|
3
4
|
Param,
|
|
4
5
|
Params,
|
|
5
6
|
Query,
|
|
@@ -10,6 +11,7 @@ export {
|
|
|
10
11
|
ResponseHeaders,
|
|
11
12
|
RouteModel
|
|
12
13
|
} from "./arguments";
|
|
14
|
+
export { Container } from "./container";
|
|
13
15
|
export { Controller } from "./controller";
|
|
14
16
|
export { Dispatcher } from "./dispatcher";
|
|
15
17
|
export { Guard } from "./guard";
|
|
@@ -29,11 +31,12 @@ export { WebSocketEvent } from "./webSocketEvent";
|
|
|
29
31
|
export { ZodSchema } from "./zodSchema";
|
|
30
32
|
|
|
31
33
|
export type { TArgumentsMetadata } from "./arguments";
|
|
34
|
+
export type { TContainerConfig, TContainerMetadata, TContainerOptions } from "./container";
|
|
32
35
|
export type { TControllerMetadata } from "./controller";
|
|
33
36
|
export type { TDispatcherMetadata } from "./dispatcher";
|
|
34
37
|
export type { TGuardMetadata } from "./guard";
|
|
35
38
|
export type { THttpMetadata } from "./http";
|
|
36
39
|
export type { TMiddlewareMetadata } from "./middleware";
|
|
37
|
-
export type { TModuleMetadata, TModuleOptions } from "./module";
|
|
40
|
+
export type { TModuleConfig, TModuleMetadata, TModuleOptions } from "./module";
|
|
38
41
|
export type { TWebSocketMetadata } from "./webSocket";
|
|
39
42
|
export type { TWebSocketEventHandlerMetadata, TWebSocketEventMetadata } from "./webSocketEvent";
|
package/src/decorators/module.ts
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
import type { IModule } from "../interfaces";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
containerKey,
|
|
5
|
+
controllerKey,
|
|
6
|
+
dispatcherKey,
|
|
7
|
+
guardKey,
|
|
8
|
+
injectableKey,
|
|
9
|
+
middlewareKey,
|
|
10
|
+
moduleKey,
|
|
11
|
+
webSocketKey
|
|
12
|
+
} from "../keys";
|
|
4
13
|
|
|
5
14
|
type TInstances = Array<new (...args: any[]) => any>;
|
|
6
15
|
type TLoaders<TConfig extends {} = {}> = Record<
|
|
7
16
|
string | symbol,
|
|
8
17
|
(args: { config: TConfig }) => [string | symbol, any] | Promise<[string | symbol, any]>
|
|
9
18
|
>;
|
|
19
|
+
export type TModuleConfig<TConfig> =
|
|
20
|
+
| TConfig
|
|
21
|
+
| (() => TConfig | Promise<TConfig>)
|
|
22
|
+
| Readonly<{
|
|
23
|
+
key: symbol;
|
|
24
|
+
value: TConfig | (() => TConfig | Promise<TConfig>);
|
|
25
|
+
}>;
|
|
10
26
|
|
|
11
27
|
export type TModuleOptions<TConfig extends {} = {}> =
|
|
12
28
|
| Partial<{
|
|
13
|
-
config:
|
|
29
|
+
config: TModuleConfig<TConfig>;
|
|
14
30
|
prefix: string;
|
|
15
31
|
dependencies: TInstances;
|
|
16
32
|
loaders: TLoaders<TConfig>;
|
|
@@ -24,7 +40,7 @@ export type TModuleOptions<TConfig extends {} = {}> =
|
|
|
24
40
|
|
|
25
41
|
export type TModuleMetadata<TConfig extends {} = {}> =
|
|
26
42
|
| Partial<{
|
|
27
|
-
config:
|
|
43
|
+
config: TModuleConfig<TConfig>;
|
|
28
44
|
prefix: string;
|
|
29
45
|
dependencies: TInstances;
|
|
30
46
|
loaders: TLoaders<TConfig>;
|
|
@@ -39,7 +55,14 @@ export type TModuleMetadata<TConfig extends {} = {}> =
|
|
|
39
55
|
export const Module =
|
|
40
56
|
<TConfig extends {} = {}>(args?: TModuleOptions<TConfig>) =>
|
|
41
57
|
<T extends { new (...args: any[]): IModule }>(target: T) => {
|
|
42
|
-
|
|
58
|
+
if (Reflect.hasOwnMetadata(containerKey, target)) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`Conflict detected! ${target.name} class cannot be both a Module and a Container.`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const { middlewares, guards, dispatchers, controllers, dependencies, webSockets } =
|
|
65
|
+
args || {};
|
|
43
66
|
|
|
44
67
|
if (middlewares) {
|
|
45
68
|
for (let i = 0; i < middlewares.length; i++) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Server } from "bun";
|
|
2
2
|
import type { IWebSocket } from "../interfaces";
|
|
3
|
+
import type { TArgumentsMetadataCollection } from "./arguments";
|
|
3
4
|
import type { TWebSocketEventMetadata } from "./webSocketEvent";
|
|
4
5
|
|
|
5
6
|
import { webSocketEventKey, webSocketKey } from "../keys";
|
|
@@ -9,6 +10,7 @@ export type TWebSocketHttpRouteMetadata = {
|
|
|
9
10
|
httpMethod: "GET" | "POST";
|
|
10
11
|
methodName: symbol;
|
|
11
12
|
descriptor: PropertyDescriptor;
|
|
13
|
+
argumentsMetadata: TArgumentsMetadataCollection;
|
|
12
14
|
};
|
|
13
15
|
|
|
14
16
|
export type TWebSocketUpgradeData = {
|
|
@@ -59,13 +61,15 @@ export const WebSocket =
|
|
|
59
61
|
path: "/",
|
|
60
62
|
httpMethod: "GET",
|
|
61
63
|
methodName: upgradeHandlerSymbol,
|
|
62
|
-
descriptor: descriptor
|
|
64
|
+
descriptor: descriptor,
|
|
65
|
+
argumentsMetadata: {}
|
|
63
66
|
},
|
|
64
67
|
{
|
|
65
68
|
path: "/",
|
|
66
69
|
httpMethod: "POST",
|
|
67
70
|
methodName: upgradeHandlerSymbol,
|
|
68
|
-
descriptor: descriptor
|
|
71
|
+
descriptor: descriptor,
|
|
72
|
+
argumentsMetadata: {}
|
|
69
73
|
}
|
|
70
74
|
];
|
|
71
75
|
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
import type { TArgumentsMetadataCollection } from "../decorators/arguments";
|
|
3
4
|
import type { THttpMethods } from "../http";
|
|
4
5
|
|
|
5
6
|
export type THttpRouteModel<T = unknown> = Readonly<{
|
|
6
7
|
class: new (...args: Array<any>) => T;
|
|
7
8
|
funcName: string | symbol;
|
|
8
9
|
func: (...args: Array<any>) => unknown;
|
|
10
|
+
argumentsMetadata: TArgumentsMetadataCollection;
|
|
9
11
|
}>;
|
|
10
12
|
|
|
11
13
|
export class HttpRoute {
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
+
export type TContextOptions = Partial<{
|
|
2
|
+
isStatic: boolean;
|
|
3
|
+
isPassthrough: boolean;
|
|
4
|
+
}>;
|
|
5
|
+
|
|
1
6
|
export interface IContext {
|
|
2
|
-
get: (key: symbol) =>
|
|
3
|
-
|
|
7
|
+
get: <T = unknown>(key: symbol, options?: TContextOptions) => T;
|
|
8
|
+
has: (key: symbol, options?: TContextOptions) => boolean;
|
|
9
|
+
set: <T = unknown>(key: symbol, value: T, options?: TContextOptions) => ThisType<IContext>;
|
|
10
|
+
setOptions: (options: TContextOptions) => ThisType<IContext>;
|
|
4
11
|
}
|
package/src/interfaces/index.ts
CHANGED