jh-be-tools 1.0.88 → 1.0.89
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/package.json
CHANGED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { AxiosRequestConfig, AxiosResponse, AxiosHeaders } from 'axios';
|
|
2
|
-
import { RouteDef } from '../route-handler/routeCreation.js';
|
|
3
|
-
export interface QueryData {
|
|
4
|
-
params?: Record<string, string>;
|
|
5
|
-
query?: AxiosRequestConfig['params'];
|
|
6
|
-
headers?: Record<string, string> | AxiosHeaders;
|
|
7
|
-
body?: unknown;
|
|
8
|
-
}
|
|
9
|
-
export interface Request {
|
|
10
|
-
baseUrl: string;
|
|
11
|
-
definition: RouteDef;
|
|
12
|
-
serviceName: string;
|
|
13
|
-
queryData?: QueryData;
|
|
14
|
-
}
|
|
15
|
-
export interface ProcessingMethods {
|
|
16
|
-
pre?: (request: QueryData) => Promise<QueryData>;
|
|
17
|
-
post?: (response: AxiosResponse | InternalError) => Promise<AxiosResponse | InternalError>;
|
|
18
|
-
logger?: (value: string) => void;
|
|
19
|
-
debug?: boolean;
|
|
20
|
-
}
|
|
21
|
-
export interface InternalError {
|
|
22
|
-
serviceName: string;
|
|
23
|
-
status: number;
|
|
24
|
-
}
|
|
25
|
-
export declare const isInternalError: <T>(res: AxiosResponse<T> | InternalError) => res is InternalError;
|
|
26
|
-
export declare const isValidResponse: <T>(res: AxiosResponse<T> | InternalError) => res is AxiosResponse<T>;
|
|
27
|
-
export declare const request: <Response>(req: Request, processing?: ProcessingMethods) => Promise<Response>;
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosError } from 'axios';
|
|
2
|
-
import { getCorrelationId } from '../correlation-id/correlationContext.js';
|
|
3
|
-
export const isInternalError = (res) => {
|
|
4
|
-
return res.status >= 400;
|
|
5
|
-
};
|
|
6
|
-
export const isValidResponse = (res) => {
|
|
7
|
-
return res.status < 400;
|
|
8
|
-
};
|
|
9
|
-
export const request = async (req, processing) => {
|
|
10
|
-
const { baseUrl, definition, serviceName } = req;
|
|
11
|
-
let { queryData } = req;
|
|
12
|
-
let path = `${baseUrl}${definition.path}`;
|
|
13
|
-
let func;
|
|
14
|
-
if (processing?.pre) {
|
|
15
|
-
if (!queryData) {
|
|
16
|
-
queryData = {};
|
|
17
|
-
}
|
|
18
|
-
queryData = await processing.pre(queryData);
|
|
19
|
-
}
|
|
20
|
-
switch (definition.method) {
|
|
21
|
-
case 'get':
|
|
22
|
-
if (queryData?.body) {
|
|
23
|
-
throw Error('Get requests with body are not supported.');
|
|
24
|
-
}
|
|
25
|
-
func = axios.get;
|
|
26
|
-
break;
|
|
27
|
-
case 'post':
|
|
28
|
-
func = axios.post;
|
|
29
|
-
if (queryData && !queryData?.body) {
|
|
30
|
-
queryData.body = {};
|
|
31
|
-
}
|
|
32
|
-
break;
|
|
33
|
-
case 'put':
|
|
34
|
-
func = axios.put;
|
|
35
|
-
if (queryData && !queryData?.body) {
|
|
36
|
-
queryData.body = {};
|
|
37
|
-
}
|
|
38
|
-
break;
|
|
39
|
-
case 'delete':
|
|
40
|
-
if (queryData?.body) {
|
|
41
|
-
throw Error('Delete requests with body are not supported.');
|
|
42
|
-
}
|
|
43
|
-
func = axios.delete;
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
if (queryData?.params) {
|
|
47
|
-
path = replaceUrlParams(path, queryData.params);
|
|
48
|
-
}
|
|
49
|
-
let response;
|
|
50
|
-
try {
|
|
51
|
-
response = await baseQuery(func, path, queryData);
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
response = errorHandler(error, { serviceName, path, queryData, debug: processing?.debug, logger: processing?.logger });
|
|
55
|
-
}
|
|
56
|
-
if (processing?.post) {
|
|
57
|
-
response = await processing.post(response);
|
|
58
|
-
}
|
|
59
|
-
if (isInternalError(response)) {
|
|
60
|
-
throw response;
|
|
61
|
-
}
|
|
62
|
-
return response.data;
|
|
63
|
-
};
|
|
64
|
-
const baseQuery = async (func, path, req) => {
|
|
65
|
-
const correlationId = getCorrelationId();
|
|
66
|
-
const options = {
|
|
67
|
-
headers: {
|
|
68
|
-
'content-type': 'application/json',
|
|
69
|
-
'x-correlation-id': correlationId,
|
|
70
|
-
...req?.headers,
|
|
71
|
-
},
|
|
72
|
-
params: req?.query,
|
|
73
|
-
};
|
|
74
|
-
if (req?.body) {
|
|
75
|
-
return await func(path, req.body, options);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
return await func(path, options);
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
const errorHandler = (error, { serviceName, path, queryData, debug, logger }) => {
|
|
82
|
-
if (!(error instanceof AxiosError))
|
|
83
|
-
throw error;
|
|
84
|
-
const data = {
|
|
85
|
-
serviceName: serviceName,
|
|
86
|
-
status: error.status ?? 500,
|
|
87
|
-
request: {
|
|
88
|
-
path,
|
|
89
|
-
query: queryData?.query,
|
|
90
|
-
body: debug ? queryData?.body : undefined,
|
|
91
|
-
},
|
|
92
|
-
response: error.response ? {
|
|
93
|
-
status: error.response.status,
|
|
94
|
-
data: error.response.data,
|
|
95
|
-
headers: error.response.headers,
|
|
96
|
-
} : undefined,
|
|
97
|
-
};
|
|
98
|
-
if (logger) {
|
|
99
|
-
logger(JSON.stringify(data));
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
console.error(JSON.stringify(data));
|
|
103
|
-
}
|
|
104
|
-
return data;
|
|
105
|
-
};
|
|
106
|
-
const replaceUrlParams = (url, params) => {
|
|
107
|
-
return url.replace(/:([a-zA-Z0-9_]+)/g, (match, key) => {
|
|
108
|
-
return params[key] || match;
|
|
109
|
-
});
|
|
110
|
-
};
|