@bool-ts/core 1.3.0 → 1.3.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/__test/controller.ts +9 -2
- package/dist/decorators/http.js +85 -183
- package/dist/decorators/zodSchema.js +0 -6
- package/dist/hooks/factory.js +3 -1
- package/dist/http/clientError.d.ts +5 -4
- package/dist/http/clientError.js +2 -2
- package/dist/http/index.js +5 -1
- package/dist/http/serverError.d.ts +5 -4
- package/dist/http/serverError.js +2 -2
- package/dist/ultils/asyncFunction.d.ts +1 -0
- package/dist/ultils/asyncFunction.js +4 -0
- package/dist/ultils/index.d.ts +1 -0
- package/dist/ultils/index.js +17 -0
- package/package.json +1 -1
- package/src/decorators/http.ts +100 -242
- package/src/decorators/zodSchema.ts +1 -7
- package/src/hooks/factory.ts +3 -1
- package/src/http/clientError.ts +7 -8
- package/src/http/index.ts +5 -1
- package/src/http/serverError.ts +7 -8
- package/src/ultils/asyncFunction.ts +1 -0
- package/src/ultils/index.ts +1 -0
package/__test/controller.ts
CHANGED
|
@@ -13,6 +13,13 @@ const getAbcSchema = Zod.object({
|
|
|
13
13
|
})
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
+
const stringSchema = Zod.object({}).refine(async (val) => {
|
|
17
|
+
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
|
18
|
+
await delay(10000);
|
|
19
|
+
|
|
20
|
+
return val;
|
|
21
|
+
});
|
|
22
|
+
|
|
16
23
|
@Controller("test")
|
|
17
24
|
export class TestController {
|
|
18
25
|
constructor(
|
|
@@ -56,8 +63,8 @@ export class TestController {
|
|
|
56
63
|
|
|
57
64
|
@Delete()
|
|
58
65
|
private _delete(
|
|
59
|
-
|
|
60
|
-
res: Response
|
|
66
|
+
req: Request,
|
|
67
|
+
@ZodSchema(stringSchema) res: Response
|
|
61
68
|
) {
|
|
62
69
|
res.json({ test: "success" }).send();
|
|
63
70
|
}
|
package/dist/decorators/http.js
CHANGED
|
@@ -2,236 +2,138 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Options = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = exports.controllerRoutesKey = void 0;
|
|
4
4
|
const http_1 = require("../http");
|
|
5
|
+
const ultils_1 = require("../ultils");
|
|
5
6
|
const zodSchema_1 = require("./zodSchema");
|
|
6
7
|
exports.controllerRoutesKey = "__bool:controller.routes__";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @returns
|
|
11
|
-
*/
|
|
12
|
-
const Get = (path = "/") => (target, methodName, descriptor) => {
|
|
13
|
-
if (typeof descriptor.value !== "function") {
|
|
14
|
-
throw Error("Get decorator only use for method.");
|
|
8
|
+
const defaultDecorator = (path, method) => (target, methodName, descriptor) => {
|
|
9
|
+
if (!(descriptor.value instanceof Function)) {
|
|
10
|
+
throw Error(`${method} decorator only use for method.`);
|
|
15
11
|
}
|
|
16
12
|
// Define controller metadata
|
|
17
13
|
Reflect.defineMetadata(exports.controllerRoutesKey, [
|
|
18
14
|
...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
|
|
19
15
|
{
|
|
20
16
|
path: !path.startsWith("/") ? `/${path}` : path,
|
|
21
|
-
httpMethod:
|
|
17
|
+
httpMethod: method.toUpperCase(),
|
|
22
18
|
methodName: methodName,
|
|
23
19
|
descriptor: descriptor
|
|
24
20
|
}
|
|
25
21
|
], target.constructor);
|
|
26
22
|
// Define route parameters zod validation
|
|
27
23
|
const currentMethod = descriptor.value;
|
|
28
|
-
descriptor.value
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
24
|
+
const isAsync = descriptor.value instanceof ultils_1.AsyncFunction;
|
|
25
|
+
if (!isAsync) {
|
|
26
|
+
descriptor.value = function () {
|
|
27
|
+
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
28
|
+
if (zodSchemaMetadata) {
|
|
29
|
+
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
30
|
+
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
31
|
+
try {
|
|
32
|
+
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
33
|
+
if (!validation.success) {
|
|
34
|
+
throw new http_1.HttpClientError({
|
|
35
|
+
httpCode: 400,
|
|
36
|
+
message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
|
|
37
|
+
data: validation.error.issues
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error instanceof http_1.HttpClientError) {
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
throw new http_1.HttpServerError({
|
|
46
|
+
httpCode: 500,
|
|
47
|
+
message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
|
|
48
|
+
data: !(error instanceof Error) ? error : [{
|
|
49
|
+
message: error.message,
|
|
50
|
+
code: error.name,
|
|
51
|
+
cause: error.cause
|
|
52
|
+
}]
|
|
53
|
+
});
|
|
54
|
+
}
|
|
39
55
|
}
|
|
40
56
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
57
|
+
return currentMethod.apply(this, arguments);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
descriptor.value = async function () {
|
|
62
|
+
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
63
|
+
if (zodSchemaMetadata) {
|
|
64
|
+
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
65
|
+
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
66
|
+
try {
|
|
67
|
+
const validation = await tmpZodMetadata.schema.safeParseAsync(arguments[tmpZodMetadata.index]);
|
|
68
|
+
if (!validation.success) {
|
|
69
|
+
throw new http_1.HttpClientError({
|
|
70
|
+
httpCode: 400,
|
|
71
|
+
message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
|
|
72
|
+
data: validation.error.issues
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (error instanceof http_1.HttpClientError) {
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
throw new http_1.HttpServerError({
|
|
81
|
+
httpCode: 500,
|
|
82
|
+
message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
|
|
83
|
+
data: !(error instanceof Error) ? error : [{
|
|
84
|
+
message: error.message,
|
|
85
|
+
code: error.name,
|
|
86
|
+
cause: error.cause
|
|
87
|
+
}]
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return currentMethod.apply(this, arguments);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
44
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
*
|
|
98
|
+
* @param path
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
const Get = (path = "/") => defaultDecorator(path, "Get");
|
|
45
102
|
exports.Get = Get;
|
|
46
103
|
/**
|
|
47
104
|
*
|
|
48
105
|
* @param path
|
|
49
106
|
* @returns
|
|
50
107
|
*/
|
|
51
|
-
const Post = (path = "/") => (
|
|
52
|
-
if (typeof descriptor.value !== "function") {
|
|
53
|
-
throw Error("Post decorator only use for method.");
|
|
54
|
-
}
|
|
55
|
-
Reflect.defineMetadata(exports.controllerRoutesKey, [
|
|
56
|
-
...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
|
|
57
|
-
{
|
|
58
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
59
|
-
httpMethod: "POST",
|
|
60
|
-
methodName: methodName,
|
|
61
|
-
descriptor: descriptor
|
|
62
|
-
}
|
|
63
|
-
], target.constructor);
|
|
64
|
-
// Define route parameters zod validation
|
|
65
|
-
const currentMethod = descriptor.value;
|
|
66
|
-
descriptor.value = function () {
|
|
67
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
68
|
-
if (zodSchemaMetadata) {
|
|
69
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
70
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
71
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
72
|
-
if (!validation.success) {
|
|
73
|
-
throw new http_1.HttpClientError({
|
|
74
|
-
httpCode: 400,
|
|
75
|
-
data: validation.error.issues
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return currentMethod.apply(this, arguments);
|
|
81
|
-
};
|
|
82
|
-
};
|
|
108
|
+
const Post = (path = "/") => defaultDecorator(path, "Post");
|
|
83
109
|
exports.Post = Post;
|
|
84
110
|
/**
|
|
85
111
|
*
|
|
86
112
|
* @param path
|
|
87
113
|
* @returns
|
|
88
114
|
*/
|
|
89
|
-
const Put = (path = "/") => (
|
|
90
|
-
if (typeof descriptor.value !== "function") {
|
|
91
|
-
throw Error("Put decorator only use for method.");
|
|
92
|
-
}
|
|
93
|
-
Reflect.defineMetadata(exports.controllerRoutesKey, [
|
|
94
|
-
...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
|
|
95
|
-
{
|
|
96
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
97
|
-
httpMethod: "PUT",
|
|
98
|
-
methodName: methodName,
|
|
99
|
-
descriptor: descriptor
|
|
100
|
-
}
|
|
101
|
-
], target.constructor);
|
|
102
|
-
// Define route parameters zod validation
|
|
103
|
-
const currentMethod = descriptor.value;
|
|
104
|
-
descriptor.value = function () {
|
|
105
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
106
|
-
if (zodSchemaMetadata) {
|
|
107
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
108
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
109
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
110
|
-
if (!validation.success) {
|
|
111
|
-
throw new http_1.HttpClientError({
|
|
112
|
-
httpCode: 400,
|
|
113
|
-
data: validation.error.issues
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return currentMethod.apply(this, arguments);
|
|
119
|
-
};
|
|
120
|
-
};
|
|
115
|
+
const Put = (path = "/") => defaultDecorator(path, "Put");
|
|
121
116
|
exports.Put = Put;
|
|
122
117
|
/**
|
|
123
118
|
*
|
|
124
119
|
* @param path
|
|
125
120
|
* @returns
|
|
126
121
|
*/
|
|
127
|
-
const Patch = (path = "/") => (
|
|
128
|
-
if (typeof descriptor.value !== "function") {
|
|
129
|
-
throw Error("Patch decorator only use for method.");
|
|
130
|
-
}
|
|
131
|
-
Reflect.defineMetadata(exports.controllerRoutesKey, [
|
|
132
|
-
...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
|
|
133
|
-
{
|
|
134
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
135
|
-
httpMethod: "PATCH",
|
|
136
|
-
methodName: methodName,
|
|
137
|
-
descriptor: descriptor
|
|
138
|
-
}
|
|
139
|
-
], target.constructor);
|
|
140
|
-
// Define route parameters zod validation
|
|
141
|
-
const currentMethod = descriptor.value;
|
|
142
|
-
descriptor.value = function () {
|
|
143
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
144
|
-
if (zodSchemaMetadata) {
|
|
145
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
146
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
147
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
148
|
-
if (!validation.success) {
|
|
149
|
-
throw new http_1.HttpClientError({
|
|
150
|
-
httpCode: 400,
|
|
151
|
-
data: validation.error.issues
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return currentMethod.apply(this, arguments);
|
|
157
|
-
};
|
|
158
|
-
};
|
|
122
|
+
const Patch = (path = "/") => defaultDecorator(path, "Patch");
|
|
159
123
|
exports.Patch = Patch;
|
|
160
124
|
/**
|
|
161
125
|
*
|
|
162
126
|
* @param path
|
|
163
127
|
* @returns
|
|
164
128
|
*/
|
|
165
|
-
const Delete = (path = "/") => (
|
|
166
|
-
if (typeof descriptor.value !== "function") {
|
|
167
|
-
throw Error("Delete decorator only use for method.");
|
|
168
|
-
}
|
|
169
|
-
Reflect.defineMetadata(exports.controllerRoutesKey, [
|
|
170
|
-
...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
|
|
171
|
-
{
|
|
172
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
173
|
-
httpMethod: "DELETE",
|
|
174
|
-
methodName: methodName,
|
|
175
|
-
descriptor: descriptor
|
|
176
|
-
}
|
|
177
|
-
], target.constructor);
|
|
178
|
-
// Define route parameters zod validation
|
|
179
|
-
const currentMethod = descriptor.value;
|
|
180
|
-
descriptor.value = function () {
|
|
181
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
182
|
-
if (zodSchemaMetadata) {
|
|
183
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
184
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
185
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
186
|
-
if (!validation.success) {
|
|
187
|
-
throw new http_1.HttpClientError({
|
|
188
|
-
httpCode: 400,
|
|
189
|
-
data: validation.error.issues
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
return currentMethod.apply(this, arguments);
|
|
195
|
-
};
|
|
196
|
-
};
|
|
129
|
+
const Delete = (path = "/") => defaultDecorator(path, "Delete");
|
|
197
130
|
exports.Delete = Delete;
|
|
198
131
|
/**
|
|
199
132
|
*
|
|
200
133
|
* @param path
|
|
201
134
|
* @returns
|
|
202
135
|
*/
|
|
203
|
-
const Options = (path = "/") => (
|
|
204
|
-
if (typeof descriptor.value !== "function") {
|
|
205
|
-
throw Error("Options decorator only use for method.");
|
|
206
|
-
}
|
|
207
|
-
Reflect.defineMetadata(exports.controllerRoutesKey, [
|
|
208
|
-
...Reflect.getOwnMetadata(exports.controllerRoutesKey, target.constructor) || [],
|
|
209
|
-
{
|
|
210
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
211
|
-
httpMethod: "OPTIONS",
|
|
212
|
-
methodName: methodName,
|
|
213
|
-
descriptor: descriptor
|
|
214
|
-
}
|
|
215
|
-
], target.constructor);
|
|
216
|
-
// Define route parameters zod validation
|
|
217
|
-
const currentMethod = descriptor.value;
|
|
218
|
-
descriptor.value = function () {
|
|
219
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(zodSchema_1.controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
220
|
-
if (zodSchemaMetadata) {
|
|
221
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
222
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
223
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
224
|
-
if (!validation.success) {
|
|
225
|
-
throw new http_1.HttpClientError({
|
|
226
|
-
httpCode: 400,
|
|
227
|
-
data: validation.error.issues
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return currentMethod.apply(this, arguments);
|
|
233
|
-
};
|
|
234
|
-
};
|
|
136
|
+
const Options = (path = "/") => defaultDecorator(path, "Options");
|
|
235
137
|
exports.Options = Options;
|
|
236
138
|
exports.default = {
|
|
237
139
|
Get: exports.Get,
|
|
@@ -3,12 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ZodSchema = exports.controllerRouteZodSchemaKey = void 0;
|
|
4
4
|
exports.controllerRouteZodSchemaKey = "__bool:controller.route.zodSchema__";
|
|
5
5
|
const ZodSchema = (schema) => {
|
|
6
|
-
try {
|
|
7
|
-
schema.safeParse(undefined);
|
|
8
|
-
}
|
|
9
|
-
catch (err) {
|
|
10
|
-
throw Error("Zod schema parameter do not allow async.");
|
|
11
|
-
}
|
|
12
6
|
return (target, methodName, parameterIndex) => {
|
|
13
7
|
if (!methodName) {
|
|
14
8
|
return;
|
package/dist/hooks/factory.js
CHANGED
|
@@ -169,7 +169,9 @@ const BoolFactory = (target, options) => {
|
|
|
169
169
|
if (!options?.debug) {
|
|
170
170
|
return;
|
|
171
171
|
}
|
|
172
|
-
console.
|
|
172
|
+
console.info("Headers:", JSON.stringify(req.headers, null, 4), "\nBody:", JSON.stringify(req.body, null, 4));
|
|
173
|
+
console.error("Error:");
|
|
174
|
+
console.error(err);
|
|
173
175
|
});
|
|
174
176
|
return app;
|
|
175
177
|
};
|
|
@@ -31,10 +31,11 @@ export declare const httpClientErrors: Readonly<{
|
|
|
31
31
|
}>;
|
|
32
32
|
export declare class HttpClientError<T extends keyof typeof httpClientErrors = keyof typeof httpClientErrors, K = any> extends Error {
|
|
33
33
|
readonly httpCode: T;
|
|
34
|
-
readonly message: typeof httpClientErrors[T];
|
|
34
|
+
readonly message: typeof httpClientErrors[T] | string;
|
|
35
35
|
readonly data: K;
|
|
36
|
-
constructor({ httpCode, data }: {
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
constructor({ httpCode, data, message }: {
|
|
37
|
+
httpCode: T;
|
|
38
|
+
data: K;
|
|
39
|
+
message?: string;
|
|
39
40
|
});
|
|
40
41
|
}
|
package/dist/http/clientError.js
CHANGED
|
@@ -36,10 +36,10 @@ class HttpClientError extends Error {
|
|
|
36
36
|
httpCode;
|
|
37
37
|
message;
|
|
38
38
|
data;
|
|
39
|
-
constructor({ httpCode, data }) {
|
|
39
|
+
constructor({ httpCode, data, message }) {
|
|
40
40
|
super();
|
|
41
41
|
this.httpCode = httpCode;
|
|
42
|
-
this.message = exports.httpClientErrors[httpCode];
|
|
42
|
+
this.message = !message?.trim() ? exports.httpClientErrors[httpCode] : message.trim();
|
|
43
43
|
this.data = data;
|
|
44
44
|
}
|
|
45
45
|
}
|
package/dist/http/index.js
CHANGED
|
@@ -34,7 +34,11 @@ const errorInfer = (res, data) => {
|
|
|
34
34
|
res.status(500).json({
|
|
35
35
|
httpCode: 500,
|
|
36
36
|
message: "INTERNAL SERVER ERROR",
|
|
37
|
-
data: data
|
|
37
|
+
data: !(data instanceof Error) ? data : {
|
|
38
|
+
message: data.message,
|
|
39
|
+
code: data.name,
|
|
40
|
+
cause: data.cause
|
|
41
|
+
}
|
|
38
42
|
});
|
|
39
43
|
return;
|
|
40
44
|
}
|
|
@@ -13,10 +13,11 @@ export declare const httpServerErrors: Readonly<{
|
|
|
13
13
|
}>;
|
|
14
14
|
export declare class HttpServerError<T extends keyof typeof httpServerErrors = keyof typeof httpServerErrors, K = any> extends Error {
|
|
15
15
|
readonly httpCode: T;
|
|
16
|
-
readonly message: typeof httpServerErrors[T];
|
|
16
|
+
readonly message: typeof httpServerErrors[T] | string;
|
|
17
17
|
readonly data: K;
|
|
18
|
-
constructor({ httpCode, data }: {
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
constructor({ httpCode, data, message }: {
|
|
19
|
+
httpCode: T;
|
|
20
|
+
data: K;
|
|
21
|
+
message?: string;
|
|
21
22
|
});
|
|
22
23
|
}
|
package/dist/http/serverError.js
CHANGED
|
@@ -18,10 +18,10 @@ class HttpServerError extends Error {
|
|
|
18
18
|
httpCode;
|
|
19
19
|
message;
|
|
20
20
|
data;
|
|
21
|
-
constructor({ httpCode, data }) {
|
|
21
|
+
constructor({ httpCode, data, message }) {
|
|
22
22
|
super();
|
|
23
23
|
this.httpCode = httpCode;
|
|
24
|
-
this.message = exports.httpServerErrors[httpCode];
|
|
24
|
+
this.message = !message?.trim() ? exports.httpServerErrors[httpCode] : message.trim();
|
|
25
25
|
this.data = data;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const AsyncFunction: Function;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./asyncFunction";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./asyncFunction"), exports);
|
package/package.json
CHANGED
package/src/decorators/http.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { HttpClientError } from "../http";
|
|
1
|
+
import { HttpClientError, HttpServerError } from "../http";
|
|
2
|
+
import { AsyncFunction } from "../ultils";
|
|
2
3
|
import { controllerRouteZodSchemaKey } from "./zodSchema";
|
|
3
4
|
|
|
4
5
|
export interface IControllerRoute {
|
|
@@ -11,20 +12,17 @@ export interface IControllerRoute {
|
|
|
11
12
|
|
|
12
13
|
export const controllerRoutesKey = "__bool:controller.routes__";
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*/
|
|
19
|
-
export const Get = (
|
|
20
|
-
path = "/"
|
|
15
|
+
|
|
16
|
+
const defaultDecorator = (
|
|
17
|
+
path: string,
|
|
18
|
+
method: "Get" | "Post" | "Put" | "Patch" | "Delete" | "Options"
|
|
21
19
|
) => (
|
|
22
20
|
target: Object,
|
|
23
21
|
methodName: string,
|
|
24
22
|
descriptor: PropertyDescriptor
|
|
25
23
|
) => {
|
|
26
|
-
if (
|
|
27
|
-
throw Error(
|
|
24
|
+
if (!(descriptor.value instanceof Function)) {
|
|
25
|
+
throw Error(`${method} decorator only use for method.`);
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
// Define controller metadata
|
|
@@ -32,7 +30,7 @@ export const Get = (
|
|
|
32
30
|
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
33
31
|
{
|
|
34
32
|
path: !path.startsWith("/") ? `/${path}` : path,
|
|
35
|
-
httpMethod:
|
|
33
|
+
httpMethod: method.toUpperCase(),
|
|
36
34
|
methodName: methodName,
|
|
37
35
|
descriptor: descriptor
|
|
38
36
|
}
|
|
@@ -40,80 +38,108 @@ export const Get = (
|
|
|
40
38
|
|
|
41
39
|
// Define route parameters zod validation
|
|
42
40
|
const currentMethod = descriptor.value;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
41
|
+
const isAsync = descriptor.value instanceof AsyncFunction;
|
|
42
|
+
|
|
43
|
+
if (!isAsync) {
|
|
44
|
+
descriptor.value = function () {
|
|
45
|
+
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
46
|
+
|
|
47
|
+
if (zodSchemaMetadata) {
|
|
48
|
+
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
49
|
+
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
53
|
+
|
|
54
|
+
if (!validation.success) {
|
|
55
|
+
throw new HttpClientError({
|
|
56
|
+
httpCode: 400,
|
|
57
|
+
message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
|
|
58
|
+
data: validation.error.issues
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (error instanceof HttpClientError) {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw new HttpServerError({
|
|
68
|
+
httpCode: 500,
|
|
69
|
+
message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
|
|
70
|
+
data: !(error instanceof Error) ? error : [{
|
|
71
|
+
message: error.message,
|
|
72
|
+
code: error.name,
|
|
73
|
+
cause: error.cause
|
|
74
|
+
}]
|
|
75
|
+
});
|
|
76
|
+
}
|
|
58
77
|
}
|
|
59
78
|
}
|
|
79
|
+
|
|
80
|
+
return currentMethod.apply(this, arguments);
|
|
60
81
|
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
descriptor.value = async function () {
|
|
85
|
+
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
86
|
+
|
|
87
|
+
if (zodSchemaMetadata) {
|
|
88
|
+
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
89
|
+
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const validation = await tmpZodMetadata.schema.safeParseAsync(arguments[tmpZodMetadata.index]);
|
|
93
|
+
|
|
94
|
+
if (!validation.success) {
|
|
95
|
+
throw new HttpClientError({
|
|
96
|
+
httpCode: 400,
|
|
97
|
+
message: `Validation at the [${methodName}] method fails at positional argument [${tmpZodMetadata.index}].`,
|
|
98
|
+
data: validation.error.issues
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (error instanceof HttpClientError) {
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
throw new HttpServerError({
|
|
108
|
+
httpCode: 500,
|
|
109
|
+
message: `Validation at the [${methodName}] method error at positional argument [${tmpZodMetadata.index}].`,
|
|
110
|
+
data: !(error instanceof Error) ? error : [{
|
|
111
|
+
message: error.message,
|
|
112
|
+
code: error.name,
|
|
113
|
+
cause: error.cause
|
|
114
|
+
}]
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
61
119
|
|
|
62
|
-
|
|
120
|
+
return currentMethod.apply(this, arguments);
|
|
121
|
+
}
|
|
63
122
|
}
|
|
64
123
|
}
|
|
65
124
|
|
|
66
|
-
|
|
67
125
|
/**
|
|
68
126
|
*
|
|
69
127
|
* @param path
|
|
70
128
|
* @returns
|
|
71
129
|
*/
|
|
72
|
-
export const
|
|
130
|
+
export const Get = (
|
|
73
131
|
path = "/"
|
|
74
|
-
) => (
|
|
75
|
-
target: Object,
|
|
76
|
-
methodName: string,
|
|
77
|
-
descriptor: PropertyDescriptor
|
|
78
|
-
) => {
|
|
79
|
-
if (typeof descriptor.value !== "function") {
|
|
80
|
-
throw Error("Post decorator only use for method.");
|
|
81
|
-
}
|
|
132
|
+
) => defaultDecorator(path, "Get");
|
|
82
133
|
|
|
83
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
84
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
85
|
-
{
|
|
86
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
87
|
-
httpMethod: "POST",
|
|
88
|
-
methodName: methodName,
|
|
89
|
-
descriptor: descriptor
|
|
90
|
-
}
|
|
91
|
-
], target.constructor);
|
|
92
|
-
|
|
93
|
-
// Define route parameters zod validation
|
|
94
|
-
const currentMethod = descriptor.value;
|
|
95
134
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (!validation.success) {
|
|
106
|
-
throw new HttpClientError({
|
|
107
|
-
httpCode: 400,
|
|
108
|
-
data: validation.error.issues
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return currentMethod.apply(this, arguments);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
135
|
+
/**
|
|
136
|
+
*
|
|
137
|
+
* @param path
|
|
138
|
+
* @returns
|
|
139
|
+
*/
|
|
140
|
+
export const Post = (
|
|
141
|
+
path = "/"
|
|
142
|
+
) => defaultDecorator(path, "Post");
|
|
117
143
|
|
|
118
144
|
|
|
119
145
|
/**
|
|
@@ -123,49 +149,7 @@ export const Post = (
|
|
|
123
149
|
*/
|
|
124
150
|
export const Put = (
|
|
125
151
|
path = "/"
|
|
126
|
-
) => (
|
|
127
|
-
target: Object,
|
|
128
|
-
methodName: string,
|
|
129
|
-
descriptor: PropertyDescriptor
|
|
130
|
-
) => {
|
|
131
|
-
if (typeof descriptor.value !== "function") {
|
|
132
|
-
throw Error("Put decorator only use for method.");
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
136
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
137
|
-
{
|
|
138
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
139
|
-
httpMethod: "PUT",
|
|
140
|
-
methodName: methodName,
|
|
141
|
-
descriptor: descriptor
|
|
142
|
-
}
|
|
143
|
-
], target.constructor);
|
|
144
|
-
|
|
145
|
-
// Define route parameters zod validation
|
|
146
|
-
const currentMethod = descriptor.value;
|
|
147
|
-
|
|
148
|
-
descriptor.value = function () {
|
|
149
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
150
|
-
|
|
151
|
-
if (zodSchemaMetadata) {
|
|
152
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
153
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
154
|
-
|
|
155
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
156
|
-
|
|
157
|
-
if (!validation.success) {
|
|
158
|
-
throw new HttpClientError({
|
|
159
|
-
httpCode: 400,
|
|
160
|
-
data: validation.error.issues
|
|
161
|
-
})
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return currentMethod.apply(this, arguments);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
152
|
+
) => defaultDecorator(path, "Put");
|
|
169
153
|
|
|
170
154
|
|
|
171
155
|
/**
|
|
@@ -175,49 +159,7 @@ export const Put = (
|
|
|
175
159
|
*/
|
|
176
160
|
export const Patch = (
|
|
177
161
|
path = "/"
|
|
178
|
-
) => (
|
|
179
|
-
target: Object,
|
|
180
|
-
methodName: string,
|
|
181
|
-
descriptor: PropertyDescriptor
|
|
182
|
-
) => {
|
|
183
|
-
if (typeof descriptor.value !== "function") {
|
|
184
|
-
throw Error("Patch decorator only use for method.");
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
188
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
189
|
-
{
|
|
190
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
191
|
-
httpMethod: "PATCH",
|
|
192
|
-
methodName: methodName,
|
|
193
|
-
descriptor: descriptor
|
|
194
|
-
}
|
|
195
|
-
], target.constructor);
|
|
196
|
-
|
|
197
|
-
// Define route parameters zod validation
|
|
198
|
-
const currentMethod = descriptor.value;
|
|
199
|
-
|
|
200
|
-
descriptor.value = function () {
|
|
201
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
202
|
-
|
|
203
|
-
if (zodSchemaMetadata) {
|
|
204
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
205
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
206
|
-
|
|
207
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
208
|
-
|
|
209
|
-
if (!validation.success) {
|
|
210
|
-
throw new HttpClientError({
|
|
211
|
-
httpCode: 400,
|
|
212
|
-
data: validation.error.issues
|
|
213
|
-
})
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return currentMethod.apply(this, arguments);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
162
|
+
) => defaultDecorator(path, "Patch");
|
|
221
163
|
|
|
222
164
|
|
|
223
165
|
/**
|
|
@@ -227,49 +169,7 @@ export const Patch = (
|
|
|
227
169
|
*/
|
|
228
170
|
export const Delete = (
|
|
229
171
|
path = "/"
|
|
230
|
-
) => (
|
|
231
|
-
target: Object,
|
|
232
|
-
methodName: string,
|
|
233
|
-
descriptor: PropertyDescriptor
|
|
234
|
-
) => {
|
|
235
|
-
if (typeof descriptor.value !== "function") {
|
|
236
|
-
throw Error("Delete decorator only use for method.");
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
240
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
241
|
-
{
|
|
242
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
243
|
-
httpMethod: "DELETE",
|
|
244
|
-
methodName: methodName,
|
|
245
|
-
descriptor: descriptor
|
|
246
|
-
}
|
|
247
|
-
], target.constructor);
|
|
248
|
-
|
|
249
|
-
// Define route parameters zod validation
|
|
250
|
-
const currentMethod = descriptor.value;
|
|
251
|
-
|
|
252
|
-
descriptor.value = function () {
|
|
253
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
254
|
-
|
|
255
|
-
if (zodSchemaMetadata) {
|
|
256
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
257
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
258
|
-
|
|
259
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
260
|
-
|
|
261
|
-
if (!validation.success) {
|
|
262
|
-
throw new HttpClientError({
|
|
263
|
-
httpCode: 400,
|
|
264
|
-
data: validation.error.issues
|
|
265
|
-
})
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return currentMethod.apply(this, arguments);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
172
|
+
) => defaultDecorator(path, "Delete");
|
|
273
173
|
|
|
274
174
|
|
|
275
175
|
/**
|
|
@@ -279,49 +179,7 @@ export const Delete = (
|
|
|
279
179
|
*/
|
|
280
180
|
export const Options = (
|
|
281
181
|
path = "/"
|
|
282
|
-
) => (
|
|
283
|
-
target: Object,
|
|
284
|
-
methodName: string,
|
|
285
|
-
descriptor: PropertyDescriptor
|
|
286
|
-
) => {
|
|
287
|
-
if (typeof descriptor.value !== "function") {
|
|
288
|
-
throw Error("Options decorator only use for method.");
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
Reflect.defineMetadata(controllerRoutesKey, [
|
|
292
|
-
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
293
|
-
{
|
|
294
|
-
path: !path.startsWith("/") ? `/${path}` : path,
|
|
295
|
-
httpMethod: "OPTIONS",
|
|
296
|
-
methodName: methodName,
|
|
297
|
-
descriptor: descriptor
|
|
298
|
-
}
|
|
299
|
-
], target.constructor);
|
|
300
|
-
|
|
301
|
-
// Define route parameters zod validation
|
|
302
|
-
const currentMethod = descriptor.value;
|
|
303
|
-
|
|
304
|
-
descriptor.value = function () {
|
|
305
|
-
const zodSchemaMetadata = Reflect.getOwnMetadata(controllerRouteZodSchemaKey, target.constructor, methodName);
|
|
306
|
-
|
|
307
|
-
if (zodSchemaMetadata) {
|
|
308
|
-
for (const zodSchemaProp in zodSchemaMetadata) {
|
|
309
|
-
const tmpZodMetadata = zodSchemaMetadata[zodSchemaProp];
|
|
310
|
-
|
|
311
|
-
const validation = tmpZodMetadata.schema.safeParse(arguments[tmpZodMetadata.index]);
|
|
312
|
-
|
|
313
|
-
if (!validation.success) {
|
|
314
|
-
throw new HttpClientError({
|
|
315
|
-
httpCode: 400,
|
|
316
|
-
data: validation.error.issues
|
|
317
|
-
})
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return currentMethod.apply(this, arguments);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
182
|
+
) => defaultDecorator(path, "Options");
|
|
325
183
|
|
|
326
184
|
export default {
|
|
327
185
|
Get,
|
|
@@ -2,16 +2,10 @@ import * as Zod from "zod";
|
|
|
2
2
|
|
|
3
3
|
export const controllerRouteZodSchemaKey = "__bool:controller.route.zodSchema__";
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
export const ZodSchema = (
|
|
6
7
|
schema: Zod.Schema
|
|
7
8
|
) => {
|
|
8
|
-
try {
|
|
9
|
-
schema.safeParse(undefined);
|
|
10
|
-
}
|
|
11
|
-
catch (err) {
|
|
12
|
-
throw Error("Zod schema parameter do not allow async.");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
9
|
return (
|
|
16
10
|
target: Object,
|
|
17
11
|
methodName: string | symbol | undefined,
|
package/src/hooks/factory.ts
CHANGED
|
@@ -193,7 +193,9 @@ export const BoolFactory = (
|
|
|
193
193
|
return;
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
console.
|
|
196
|
+
console.info("Headers:", JSON.stringify(req.headers, null, 4), "\nBody:", JSON.stringify(req.body, null, 4));
|
|
197
|
+
console.error("Error:");
|
|
198
|
+
console.error(err);
|
|
197
199
|
}
|
|
198
200
|
);
|
|
199
201
|
|
package/src/http/clientError.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import * as ExpressJS from "express";
|
|
2
|
-
|
|
3
|
-
|
|
4
1
|
export const httpClientErrors = Object.freeze({
|
|
5
2
|
400: "BAD_REQUEST",
|
|
6
3
|
401: "UNAUTHORIZED",
|
|
@@ -38,20 +35,22 @@ export class HttpClientError<
|
|
|
38
35
|
K = any
|
|
39
36
|
> extends Error {
|
|
40
37
|
public readonly httpCode: T;
|
|
41
|
-
public readonly message: typeof httpClientErrors[T];
|
|
38
|
+
public readonly message: typeof httpClientErrors[T] | string;
|
|
42
39
|
public readonly data: K;
|
|
43
40
|
|
|
44
41
|
constructor({
|
|
45
42
|
httpCode,
|
|
46
|
-
data
|
|
43
|
+
data,
|
|
44
|
+
message
|
|
47
45
|
}: {
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
httpCode: T;
|
|
47
|
+
data: K;
|
|
48
|
+
message?: string;
|
|
50
49
|
}) {
|
|
51
50
|
super();
|
|
52
51
|
|
|
53
52
|
this.httpCode = httpCode;
|
|
54
|
-
this.message = httpClientErrors[httpCode];
|
|
53
|
+
this.message = !message?.trim() ? httpClientErrors[httpCode] : message.trim();
|
|
55
54
|
this.data = data;
|
|
56
55
|
}
|
|
57
56
|
}
|
package/src/http/index.ts
CHANGED
|
@@ -23,7 +23,11 @@ export const errorInfer = (res: Response, data: any) => {
|
|
|
23
23
|
res.status(500).json({
|
|
24
24
|
httpCode: 500,
|
|
25
25
|
message: "INTERNAL SERVER ERROR",
|
|
26
|
-
data: data
|
|
26
|
+
data: !(data instanceof Error) ? data : {
|
|
27
|
+
message: data.message,
|
|
28
|
+
code: data.name,
|
|
29
|
+
cause: data.cause
|
|
30
|
+
}
|
|
27
31
|
});
|
|
28
32
|
return;
|
|
29
33
|
}
|
package/src/http/serverError.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import * as ExpressJS from "express";
|
|
2
|
-
|
|
3
|
-
|
|
4
1
|
export const httpServerErrors = Object.freeze({
|
|
5
2
|
500: "INTERNAL_SERVER_ERROR",
|
|
6
3
|
501: "NOT_IMPLEMENTED",
|
|
@@ -20,20 +17,22 @@ export class HttpServerError<
|
|
|
20
17
|
K = any
|
|
21
18
|
> extends Error {
|
|
22
19
|
public readonly httpCode: T;
|
|
23
|
-
public readonly message: typeof httpServerErrors[T];
|
|
20
|
+
public readonly message: typeof httpServerErrors[T] | string;
|
|
24
21
|
public readonly data: K;
|
|
25
22
|
|
|
26
23
|
constructor({
|
|
27
24
|
httpCode,
|
|
28
|
-
data
|
|
25
|
+
data,
|
|
26
|
+
message
|
|
29
27
|
}: {
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
httpCode: T;
|
|
29
|
+
data: K;
|
|
30
|
+
message?: string;
|
|
32
31
|
}) {
|
|
33
32
|
super();
|
|
34
33
|
|
|
35
34
|
this.httpCode = httpCode;
|
|
36
|
-
this.message = httpServerErrors[httpCode];
|
|
35
|
+
this.message = !message?.trim() ? httpServerErrors[httpCode] : message.trim();
|
|
37
36
|
this.data = data;
|
|
38
37
|
}
|
|
39
38
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const AsyncFunction = async function () { }.constructor;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./asyncFunction";
|