@bool-ts/core 1.6.13 → 1.7.0

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.
Files changed (68) hide show
  1. package/.prettierrc +11 -11
  2. package/LICENSE +21 -21
  3. package/__test/controller.ts +93 -79
  4. package/__test/dispatcher.ts +16 -0
  5. package/__test/firstGuard.ts +10 -10
  6. package/__test/firstMiddleware.ts +15 -9
  7. package/__test/index.ts +8 -8
  8. package/__test/interfaces.ts +7 -7
  9. package/__test/module.ts +28 -30
  10. package/__test/repository.ts +16 -16
  11. package/__test/secondGuard.ts +10 -10
  12. package/__test/secondMiddleware.ts +15 -9
  13. package/__test/service.ts +20 -20
  14. package/bun.lockb +0 -0
  15. package/dist/decorators/arguments.d.ts +3 -3
  16. package/dist/decorators/arguments.js +3 -3
  17. package/dist/decorators/dispatcher.js +0 -3
  18. package/dist/decorators/index.d.ts +1 -1
  19. package/dist/decorators/index.js +1 -1
  20. package/dist/decorators/middleware.js +0 -3
  21. package/dist/decorators/module.d.ts +2 -4
  22. package/dist/decorators/module.js +5 -12
  23. package/dist/hooks/factory.d.ts +41 -2
  24. package/dist/hooks/factory.js +501 -406
  25. package/dist/hooks/injector.d.ts +14 -1
  26. package/dist/hooks/injector.js +3 -3
  27. package/dist/http/index.d.ts +1 -1
  28. package/dist/http/index.js +2 -1
  29. package/dist/interfaces/dispatcher.d.ts +3 -2
  30. package/dist/interfaces/middleware.d.ts +3 -2
  31. package/dist/keys/index.d.ts +2 -1
  32. package/dist/keys/index.js +2 -1
  33. package/jsconfig.json +27 -27
  34. package/package.json +3 -3
  35. package/src/decorators/arguments.ts +286 -286
  36. package/src/decorators/controller.ts +21 -21
  37. package/src/decorators/dispatcher.ts +14 -18
  38. package/src/decorators/guard.ts +18 -18
  39. package/src/decorators/http.ts +81 -81
  40. package/src/decorators/index.ts +29 -29
  41. package/src/decorators/inject.ts +13 -13
  42. package/src/decorators/injectable.ts +8 -8
  43. package/src/decorators/middleware.ts +14 -18
  44. package/src/decorators/module.ts +84 -94
  45. package/src/decorators/zodSchema.ts +19 -19
  46. package/src/entities/index.ts +5 -5
  47. package/src/entities/route.ts +327 -327
  48. package/src/entities/router.ts +35 -35
  49. package/src/entities/routerGroup.ts +34 -34
  50. package/src/hooks/factory.ts +990 -806
  51. package/src/hooks/index.ts +2 -2
  52. package/src/hooks/injector.ts +57 -57
  53. package/src/http/clientError.ts +45 -45
  54. package/src/http/index.ts +62 -61
  55. package/src/http/serverError.ts +27 -27
  56. package/src/index.ts +9 -9
  57. package/src/interfaces/context.ts +4 -4
  58. package/src/interfaces/controller.ts +1 -1
  59. package/src/interfaces/dispatcher.ts +4 -3
  60. package/src/interfaces/guard.ts +3 -3
  61. package/src/interfaces/index.ts +6 -6
  62. package/src/interfaces/middleware.ts +4 -3
  63. package/src/interfaces/module.ts +1 -1
  64. package/src/keys/index.ts +23 -22
  65. package/test.http +31 -31
  66. package/tsconfig.json +108 -108
  67. package/__test/afterDispatcher.ts +0 -9
  68. package/__test/beforeDispatcher.ts +0 -9
@@ -1,18 +1,18 @@
1
- import type { IGuard } from "../interfaces";
2
- import { guardKey } from "../keys";
3
-
4
- export type TGuardMetadata = undefined;
5
-
6
- export const Guard =
7
- () =>
8
- <T extends { new (...args: any[]): IGuard }>(target: T) => {
9
- if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
10
- return;
11
- }
12
-
13
- const metadata = undefined;
14
-
15
- Reflect.defineMetadata(guardKey, metadata, target);
16
- };
17
-
18
- export default Guard;
1
+ import type { IGuard } from "../interfaces";
2
+ import { guardKey } from "../keys";
3
+
4
+ export type TGuardMetadata = undefined;
5
+
6
+ export const Guard =
7
+ () =>
8
+ <T extends { new (...args: any[]): IGuard }>(target: T) => {
9
+ if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
10
+ return;
11
+ }
12
+
13
+ const metadata = undefined;
14
+
15
+ Reflect.defineMetadata(guardKey, metadata, target);
16
+ };
17
+
18
+ export default Guard;
@@ -1,81 +1,81 @@
1
- import { controllerHttpKey } from "../keys";
2
-
3
- export type TRoute = {
4
- path: string;
5
- httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
6
- methodName: string;
7
- descriptor: PropertyDescriptor;
8
- };
9
-
10
- export type THttpMetadata = Array<TRoute>;
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
- };
1
+ import { controllerHttpKey } from "../keys";
2
+
3
+ export type TRoute = {
4
+ path: string;
5
+ httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
6
+ methodName: string;
7
+ descriptor: PropertyDescriptor;
8
+ };
9
+
10
+ export type THttpMetadata = Array<TRoute>;
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
+ };
@@ -1,29 +1,29 @@
1
- export {
2
- Body,
3
- Context,
4
- Param,
5
- Params,
6
- Query,
7
- Request,
8
- RequestHeader,
9
- RequestHeaders,
10
- ResponseHeaders,
11
- RouteModel
12
- } from "./arguments";
13
- export { Controller } from "./controller";
14
- export { Dispatcher } from "./dispatcher";
15
- export { Guard } from "./guard";
16
- export { Delete, Get, Options, Patch, Post, Put } from "./http";
17
- export { Inject } from "./inject";
18
- export { Injectable } from "./injectable";
19
- export { Middleware } from "./middleware";
20
- export { Module } from "./module";
21
- export { ZodSchema } from "./zodSchema";
22
-
23
- export type { TArgumentsMetadata } from "./arguments";
24
- export type { TControllerMetadata } from "./controller";
25
- export type { TDispatcherMetadata } from "./dispatcher";
26
- export type { TGuardMetadata } from "./guard";
27
- export type { THttpMetadata } from "./http";
28
- export type { TMiddlewareMetadata } from "./middleware";
29
- export type { TModuleMetadata, TModuleOptions } from "./module";
1
+ export {
2
+ Context,
3
+ Param,
4
+ Params,
5
+ Query,
6
+ Request,
7
+ RequestBody,
8
+ RequestHeader,
9
+ RequestHeaders,
10
+ ResponseHeaders,
11
+ RouteModel
12
+ } from "./arguments";
13
+ export { Controller } from "./controller";
14
+ export { Dispatcher } from "./dispatcher";
15
+ export { Guard } from "./guard";
16
+ export { Delete, Get, Options, Patch, Post, Put } from "./http";
17
+ export { Inject } from "./inject";
18
+ export { Injectable } from "./injectable";
19
+ export { Middleware } from "./middleware";
20
+ export { Module } from "./module";
21
+ export { ZodSchema } from "./zodSchema";
22
+
23
+ export type { TArgumentsMetadata } from "./arguments";
24
+ export type { TControllerMetadata } from "./controller";
25
+ export type { TDispatcherMetadata } from "./dispatcher";
26
+ export type { TGuardMetadata } from "./guard";
27
+ export type { THttpMetadata } from "./http";
28
+ export type { TMiddlewareMetadata } from "./middleware";
29
+ export type { TModuleMetadata, TModuleOptions } from "./module";
@@ -1,13 +1,13 @@
1
- import { injectKey } from "../keys";
2
-
3
- export const Inject = <T extends Object>(definition: { new (...args: any[]): T } | string | symbol) => {
4
- return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
5
- const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
6
-
7
- designParameterTypes[parameterIndex] = definition;
8
-
9
- Reflect.defineMetadata(injectKey, designParameterTypes, target);
10
- };
11
- };
12
-
13
- export default Inject;
1
+ import { injectKey } from "../keys";
2
+
3
+ export const Inject = <T extends Object>(definition: { new (...args: any[]): T } | string | symbol) => {
4
+ return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
5
+ const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
6
+
7
+ designParameterTypes[parameterIndex] = definition;
8
+
9
+ Reflect.defineMetadata(injectKey, designParameterTypes, target);
10
+ };
11
+ };
12
+
13
+ export default Inject;
@@ -1,8 +1,8 @@
1
- import { injectableKey } from "../keys";
2
-
3
- export const Injectable =
4
- () =>
5
- <T extends Object>(target: T) =>
6
- Reflect.defineMetadata(injectableKey, undefined, target);
7
-
8
- export default Injectable;
1
+ import { injectableKey } from "../keys";
2
+
3
+ export const Injectable =
4
+ () =>
5
+ <T extends Object>(target: T) =>
6
+ Reflect.defineMetadata(injectableKey, undefined, target);
7
+
8
+ export default Injectable;
@@ -1,18 +1,14 @@
1
- import type { IMiddleware } from "../interfaces";
2
- import { middlewareKey } from "../keys";
3
-
4
- export type TMiddlewareMetadata = undefined;
5
-
6
- export const Middleware =
7
- () =>
8
- <T extends { new (...args: any[]): IMiddleware }>(target: T) => {
9
- if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
10
- return;
11
- }
12
-
13
- const metadata: TMiddlewareMetadata = undefined;
14
-
15
- Reflect.defineMetadata(middlewareKey, metadata, target);
16
- };
17
-
18
- export default Middleware;
1
+ import type { IMiddleware } from "../interfaces";
2
+ import { middlewareKey } from "../keys";
3
+
4
+ export type TMiddlewareMetadata = undefined;
5
+
6
+ export const Middleware =
7
+ () =>
8
+ <T extends { new (...args: any[]): IMiddleware }>(target: T) => {
9
+ const metadata: TMiddlewareMetadata = undefined;
10
+
11
+ Reflect.defineMetadata(middlewareKey, metadata, target);
12
+ };
13
+
14
+ export default Middleware;
@@ -1,94 +1,84 @@
1
- import type { IModule } from "../interfaces/module";
2
- import { controllerKey, dispatcherKey, guardKey, injectableKey, middlewareKey, moduleKey } from "../keys";
3
-
4
- type TInstances = Array<new (...args: any[]) => any>;
5
- type TLoaders<TConfig extends {} = {}> = Record<
6
- string | symbol,
7
- (args: { config: TConfig }) => [string | symbol, any] | Promise<[string | symbol, any]>
8
- >;
9
-
10
- export type TModuleOptions<TConfig extends {} = {}> =
11
- | Partial<{
12
- config: TConfig | (() => TConfig | Promise<TConfig>);
13
- prefix: string;
14
- dependencies: TInstances;
15
- loaders: TLoaders<TConfig>;
16
- middlewares: TInstances;
17
- guards: TInstances;
18
- beforeDispatchers: TInstances;
19
- controllers: TInstances;
20
- afterDispatchers: TInstances;
21
- }>
22
- | undefined;
23
-
24
- export type TModuleMetadata<TConfig extends {} = {}> =
25
- | Partial<{
26
- config: TConfig | ((...args: any[]) => TConfig | Promise<TConfig>);
27
- prefix: string;
28
- dependencies: TInstances;
29
- loaders: TLoaders<TConfig>;
30
- middlewares: TInstances;
31
- guards: TInstances;
32
- beforeDispatchers: TInstances;
33
- controllers: TInstances;
34
- afterDispatchers: TInstances;
35
- }>
36
- | undefined;
37
-
38
- export const Module =
39
- <TConfig extends {} = {}>(args?: TModuleOptions<TConfig>) =>
40
- <T extends { new (...args: any[]): IModule }>(target: T) => {
41
- const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, dependencies } = args || {};
42
-
43
- if (middlewares) {
44
- for (let i = 0; i < middlewares.length; i++) {
45
- if (!Reflect.getOwnMetadataKeys(middlewares[i]).includes(middlewareKey)) {
46
- throw Error(`${middlewares[i].name} is not a middleware.`);
47
- }
48
- }
49
- }
50
-
51
- if (guards) {
52
- for (let i = 0; i < guards.length; i++) {
53
- if (!Reflect.getOwnMetadataKeys(guards[i]).includes(guardKey)) {
54
- throw Error(`${guards[i].name} is not a guard.`);
55
- }
56
- }
57
- }
58
-
59
- if (beforeDispatchers) {
60
- for (let i = 0; i < beforeDispatchers.length; i++) {
61
- if (!Reflect.getOwnMetadataKeys(beforeDispatchers[i]).includes(dispatcherKey)) {
62
- throw Error(`${beforeDispatchers[i].name} is not a dispatcher.`);
63
- }
64
- }
65
- }
66
-
67
- if (controllers) {
68
- for (let i = 0; i < controllers.length; i++) {
69
- if (!Reflect.getOwnMetadataKeys(controllers[i]).includes(controllerKey)) {
70
- throw Error(`${controllers[i].name} is not a controller.`);
71
- }
72
- }
73
- }
74
-
75
- if (afterDispatchers) {
76
- for (let i = 0; i < afterDispatchers.length; i++) {
77
- if (!Reflect.getOwnMetadataKeys(afterDispatchers[i]).includes(dispatcherKey)) {
78
- throw Error(`${afterDispatchers[i].name} is not a dispatcher.`);
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(moduleKey, args, target);
92
- };
93
-
94
- export default Module;
1
+ import type { IModule } from "../interfaces/module";
2
+ import { controllerKey, dispatcherKey, guardKey, injectableKey, middlewareKey, moduleKey } from "../keys";
3
+
4
+ type TInstances = Array<new (...args: any[]) => any>;
5
+ type TLoaders<TConfig extends {} = {}> = Record<
6
+ string | symbol,
7
+ (args: { config: TConfig }) => [string | symbol, any] | Promise<[string | symbol, any]>
8
+ >;
9
+
10
+ export type TModuleOptions<TConfig extends {} = {}> =
11
+ | Partial<{
12
+ config: TConfig | (() => TConfig | Promise<TConfig>);
13
+ prefix: string;
14
+ dependencies: TInstances;
15
+ loaders: TLoaders<TConfig>;
16
+ middlewares: TInstances;
17
+ guards: TInstances;
18
+ controllers: TInstances;
19
+ dispatchers: TInstances;
20
+ }>
21
+ | undefined;
22
+
23
+ export type TModuleMetadata<TConfig extends {} = {}> =
24
+ | Partial<{
25
+ config: TConfig | ((...args: any[]) => TConfig | Promise<TConfig>);
26
+ prefix: string;
27
+ dependencies: TInstances;
28
+ loaders: TLoaders<TConfig>;
29
+ middlewares: TInstances;
30
+ guards: TInstances;
31
+ controllers: TInstances;
32
+ dispatchers: TInstances;
33
+ }>
34
+ | undefined;
35
+
36
+ export const Module =
37
+ <TConfig extends {} = {}>(args?: TModuleOptions<TConfig>) =>
38
+ <T extends { new (...args: any[]): IModule }>(target: T) => {
39
+ const { middlewares, guards, dispatchers, controllers, dependencies } = args || {};
40
+
41
+ if (middlewares) {
42
+ for (let i = 0; i < middlewares.length; i++) {
43
+ if (!Reflect.getOwnMetadataKeys(middlewares[i]).includes(middlewareKey)) {
44
+ throw Error(`${middlewares[i].name} is not a middleware.`);
45
+ }
46
+ }
47
+ }
48
+
49
+ if (guards) {
50
+ for (let i = 0; i < guards.length; i++) {
51
+ if (!Reflect.getOwnMetadataKeys(guards[i]).includes(guardKey)) {
52
+ throw Error(`${guards[i].name} is not a guard.`);
53
+ }
54
+ }
55
+ }
56
+
57
+ if (dispatchers) {
58
+ for (let i = 0; i < dispatchers.length; i++) {
59
+ if (!Reflect.getOwnMetadataKeys(dispatchers[i]).includes(dispatcherKey)) {
60
+ throw Error(`${dispatchers[i].name} is not a dispatcher.`);
61
+ }
62
+ }
63
+ }
64
+
65
+ if (controllers) {
66
+ for (let i = 0; i < controllers.length; i++) {
67
+ if (!Reflect.getOwnMetadataKeys(controllers[i]).includes(controllerKey)) {
68
+ throw Error(`${controllers[i].name} is not a controller.`);
69
+ }
70
+ }
71
+ }
72
+
73
+ if (dependencies) {
74
+ for (let i = 0; i < dependencies.length; i++) {
75
+ if (!Reflect.getOwnMetadataKeys(dependencies[i]).includes(injectableKey)) {
76
+ throw Error(`${dependencies[i].name} is not an injectable.`);
77
+ }
78
+ }
79
+ }
80
+
81
+ Reflect.defineMetadata(moduleKey, args, target);
82
+ };
83
+
84
+ export default Module;
@@ -1,19 +1,19 @@
1
- import * as Zod from "zod";
2
- import { controllerRouteZodSchemaKey } from "../keys";
3
-
4
- export const ZodSchema = (schema: Zod.Schema) => {
5
- return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
6
- if (!methodName) {
7
- return;
8
- }
9
-
10
- const zodSchemasMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName) || {};
11
-
12
- zodSchemasMetadata[`paramterIndexes.${parameterIndex}`] = {
13
- index: parameterIndex,
14
- schema: schema
15
- };
16
-
17
- Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
18
- };
19
- };
1
+ import * as Zod from "zod";
2
+ import { controllerRouteZodSchemaKey } from "../keys";
3
+
4
+ export const ZodSchema = (schema: Zod.Schema) => {
5
+ return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
6
+ if (!methodName) {
7
+ return;
8
+ }
9
+
10
+ const zodSchemasMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName) || {};
11
+
12
+ zodSchemasMetadata[`paramterIndexes.${parameterIndex}`] = {
13
+ index: parameterIndex,
14
+ schema: schema
15
+ };
16
+
17
+ Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
18
+ };
19
+ };
@@ -1,5 +1,5 @@
1
- export type { TRouteModel } from "./route";
2
-
3
- export { Route } from "./route";
4
- export { Router } from "./router";
5
- export { RouterGroup } from "./routerGroup";
1
+ export type { TRouteModel } from "./route";
2
+
3
+ export { Route } from "./route";
4
+ export { Router } from "./router";
5
+ export { RouterGroup } from "./routerGroup";