namirasoft-core 1.4.92 → 1.4.94
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/BaseServer.d.ts +18 -8
- package/dist/BaseServer.js +36 -29
- package/dist/BaseServer.js.map +1 -1
- package/package.json +1 -1
- package/src/BaseServer.ts +53 -35
package/dist/BaseServer.d.ts
CHANGED
|
@@ -1,45 +1,55 @@
|
|
|
1
1
|
import { AxiosRequestConfig, AxiosResponse } from "axios";
|
|
2
2
|
import { ParsedNameValue } from "./ParsedNameValue";
|
|
3
|
+
export interface BaseServerConfig<ReqData> extends AxiosRequestConfig<ReqData> {
|
|
4
|
+
sign?: {
|
|
5
|
+
header: string;
|
|
6
|
+
key: string;
|
|
7
|
+
};
|
|
8
|
+
onBeforeRequest?: (url: string, config?: AxiosRequestConfig) => void;
|
|
9
|
+
onAfterRequest?: (url: string, res: AxiosResponse) => void;
|
|
10
|
+
onError?: (error: Error) => void;
|
|
11
|
+
error_suppress?: boolean;
|
|
12
|
+
error_verbose?: boolean;
|
|
13
|
+
}
|
|
3
14
|
export declare abstract class BaseServer {
|
|
4
15
|
static isErrorCode(e: any, code: number): boolean;
|
|
5
16
|
protected base_url: string;
|
|
6
17
|
private onBeforeRequests;
|
|
7
18
|
private onAfterRequests;
|
|
8
19
|
protected abstract onError(error: Error): void;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
verboseOnErrors: boolean;
|
|
20
|
+
error_suppress?: boolean;
|
|
21
|
+
error_verbose?: boolean;
|
|
12
22
|
constructor(base_url: string);
|
|
13
23
|
addOnBeforeRequest(onBeforeRequest: (url: string, config?: AxiosRequestConfig) => void): void;
|
|
14
24
|
addOnAfterRequest(onAfterRequest: (url: string, res: AxiosResponse) => void): void;
|
|
15
25
|
private _request;
|
|
16
26
|
_get<ResData = any, ReqData = any>(sub: string, query?: {
|
|
17
27
|
[name: string]: ParsedNameValue;
|
|
18
|
-
}, config?:
|
|
28
|
+
}, config?: BaseServerConfig<ReqData>): Promise<{
|
|
19
29
|
response: AxiosResponse<ResData>;
|
|
20
30
|
data: ResData;
|
|
21
31
|
}>;
|
|
22
32
|
_post<ResData = any, ReqData = any>(sub: string, query?: {
|
|
23
33
|
[name: string]: ParsedNameValue;
|
|
24
|
-
}, data?: ReqData, config?:
|
|
34
|
+
}, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{
|
|
25
35
|
response: AxiosResponse<ResData>;
|
|
26
36
|
data: ResData;
|
|
27
37
|
}>;
|
|
28
38
|
_put<ResData = any, ReqData = any>(sub: string, query?: {
|
|
29
39
|
[name: string]: ParsedNameValue;
|
|
30
|
-
}, data?: ReqData, config?:
|
|
40
|
+
}, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{
|
|
31
41
|
response: AxiosResponse<ResData>;
|
|
32
42
|
data: ResData;
|
|
33
43
|
}>;
|
|
34
44
|
_patch<ResData = any, ReqData = any>(sub: string, query?: {
|
|
35
45
|
[name: string]: ParsedNameValue;
|
|
36
|
-
}, data?: ReqData, config?:
|
|
46
|
+
}, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{
|
|
37
47
|
response: AxiosResponse<ResData>;
|
|
38
48
|
data: ResData;
|
|
39
49
|
}>;
|
|
40
50
|
_delete<ResData = any, ReqData = any>(sub: string, query?: {
|
|
41
51
|
[name: string]: ParsedNameValue;
|
|
42
|
-
}, config?:
|
|
52
|
+
}, config?: BaseServerConfig<ReqData>): Promise<{
|
|
43
53
|
response: AxiosResponse<ResData>;
|
|
44
54
|
data: ResData;
|
|
45
55
|
}>;
|
package/dist/BaseServer.js
CHANGED
|
@@ -35,10 +35,10 @@ class BaseServer {
|
|
|
35
35
|
constructor(base_url) {
|
|
36
36
|
this.onBeforeRequests = [];
|
|
37
37
|
this.onAfterRequests = [];
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
38
|
+
this.error_suppress = false;
|
|
39
|
+
this.error_verbose = false;
|
|
40
40
|
this.base_url = base_url;
|
|
41
|
-
this.
|
|
41
|
+
this.error_verbose = new EnvService_1.EnvService("BASESERVER_ERROR_VERBOSE", false).getBoolean(false);
|
|
42
42
|
}
|
|
43
43
|
addOnBeforeRequest(onBeforeRequest) {
|
|
44
44
|
this.onBeforeRequests.push(onBeforeRequest);
|
|
@@ -46,18 +46,23 @@ class BaseServer {
|
|
|
46
46
|
addOnAfterRequest(onAfterRequest) {
|
|
47
47
|
this.onAfterRequests.push(onAfterRequest);
|
|
48
48
|
}
|
|
49
|
-
_request(onRequest, sub, query, data, config
|
|
49
|
+
_request(onRequest, sub, query, data, config) {
|
|
50
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
var _a;
|
|
51
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
52
52
|
let url = URLOperation_1.URLOperation.getLink(this.base_url, sub, query);
|
|
53
53
|
if (!config)
|
|
54
|
-
config = {
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
config = {};
|
|
55
|
+
if (!config.headers)
|
|
56
|
+
config.headers = {};
|
|
57
|
+
if (config.withCredentials == null)
|
|
58
|
+
config.withCredentials = true;
|
|
59
|
+
if (config.sign)
|
|
60
|
+
if ((_a = config.sign) === null || _a === void 0 ? void 0 : _a.header)
|
|
61
|
+
if ((_b = config.sign) === null || _b === void 0 ? void 0 : _b.key)
|
|
62
|
+
if (data)
|
|
63
|
+
config.headers[config.sign.header] = HashOperation_1.HashOperation.SHA256Secret(config.sign.key, data);
|
|
60
64
|
try {
|
|
65
|
+
(_c = config.onBeforeRequest) === null || _c === void 0 ? void 0 : _c.call(config, url, config);
|
|
61
66
|
this.onBeforeRequests.forEach(onBeforeRequest => {
|
|
62
67
|
onBeforeRequest(url, config);
|
|
63
68
|
});
|
|
@@ -65,30 +70,32 @@ class BaseServer {
|
|
|
65
70
|
this.onAfterRequests.forEach(onAfterRequest => {
|
|
66
71
|
onAfterRequest(url, response);
|
|
67
72
|
});
|
|
73
|
+
(_d = config.onAfterRequest) === null || _d === void 0 ? void 0 : _d.call(config, url, response);
|
|
68
74
|
return { response, data: response.data };
|
|
69
75
|
}
|
|
70
76
|
catch (error) {
|
|
77
|
+
let onError = (_e = config.onError) !== null && _e !== void 0 ? _e : this.onError;
|
|
78
|
+
let error_verbose = (_f = config.error_verbose) !== null && _f !== void 0 ? _f : this.error_verbose;
|
|
79
|
+
let error_suppress = (_g = config.error_suppress) !== null && _g !== void 0 ? _g : this.error_suppress;
|
|
71
80
|
if (error instanceof Error) {
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.onError(error);
|
|
77
|
-
}
|
|
81
|
+
if (error_verbose)
|
|
82
|
+
error.message = `Error calling url '${url}' ` + error.message;
|
|
83
|
+
if (onError)
|
|
84
|
+
onError(error);
|
|
78
85
|
else {
|
|
79
86
|
ConsoleOperation_1.ConsoleOperation.warning("onError function has not been properly set.");
|
|
80
87
|
ConsoleOperation_1.ConsoleOperation.error(error === null || error === void 0 ? void 0 : error.message);
|
|
81
88
|
}
|
|
82
89
|
if (axios_1.default.isAxiosError(error))
|
|
83
|
-
if ((
|
|
84
|
-
if (!
|
|
85
|
-
if (
|
|
90
|
+
if ((_h = error === null || error === void 0 ? void 0 : error.response) === null || _h === void 0 ? void 0 : _h.data)
|
|
91
|
+
if (!error_suppress) {
|
|
92
|
+
if (error_suppress)
|
|
86
93
|
ErrorOperation_1.ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
|
|
87
94
|
else
|
|
88
95
|
ErrorOperation_1.ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
|
|
89
96
|
}
|
|
90
97
|
}
|
|
91
|
-
if (!
|
|
98
|
+
if (!error_suppress)
|
|
92
99
|
throw error;
|
|
93
100
|
}
|
|
94
101
|
return {};
|
|
@@ -101,32 +108,32 @@ class BaseServer {
|
|
|
101
108
|
}), sub, query, undefined, config);
|
|
102
109
|
});
|
|
103
110
|
}
|
|
104
|
-
_post(sub, query, data, config
|
|
111
|
+
_post(sub, query, data, config) {
|
|
105
112
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
113
|
return yield this._request((url, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
107
114
|
return yield axios_1.default.post(url, data, config);
|
|
108
|
-
}), sub, query, data, config
|
|
115
|
+
}), sub, query, data, config);
|
|
109
116
|
});
|
|
110
117
|
}
|
|
111
|
-
_put(sub, query, data, config
|
|
118
|
+
_put(sub, query, data, config) {
|
|
112
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
113
120
|
return yield this._request((url, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
114
121
|
return yield axios_1.default.put(url, data, config);
|
|
115
|
-
}), sub, query, data, config
|
|
122
|
+
}), sub, query, data, config);
|
|
116
123
|
});
|
|
117
124
|
}
|
|
118
|
-
_patch(sub, query, data, config
|
|
125
|
+
_patch(sub, query, data, config) {
|
|
119
126
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
127
|
return yield this._request((url, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
121
128
|
return yield axios_1.default.patch(url, data, config);
|
|
122
|
-
}), sub, query, data, config
|
|
129
|
+
}), sub, query, data, config);
|
|
123
130
|
});
|
|
124
131
|
}
|
|
125
|
-
_delete(sub, query, config
|
|
132
|
+
_delete(sub, query, config) {
|
|
126
133
|
return __awaiter(this, void 0, void 0, function* () {
|
|
127
134
|
return yield this._request((url, _, config) => __awaiter(this, void 0, void 0, function* () {
|
|
128
135
|
return yield axios_1.default.delete(url, config);
|
|
129
|
-
}), sub, query, undefined, config
|
|
136
|
+
}), sub, query, undefined, config);
|
|
130
137
|
});
|
|
131
138
|
}
|
|
132
139
|
}
|
package/dist/BaseServer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseServer.js","sourceRoot":"","sources":["../src/BaseServer.ts"],"names":[],"mappings":";;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EAAE,MAAkC,EAAE,EAAE;gBAEhG,OAAO,MAAM,eAAK,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;YAChD,CAAC,CAAA,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QACjC,CAAC;KAAA;IACY,OAAO,CAA+B,GAAW,EAAE,KAA2C,EAAE,MAAkC;;YAE3I,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,CAAO,GAAW,EAAE,CAAO,EAAE,MAAkC,EAAE,EAAE;gBAE1F,OAAO,MAAM,eAAK,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YAC3C,CAAC,CAAA,EAAE,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;KAAA;CACJ;AAjID,gCAiIC"}
|
package/package.json
CHANGED
package/src/BaseServer.ts
CHANGED
|
@@ -7,6 +7,18 @@ import { ConsoleOperation } from "./ConsoleOperation";
|
|
|
7
7
|
import { EnvService } from "./EnvService";
|
|
8
8
|
import { HTTPError } from "./HTTPError";
|
|
9
9
|
|
|
10
|
+
export interface BaseServerConfig<ReqData> extends AxiosRequestConfig<ReqData>
|
|
11
|
+
{
|
|
12
|
+
sign?: {
|
|
13
|
+
header: string;
|
|
14
|
+
key: string;
|
|
15
|
+
}
|
|
16
|
+
onBeforeRequest?: (url: string, config?: AxiosRequestConfig) => void;
|
|
17
|
+
onAfterRequest?: (url: string, res: AxiosResponse) => void;
|
|
18
|
+
onError?: (error: Error) => void;
|
|
19
|
+
error_suppress?: boolean;
|
|
20
|
+
error_verbose?: boolean;
|
|
21
|
+
}
|
|
10
22
|
export abstract class BaseServer
|
|
11
23
|
{
|
|
12
24
|
public static isErrorCode(e: any, code: number): boolean
|
|
@@ -25,14 +37,13 @@ export abstract class BaseServer
|
|
|
25
37
|
private onBeforeRequests: ((url: string, config?: AxiosRequestConfig) => void)[] = [];
|
|
26
38
|
private onAfterRequests: ((url: string, res: AxiosResponse) => void)[] = [];
|
|
27
39
|
protected abstract onError(error: Error): void;
|
|
28
|
-
public
|
|
29
|
-
public
|
|
30
|
-
public verboseOnErrors: boolean;
|
|
40
|
+
public error_suppress?: boolean = false;
|
|
41
|
+
public error_verbose?: boolean = false;
|
|
31
42
|
|
|
32
43
|
constructor(base_url: string)
|
|
33
44
|
{
|
|
34
45
|
this.base_url = base_url;
|
|
35
|
-
this.
|
|
46
|
+
this.error_verbose = new EnvService("BASESERVER_ERROR_VERBOSE", false).getBoolean(false);
|
|
36
47
|
}
|
|
37
48
|
public addOnBeforeRequest(onBeforeRequest: (url: string, config?: AxiosRequestConfig) => void)
|
|
38
49
|
{
|
|
@@ -42,18 +53,24 @@ export abstract class BaseServer
|
|
|
42
53
|
{
|
|
43
54
|
this.onAfterRequests.push(onAfterRequest);
|
|
44
55
|
}
|
|
45
|
-
private async _request<ResData = any, ReqData = any>(onRequest: (url: string, data?: any, config?:
|
|
56
|
+
private async _request<ResData = any, ReqData = any>(onRequest: (url: string, data?: any, config?: BaseServerConfig<ReqData>) => Promise<AxiosResponse<ResData>>, sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
46
57
|
{
|
|
47
58
|
let url: string = URLOperation.getLink(this.base_url, sub, query);
|
|
48
59
|
if (!config)
|
|
49
|
-
config = {
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
60
|
+
config = {};
|
|
61
|
+
if (!config.headers)
|
|
62
|
+
config.headers = {};
|
|
63
|
+
if (config.withCredentials == null)
|
|
64
|
+
config.withCredentials = true;
|
|
65
|
+
|
|
66
|
+
if (config.sign)
|
|
67
|
+
if (config.sign?.header)
|
|
68
|
+
if (config.sign?.key)
|
|
69
|
+
if (data)
|
|
70
|
+
config.headers[config.sign.header] = HashOperation.SHA256Secret(config.sign.key, data);
|
|
55
71
|
try
|
|
56
72
|
{
|
|
73
|
+
config.onBeforeRequest?.(url, config);
|
|
57
74
|
this.onBeforeRequests.forEach(onBeforeRequest =>
|
|
58
75
|
{
|
|
59
76
|
onBeforeRequest(url, config);
|
|
@@ -63,18 +80,19 @@ export abstract class BaseServer
|
|
|
63
80
|
{
|
|
64
81
|
onAfterRequest(url, response);
|
|
65
82
|
});
|
|
83
|
+
config.onAfterRequest?.(url, response);
|
|
66
84
|
return { response, data: response.data };
|
|
67
85
|
} catch (error)
|
|
68
86
|
{
|
|
87
|
+
let onError = config.onError ?? this.onError;
|
|
88
|
+
let error_verbose = config.error_verbose ?? this.error_verbose;
|
|
89
|
+
let error_suppress = config.error_suppress ?? this.error_suppress;
|
|
69
90
|
if (error instanceof Error)
|
|
70
91
|
{
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (!this.suppressOnErrors)
|
|
76
|
-
this.onError(error);
|
|
77
|
-
}
|
|
92
|
+
if (error_verbose)
|
|
93
|
+
error.message = `Error calling url '${url}' ` + error.message;
|
|
94
|
+
if (onError)
|
|
95
|
+
onError(error);
|
|
78
96
|
else
|
|
79
97
|
{
|
|
80
98
|
ConsoleOperation.warning("onError function has not been properly set.");
|
|
@@ -82,52 +100,52 @@ export abstract class BaseServer
|
|
|
82
100
|
}
|
|
83
101
|
if (axios.isAxiosError(error))
|
|
84
102
|
if (error?.response?.data)
|
|
85
|
-
if (!
|
|
103
|
+
if (!error_suppress)
|
|
86
104
|
{
|
|
87
|
-
if (
|
|
105
|
+
if (error_suppress)
|
|
88
106
|
ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
|
|
89
107
|
else
|
|
90
108
|
ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
|
|
91
109
|
}
|
|
92
110
|
}
|
|
93
|
-
if (!
|
|
111
|
+
if (!error_suppress)
|
|
94
112
|
throw error;
|
|
95
113
|
}
|
|
96
114
|
return {} as any;
|
|
97
115
|
}
|
|
98
|
-
public async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?:
|
|
116
|
+
public async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
99
117
|
{
|
|
100
|
-
return await this._request(async (url: string, _?: any, config?:
|
|
118
|
+
return await this._request(async (url: string, _?: any, config?: BaseServerConfig<ReqData>) =>
|
|
101
119
|
{
|
|
102
120
|
return await axios.get(url, config);
|
|
103
121
|
}, sub, query, undefined, config);
|
|
104
122
|
}
|
|
105
|
-
public async _post<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?:
|
|
123
|
+
public async _post<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
106
124
|
{
|
|
107
|
-
return await this._request(async (url: string, data: ReqData, config?:
|
|
125
|
+
return await this._request(async (url: string, data: ReqData, config?: BaseServerConfig<ReqData>) =>
|
|
108
126
|
{
|
|
109
127
|
return await axios.post(url, data, config);
|
|
110
|
-
}, sub, query, data, config
|
|
128
|
+
}, sub, query, data, config);
|
|
111
129
|
}
|
|
112
|
-
public async _put<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?:
|
|
130
|
+
public async _put<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
113
131
|
{
|
|
114
|
-
return await this._request(async (url: string, data: ReqData, config?:
|
|
132
|
+
return await this._request(async (url: string, data: ReqData, config?: BaseServerConfig<ReqData>) =>
|
|
115
133
|
{
|
|
116
134
|
return await axios.put(url, data, config);
|
|
117
|
-
}, sub, query, data, config
|
|
135
|
+
}, sub, query, data, config);
|
|
118
136
|
}
|
|
119
|
-
public async _patch<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?:
|
|
137
|
+
public async _patch<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
120
138
|
{
|
|
121
|
-
return await this._request(async (url: string, data: ReqData, config?:
|
|
139
|
+
return await this._request(async (url: string, data: ReqData, config?: BaseServerConfig<ReqData>) =>
|
|
122
140
|
{
|
|
123
141
|
return await axios.patch(url, data, config);
|
|
124
|
-
}, sub, query, data, config
|
|
142
|
+
}, sub, query, data, config);
|
|
125
143
|
}
|
|
126
|
-
public async _delete<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?:
|
|
144
|
+
public async _delete<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
127
145
|
{
|
|
128
|
-
return await this._request(async (url: string, _?: any, config?:
|
|
146
|
+
return await this._request(async (url: string, _?: any, config?: BaseServerConfig<ReqData>) =>
|
|
129
147
|
{
|
|
130
148
|
return await axios.delete(url, config);
|
|
131
|
-
}, sub, query, undefined, config
|
|
149
|
+
}, sub, query, undefined, config);
|
|
132
150
|
}
|
|
133
151
|
}
|