@bool-ts/core 1.5.6 → 1.5.8

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 (51) hide show
  1. package/.prettierrc +11 -11
  2. package/LICENSE +21 -21
  3. package/__test/afterDispatcher.ts +9 -9
  4. package/__test/beforeDispatcher.ts +9 -9
  5. package/__test/controller.ts +68 -68
  6. package/__test/firstGuard.ts +10 -10
  7. package/__test/firstMiddleware.ts +9 -9
  8. package/__test/index.ts +8 -8
  9. package/__test/interfaces.ts +7 -7
  10. package/__test/module.ts +22 -22
  11. package/__test/repository.ts +16 -16
  12. package/__test/secondGuard.ts +10 -10
  13. package/__test/secondMiddleware.ts +9 -9
  14. package/__test/service.ts +20 -20
  15. package/bun.lockb +0 -0
  16. package/dist/entities/route.js +0 -1
  17. package/dist/hooks/factory.js +1 -1
  18. package/dist/http/index.d.ts +1 -1
  19. package/dist/http/index.js +2 -3
  20. package/jsconfig.json +27 -0
  21. package/package.json +31 -31
  22. package/src/decorators/arguments.ts +214 -214
  23. package/src/decorators/controller.ts +22 -22
  24. package/src/decorators/dispatcher.ts +19 -19
  25. package/src/decorators/guard.ts +19 -19
  26. package/src/decorators/http.ts +81 -81
  27. package/src/decorators/index.ts +28 -28
  28. package/src/decorators/inject.ts +13 -13
  29. package/src/decorators/injectable.ts +8 -8
  30. package/src/decorators/middleware.ts +19 -19
  31. package/src/decorators/module.ts +92 -92
  32. package/src/decorators/zodSchema.ts +20 -20
  33. package/src/entities/index.ts +3 -3
  34. package/src/entities/route.ts +327 -328
  35. package/src/entities/router.ts +35 -35
  36. package/src/entities/routerGroup.ts +34 -34
  37. package/src/hooks/factory.ts +616 -616
  38. package/src/hooks/index.ts +2 -2
  39. package/src/hooks/injector.ts +43 -43
  40. package/src/http/clientError.ts +45 -45
  41. package/src/http/index.ts +61 -63
  42. package/src/http/serverError.ts +38 -38
  43. package/src/index.ts +6 -6
  44. package/src/interfaces/controller.ts +1 -1
  45. package/src/interfaces/dispatcher.ts +3 -3
  46. package/src/interfaces/guard.ts +3 -3
  47. package/src/interfaces/index.ts +5 -5
  48. package/src/interfaces/middleware.ts +3 -3
  49. package/src/interfaces/module.ts +1 -1
  50. package/test.http +31 -31
  51. package/tsconfig.json +108 -107
@@ -1,81 +1,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
- };
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
+ };
@@ -1,28 +1,28 @@
1
- export {
2
- RequestHeaders,
3
- Body,
4
- Params,
5
- Param,
6
- Query,
7
- Request,
8
- ResponseHeaders,
9
- EArgumentTypes,
10
- argumentsKey
11
- } from "./arguments";
12
- export { Controller, controllerKey } from "./controller";
13
- export { Guard, guardKey } from "./guard";
14
- export { Inject, injectKey } from "./inject";
15
- export { Injectable, injectableKey } from "./injectable";
16
- export { Middleware, middlewareKey } from "./middleware";
17
- export { Module, moduleKey } from "./module";
18
- export { Get, Post, Put, Patch, Delete, Options, controllerHttpKey } from "./http";
19
- export { ZodSchema, controllerRouteZodSchemaKey } from "./zodSchema";
20
- export { Dispatcher, dispatcherKey } from "./dispatcher";
21
-
22
- export type { TControllerMetadata } from "./controller";
23
- export type { TModuleMetadata, TModuleOptions } from "./module";
24
- export type { THttpMetadata } from "./http";
25
- export type { TArgumentsMetadata } from "./arguments";
26
- export type { TMiddlewareMetadata } from "./middleware";
27
- export type { TGuardMetadata } from "./guard";
28
- export type { TDispatcherMetadata } from "./dispatcher";
1
+ export {
2
+ RequestHeaders,
3
+ Body,
4
+ Params,
5
+ Param,
6
+ Query,
7
+ Request,
8
+ ResponseHeaders,
9
+ EArgumentTypes,
10
+ argumentsKey
11
+ } from "./arguments";
12
+ export { Controller, controllerKey } from "./controller";
13
+ export { Guard, guardKey } from "./guard";
14
+ export { Inject, injectKey } from "./inject";
15
+ export { Injectable, injectableKey } from "./injectable";
16
+ export { Middleware, middlewareKey } from "./middleware";
17
+ export { Module, moduleKey } from "./module";
18
+ export { Get, Post, Put, Patch, Delete, Options, controllerHttpKey } from "./http";
19
+ export { ZodSchema, controllerRouteZodSchemaKey } from "./zodSchema";
20
+ export { Dispatcher, dispatcherKey } from "./dispatcher";
21
+
22
+ export type { TControllerMetadata } from "./controller";
23
+ export type { TModuleMetadata, TModuleOptions } from "./module";
24
+ export type { THttpMetadata } from "./http";
25
+ export type { TArgumentsMetadata } from "./arguments";
26
+ export type { TMiddlewareMetadata } from "./middleware";
27
+ export type { TGuardMetadata } from "./guard";
28
+ export type { TDispatcherMetadata } from "./dispatcher";
@@ -1,13 +1,13 @@
1
- export const injectKey = "design:paramtypes";
2
-
3
- export const Inject = <T extends Object>(classDefinition: { new (...args: any[]): T }) => {
4
- return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
5
- const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
6
-
7
- designParameterTypes[parameterIndex] = classDefinition;
8
-
9
- Reflect.defineMetadata(injectKey, designParameterTypes, target);
10
- };
11
- };
12
-
13
- export default Inject;
1
+ export const injectKey = "design:paramtypes";
2
+
3
+ export const Inject = <T extends Object>(classDefinition: { new (...args: any[]): T }) => {
4
+ return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
5
+ const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
6
+
7
+ designParameterTypes[parameterIndex] = classDefinition;
8
+
9
+ Reflect.defineMetadata(injectKey, designParameterTypes, target);
10
+ };
11
+ };
12
+
13
+ export default Inject;
@@ -1,8 +1,8 @@
1
- export const injectableKey = Symbol.for("__bool:injectable__");
2
-
3
- export const Injectable =
4
- () =>
5
- <T extends Object>(target: T) =>
6
- Reflect.defineMetadata(injectableKey, undefined, target);
7
-
8
- export default Injectable;
1
+ export const injectableKey = Symbol.for("__bool:injectable__");
2
+
3
+ export const Injectable =
4
+ () =>
5
+ <T extends Object>(target: T) =>
6
+ Reflect.defineMetadata(injectableKey, undefined, target);
7
+
8
+ export default Injectable;
@@ -1,19 +1,19 @@
1
- import type { IMiddleware } from "../interfaces";
2
-
3
- export type TMiddlewareMetadata = undefined;
4
-
5
- export const middlewareKey = Symbol.for("__bool:middleware__");
6
-
7
- export const Middleware =
8
- () =>
9
- <T extends { new (...args: any[]): IMiddleware }>(target: T) => {
10
- if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
11
- return;
12
- }
13
-
14
- const metadata: TMiddlewareMetadata = undefined;
15
-
16
- Reflect.defineMetadata(middlewareKey, metadata, target);
17
- };
18
-
19
- export default Middleware;
1
+ import type { IMiddleware } from "../interfaces";
2
+
3
+ export type TMiddlewareMetadata = undefined;
4
+
5
+ export const middlewareKey = Symbol.for("__bool:middleware__");
6
+
7
+ export const Middleware =
8
+ () =>
9
+ <T extends { new (...args: any[]): IMiddleware }>(target: T) => {
10
+ if (!("enforce" in target.prototype) || typeof target.prototype.enforce !== "function") {
11
+ return;
12
+ }
13
+
14
+ const metadata: TMiddlewareMetadata = undefined;
15
+
16
+ Reflect.defineMetadata(middlewareKey, metadata, target);
17
+ };
18
+
19
+ export default Middleware;
@@ -1,92 +1,92 @@
1
- import type { IModule } from "../interfaces/module";
2
- import { controllerKey } from "./controller";
3
- import { dispatcherKey } from "./dispatcher";
4
- import { guardKey } from "./guard";
5
- import { injectableKey } from "./injectable";
6
- import { middlewareKey } from "./middleware";
7
-
8
- type TInstances = Array<new (...args: any[]) => any>;
9
-
10
- export type TModuleOptions =
11
- | Partial<{
12
- prefix: string;
13
- dependencies: TInstances;
14
- controllers: TInstances;
15
- middlewares: TInstances;
16
- guards: TInstances;
17
- beforeDispatchers: TInstances;
18
- afterDispatchers: TInstances;
19
- }>
20
- | undefined;
21
-
22
- export type TModuleMetadata =
23
- | Partial<{
24
- prefix: string;
25
- controllers: TInstances;
26
- dependencies: TInstances;
27
- middlewares: TInstances;
28
- guards: TInstances;
29
- beforeDispatchers: TInstances;
30
- afterDispatchers: TInstances;
31
- }>
32
- | undefined;
33
-
34
- export const moduleKey = Symbol.for("__bool:module__");
35
-
36
- export const Module =
37
- (args?: TModuleOptions) =>
38
- <T extends { new (...args: any[]): IModule }>(target: T) => {
39
- const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, 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 (beforeDispatchers) {
58
- for (let i = 0; i < beforeDispatchers.length; i++) {
59
- if (!Reflect.getOwnMetadataKeys(beforeDispatchers[i]).includes(dispatcherKey)) {
60
- throw Error(`${beforeDispatchers[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 (afterDispatchers) {
74
- for (let i = 0; i < afterDispatchers.length; i++) {
75
- if (!Reflect.getOwnMetadataKeys(afterDispatchers[i]).includes(dispatcherKey)) {
76
- throw Error(`${afterDispatchers[i].name} is not a dispatcher.`);
77
- }
78
- }
79
- }
80
-
81
- if (dependencies) {
82
- for (let i = 0; i < dependencies.length; i++) {
83
- if (!Reflect.getOwnMetadataKeys(dependencies[i]).includes(injectableKey)) {
84
- throw Error(`${dependencies[i].name} is not an injectable.`);
85
- }
86
- }
87
- }
88
-
89
- Reflect.defineMetadata(moduleKey, args, target);
90
- };
91
-
92
- export default Module;
1
+ import type { IModule } from "../interfaces/module";
2
+ import { controllerKey } from "./controller";
3
+ import { dispatcherKey } from "./dispatcher";
4
+ import { guardKey } from "./guard";
5
+ import { injectableKey } from "./injectable";
6
+ import { middlewareKey } from "./middleware";
7
+
8
+ type TInstances = Array<new (...args: any[]) => any>;
9
+
10
+ export type TModuleOptions =
11
+ | Partial<{
12
+ prefix: string;
13
+ dependencies: TInstances;
14
+ controllers: TInstances;
15
+ middlewares: TInstances;
16
+ guards: TInstances;
17
+ beforeDispatchers: TInstances;
18
+ afterDispatchers: TInstances;
19
+ }>
20
+ | undefined;
21
+
22
+ export type TModuleMetadata =
23
+ | Partial<{
24
+ prefix: string;
25
+ controllers: TInstances;
26
+ dependencies: TInstances;
27
+ middlewares: TInstances;
28
+ guards: TInstances;
29
+ beforeDispatchers: TInstances;
30
+ afterDispatchers: TInstances;
31
+ }>
32
+ | undefined;
33
+
34
+ export const moduleKey = Symbol.for("__bool:module__");
35
+
36
+ export const Module =
37
+ (args?: TModuleOptions) =>
38
+ <T extends { new (...args: any[]): IModule }>(target: T) => {
39
+ const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, 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 (beforeDispatchers) {
58
+ for (let i = 0; i < beforeDispatchers.length; i++) {
59
+ if (!Reflect.getOwnMetadataKeys(beforeDispatchers[i]).includes(dispatcherKey)) {
60
+ throw Error(`${beforeDispatchers[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 (afterDispatchers) {
74
+ for (let i = 0; i < afterDispatchers.length; i++) {
75
+ if (!Reflect.getOwnMetadataKeys(afterDispatchers[i]).includes(dispatcherKey)) {
76
+ throw Error(`${afterDispatchers[i].name} is not a dispatcher.`);
77
+ }
78
+ }
79
+ }
80
+
81
+ if (dependencies) {
82
+ for (let i = 0; i < dependencies.length; i++) {
83
+ if (!Reflect.getOwnMetadataKeys(dependencies[i]).includes(injectableKey)) {
84
+ throw Error(`${dependencies[i].name} is not an injectable.`);
85
+ }
86
+ }
87
+ }
88
+
89
+ Reflect.defineMetadata(moduleKey, args, target);
90
+ };
91
+
92
+ export default Module;
@@ -1,20 +1,20 @@
1
- import * as Zod from "zod";
2
-
3
- export const controllerRouteZodSchemaKey = Symbol.for("__bool:controller.route.zodSchema__");
4
-
5
- export const ZodSchema = (schema: Zod.Schema) => {
6
- return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
7
- if (!methodName) {
8
- return;
9
- }
10
-
11
- const zodSchemasMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName) || {};
12
-
13
- zodSchemasMetadata[`paramterIndexes.${parameterIndex}`] = {
14
- index: parameterIndex,
15
- schema: schema
16
- };
17
-
18
- Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
19
- };
20
- };
1
+ import * as Zod from "zod";
2
+
3
+ export const controllerRouteZodSchemaKey = Symbol.for("__bool:controller.route.zodSchema__");
4
+
5
+ export const ZodSchema = (schema: Zod.Schema) => {
6
+ return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
7
+ if (!methodName) {
8
+ return;
9
+ }
10
+
11
+ const zodSchemasMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName) || {};
12
+
13
+ zodSchemasMetadata[`paramterIndexes.${parameterIndex}`] = {
14
+ index: parameterIndex,
15
+ schema: schema
16
+ };
17
+
18
+ Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
19
+ };
20
+ };
@@ -1,3 +1,3 @@
1
- export { Route } from "./route";
2
- export { Router } from "./router";
3
- export { RouterGroup } from "./routerGroup";
1
+ export { Route } from "./route";
2
+ export { Router } from "./router";
3
+ export { RouterGroup } from "./routerGroup";