jh-be-tools 1.0.47 → 1.0.48
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/dist/api-request/apiRequest.d.ts +6 -2
- package/dist/api-request/apiRequest.js +17 -5
- package/dist/config/eslint.config.d.mts +2 -1
- package/dist/config/eslint.config.mjs +4 -4
- package/dist/create-package-index-files/createPackageIndexFiles.js +0 -0
- package/dist/route-handler/routeCreation.d.ts +2 -1
- package/dist/route-handler/routeCreation.js +2 -1
- package/dist/route-handler/routeHandler.js +29 -16
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxiosError, AxiosRequestConfig, AxiosResponseHeaders } from 'axios';
|
|
1
|
+
import { AxiosError, AxiosRequestConfig, AxiosResponseHeaders, AxiosResponse } from 'axios';
|
|
2
2
|
import { RouteDef } from '../route-handler/routeCreation';
|
|
3
3
|
export interface QueryData {
|
|
4
4
|
params?: Record<string, string>;
|
|
@@ -12,9 +12,13 @@ export interface Request {
|
|
|
12
12
|
serviceName: string;
|
|
13
13
|
queryData?: QueryData;
|
|
14
14
|
}
|
|
15
|
+
export interface ProcessingMethods {
|
|
16
|
+
pre?: (request: QueryData) => Promise<QueryData>;
|
|
17
|
+
post?: (response: AxiosResponse) => AxiosResponse;
|
|
18
|
+
}
|
|
15
19
|
export interface Error {
|
|
16
20
|
serviceName: string;
|
|
17
21
|
status: number;
|
|
18
22
|
error: AxiosError;
|
|
19
23
|
}
|
|
20
|
-
export declare const request: <Response>(req: Request) => Promise<Response>;
|
|
24
|
+
export declare const request: <Response>(req: Request, processing?: ProcessingMethods) => Promise<Response>;
|
|
@@ -35,10 +35,17 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.request = void 0;
|
|
37
37
|
const axios_1 = __importStar(require("axios"));
|
|
38
|
-
const request = async (req) => {
|
|
39
|
-
const { baseUrl, definition,
|
|
38
|
+
const request = async (req, processing) => {
|
|
39
|
+
const { baseUrl, definition, serviceName } = req;
|
|
40
|
+
let { queryData } = req;
|
|
40
41
|
let path = `${baseUrl}${definition.path}`;
|
|
41
42
|
let func;
|
|
43
|
+
if (processing?.pre) {
|
|
44
|
+
if (!queryData) {
|
|
45
|
+
queryData = {};
|
|
46
|
+
}
|
|
47
|
+
queryData = await processing.pre(queryData);
|
|
48
|
+
}
|
|
42
49
|
switch (definition.method) {
|
|
43
50
|
case 'get':
|
|
44
51
|
if (queryData?.body) {
|
|
@@ -68,12 +75,17 @@ const request = async (req) => {
|
|
|
68
75
|
if (req.queryData && req.queryData.params) {
|
|
69
76
|
path = replaceUrlParams(path, req.queryData.params);
|
|
70
77
|
}
|
|
78
|
+
let response;
|
|
71
79
|
try {
|
|
72
|
-
|
|
80
|
+
response = await baseQuery(func, path, queryData);
|
|
73
81
|
}
|
|
74
82
|
catch (error) {
|
|
75
83
|
return errorHandler(error, serviceName);
|
|
76
84
|
}
|
|
85
|
+
if (processing?.post) {
|
|
86
|
+
response = processing.post(response);
|
|
87
|
+
}
|
|
88
|
+
return response.data;
|
|
77
89
|
};
|
|
78
90
|
exports.request = request;
|
|
79
91
|
const baseQuery = async (func, path, req) => {
|
|
@@ -82,10 +94,10 @@ const baseQuery = async (func, path, req) => {
|
|
|
82
94
|
params: req?.query,
|
|
83
95
|
};
|
|
84
96
|
if (req?.body) {
|
|
85
|
-
return (await func(path, req.body, options))
|
|
97
|
+
return (await func(path, req.body, options));
|
|
86
98
|
}
|
|
87
99
|
else {
|
|
88
|
-
return (await func(path, options))
|
|
100
|
+
return (await func(path, options));
|
|
89
101
|
}
|
|
90
102
|
};
|
|
91
103
|
const errorHandler = (error, serviceName) => {
|
|
@@ -104,10 +104,11 @@ declare const _default: (import("eslint").Linter.Config<import("eslint").Linter.
|
|
|
104
104
|
})[];
|
|
105
105
|
'import/export': string;
|
|
106
106
|
quotes: string[];
|
|
107
|
-
|
|
107
|
+
'prettier/prettier': (string | {
|
|
108
108
|
singleQuote: boolean;
|
|
109
109
|
semi: boolean;
|
|
110
110
|
useTabs: boolean;
|
|
111
|
+
printWidth: number;
|
|
111
112
|
})[];
|
|
112
113
|
semi: string[];
|
|
113
114
|
indent: string[];
|
|
@@ -58,10 +58,10 @@ export default [
|
|
|
58
58
|
},
|
|
59
59
|
],
|
|
60
60
|
'import/export': 'off',
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
quotes: ['off'],
|
|
62
|
+
'prettier/prettier': ['error', { singleQuote: true, semi: false, useTabs: true, printWidth: 150 }],
|
|
63
|
+
semi: ['error', 'never'],
|
|
64
|
+
indent: ['error', 'tab'],
|
|
65
65
|
},
|
|
66
66
|
},
|
|
67
67
|
{
|
|
File without changes
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { Router } from 'express';
|
|
1
|
+
import { Router, RequestHandler } from 'express';
|
|
2
2
|
import { RouteSchema } from './routeHandler';
|
|
3
3
|
export type RouteMethods = 'get' | 'post' | 'put' | 'delete';
|
|
4
4
|
export type RouteDef = {
|
|
5
5
|
method: RouteMethods;
|
|
6
6
|
path: string;
|
|
7
7
|
routeSchema: RouteSchema;
|
|
8
|
+
middleware?: RequestHandler[];
|
|
8
9
|
};
|
|
9
10
|
export type RouteData = [RouteDef, (req?: any) => any];
|
|
10
11
|
export declare const createRoutes: (routeData: RouteData[], router: Router) => void;
|
|
@@ -4,7 +4,8 @@ exports.createRoutes = void 0;
|
|
|
4
4
|
const routeHandler_1 = require("./routeHandler");
|
|
5
5
|
const createRoute = (meta, route) => {
|
|
6
6
|
return (app) => {
|
|
7
|
-
|
|
7
|
+
const middlewares = meta.middleware ?? [];
|
|
8
|
+
app[meta.method](meta.path, ...middlewares, async (req, res) => {
|
|
8
9
|
await (0, routeHandler_1.routeHandler)(req, res, route, meta.routeSchema);
|
|
9
10
|
});
|
|
10
11
|
};
|
|
@@ -2,19 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.routeHandler = void 0;
|
|
4
4
|
const routeHandler = async (req, res, fun, schema) => {
|
|
5
|
-
if (schema
|
|
6
|
-
|
|
7
|
-
await schema.request.parseAsync({
|
|
8
|
-
body: req.body,
|
|
9
|
-
query: req.query,
|
|
10
|
-
params: req.params,
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
console.log(JSON.stringify(error));
|
|
15
|
-
return res.status(400).json(error.errors);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
5
|
+
if (!(await parseRequest(schema, req, res)))
|
|
6
|
+
return;
|
|
18
7
|
let data;
|
|
19
8
|
let status;
|
|
20
9
|
try {
|
|
@@ -29,15 +18,39 @@ const routeHandler = async (req, res, fun, schema) => {
|
|
|
29
18
|
}
|
|
30
19
|
return res.status(500).json(error);
|
|
31
20
|
}
|
|
21
|
+
if (!(await parseResponse(schema, res, data)))
|
|
22
|
+
return;
|
|
23
|
+
res.status(status).json({ data });
|
|
24
|
+
};
|
|
25
|
+
exports.routeHandler = routeHandler;
|
|
26
|
+
const parseRequest = async (schema, req, res) => {
|
|
27
|
+
if (schema.request) {
|
|
28
|
+
try {
|
|
29
|
+
await schema.request.parseAsync({
|
|
30
|
+
body: req.body,
|
|
31
|
+
query: req.query,
|
|
32
|
+
params: req.params,
|
|
33
|
+
headers: req.headers,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
console.log(JSON.stringify(error));
|
|
38
|
+
res.status(400).json(error.errors);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
};
|
|
44
|
+
const parseResponse = async (schema, res, data) => {
|
|
32
45
|
if (schema.response) {
|
|
33
46
|
try {
|
|
34
47
|
await schema.response.parseAsync({ data });
|
|
35
48
|
}
|
|
36
49
|
catch (error) {
|
|
37
50
|
console.log(JSON.stringify(error));
|
|
38
|
-
|
|
51
|
+
res.status(500).json(error.errors);
|
|
52
|
+
return false;
|
|
39
53
|
}
|
|
40
54
|
}
|
|
41
|
-
|
|
55
|
+
return true;
|
|
42
56
|
};
|
|
43
|
-
exports.routeHandler = routeHandler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/api-request/apirequest.ts","../src/config/eslint.config.mjs","../src/
|
|
1
|
+
{"root":["../src/index.ts","../src/api-request/apirequest.ts","../src/config/eslint.config.mjs","../src/create-package-index-files/createpackageindexfiles.ts","../src/route-handler/routecreation.ts","../src/route-handler/routehandler.ts"],"version":"5.8.2"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jh-be-tools",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.48",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,18 +21,18 @@
|
|
|
21
21
|
"zod": "^3.24.2"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"eslint": "^
|
|
24
|
+
"@eslint/compat": "^1.2.4",
|
|
25
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
26
|
+
"@eslint/js": "^9.23.0",
|
|
25
27
|
"@typescript-eslint/eslint-plugin": "^8.27.0",
|
|
26
28
|
"@typescript-eslint/parser": "^8.27.0",
|
|
29
|
+
"eslint": "^9.23.0",
|
|
27
30
|
"eslint-config-prettier": "^10.1.1",
|
|
28
31
|
"eslint-plugin-import": "^2.31.0",
|
|
29
32
|
"eslint-plugin-n": "^17.16.2",
|
|
30
33
|
"eslint-plugin-node": "^11.1.0",
|
|
31
34
|
"eslint-plugin-prettier": "^5.2.1",
|
|
32
|
-
"eslint-plugin-unused-imports": "^4.1.4"
|
|
33
|
-
"@eslint/compat": "^1.2.4",
|
|
34
|
-
"@eslint/eslintrc": "^3.3.1",
|
|
35
|
-
"@eslint/js": "^9.23.0"
|
|
35
|
+
"eslint-plugin-unused-imports": "^4.1.4"
|
|
36
36
|
},
|
|
37
37
|
"bin": {
|
|
38
38
|
"createPackageIndexFiles": "./dist/create-package-index-files/createPackageIndexFiles.js"
|