@bool-ts/core 1.3.2 → 1.4.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 -0
- package/LICENSE +21 -21
- package/__test/controller.ts +66 -79
- package/__test/index.ts +8 -11
- package/__test/interfaces.ts +7 -7
- package/__test/module.ts +16 -17
- package/__test/repository.ts +16 -16
- package/__test/service.ts +20 -20
- package/bun.lockb +0 -0
- package/dist/decorators/arguments.d.ts +43 -0
- package/dist/decorators/arguments.js +97 -0
- package/dist/decorators/controller.d.ts +2 -2
- package/dist/decorators/controller.js +6 -10
- package/dist/decorators/http.d.ts +1 -1
- package/dist/decorators/http.js +16 -103
- package/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.js +7 -26
- package/dist/decorators/inject.js +5 -9
- package/dist/decorators/injectable.d.ts +2 -2
- package/dist/decorators/injectable.js +4 -8
- package/dist/decorators/module.d.ts +4 -2
- package/dist/decorators/module.js +4 -8
- package/dist/decorators/zodSchema.d.ts +1 -1
- package/dist/decorators/zodSchema.js +5 -8
- package/dist/entities/index.d.ts +3 -0
- package/dist/entities/index.js +3 -0
- package/dist/entities/route.d.ts +101 -0
- package/dist/entities/route.js +257 -0
- package/dist/entities/router.d.ts +10 -0
- package/dist/entities/router.js +25 -0
- package/dist/entities/routerGroup.d.ts +14 -0
- package/dist/entities/routerGroup.js +24 -0
- package/dist/hooks/factory.d.ts +12 -12
- package/dist/hooks/factory.js +202 -150
- package/dist/hooks/index.js +2 -7
- package/dist/hooks/injector.js +7 -10
- package/dist/http/clientError.d.ts +1 -1
- package/dist/http/clientError.js +3 -7
- package/dist/http/index.d.ts +12 -2
- package/dist/http/index.js +34 -73
- package/dist/http/serverError.js +3 -7
- package/dist/index.js +5 -21
- package/dist/interfaces/index.js +1 -3
- package/dist/ultils/asyncFunction.js +1 -4
- package/dist/ultils/index.js +1 -17
- package/package.json +30 -33
- package/src/decorators/arguments.ts +186 -0
- package/src/decorators/controller.ts +14 -18
- package/src/decorators/http.ts +81 -195
- package/src/decorators/index.ts +7 -6
- package/src/decorators/inject.ts +13 -19
- package/src/decorators/injectable.ts +11 -12
- package/src/decorators/module.ts +21 -22
- package/src/decorators/zodSchema.ts +20 -27
- package/src/entities/index.ts +3 -0
- package/src/entities/route.ts +328 -0
- package/src/entities/router.ts +35 -0
- package/src/entities/routerGroup.ts +34 -0
- package/src/hooks/factory.ts +319 -205
- package/src/hooks/index.ts +2 -2
- package/src/hooks/injector.ts +43 -43
- package/src/http/clientError.ts +45 -56
- package/src/http/index.ts +63 -72
- package/src/http/serverError.ts +38 -38
- package/src/index.ts +6 -6
- package/src/interfaces/index.ts +3 -3
- package/test.http +30 -28
- package/tsconfig.json +107 -109
package/src/decorators/http.ts
CHANGED
|
@@ -1,195 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
return currentMethod.apply(this, arguments);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
descriptor.value = async function () {
|
|
87
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
88
|
-
|
|
89
|
-
if (zodSchemaMetadata) {
|
|
90
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
91
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
92
|
-
|
|
93
|
-
try {
|
|
94
|
-
const validation = await tmpZodMetadata.schema.safeParseAsync(arguments[tmpZodMetadata.index]);
|
|
95
|
-
|
|
96
|
-
if (!validation.success) {
|
|
97
|
-
throw new HttpClientError({
|
|
98
|
-
httpCode: 400,
|
|
99
|
-
message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
|
|
100
|
-
data: validation.error.issues
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
arguments[tmpZodMetadata.index] = validation.data;
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
if (error instanceof HttpClientError) {
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
throw new HttpServerError({
|
|
112
|
-
httpCode: 500,
|
|
113
|
-
message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
|
|
114
|
-
data: !(error instanceof Error) ? error : [{
|
|
115
|
-
message: error.message,
|
|
116
|
-
code: error.name,
|
|
117
|
-
cause: error.cause
|
|
118
|
-
}]
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return currentMethod.apply(this, arguments);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
*
|
|
131
|
-
* @param path
|
|
132
|
-
* @returns
|
|
133
|
-
*/
|
|
134
|
-
export const Get = (
|
|
135
|
-
path = "/"
|
|
136
|
-
) => defaultDecorator(path, "Get");
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
*
|
|
141
|
-
* @param path
|
|
142
|
-
* @returns
|
|
143
|
-
*/
|
|
144
|
-
export const Post = (
|
|
145
|
-
path = "/"
|
|
146
|
-
) => defaultDecorator(path, "Post");
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
*
|
|
151
|
-
* @param path
|
|
152
|
-
* @returns
|
|
153
|
-
*/
|
|
154
|
-
export const Put = (
|
|
155
|
-
path = "/"
|
|
156
|
-
) => defaultDecorator(path, "Put");
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
*
|
|
161
|
-
* @param path
|
|
162
|
-
* @returns
|
|
163
|
-
*/
|
|
164
|
-
export const Patch = (
|
|
165
|
-
path = "/"
|
|
166
|
-
) => defaultDecorator(path, "Patch");
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
*
|
|
171
|
-
* @param path
|
|
172
|
-
* @returns
|
|
173
|
-
*/
|
|
174
|
-
export const Delete = (
|
|
175
|
-
path = "/"
|
|
176
|
-
) => defaultDecorator(path, "Delete");
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
*
|
|
181
|
-
* @param path
|
|
182
|
-
* @returns
|
|
183
|
-
*/
|
|
184
|
-
export const Options = (
|
|
185
|
-
path = "/"
|
|
186
|
-
) => defaultDecorator(path, "Options");
|
|
187
|
-
|
|
188
|
-
export default {
|
|
189
|
-
Get,
|
|
190
|
-
Post,
|
|
191
|
-
Put,
|
|
192
|
-
Patch,
|
|
193
|
-
Delete
|
|
194
|
-
};
|
|
195
|
-
|
|
1
|
+
export interface IControllerRoute {
|
|
2
|
+
path: string;
|
|
3
|
+
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
|
+
methodName: string;
|
|
5
|
+
descriptor: PropertyDescriptor;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const controllerRoutesKey = Symbol.for("__bool:controller.routes__");
|
|
9
|
+
|
|
10
|
+
const defaultDecorator =
|
|
11
|
+
(path: string, method: "Get" | "Post" | "Put" | "Patch" | "Delete" | "Options") =>
|
|
12
|
+
(target: Object, methodName: string, descriptor: PropertyDescriptor) => {
|
|
13
|
+
if (!(descriptor.value instanceof Function)) {
|
|
14
|
+
throw Error(`${method} decorator only use for class method.`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Define controller metadata
|
|
18
|
+
Reflect.defineMetadata(
|
|
19
|
+
controllerRoutesKey,
|
|
20
|
+
[
|
|
21
|
+
...(Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || []),
|
|
22
|
+
{
|
|
23
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
24
|
+
httpMethod: method.toUpperCase(),
|
|
25
|
+
methodName: methodName,
|
|
26
|
+
descriptor: descriptor
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
target.constructor
|
|
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
|
+
};
|
package/src/decorators/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
1
|
+
export { Headers, Body, Params, Param, Query, EArgumentTypes } from "./arguments";
|
|
2
|
+
export { Controller, controllerKey } from "./controller";
|
|
3
|
+
export { Inject, injectKey } from "./inject";
|
|
4
|
+
export { Injectable, injectableKey } from "./injectable";
|
|
5
|
+
export { Module, moduleKey, type TModuleOptions } from "./module";
|
|
6
|
+
export { Get, Post, Put, Patch, Delete, Options, controllerRoutesKey, type IControllerRoute } from "./http";
|
|
7
|
+
export { ZodSchema, controllerRouteZodSchemaKey } from "./zodSchema";
|
package/src/decorators/inject.ts
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
export const injectKey = "design:paramtypes";
|
|
2
|
-
|
|
3
|
-
export const Inject = <T extends Object>(
|
|
4
|
-
|
|
5
|
-
)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Reflect.defineMetadata(injectKey, designParameterTypes, target);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
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,12 +1,11 @@
|
|
|
1
|
-
export const injectableKey = "__bool:injectable__";
|
|
2
|
-
|
|
3
|
-
export const Injectable =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
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
|
+
return target;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default Injectable;
|
package/src/decorators/module.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
export type TModuleOptions =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export default Module;
|
|
1
|
+
export type TModuleOptions =
|
|
2
|
+
| Partial<{
|
|
3
|
+
prefix: string;
|
|
4
|
+
controllers: Array<new (...args: any[]) => unknown>;
|
|
5
|
+
dependencies: Array<new (...args: any[]) => unknown>;
|
|
6
|
+
allowOrigins: string | Array<string>;
|
|
7
|
+
allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
8
|
+
}>
|
|
9
|
+
| undefined;
|
|
10
|
+
|
|
11
|
+
export const moduleKey = Symbol.for("__bool:module__");
|
|
12
|
+
|
|
13
|
+
export const Module =
|
|
14
|
+
(args?: TModuleOptions) =>
|
|
15
|
+
<T extends { new (...args: any[]): {} }>(target: T) => {
|
|
16
|
+
Reflect.defineMetadata(moduleKey, args, target);
|
|
17
|
+
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default Module;
|
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
import * as Zod from "zod";
|
|
2
|
-
|
|
3
|
-
export const controllerRouteZodSchemaKey = "__bool:controller.route.zodSchema__";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
index: parameterIndex,
|
|
22
|
-
schema: schema
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
Reflect.defineMetadata(controllerRouteZodSchemaKey, zodSchemasMetadata, target.constructor, methodName);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
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
|
+
};
|