@bool-ts/core 1.6.1 → 1.6.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/controller.ts +6 -1
- package/__test/module.ts +11 -3
- package/dist/decorators/arguments.d.ts +10 -21
- package/dist/decorators/arguments.js +73 -99
- package/dist/decorators/controller.d.ts +0 -1
- package/dist/decorators/controller.js +2 -2
- package/dist/decorators/dispatcher.d.ts +0 -1
- package/dist/decorators/dispatcher.js +1 -1
- package/dist/decorators/guard.d.ts +0 -1
- package/dist/decorators/guard.js +1 -1
- package/dist/decorators/http.d.ts +0 -1
- package/dist/decorators/http.js +1 -1
- package/dist/decorators/index.d.ts +10 -10
- package/dist/decorators/index.js +10 -10
- package/dist/decorators/inject.d.ts +2 -3
- package/dist/decorators/inject.js +3 -3
- package/dist/decorators/injectable.d.ts +0 -1
- package/dist/decorators/injectable.js +1 -1
- package/dist/decorators/middleware.d.ts +0 -1
- package/dist/decorators/middleware.js +1 -1
- package/dist/decorators/module.d.ts +12 -8
- package/dist/decorators/module.js +1 -6
- package/dist/decorators/zodSchema.d.ts +0 -1
- package/dist/decorators/zodSchema.js +1 -1
- package/dist/hooks/factory.d.ts +1 -1
- package/dist/hooks/factory.js +126 -129
- package/dist/hooks/injector.d.ts +5 -3
- package/dist/hooks/injector.js +20 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/context.d.ts +4 -0
- package/dist/interfaces/context.js +1 -0
- package/dist/interfaces/index.d.ts +3 -2
- package/dist/keys/index.d.ts +19 -0
- package/dist/keys/index.js +19 -0
- package/package.json +1 -1
- package/src/decorators/arguments.ts +74 -84
- package/src/decorators/controller.ts +2 -3
- package/src/decorators/dispatcher.ts +1 -2
- package/src/decorators/guard.ts +1 -2
- package/src/decorators/http.ts +2 -2
- package/src/decorators/index.ts +10 -21
- package/src/decorators/inject.ts +3 -3
- package/src/decorators/injectable.ts +1 -1
- package/src/decorators/middleware.ts +1 -2
- package/src/decorators/module.ts +14 -14
- package/src/decorators/zodSchema.ts +1 -2
- package/src/hooks/factory.ts +153 -149
- package/src/hooks/injector.ts +26 -11
- package/src/index.ts +1 -0
- package/src/interfaces/context.ts +4 -0
- package/src/interfaces/index.ts +3 -2
- package/src/keys/index.ts +20 -0
package/src/hooks/factory.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { IGuard, IMiddleware } from "../interfaces";
|
|
1
|
+
import type { TArgumentsMetadata, TControllerMetadata, THttpMetadata, TModuleMetadata } from "../decorators";
|
|
2
|
+
import type { IContext, IGuard, IMiddleware } from "../interfaces";
|
|
3
3
|
import type { IDispatcher } from "../interfaces/dispatcher";
|
|
4
4
|
|
|
5
5
|
import "colors";
|
|
@@ -9,8 +9,22 @@ import Qs from "qs";
|
|
|
9
9
|
import * as Zod from "zod";
|
|
10
10
|
|
|
11
11
|
import { Router, RouterGroup } from "../entities";
|
|
12
|
-
import { controllerHttpKey, controllerKey, argumentsKey, moduleKey, EArgumentTypes } from "../decorators";
|
|
13
12
|
import { HttpClientError, HttpServerError, jsonErrorInfer, type THttpMethods } from "../http";
|
|
13
|
+
import {
|
|
14
|
+
argumentsKey,
|
|
15
|
+
bodyArgsKey,
|
|
16
|
+
configKey,
|
|
17
|
+
contextArgsKey,
|
|
18
|
+
controllerHttpKey,
|
|
19
|
+
controllerKey,
|
|
20
|
+
moduleKey,
|
|
21
|
+
paramArgsKey,
|
|
22
|
+
paramsArgsKey,
|
|
23
|
+
queryArgsKey,
|
|
24
|
+
requestArgsKey,
|
|
25
|
+
requestHeadersArgsKey,
|
|
26
|
+
responseHeadersArgsKey
|
|
27
|
+
} from "../keys";
|
|
14
28
|
import { Injector } from "./injector";
|
|
15
29
|
|
|
16
30
|
export type TBoolFactoryOptions = Required<{
|
|
@@ -97,7 +111,7 @@ export const argumentsResolution = async (
|
|
|
97
111
|
});
|
|
98
112
|
}
|
|
99
113
|
|
|
100
|
-
return
|
|
114
|
+
return data;
|
|
101
115
|
} catch (error) {
|
|
102
116
|
if (error instanceof HttpClientError) {
|
|
103
117
|
throw error;
|
|
@@ -135,6 +149,7 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
135
149
|
}
|
|
136
150
|
|
|
137
151
|
const {
|
|
152
|
+
loaders,
|
|
138
153
|
middlewares,
|
|
139
154
|
guards,
|
|
140
155
|
beforeDispatchers,
|
|
@@ -144,6 +159,47 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
144
159
|
config: moduleConfig
|
|
145
160
|
} = moduleMetadata;
|
|
146
161
|
|
|
162
|
+
// Configuration(s)
|
|
163
|
+
const { allowLogsMethods, config } = Object.freeze({
|
|
164
|
+
allowLogsMethods: options?.log?.methods,
|
|
165
|
+
config: {
|
|
166
|
+
...(typeof options.config !== "function" ? options.config : await options.config()),
|
|
167
|
+
...(typeof moduleConfig !== "function"
|
|
168
|
+
? typeof moduleConfig !== "object"
|
|
169
|
+
? undefined
|
|
170
|
+
: moduleConfig
|
|
171
|
+
: await moduleConfig())
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Register config like an injection
|
|
176
|
+
Injector.set(configKey, config);
|
|
177
|
+
|
|
178
|
+
if (loaders) {
|
|
179
|
+
const loaderFunctions = [];
|
|
180
|
+
|
|
181
|
+
for (const [key, func] of Object.entries(loaders)) {
|
|
182
|
+
loaderFunctions.push(async () => {
|
|
183
|
+
try {
|
|
184
|
+
const result = await func({ config });
|
|
185
|
+
console.info(`INFO! Loader [${key}] initialized successfully.`);
|
|
186
|
+
return result;
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.error(`WARNING! Loader [${key}] initialization failed.`);
|
|
189
|
+
options.debug && console.error(error);
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const results = await Promise.all(loaderFunctions.map((func) => func()));
|
|
196
|
+
|
|
197
|
+
for (let i = 0; i < results.length; i++) {
|
|
198
|
+
const [key, value] = results[i];
|
|
199
|
+
Injector.set(key, value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
147
203
|
// Middleware(s)
|
|
148
204
|
const middlewareGroup = !middlewares
|
|
149
205
|
? []
|
|
@@ -204,22 +260,31 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
204
260
|
});
|
|
205
261
|
});
|
|
206
262
|
|
|
207
|
-
const { allowLogsMethods, config } = Object.freeze({
|
|
208
|
-
allowLogsMethods: options?.log?.methods,
|
|
209
|
-
config: {
|
|
210
|
-
...(typeof options.config !== "function" ? options.config : await options.config()),
|
|
211
|
-
...(typeof moduleConfig !== "function" ? moduleConfig : await moduleConfig())
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
|
|
215
263
|
Bun.serve({
|
|
216
264
|
port: options.port,
|
|
217
265
|
fetch: async (request) => {
|
|
266
|
+
const { headers } = request;
|
|
218
267
|
const start = performance.now();
|
|
219
268
|
const url = new URL(request.url);
|
|
220
|
-
|
|
221
|
-
const
|
|
222
|
-
|
|
269
|
+
|
|
270
|
+
const context: Record<symbol, any> = {
|
|
271
|
+
[requestHeadersArgsKey]: headers,
|
|
272
|
+
[responseHeadersArgsKey]: new Headers(),
|
|
273
|
+
[queryArgsKey]: Qs.parse(url.searchParams.toString(), options.queryParser)
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const contextHook = {
|
|
277
|
+
get(key) {
|
|
278
|
+
return context[key];
|
|
279
|
+
},
|
|
280
|
+
set(key, value) {
|
|
281
|
+
if (key in context) {
|
|
282
|
+
throw Error(`${String(key)} already exists in context.`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
context[key] = value;
|
|
286
|
+
}
|
|
287
|
+
} satisfies IContext;
|
|
223
288
|
|
|
224
289
|
try {
|
|
225
290
|
// Execute middleware(s)
|
|
@@ -233,17 +298,17 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
233
298
|
if (middlewareMetadata) {
|
|
234
299
|
for (const [_key, argsMetadata] of Object.entries(middlewareMetadata)) {
|
|
235
300
|
switch (argsMetadata.type) {
|
|
236
|
-
case
|
|
301
|
+
case requestArgsKey:
|
|
237
302
|
middlewareArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
238
|
-
?
|
|
303
|
+
? request
|
|
239
304
|
: await argumentsResolution(
|
|
240
|
-
|
|
305
|
+
request,
|
|
241
306
|
argsMetadata.zodSchema,
|
|
242
307
|
argsMetadata.index,
|
|
243
308
|
middlewareCollection.funcName
|
|
244
309
|
);
|
|
245
310
|
break;
|
|
246
|
-
case
|
|
311
|
+
case bodyArgsKey:
|
|
247
312
|
middlewareArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
248
313
|
? await request[argsMetadata.parser || "json"]()
|
|
249
314
|
: await argumentsResolution(
|
|
@@ -253,25 +318,21 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
253
318
|
middlewareCollection.funcName
|
|
254
319
|
);
|
|
255
320
|
break;
|
|
256
|
-
case
|
|
321
|
+
case contextArgsKey:
|
|
322
|
+
middlewareArguments[argsMetadata.index] = contextHook;
|
|
323
|
+
break;
|
|
324
|
+
default:
|
|
257
325
|
middlewareArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
258
|
-
?
|
|
326
|
+
? !(argsMetadata.type in context)
|
|
327
|
+
? undefined
|
|
328
|
+
: context[argsMetadata.type]
|
|
259
329
|
: await argumentsResolution(
|
|
260
|
-
|
|
330
|
+
!(argsMetadata.type in context) ? undefined : context[argsMetadata.type],
|
|
261
331
|
argsMetadata.zodSchema,
|
|
262
332
|
argsMetadata.index,
|
|
263
333
|
middlewareCollection.funcName
|
|
264
334
|
);
|
|
265
335
|
break;
|
|
266
|
-
case EArgumentTypes.request:
|
|
267
|
-
middlewareArguments[argsMetadata.index] = request;
|
|
268
|
-
break;
|
|
269
|
-
case EArgumentTypes.responseHeaders:
|
|
270
|
-
middlewareArguments[argsMetadata.index] = resHeaders;
|
|
271
|
-
break;
|
|
272
|
-
case EArgumentTypes.config:
|
|
273
|
-
middlewareArguments[argsMetadata.index] = config;
|
|
274
|
-
break;
|
|
275
336
|
}
|
|
276
337
|
}
|
|
277
338
|
}
|
|
@@ -295,17 +356,17 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
295
356
|
if (guardMetadata) {
|
|
296
357
|
for (const [_key, argsMetadata] of Object.entries(guardMetadata)) {
|
|
297
358
|
switch (argsMetadata.type) {
|
|
298
|
-
case
|
|
359
|
+
case requestArgsKey:
|
|
299
360
|
guardArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
300
|
-
?
|
|
361
|
+
? request
|
|
301
362
|
: await argumentsResolution(
|
|
302
|
-
|
|
363
|
+
request,
|
|
303
364
|
argsMetadata.zodSchema,
|
|
304
365
|
argsMetadata.index,
|
|
305
366
|
guardCollection.funcName
|
|
306
367
|
);
|
|
307
368
|
break;
|
|
308
|
-
case
|
|
369
|
+
case bodyArgsKey:
|
|
309
370
|
guardArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
310
371
|
? await request[argsMetadata.parser || "json"]()
|
|
311
372
|
: await argumentsResolution(
|
|
@@ -315,25 +376,19 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
315
376
|
guardCollection.funcName
|
|
316
377
|
);
|
|
317
378
|
break;
|
|
318
|
-
case
|
|
379
|
+
case contextArgsKey:
|
|
380
|
+
guardArguments[argsMetadata.index] = contextHook;
|
|
381
|
+
break;
|
|
382
|
+
default:
|
|
319
383
|
guardArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
320
|
-
?
|
|
384
|
+
? context[argsMetadata.type]
|
|
321
385
|
: await argumentsResolution(
|
|
322
|
-
|
|
386
|
+
context[argsMetadata.type],
|
|
323
387
|
argsMetadata.zodSchema,
|
|
324
388
|
argsMetadata.index,
|
|
325
389
|
guardCollection.funcName
|
|
326
390
|
);
|
|
327
391
|
break;
|
|
328
|
-
case EArgumentTypes.request:
|
|
329
|
-
guardArguments[argsMetadata.index] = request;
|
|
330
|
-
break;
|
|
331
|
-
case EArgumentTypes.responseHeaders:
|
|
332
|
-
guardArguments[argsMetadata.index] = resHeaders;
|
|
333
|
-
break;
|
|
334
|
-
case EArgumentTypes.config:
|
|
335
|
-
guardArguments[argsMetadata.index] = config;
|
|
336
|
-
break;
|
|
337
392
|
}
|
|
338
393
|
}
|
|
339
394
|
}
|
|
@@ -359,7 +414,7 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
359
414
|
});
|
|
360
415
|
}
|
|
361
416
|
|
|
362
|
-
|
|
417
|
+
context[paramsArgsKey] = result.params;
|
|
363
418
|
|
|
364
419
|
let responseBody = undefined;
|
|
365
420
|
|
|
@@ -377,17 +432,17 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
377
432
|
if (beforeDispatcherMetadata) {
|
|
378
433
|
for (const [_key, argsMetadata] of Object.entries(beforeDispatcherMetadata)) {
|
|
379
434
|
switch (argsMetadata.type) {
|
|
380
|
-
case
|
|
435
|
+
case requestArgsKey:
|
|
381
436
|
beforeDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
382
|
-
?
|
|
437
|
+
? request
|
|
383
438
|
: await argumentsResolution(
|
|
384
|
-
|
|
439
|
+
request,
|
|
385
440
|
argsMetadata.zodSchema,
|
|
386
441
|
argsMetadata.index,
|
|
387
442
|
beforeDispatcherCollection.funcName
|
|
388
443
|
);
|
|
389
444
|
break;
|
|
390
|
-
case
|
|
445
|
+
case bodyArgsKey:
|
|
391
446
|
beforeDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
392
447
|
? await request[argsMetadata.parser || "json"]()
|
|
393
448
|
: await argumentsResolution(
|
|
@@ -397,47 +452,29 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
397
452
|
beforeDispatcherCollection.funcName
|
|
398
453
|
);
|
|
399
454
|
break;
|
|
400
|
-
case
|
|
455
|
+
case paramArgsKey:
|
|
401
456
|
beforeDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
402
|
-
?
|
|
457
|
+
? context[paramsArgsKey]?.[argsMetadata.key]
|
|
403
458
|
: await argumentsResolution(
|
|
404
|
-
|
|
459
|
+
context[paramsArgsKey]?.[argsMetadata.key],
|
|
405
460
|
argsMetadata.zodSchema,
|
|
406
461
|
argsMetadata.index,
|
|
407
462
|
beforeDispatcherCollection.funcName
|
|
408
463
|
);
|
|
409
464
|
break;
|
|
410
|
-
case
|
|
411
|
-
beforeDispatcherArguments[argsMetadata.index] =
|
|
412
|
-
? query
|
|
413
|
-
: await argumentsResolution(
|
|
414
|
-
query,
|
|
415
|
-
argsMetadata.zodSchema,
|
|
416
|
-
argsMetadata.index,
|
|
417
|
-
beforeDispatcherCollection.funcName
|
|
418
|
-
);
|
|
465
|
+
case contextArgsKey:
|
|
466
|
+
beforeDispatcherArguments[argsMetadata.index] = contextHook;
|
|
419
467
|
break;
|
|
420
|
-
|
|
468
|
+
default:
|
|
421
469
|
beforeDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
422
|
-
?
|
|
423
|
-
? undefined
|
|
424
|
-
: params[argsMetadata.key]
|
|
470
|
+
? context[argsMetadata.type]
|
|
425
471
|
: await argumentsResolution(
|
|
426
|
-
|
|
472
|
+
context[argsMetadata.type],
|
|
427
473
|
argsMetadata.zodSchema,
|
|
428
474
|
argsMetadata.index,
|
|
429
475
|
beforeDispatcherCollection.funcName
|
|
430
476
|
);
|
|
431
477
|
break;
|
|
432
|
-
case EArgumentTypes.request:
|
|
433
|
-
beforeDispatcherArguments[argsMetadata.index] = request;
|
|
434
|
-
break;
|
|
435
|
-
case EArgumentTypes.responseHeaders:
|
|
436
|
-
beforeDispatcherArguments[argsMetadata.index] = resHeaders;
|
|
437
|
-
break;
|
|
438
|
-
case EArgumentTypes.config:
|
|
439
|
-
beforeDispatcherArguments[argsMetadata.index] = config;
|
|
440
|
-
break;
|
|
441
478
|
}
|
|
442
479
|
}
|
|
443
480
|
}
|
|
@@ -448,79 +485,62 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
448
485
|
// Execute controller action
|
|
449
486
|
for (let i = 0; i < result.handlers.length; i++) {
|
|
450
487
|
const controllerActionArguments = [];
|
|
451
|
-
const
|
|
488
|
+
const controllerCollection = result.handlers[i];
|
|
452
489
|
const handlerMetadata: Record<string, TArgumentsMetadata> =
|
|
453
|
-
Reflect.getOwnMetadata(argumentsKey,
|
|
490
|
+
Reflect.getOwnMetadata(argumentsKey, controllerCollection.class, controllerCollection.funcName) ||
|
|
491
|
+
{};
|
|
454
492
|
|
|
455
493
|
if (handlerMetadata) {
|
|
456
494
|
for (const [_key, argsMetadata] of Object.entries(handlerMetadata)) {
|
|
457
495
|
switch (argsMetadata.type) {
|
|
458
|
-
case
|
|
496
|
+
case requestArgsKey:
|
|
459
497
|
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
460
|
-
?
|
|
498
|
+
? request
|
|
461
499
|
: await argumentsResolution(
|
|
462
|
-
|
|
500
|
+
request,
|
|
463
501
|
argsMetadata.zodSchema,
|
|
464
502
|
argsMetadata.index,
|
|
465
|
-
|
|
503
|
+
controllerCollection.funcName
|
|
466
504
|
);
|
|
467
505
|
break;
|
|
468
|
-
case
|
|
506
|
+
case bodyArgsKey:
|
|
469
507
|
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
470
508
|
? await request[argsMetadata.parser || "json"]()
|
|
471
509
|
: await argumentsResolution(
|
|
472
510
|
await request[argsMetadata.parser || "json"](),
|
|
473
511
|
argsMetadata.zodSchema,
|
|
474
512
|
argsMetadata.index,
|
|
475
|
-
|
|
513
|
+
controllerCollection.funcName
|
|
476
514
|
);
|
|
477
515
|
break;
|
|
478
|
-
case
|
|
516
|
+
case paramArgsKey:
|
|
479
517
|
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
480
|
-
?
|
|
518
|
+
? context[paramsArgsKey]?.[argsMetadata.key]
|
|
481
519
|
: await argumentsResolution(
|
|
482
|
-
|
|
520
|
+
context[paramsArgsKey]?.[argsMetadata.key],
|
|
483
521
|
argsMetadata.zodSchema,
|
|
484
522
|
argsMetadata.index,
|
|
485
|
-
|
|
523
|
+
controllerCollection.funcName
|
|
486
524
|
);
|
|
487
525
|
break;
|
|
488
|
-
case
|
|
489
|
-
controllerActionArguments[argsMetadata.index] =
|
|
490
|
-
? query
|
|
491
|
-
: await argumentsResolution(
|
|
492
|
-
query,
|
|
493
|
-
argsMetadata.zodSchema,
|
|
494
|
-
argsMetadata.index,
|
|
495
|
-
handler.funcName
|
|
496
|
-
);
|
|
526
|
+
case contextArgsKey:
|
|
527
|
+
controllerActionArguments[argsMetadata.index] = contextHook;
|
|
497
528
|
break;
|
|
498
|
-
|
|
529
|
+
default:
|
|
499
530
|
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
500
|
-
?
|
|
501
|
-
? undefined
|
|
502
|
-
: params[argsMetadata.key]
|
|
531
|
+
? context[argsMetadata.type]
|
|
503
532
|
: await argumentsResolution(
|
|
504
|
-
|
|
533
|
+
context[argsMetadata.type],
|
|
505
534
|
argsMetadata.zodSchema,
|
|
506
535
|
argsMetadata.index,
|
|
507
|
-
|
|
536
|
+
controllerCollection.funcName
|
|
508
537
|
);
|
|
509
538
|
break;
|
|
510
|
-
case EArgumentTypes.request:
|
|
511
|
-
controllerActionArguments[argsMetadata.index] = request;
|
|
512
|
-
break;
|
|
513
|
-
case EArgumentTypes.responseHeaders:
|
|
514
|
-
controllerActionArguments[argsMetadata.index] = resHeaders;
|
|
515
|
-
break;
|
|
516
|
-
case EArgumentTypes.config:
|
|
517
|
-
controllerActionArguments[argsMetadata.index] = config;
|
|
518
|
-
break;
|
|
519
539
|
}
|
|
520
540
|
}
|
|
521
541
|
}
|
|
522
542
|
|
|
523
|
-
responseBody = await
|
|
543
|
+
responseBody = await controllerCollection.func(...controllerActionArguments);
|
|
524
544
|
}
|
|
525
545
|
|
|
526
546
|
// Execute after dispatcher(s)
|
|
@@ -537,17 +557,17 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
537
557
|
if (afterDispatcherMetadata) {
|
|
538
558
|
for (const [_key, argsMetadata] of Object.entries(afterDispatcherMetadata)) {
|
|
539
559
|
switch (argsMetadata.type) {
|
|
540
|
-
case
|
|
560
|
+
case requestArgsKey:
|
|
541
561
|
afterDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
542
|
-
?
|
|
562
|
+
? request
|
|
543
563
|
: await argumentsResolution(
|
|
544
|
-
|
|
564
|
+
request,
|
|
545
565
|
argsMetadata.zodSchema,
|
|
546
566
|
argsMetadata.index,
|
|
547
567
|
afterDispatcherCollection.funcName
|
|
548
568
|
);
|
|
549
569
|
break;
|
|
550
|
-
case
|
|
570
|
+
case bodyArgsKey:
|
|
551
571
|
afterDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
552
572
|
? await request[argsMetadata.parser || "json"]()
|
|
553
573
|
: await argumentsResolution(
|
|
@@ -557,47 +577,31 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
557
577
|
afterDispatcherCollection.funcName
|
|
558
578
|
);
|
|
559
579
|
break;
|
|
560
|
-
case
|
|
580
|
+
case paramArgsKey:
|
|
561
581
|
afterDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
562
|
-
?
|
|
582
|
+
? context[paramsArgsKey]?.[argsMetadata.key]
|
|
563
583
|
: await argumentsResolution(
|
|
564
|
-
|
|
584
|
+
context[paramsArgsKey]?.[argsMetadata.key],
|
|
565
585
|
argsMetadata.zodSchema,
|
|
566
586
|
argsMetadata.index,
|
|
567
587
|
afterDispatcherCollection.funcName
|
|
568
588
|
);
|
|
569
589
|
break;
|
|
570
|
-
case
|
|
571
|
-
afterDispatcherArguments[argsMetadata.index] =
|
|
572
|
-
? query
|
|
573
|
-
: await argumentsResolution(
|
|
574
|
-
query,
|
|
575
|
-
argsMetadata.zodSchema,
|
|
576
|
-
argsMetadata.index,
|
|
577
|
-
afterDispatcherCollection.funcName
|
|
578
|
-
);
|
|
590
|
+
case contextArgsKey:
|
|
591
|
+
afterDispatcherArguments[argsMetadata.index] = contextHook;
|
|
579
592
|
break;
|
|
580
|
-
|
|
593
|
+
default:
|
|
581
594
|
afterDispatcherArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
582
|
-
? !(argsMetadata.
|
|
595
|
+
? !(argsMetadata.type in context)
|
|
583
596
|
? undefined
|
|
584
|
-
:
|
|
597
|
+
: context[argsMetadata.type]
|
|
585
598
|
: await argumentsResolution(
|
|
586
|
-
|
|
599
|
+
!(argsMetadata.type in context) ? undefined : context[argsMetadata.type],
|
|
587
600
|
argsMetadata.zodSchema,
|
|
588
601
|
argsMetadata.index,
|
|
589
602
|
afterDispatcherCollection.funcName
|
|
590
603
|
);
|
|
591
604
|
break;
|
|
592
|
-
case EArgumentTypes.request:
|
|
593
|
-
afterDispatcherArguments[argsMetadata.index] = request;
|
|
594
|
-
break;
|
|
595
|
-
case EArgumentTypes.responseHeaders:
|
|
596
|
-
afterDispatcherArguments[argsMetadata.index] = resHeaders;
|
|
597
|
-
break;
|
|
598
|
-
case EArgumentTypes.config:
|
|
599
|
-
afterDispatcherArguments[argsMetadata.index] = config;
|
|
600
|
-
break;
|
|
601
605
|
}
|
|
602
606
|
}
|
|
603
607
|
}
|
|
@@ -606,7 +610,7 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
606
610
|
}
|
|
607
611
|
|
|
608
612
|
// Set default header(s)
|
|
609
|
-
|
|
613
|
+
context[responseHeadersArgsKey].set("X-Powered-By", "Bool Typescript");
|
|
610
614
|
|
|
611
615
|
return responseBody instanceof Response
|
|
612
616
|
? responseBody
|
|
@@ -621,14 +625,14 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
621
625
|
{
|
|
622
626
|
status: !responseBody ? 204 : 200,
|
|
623
627
|
statusText: "SUCCESS",
|
|
624
|
-
headers:
|
|
628
|
+
headers: context[responseHeadersArgsKey]
|
|
625
629
|
}
|
|
626
630
|
);
|
|
627
631
|
} catch (error) {
|
|
628
632
|
// Set default header(s)
|
|
629
|
-
|
|
633
|
+
context[responseHeadersArgsKey].set("X-Powered-By", "Bool Typescript");
|
|
630
634
|
|
|
631
|
-
return jsonErrorInfer(error,
|
|
635
|
+
return jsonErrorInfer(error, context[responseHeadersArgsKey]);
|
|
632
636
|
} finally {
|
|
633
637
|
if (allowLogsMethods) {
|
|
634
638
|
const end = performance.now();
|
|
@@ -648,7 +652,7 @@ export const BoolFactory = async (target: new (...args: any[]) => unknown, optio
|
|
|
648
652
|
}
|
|
649
653
|
});
|
|
650
654
|
} catch (error) {
|
|
651
|
-
console.error(error);
|
|
655
|
+
options.debug && console.error(error);
|
|
652
656
|
throw error;
|
|
653
657
|
}
|
|
654
658
|
};
|
package/src/hooks/injector.ts
CHANGED
|
@@ -1,43 +1,58 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
+
import { injectableKey, controllerKey, middlewareKey, guardKey, dispatcherKey, injectKey } from "../keys";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
type TDefinition<T = any> = { new (...args: any[]): T } | string | symbol;
|
|
4
5
|
|
|
5
6
|
interface IInjector {
|
|
6
|
-
|
|
7
|
+
set(key: TDefinition, value: any): void;
|
|
8
|
+
get<T>(definition: TDefinition): T;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export const Injector: IInjector = new (class {
|
|
10
|
-
private readonly _mapper: Map<Function, any> = new Map();
|
|
12
|
+
private readonly _mapper: Map<Function | string | symbol, any> = new Map();
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
*
|
|
14
16
|
* @param constructor
|
|
15
17
|
*/
|
|
16
|
-
get<T>(
|
|
17
|
-
if (this._mapper.has(
|
|
18
|
-
return this._mapper.get(
|
|
18
|
+
get<T>(definition: TDefinition) {
|
|
19
|
+
if (this._mapper.has(definition)) {
|
|
20
|
+
return this._mapper.get(definition) as T;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
if (typeof definition !== "function") {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const ownMetadataKeys = Reflect.getMetadataKeys(definition);
|
|
22
28
|
|
|
23
29
|
if (
|
|
24
30
|
![injectableKey, controllerKey, middlewareKey, guardKey, dispatcherKey].some((value) =>
|
|
25
31
|
ownMetadataKeys.includes(value)
|
|
26
32
|
)
|
|
27
33
|
) {
|
|
28
|
-
console.error(
|
|
34
|
+
console.error(definition);
|
|
29
35
|
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
// Initialize dependencies injection
|
|
33
|
-
const dependencies: any[] = Reflect.getOwnMetadata(injectKey,
|
|
39
|
+
const dependencies: any[] = Reflect.getOwnMetadata(injectKey, definition) || [];
|
|
34
40
|
const injections: any[] = dependencies.map((dependency) => Injector.get(dependency));
|
|
35
|
-
const instance = new
|
|
41
|
+
const instance = new definition(...injections);
|
|
36
42
|
|
|
37
|
-
this._mapper.set(
|
|
43
|
+
this._mapper.set(definition, instance);
|
|
38
44
|
|
|
39
45
|
return instance;
|
|
40
46
|
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param key
|
|
51
|
+
* @param value
|
|
52
|
+
*/
|
|
53
|
+
set(key: TDefinition, value: any) {
|
|
54
|
+
this._mapper.set(key, value);
|
|
55
|
+
}
|
|
41
56
|
})();
|
|
42
57
|
|
|
43
58
|
export default Injector;
|
package/src/index.ts
CHANGED
package/src/interfaces/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
export type { IMiddleware } from "./middleware";
|
|
1
|
+
export type { IContext } from "./context";
|
|
3
2
|
export type { IController } from "./controller";
|
|
4
3
|
export type { IDispatcher } from "./dispatcher";
|
|
4
|
+
export type { IGuard } from "./guard";
|
|
5
|
+
export type { IMiddleware } from "./middleware";
|
|
5
6
|
export type { IModule } from "./module";
|