@bool-ts/core 1.3.2 → 1.4.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.
- 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 +31 -0
- package/dist/decorators/arguments.js +67 -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 +180 -151
- 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 +128 -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 +285 -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/dist/hooks/factory.js
CHANGED
|
@@ -1,179 +1,208 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.BoolFactory = void 0;
|
|
27
|
-
require("reflect-metadata");
|
|
28
|
-
require("colors");
|
|
29
|
-
const Qs = __importStar(require("qs"));
|
|
30
|
-
const ResponseTime = __importStar(require("response-time"));
|
|
31
|
-
const decorators_1 = require("../decorators");
|
|
32
|
-
const express_1 = __importStar(require("express"));
|
|
33
|
-
const injector_1 = require("./injector");
|
|
34
|
-
const http_1 = require("../http");
|
|
35
|
-
/**
|
|
36
|
-
*
|
|
37
|
-
* @param target
|
|
38
|
-
* @param router
|
|
39
|
-
*/
|
|
40
|
-
const controllerCreator = (controllerConstructor, parentRouter = (0, express_1.Router)()) => {
|
|
41
|
-
if (!Reflect.getOwnMetadataKeys(controllerConstructor).includes(decorators_1.controllerKey)) {
|
|
1
|
+
import "colors";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
import Qs from "qs";
|
|
4
|
+
import * as Zod from "zod";
|
|
5
|
+
import { Router, RouterGroup } from "../entities";
|
|
6
|
+
import { controllerKey, controllerRoutesKey, moduleKey } from "../decorators";
|
|
7
|
+
import { HttpClientError, HttpServerError, jsonErrorInfer } from "../http";
|
|
8
|
+
import { Injector } from "./injector";
|
|
9
|
+
import { controllerActionArgumentsKey, EArgumentTypes } from "../decorators/arguments";
|
|
10
|
+
export const controllerCreator = (controllerConstructor, group) => {
|
|
11
|
+
if (!Reflect.getOwnMetadataKeys(controllerConstructor).includes(controllerKey)) {
|
|
42
12
|
throw Error(`${controllerConstructor.name} is not a controller.`);
|
|
43
13
|
}
|
|
44
|
-
const controller =
|
|
14
|
+
const controller = Injector.get(controllerConstructor);
|
|
45
15
|
if (!controller) {
|
|
46
16
|
throw Error("Can not initialize controller.");
|
|
47
17
|
}
|
|
48
|
-
const controllerMetadata = Reflect.getOwnMetadata(
|
|
49
|
-
const routesMetadata = (Reflect.getOwnMetadata(
|
|
50
|
-
|
|
51
|
-
|
|
18
|
+
const controllerMetadata = Reflect.getOwnMetadata(controllerKey, controllerConstructor) || "/";
|
|
19
|
+
const routesMetadata = (Reflect.getOwnMetadata(controllerRoutesKey, controllerConstructor) ||
|
|
20
|
+
[]);
|
|
21
|
+
const router = new Router(controllerMetadata);
|
|
22
|
+
routesMetadata.forEach((routeMetadata) => {
|
|
52
23
|
if (typeof routeMetadata.descriptor.value !== "function") {
|
|
53
24
|
return;
|
|
54
25
|
}
|
|
55
|
-
const route = router.route(routeMetadata.path);
|
|
26
|
+
const route = router.route(`/${routeMetadata.path}`);
|
|
27
|
+
const handler = routeMetadata.descriptor.value.bind(controller);
|
|
28
|
+
const routeArgument = {
|
|
29
|
+
constructor: controllerConstructor,
|
|
30
|
+
funcName: routeMetadata.methodName,
|
|
31
|
+
func: handler
|
|
32
|
+
};
|
|
56
33
|
switch (routeMetadata.httpMethod) {
|
|
57
34
|
case "GET":
|
|
58
|
-
return route.get(
|
|
35
|
+
return route.get(routeArgument);
|
|
59
36
|
case "POST":
|
|
60
|
-
return route.post(
|
|
37
|
+
return route.post(routeArgument);
|
|
61
38
|
case "PUT":
|
|
62
|
-
return route.put(
|
|
39
|
+
return route.put(routeArgument);
|
|
63
40
|
case "PATCH":
|
|
64
|
-
return route.patch(
|
|
41
|
+
return route.patch(routeArgument);
|
|
65
42
|
case "DELETE":
|
|
66
|
-
return route.delete(
|
|
43
|
+
return route.delete(routeArgument);
|
|
67
44
|
case "OPTIONS":
|
|
68
|
-
return route.options(
|
|
45
|
+
return route.options(routeArgument);
|
|
69
46
|
}
|
|
70
47
|
});
|
|
71
|
-
return
|
|
48
|
+
return group.add(router);
|
|
72
49
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
50
|
+
export const controllerActionArgumentsResolution = async (data, zodSchema, argumentIndex, funcName) => {
|
|
51
|
+
try {
|
|
52
|
+
const validation = await zodSchema.safeParseAsync(data);
|
|
53
|
+
if (!validation.success) {
|
|
54
|
+
throw new HttpClientError({
|
|
55
|
+
httpCode: 400,
|
|
56
|
+
message: `Validation at the [${funcName.toString()}] method fails at positional argument [${argumentIndex}].`,
|
|
57
|
+
data: validation.error.issues
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return validation.data;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (error instanceof HttpClientError) {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
throw new HttpServerError({
|
|
67
|
+
httpCode: 500,
|
|
68
|
+
message: `Validation at the [${funcName.toString()}] method error at positional argument [${argumentIndex}].`,
|
|
69
|
+
data: !(error instanceof Error)
|
|
70
|
+
? error
|
|
71
|
+
: [
|
|
72
|
+
{
|
|
73
|
+
message: error.message,
|
|
74
|
+
code: error.name,
|
|
75
|
+
cause: error.cause
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
export const BoolFactory = (target, options) => {
|
|
82
|
+
if (!Reflect.getOwnMetadataKeys(target).includes(moduleKey)) {
|
|
79
83
|
throw Error(`${target.name} is not a module.`);
|
|
80
84
|
}
|
|
81
|
-
const
|
|
82
|
-
const allowOrigins = !
|
|
83
|
-
["*"]
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
85
|
+
const moduleMetadata = Reflect.getOwnMetadata(moduleKey, target);
|
|
86
|
+
const allowOrigins = !moduleMetadata?.allowOrigins
|
|
87
|
+
? ["*"]
|
|
88
|
+
: typeof moduleMetadata.allowOrigins !== "string"
|
|
89
|
+
? moduleMetadata.allowOrigins
|
|
90
|
+
: [moduleMetadata.allowOrigins];
|
|
91
|
+
const allowMethods = !moduleMetadata?.allowMethods
|
|
92
|
+
? ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
|
|
93
|
+
: moduleMetadata.allowMethods;
|
|
94
|
+
const { allowLogsMethods } = Object.freeze({
|
|
89
95
|
allowLogsMethods: !options?.log?.methods ? ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] : options.log.methods
|
|
90
96
|
});
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
97
|
+
const routerGroup = new RouterGroup();
|
|
98
|
+
moduleMetadata?.controllers &&
|
|
99
|
+
moduleMetadata.controllers.map((controllerConstructor) => controllerCreator(controllerConstructor, routerGroup));
|
|
100
|
+
Bun.serve({
|
|
101
|
+
port: options.port,
|
|
102
|
+
async fetch(request) {
|
|
103
|
+
const start = performance.now();
|
|
104
|
+
const url = new URL(request.url);
|
|
105
|
+
try {
|
|
106
|
+
const headers = request.headers;
|
|
107
|
+
const origin = headers.get("origin");
|
|
108
|
+
const response = new Response();
|
|
109
|
+
if (!allowOrigins.includes("*")) {
|
|
110
|
+
if (!origin) {
|
|
111
|
+
throw new HttpClientError({
|
|
112
|
+
httpCode: 403,
|
|
113
|
+
message: "Origin not found.",
|
|
114
|
+
data: {
|
|
115
|
+
origin: {
|
|
116
|
+
code: "origin:invalid:0x00001",
|
|
117
|
+
message: "Origin not found."
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (!allowOrigins.includes(origin)) {
|
|
123
|
+
throw new HttpClientError({
|
|
124
|
+
httpCode: 403,
|
|
125
|
+
message: "Invalid origin.",
|
|
126
|
+
data: {
|
|
127
|
+
origin: {
|
|
128
|
+
code: "origin:invalid:0x00002",
|
|
129
|
+
message: "Invalid origin."
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
response.headers.set("Access-Control-Allow-Origin", origin || "*");
|
|
136
|
+
response.headers.set("Access-Control-Allow-Headers", "*");
|
|
137
|
+
response.headers.set("Access-Control-Allow-Credentials", "true");
|
|
138
|
+
response.headers.set("Access-Control-Allow-Methods", allowMethods.join(", "));
|
|
139
|
+
if (!allowMethods.includes(request.method.toUpperCase())) {
|
|
140
|
+
throw new HttpClientError({
|
|
141
|
+
httpCode: 405,
|
|
142
|
+
message: "Method Not Allowed.",
|
|
143
|
+
data: undefined
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const result = routerGroup.find(url.pathname, request.method);
|
|
147
|
+
if (!result) {
|
|
148
|
+
throw new HttpClientError({
|
|
149
|
+
httpCode: 404,
|
|
150
|
+
message: "Route not found.",
|
|
151
|
+
data: undefined
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const params = result.params;
|
|
155
|
+
const query = Qs.parse(url.search, options.queryParser);
|
|
156
|
+
for (let i = 0; i < result.handlers.length; i++) {
|
|
157
|
+
const handler = result.handlers[i];
|
|
158
|
+
const handlerMetadata = (Reflect.getOwnMetadata(controllerActionArgumentsKey, handler.constructor, handler.funcName) || {});
|
|
159
|
+
const controllerActionArguments = [];
|
|
160
|
+
if (handlerMetadata) {
|
|
161
|
+
for (const [_key, argsMetadata] of Object.entries(handlerMetadata)) {
|
|
162
|
+
switch (argsMetadata.type) {
|
|
163
|
+
case EArgumentTypes.headers:
|
|
164
|
+
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
165
|
+
? headers
|
|
166
|
+
: await controllerActionArgumentsResolution(headers, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
|
|
167
|
+
break;
|
|
168
|
+
case EArgumentTypes.body:
|
|
169
|
+
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
170
|
+
? await request[argsMetadata.parser || "json"]()
|
|
171
|
+
: await controllerActionArgumentsResolution(await request[argsMetadata.parser || "json"](), argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
|
|
172
|
+
break;
|
|
173
|
+
case EArgumentTypes.params:
|
|
174
|
+
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
175
|
+
? params
|
|
176
|
+
: await controllerActionArgumentsResolution(params, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
|
|
177
|
+
break;
|
|
178
|
+
case EArgumentTypes.query:
|
|
179
|
+
controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
|
|
180
|
+
? query
|
|
181
|
+
: await controllerActionArgumentsResolution(query, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
148
184
|
}
|
|
149
185
|
}
|
|
150
|
-
|
|
186
|
+
const response = await handler.func(...controllerActionArguments);
|
|
187
|
+
if (response instanceof Response) {
|
|
188
|
+
return response;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return response;
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
return jsonErrorInfer(error);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
const end = performance.now();
|
|
198
|
+
const convertedPID = `${process.pid}`.yellow;
|
|
199
|
+
const convertedMethod = `${request.method.yellow}`.bgBlue;
|
|
200
|
+
const convertedReqIp = `${request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || "<Unknown>"}`.yellow;
|
|
201
|
+
const convertedTime = `${Math.round((end - start + Number.EPSILON) * 10 ** 2) / 10 ** 2}ms`.yellow;
|
|
202
|
+
allowLogsMethods.includes(request.method.toUpperCase()) &&
|
|
203
|
+
console.info(`PID: ${convertedPID} - Method: ${convertedMethod} - IP: ${convertedReqIp} - ${url.pathname.blue} - Time: ${convertedTime}`);
|
|
151
204
|
}
|
|
152
205
|
}
|
|
153
|
-
res.header("Access-Control-Allow-Origin", req.headers.origin || "*");
|
|
154
|
-
res.header("Access-Control-Allow-Headers", "*");
|
|
155
|
-
res.header("Access-Control-Allow-Credentials", "true");
|
|
156
|
-
res.header("Access-Control-Allow-Methods", allowMethods.join(", "));
|
|
157
|
-
next();
|
|
158
|
-
});
|
|
159
|
-
if (routers.length > 0) {
|
|
160
|
-
!metadata?.prefix ?
|
|
161
|
-
app.use(routers) : app.use(!metadata.prefix.startsWith("/") ?
|
|
162
|
-
`/${metadata.prefix}` : metadata.prefix, routers);
|
|
163
|
-
}
|
|
164
|
-
// Register error catcher
|
|
165
|
-
app.use(
|
|
166
|
-
// Error catcher
|
|
167
|
-
(err, req, res, next) => {
|
|
168
|
-
(0, http_1.errorInfer)(res, err);
|
|
169
|
-
if (!options?.debug) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
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);
|
|
175
206
|
});
|
|
176
|
-
return app;
|
|
177
207
|
};
|
|
178
|
-
|
|
179
|
-
exports.default = exports.BoolFactory;
|
|
208
|
+
export default BoolFactory;
|
package/dist/hooks/index.js
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Injector = exports.BoolFactory = void 0;
|
|
4
|
-
var factory_1 = require("./factory");
|
|
5
|
-
Object.defineProperty(exports, "BoolFactory", { enumerable: true, get: function () { return factory_1.BoolFactory; } });
|
|
6
|
-
var injector_1 = require("./injector");
|
|
7
|
-
Object.defineProperty(exports, "Injector", { enumerable: true, get: function () { return injector_1.Injector; } });
|
|
1
|
+
export { BoolFactory } from "./factory";
|
|
2
|
+
export { Injector } from "./injector";
|
package/dist/hooks/injector.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
require("reflect-metadata");
|
|
5
|
-
const decorators_1 = require("../decorators");
|
|
6
|
-
exports.Injector = new class {
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { injectableKey, injectKey } from "../decorators";
|
|
3
|
+
export const Injector = new class {
|
|
7
4
|
_mapper = new Map();
|
|
8
5
|
/**
|
|
9
6
|
*
|
|
@@ -14,15 +11,15 @@ exports.Injector = new class {
|
|
|
14
11
|
return this._mapper.get(classDefinition);
|
|
15
12
|
}
|
|
16
13
|
const ownMetadataKeys = Reflect.getMetadataKeys(classDefinition);
|
|
17
|
-
if (!ownMetadataKeys.includes(
|
|
14
|
+
if (!ownMetadataKeys.includes(injectableKey)) {
|
|
18
15
|
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
19
16
|
}
|
|
20
17
|
// Initialize dependencies injection
|
|
21
|
-
const dependencies = Reflect.getOwnMetadata(
|
|
22
|
-
const injections = dependencies.map(dependency =>
|
|
18
|
+
const dependencies = Reflect.getOwnMetadata(injectKey, classDefinition) || [];
|
|
19
|
+
const injections = dependencies.map(dependency => Injector.get(dependency));
|
|
23
20
|
const instance = new classDefinition(...injections);
|
|
24
21
|
this._mapper.set(classDefinition, instance);
|
|
25
22
|
return instance;
|
|
26
23
|
}
|
|
27
24
|
};
|
|
28
|
-
|
|
25
|
+
export default Injector;
|
|
@@ -31,7 +31,7 @@ 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] | string;
|
|
34
|
+
readonly message: (typeof httpClientErrors)[T] | string;
|
|
35
35
|
readonly data: K;
|
|
36
36
|
constructor({ httpCode, data, message }: {
|
|
37
37
|
httpCode: T;
|
package/dist/http/clientError.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpClientError = exports.httpClientErrors = void 0;
|
|
4
|
-
exports.httpClientErrors = Object.freeze({
|
|
1
|
+
export const httpClientErrors = Object.freeze({
|
|
5
2
|
400: "BAD_REQUEST",
|
|
6
3
|
401: "UNAUTHORIZED",
|
|
7
4
|
402: "PAYMENT_REQUIRED",
|
|
@@ -32,15 +29,14 @@ exports.httpClientErrors = Object.freeze({
|
|
|
32
29
|
431: "REQUEST_HEADER_FIELDS_TOO_LARGE",
|
|
33
30
|
451: "UNAVAILABLE_FOR_LEGAL_REASONS"
|
|
34
31
|
});
|
|
35
|
-
class HttpClientError extends Error {
|
|
32
|
+
export class HttpClientError extends Error {
|
|
36
33
|
httpCode;
|
|
37
34
|
message;
|
|
38
35
|
data;
|
|
39
36
|
constructor({ httpCode, data, message }) {
|
|
40
37
|
super();
|
|
41
38
|
this.httpCode = httpCode;
|
|
42
|
-
this.message = !message?.trim() ?
|
|
39
|
+
this.message = !message?.trim() ? httpClientErrors[httpCode] : message.trim();
|
|
43
40
|
this.data = data;
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
|
-
exports.HttpClientError = HttpClientError;
|
package/dist/http/index.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export type THttpMethods = {
|
|
2
|
+
GET: "GET";
|
|
3
|
+
HEAD: "HEAD";
|
|
4
|
+
POST: "POST";
|
|
5
|
+
PUT: "PUT";
|
|
6
|
+
DELETE: "DELETE";
|
|
7
|
+
CONNECT: "CONNECT";
|
|
8
|
+
OPTIONS: "OPTIONS";
|
|
9
|
+
TRACE: "TRACE";
|
|
10
|
+
PATCH: "PATCH";
|
|
11
|
+
};
|
|
12
|
+
export declare const jsonErrorInfer: (data: any) => import("undici-types").Response;
|
|
3
13
|
export * from "./clientError";
|
|
4
14
|
export * from "./serverError";
|
package/dist/http/index.js
CHANGED
|
@@ -1,80 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { HttpClientError } from "./clientError";
|
|
2
|
+
import { HttpServerError } from "./serverError";
|
|
3
|
+
export const jsonErrorInfer = (data) => {
|
|
4
|
+
const headers = new Headers();
|
|
5
|
+
headers.append("Content-Type", "application/json");
|
|
6
|
+
if (data instanceof HttpClientError || data instanceof HttpServerError) {
|
|
7
|
+
return new Response(JSON.stringify(data), {
|
|
8
|
+
status: data.httpCode,
|
|
9
|
+
statusText: data.message,
|
|
10
|
+
headers: headers
|
|
11
|
+
});
|
|
7
12
|
}
|
|
8
|
-
|
|
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
|
-
exports.errorInfer = void 0;
|
|
18
|
-
const clientError_1 = require("./clientError");
|
|
19
|
-
const serverError_1 = require("./serverError");
|
|
20
|
-
const errorInfer = (res, data) => {
|
|
21
|
-
if (res.headersSent) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
if (data instanceof clientError_1.HttpClientError) {
|
|
26
|
-
res.status(data.httpCode).json(data);
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
if (data instanceof serverError_1.HttpServerError) {
|
|
30
|
-
res.status(data.httpCode).json(data);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if (typeof data === "object") {
|
|
34
|
-
res.status(500).json({
|
|
35
|
-
httpCode: 500,
|
|
36
|
-
message: "INTERNAL SERVER ERROR",
|
|
37
|
-
data: !(data instanceof Error) ? data : {
|
|
38
|
-
message: data.message,
|
|
39
|
-
code: data.name,
|
|
40
|
-
cause: data.cause
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
13
|
+
return new Response(JSON.stringify((() => {
|
|
45
14
|
switch (typeof data) {
|
|
15
|
+
case "object":
|
|
16
|
+
return !(data instanceof Error)
|
|
17
|
+
? data
|
|
18
|
+
: {
|
|
19
|
+
message: data.message,
|
|
20
|
+
code: data.name,
|
|
21
|
+
cause: data.cause
|
|
22
|
+
};
|
|
46
23
|
case "string":
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
data: {
|
|
51
|
-
message: data
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
return;
|
|
24
|
+
return {
|
|
25
|
+
message: data
|
|
26
|
+
};
|
|
55
27
|
case "number":
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
data: {
|
|
60
|
-
code: data
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
return;
|
|
28
|
+
return {
|
|
29
|
+
code: data
|
|
30
|
+
};
|
|
64
31
|
default:
|
|
65
|
-
|
|
66
|
-
httpCode: 500,
|
|
67
|
-
message: "INTERNAL SERVER ERROR",
|
|
68
|
-
data: undefined
|
|
69
|
-
});
|
|
70
|
-
return;
|
|
32
|
+
return undefined;
|
|
71
33
|
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
34
|
+
})()), {
|
|
35
|
+
status: 500,
|
|
36
|
+
statusText: "INTERNAL SERVER ERROR",
|
|
37
|
+
headers: headers
|
|
38
|
+
});
|
|
77
39
|
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
__exportStar(require("./serverError"), exports);
|
|
40
|
+
export * from "./clientError";
|
|
41
|
+
export * from "./serverError";
|
package/dist/http/serverError.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpServerError = exports.httpServerErrors = void 0;
|
|
4
|
-
exports.httpServerErrors = Object.freeze({
|
|
1
|
+
export const httpServerErrors = Object.freeze({
|
|
5
2
|
500: "INTERNAL_SERVER_ERROR",
|
|
6
3
|
501: "NOT_IMPLEMENTED",
|
|
7
4
|
502: "BAD_GATEWAY",
|
|
@@ -14,15 +11,14 @@ exports.httpServerErrors = Object.freeze({
|
|
|
14
11
|
510: "NOT_EXTENDED",
|
|
15
12
|
511: "NETWORK_AUTHENTICATION_REQUIRED"
|
|
16
13
|
});
|
|
17
|
-
class HttpServerError extends Error {
|
|
14
|
+
export class HttpServerError extends Error {
|
|
18
15
|
httpCode;
|
|
19
16
|
message;
|
|
20
17
|
data;
|
|
21
18
|
constructor({ httpCode, data, message }) {
|
|
22
19
|
super();
|
|
23
20
|
this.httpCode = httpCode;
|
|
24
|
-
this.message = !message?.trim() ?
|
|
21
|
+
this.message = !message?.trim() ? httpServerErrors[httpCode] : message.trim();
|
|
25
22
|
this.data = data;
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
|
-
exports.HttpServerError = HttpServerError;
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
require("reflect-metadata");
|
|
18
|
-
__exportStar(require("./interfaces"), exports);
|
|
19
|
-
__exportStar(require("./hooks"), exports);
|
|
20
|
-
__exportStar(require("./decorators"), exports);
|
|
21
|
-
__exportStar(require("./http"), exports);
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
export * from "./interfaces";
|
|
3
|
+
export * from "./hooks";
|
|
4
|
+
export * from "./decorators";
|
|
5
|
+
export * from "./http";
|
package/dist/interfaces/index.js
CHANGED