@bool-ts/core 1.1.1 → 1.2.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/LICENSE +21 -21
- package/__test/controller.ts +64 -63
- package/__test/index.ts +11 -11
- package/__test/interfaces.ts +7 -7
- package/__test/module.ts +17 -17
- package/__test/repository.ts +16 -16
- package/__test/service.ts +20 -20
- package/bun.lockb +0 -0
- package/dist/decorators/controller.js +10 -6
- package/dist/decorators/http.js +34 -25
- package/dist/decorators/index.js +23 -5
- package/dist/decorators/inject.js +9 -5
- package/dist/decorators/injectable.js +8 -4
- package/dist/decorators/module.d.ts +1 -0
- package/dist/decorators/module.js +8 -4
- package/dist/hooks/factory.d.ts +1 -0
- package/dist/hooks/factory.js +66 -35
- package/dist/hooks/index.js +7 -2
- package/dist/hooks/injector.js +10 -7
- package/dist/http/clientError.js +7 -3
- package/dist/http/index.js +25 -7
- package/dist/http/serverError.js +7 -3
- package/dist/index.js +21 -5
- package/dist/interfaces/index.js +3 -1
- package/package.json +2 -2
- package/src/decorators/controller.ts +18 -18
- package/src/decorators/http.ts +185 -185
- package/src/decorators/index.ts +5 -5
- package/src/decorators/inject.ts +19 -19
- package/src/decorators/injectable.ts +12 -12
- package/src/decorators/module.ts +22 -21
- package/src/hooks/factory.ts +199 -193
- package/src/hooks/index.ts +2 -2
- package/src/hooks/injector.ts +43 -43
- package/src/http/clientError.ts +57 -57
- package/src/http/index.ts +68 -68
- package/src/http/serverError.ts +39 -39
- package/src/index.ts +6 -6
- package/src/interfaces/index.ts +3 -3
- package/test.http +28 -28
- package/tsconfig.json +108 -108
package/src/decorators/http.ts
CHANGED
|
@@ -1,185 +1,185 @@
|
|
|
1
|
-
export interface IControllerRoute {
|
|
2
|
-
path: string;
|
|
3
|
-
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
|
-
methodName: string;
|
|
5
|
-
descriptor: PropertyDescriptor;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export const controllerRoutesKey = "__bool:controller.routes__";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param path
|
|
14
|
-
* @returns
|
|
15
|
-
*/
|
|
16
|
-
export const Get = (
|
|
17
|
-
path = "/"
|
|
18
|
-
) => (
|
|
19
|
-
target: Object,
|
|
20
|
-
methodName: string,
|
|
21
|
-
descriptor: PropertyDescriptor
|
|
22
|
-
) => {
|
|
23
|
-
if (typeof descriptor.value !== "function") {
|
|
24
|
-
throw Error("Get decorator only use for method.");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
28
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
29
|
-
{
|
|
30
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
31
|
-
httpMethod: "GET",
|
|
32
|
-
methodName: methodName,
|
|
33
|
-
descriptor: descriptor
|
|
34
|
-
}
|
|
35
|
-
], target.constructor);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
*
|
|
41
|
-
* @param path
|
|
42
|
-
* @returns
|
|
43
|
-
*/
|
|
44
|
-
export const Post = (
|
|
45
|
-
path = "/"
|
|
46
|
-
) => (
|
|
47
|
-
target: Object,
|
|
48
|
-
methodName: string,
|
|
49
|
-
descriptor: PropertyDescriptor
|
|
50
|
-
) => {
|
|
51
|
-
if (typeof descriptor.value !== "function") {
|
|
52
|
-
throw Error("Post decorator only use for method.");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
56
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
57
|
-
{
|
|
58
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
59
|
-
httpMethod: "POST",
|
|
60
|
-
methodName: methodName,
|
|
61
|
-
descriptor: descriptor
|
|
62
|
-
}
|
|
63
|
-
], target.constructor);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
*
|
|
69
|
-
* @param path
|
|
70
|
-
* @returns
|
|
71
|
-
*/
|
|
72
|
-
export const Put = (
|
|
73
|
-
path = "/"
|
|
74
|
-
) => (
|
|
75
|
-
target: Object,
|
|
76
|
-
methodName: string,
|
|
77
|
-
descriptor: PropertyDescriptor
|
|
78
|
-
) => {
|
|
79
|
-
if (typeof descriptor.value !== "function") {
|
|
80
|
-
throw Error("Put decorator only use for method.");
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
84
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
85
|
-
{
|
|
86
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
87
|
-
httpMethod: "PUT",
|
|
88
|
-
methodName: methodName,
|
|
89
|
-
descriptor: descriptor
|
|
90
|
-
}
|
|
91
|
-
], target.constructor);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
*
|
|
97
|
-
* @param path
|
|
98
|
-
* @returns
|
|
99
|
-
*/
|
|
100
|
-
export const Patch = (
|
|
101
|
-
path = "/"
|
|
102
|
-
) => (
|
|
103
|
-
target: Object,
|
|
104
|
-
methodName: string,
|
|
105
|
-
descriptor: PropertyDescriptor
|
|
106
|
-
) => {
|
|
107
|
-
if (typeof descriptor.value !== "function") {
|
|
108
|
-
throw Error("Patch decorator only use for method.");
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
112
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
113
|
-
{
|
|
114
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
115
|
-
httpMethod: "PATCH",
|
|
116
|
-
methodName: methodName,
|
|
117
|
-
descriptor: descriptor
|
|
118
|
-
}
|
|
119
|
-
], target.constructor);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
*
|
|
125
|
-
* @param path
|
|
126
|
-
* @returns
|
|
127
|
-
*/
|
|
128
|
-
export const Delete = (
|
|
129
|
-
path = "/"
|
|
130
|
-
) => (
|
|
131
|
-
target: Object,
|
|
132
|
-
methodName: string,
|
|
133
|
-
descriptor: PropertyDescriptor
|
|
134
|
-
) => {
|
|
135
|
-
if (typeof descriptor.value !== "function") {
|
|
136
|
-
throw Error("Delete decorator only use for method.");
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
140
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
141
|
-
{
|
|
142
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
143
|
-
httpMethod: "DELETE",
|
|
144
|
-
methodName: methodName,
|
|
145
|
-
descriptor: descriptor
|
|
146
|
-
}
|
|
147
|
-
], target.constructor);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
*
|
|
153
|
-
* @param path
|
|
154
|
-
* @returns
|
|
155
|
-
*/
|
|
156
|
-
export const Options = (
|
|
157
|
-
path = "/"
|
|
158
|
-
) => (
|
|
159
|
-
target: Object,
|
|
160
|
-
methodName: string,
|
|
161
|
-
descriptor: PropertyDescriptor
|
|
162
|
-
) => {
|
|
163
|
-
if (typeof descriptor.value !== "function") {
|
|
164
|
-
throw Error("Options decorator only use for method.");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
168
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
169
|
-
{
|
|
170
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
171
|
-
httpMethod: "OPTIONS",
|
|
172
|
-
methodName: methodName,
|
|
173
|
-
descriptor: descriptor
|
|
174
|
-
}
|
|
175
|
-
], target.constructor);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export default {
|
|
179
|
-
Get,
|
|
180
|
-
Post,
|
|
181
|
-
Put,
|
|
182
|
-
Patch,
|
|
183
|
-
Delete
|
|
184
|
-
};
|
|
185
|
-
|
|
1
|
+
export interface IControllerRoute {
|
|
2
|
+
path: string;
|
|
3
|
+
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
|
+
methodName: string;
|
|
5
|
+
descriptor: PropertyDescriptor;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export const controllerRoutesKey = "__bool:controller.routes__";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param path
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export const Get = (
|
|
17
|
+
path = "/"
|
|
18
|
+
) => (
|
|
19
|
+
target: Object,
|
|
20
|
+
methodName: string,
|
|
21
|
+
descriptor: PropertyDescriptor
|
|
22
|
+
) => {
|
|
23
|
+
if (typeof descriptor.value !== "function") {
|
|
24
|
+
throw Error("Get decorator only use for method.");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
28
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
29
|
+
{
|
|
30
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
31
|
+
httpMethod: "GET",
|
|
32
|
+
methodName: methodName,
|
|
33
|
+
descriptor: descriptor
|
|
34
|
+
}
|
|
35
|
+
], target.constructor);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
*
|
|
41
|
+
* @param path
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export const Post = (
|
|
45
|
+
path = "/"
|
|
46
|
+
) => (
|
|
47
|
+
target: Object,
|
|
48
|
+
methodName: string,
|
|
49
|
+
descriptor: PropertyDescriptor
|
|
50
|
+
) => {
|
|
51
|
+
if (typeof descriptor.value !== "function") {
|
|
52
|
+
throw Error("Post decorator only use for method.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
56
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
57
|
+
{
|
|
58
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
59
|
+
httpMethod: "POST",
|
|
60
|
+
methodName: methodName,
|
|
61
|
+
descriptor: descriptor
|
|
62
|
+
}
|
|
63
|
+
], target.constructor);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
* @param path
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
export const Put = (
|
|
73
|
+
path = "/"
|
|
74
|
+
) => (
|
|
75
|
+
target: Object,
|
|
76
|
+
methodName: string,
|
|
77
|
+
descriptor: PropertyDescriptor
|
|
78
|
+
) => {
|
|
79
|
+
if (typeof descriptor.value !== "function") {
|
|
80
|
+
throw Error("Put decorator only use for method.");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
84
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
85
|
+
{
|
|
86
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
87
|
+
httpMethod: "PUT",
|
|
88
|
+
methodName: methodName,
|
|
89
|
+
descriptor: descriptor
|
|
90
|
+
}
|
|
91
|
+
], target.constructor);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
*
|
|
97
|
+
* @param path
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
export const Patch = (
|
|
101
|
+
path = "/"
|
|
102
|
+
) => (
|
|
103
|
+
target: Object,
|
|
104
|
+
methodName: string,
|
|
105
|
+
descriptor: PropertyDescriptor
|
|
106
|
+
) => {
|
|
107
|
+
if (typeof descriptor.value !== "function") {
|
|
108
|
+
throw Error("Patch decorator only use for method.");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
112
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
113
|
+
{
|
|
114
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
115
|
+
httpMethod: "PATCH",
|
|
116
|
+
methodName: methodName,
|
|
117
|
+
descriptor: descriptor
|
|
118
|
+
}
|
|
119
|
+
], target.constructor);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
*
|
|
125
|
+
* @param path
|
|
126
|
+
* @returns
|
|
127
|
+
*/
|
|
128
|
+
export const Delete = (
|
|
129
|
+
path = "/"
|
|
130
|
+
) => (
|
|
131
|
+
target: Object,
|
|
132
|
+
methodName: string,
|
|
133
|
+
descriptor: PropertyDescriptor
|
|
134
|
+
) => {
|
|
135
|
+
if (typeof descriptor.value !== "function") {
|
|
136
|
+
throw Error("Delete decorator only use for method.");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
140
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
141
|
+
{
|
|
142
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
143
|
+
httpMethod: "DELETE",
|
|
144
|
+
methodName: methodName,
|
|
145
|
+
descriptor: descriptor
|
|
146
|
+
}
|
|
147
|
+
], target.constructor);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* @param path
|
|
154
|
+
* @returns
|
|
155
|
+
*/
|
|
156
|
+
export const Options = (
|
|
157
|
+
path = "/"
|
|
158
|
+
) => (
|
|
159
|
+
target: Object,
|
|
160
|
+
methodName: string,
|
|
161
|
+
descriptor: PropertyDescriptor
|
|
162
|
+
) => {
|
|
163
|
+
if (typeof descriptor.value !== "function") {
|
|
164
|
+
throw Error("Options decorator only use for method.");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
168
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
169
|
+
{
|
|
170
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
171
|
+
httpMethod: "OPTIONS",
|
|
172
|
+
methodName: methodName,
|
|
173
|
+
descriptor: descriptor
|
|
174
|
+
}
|
|
175
|
+
], target.constructor);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export default {
|
|
179
|
+
Get,
|
|
180
|
+
Post,
|
|
181
|
+
Put,
|
|
182
|
+
Patch,
|
|
183
|
+
Delete
|
|
184
|
+
};
|
|
185
|
+
|
package/src/decorators/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Controller, controllerKey } from "./controller";
|
|
2
|
-
export { Inject, injectKey } from "./inject";
|
|
3
|
-
export { Injectable, injectableKey } from "./injectable";
|
|
4
|
-
export { Module, moduleKey, type TModuleOptions } from "./module";
|
|
5
|
-
export { Get, Post, Put, Patch, Delete, Options, controllerRoutesKey, type IControllerRoute } from "./http";
|
|
1
|
+
export { Controller, controllerKey } from "./controller";
|
|
2
|
+
export { Inject, injectKey } from "./inject";
|
|
3
|
+
export { Injectable, injectableKey } from "./injectable";
|
|
4
|
+
export { Module, moduleKey, type TModuleOptions } from "./module";
|
|
5
|
+
export { Get, Post, Put, Patch, Delete, Options, controllerRoutesKey, type IControllerRoute } from "./http";
|
package/src/decorators/inject.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export const injectKey = "design:paramtypes";
|
|
2
|
-
|
|
3
|
-
export const Inject = <T extends Object>(
|
|
4
|
-
classDefinition: { new(...args: any[]): T }
|
|
5
|
-
) => {
|
|
6
|
-
return (
|
|
7
|
-
target: Object,
|
|
8
|
-
parameterName: string | Symbol | undefined,
|
|
9
|
-
parameterIndex: number
|
|
10
|
-
) => {
|
|
11
|
-
const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
|
|
12
|
-
|
|
13
|
-
designParameterTypes[parameterIndex] = classDefinition;
|
|
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>(
|
|
4
|
+
classDefinition: { new(...args: any[]): T }
|
|
5
|
+
) => {
|
|
6
|
+
return (
|
|
7
|
+
target: Object,
|
|
8
|
+
parameterName: string | Symbol | undefined,
|
|
9
|
+
parameterIndex: number
|
|
10
|
+
) => {
|
|
11
|
+
const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
|
|
12
|
+
|
|
13
|
+
designParameterTypes[parameterIndex] = classDefinition;
|
|
14
|
+
|
|
15
|
+
Reflect.defineMetadata(injectKey, designParameterTypes, target);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default Inject;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export const injectableKey = "__bool:injectable__";
|
|
2
|
-
|
|
3
|
-
export const Injectable = () => <T extends Object>(
|
|
4
|
-
target: T,
|
|
5
|
-
context?: ClassDecoratorContext
|
|
6
|
-
) => {
|
|
7
|
-
Reflect.defineMetadata(injectableKey, undefined, target);
|
|
8
|
-
|
|
9
|
-
return target;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default Injectable;
|
|
1
|
+
export const injectableKey = "__bool:injectable__";
|
|
2
|
+
|
|
3
|
+
export const Injectable = () => <T extends Object>(
|
|
4
|
+
target: T,
|
|
5
|
+
context?: ClassDecoratorContext
|
|
6
|
+
) => {
|
|
7
|
+
Reflect.defineMetadata(injectableKey, undefined, target);
|
|
8
|
+
|
|
9
|
+
return target;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default Injectable;
|
package/src/decorators/module.ts
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
export type TModuleOptions = Partial<{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
export type TModuleOptions = Partial<{
|
|
2
|
+
prefix: string;
|
|
3
|
+
controllers: Array<new (...args: any[]) => unknown>;
|
|
4
|
+
dependencies: Array<new (...args: any[]) => unknown>;
|
|
5
|
+
allowOrigins: string | Array<string>;
|
|
6
|
+
allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
7
|
+
}> | undefined;
|
|
8
|
+
|
|
9
|
+
export const moduleKey = "__bool:module__";
|
|
10
|
+
|
|
11
|
+
export const Module = (
|
|
12
|
+
args?: TModuleOptions
|
|
13
|
+
) => <T extends { new(...args: any[]): {} }>(
|
|
14
|
+
target: T,
|
|
15
|
+
context?: ClassDecoratorContext
|
|
16
|
+
) => {
|
|
17
|
+
Reflect.defineMetadata(moduleKey, args, target);
|
|
18
|
+
|
|
19
|
+
return target;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default Module;
|