namirasoft-core 1.4.91 → 1.4.93
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 +29 -25
- package/dist/BaseServer.js.map +1 -1
- package/package.json +3 -3
- package/src/BaseServer.ts +45 -30
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,19 @@ 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 = { headers: {} };
|
|
55
|
-
if (
|
|
56
|
-
if (
|
|
54
|
+
config = { headers: {}, withCredentials: true };
|
|
55
|
+
if ((_a = config.sign) === null || _a === void 0 ? void 0 : _a.header)
|
|
56
|
+
if ((_b = config.sign) === null || _b === void 0 ? void 0 : _b.key)
|
|
57
57
|
if (data)
|
|
58
58
|
if (config === null || config === void 0 ? void 0 : config.headers)
|
|
59
|
-
config.headers[
|
|
59
|
+
config.headers[config.sign.header] = HashOperation_1.HashOperation.SHA256Secret(config.sign.key, data);
|
|
60
60
|
try {
|
|
61
|
+
(_c = config.onBeforeRequest) === null || _c === void 0 ? void 0 : _c.call(config, url, config);
|
|
61
62
|
this.onBeforeRequests.forEach(onBeforeRequest => {
|
|
62
63
|
onBeforeRequest(url, config);
|
|
63
64
|
});
|
|
@@ -65,30 +66,33 @@ class BaseServer {
|
|
|
65
66
|
this.onAfterRequests.forEach(onAfterRequest => {
|
|
66
67
|
onAfterRequest(url, response);
|
|
67
68
|
});
|
|
69
|
+
(_d = config.onAfterRequest) === null || _d === void 0 ? void 0 : _d.call(config, url, response);
|
|
68
70
|
return { response, data: response.data };
|
|
69
71
|
}
|
|
70
72
|
catch (error) {
|
|
73
|
+
let onError = (_e = config.onError) !== null && _e !== void 0 ? _e : this.onError;
|
|
74
|
+
let error_verbose = (_f = config.error_verbose) !== null && _f !== void 0 ? _f : this.error_verbose;
|
|
75
|
+
let error_suppress = (_g = config.error_suppress) !== null && _g !== void 0 ? _g : this.error_suppress;
|
|
71
76
|
if (error instanceof Error) {
|
|
72
|
-
if (
|
|
73
|
-
if (
|
|
77
|
+
if (onError) {
|
|
78
|
+
if (error_verbose)
|
|
74
79
|
error.message = `Error calling url '${url}' ` + error.message;
|
|
75
|
-
|
|
76
|
-
this.onError(error);
|
|
80
|
+
onError(error);
|
|
77
81
|
}
|
|
78
82
|
else {
|
|
79
83
|
ConsoleOperation_1.ConsoleOperation.warning("onError function has not been properly set.");
|
|
80
84
|
ConsoleOperation_1.ConsoleOperation.error(error === null || error === void 0 ? void 0 : error.message);
|
|
81
85
|
}
|
|
82
86
|
if (axios_1.default.isAxiosError(error))
|
|
83
|
-
if ((
|
|
84
|
-
if (!
|
|
85
|
-
if (
|
|
87
|
+
if ((_h = error === null || error === void 0 ? void 0 : error.response) === null || _h === void 0 ? void 0 : _h.data)
|
|
88
|
+
if (!error_suppress) {
|
|
89
|
+
if (error_suppress)
|
|
86
90
|
ErrorOperation_1.ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
|
|
87
91
|
else
|
|
88
92
|
ErrorOperation_1.ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
|
|
89
93
|
}
|
|
90
94
|
}
|
|
91
|
-
if (!
|
|
95
|
+
if (!error_suppress)
|
|
92
96
|
throw error;
|
|
93
97
|
}
|
|
94
98
|
return {};
|
|
@@ -101,32 +105,32 @@ class BaseServer {
|
|
|
101
105
|
}), sub, query, undefined, config);
|
|
102
106
|
});
|
|
103
107
|
}
|
|
104
|
-
_post(sub, query, data, config
|
|
108
|
+
_post(sub, query, data, config) {
|
|
105
109
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
110
|
return yield this._request((url, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
107
111
|
return yield axios_1.default.post(url, data, config);
|
|
108
|
-
}), sub, query, data, config
|
|
112
|
+
}), sub, query, data, config);
|
|
109
113
|
});
|
|
110
114
|
}
|
|
111
|
-
_put(sub, query, data, config
|
|
115
|
+
_put(sub, query, data, config) {
|
|
112
116
|
return __awaiter(this, void 0, void 0, function* () {
|
|
113
117
|
return yield this._request((url, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
114
118
|
return yield axios_1.default.put(url, data, config);
|
|
115
|
-
}), sub, query, data, config
|
|
119
|
+
}), sub, query, data, config);
|
|
116
120
|
});
|
|
117
121
|
}
|
|
118
|
-
_patch(sub, query, data, config
|
|
122
|
+
_patch(sub, query, data, config) {
|
|
119
123
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
124
|
return yield this._request((url, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
121
125
|
return yield axios_1.default.patch(url, data, config);
|
|
122
|
-
}), sub, query, data, config
|
|
126
|
+
}), sub, query, data, config);
|
|
123
127
|
});
|
|
124
128
|
}
|
|
125
|
-
_delete(sub, query, config
|
|
129
|
+
_delete(sub, query, config) {
|
|
126
130
|
return __awaiter(this, void 0, void 0, function* () {
|
|
127
131
|
return yield this._request((url, _, config) => __awaiter(this, void 0, void 0, function* () {
|
|
128
132
|
return yield axios_1.default.delete(url, config);
|
|
129
|
-
}), sub, query, undefined, config
|
|
133
|
+
}), sub, query, undefined, config);
|
|
130
134
|
});
|
|
131
135
|
}
|
|
132
136
|
}
|
package/dist/BaseServer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseServer.js","sourceRoot":"","sources":["../src/BaseServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,kDAAiE;AACjE,mDAAgD;AAChD,iDAA8C;AAC9C,qDAAkD;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AE,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;AA9HD,gCA8HC"}
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"framework": "npm",
|
|
9
9
|
"application": "package",
|
|
10
10
|
"private": false,
|
|
11
|
-
"version": "1.4.
|
|
11
|
+
"version": "1.4.93",
|
|
12
12
|
"author": "Amir Abolhasani",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"main": "./dist/index.js",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"build": ""
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@types/node": "^24.
|
|
20
|
+
"@types/node": "^24.6.0",
|
|
21
21
|
"async-mutex": "^0.5.0",
|
|
22
|
-
"axios": "^1.12.
|
|
22
|
+
"axios": "^1.12.2",
|
|
23
23
|
"buffer": "^6.0.3",
|
|
24
24
|
"moment": "^2.30.1",
|
|
25
25
|
"phone": "^3.1.67",
|
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,19 @@ 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 = { headers: {} };
|
|
50
|
-
if (
|
|
51
|
-
if (
|
|
60
|
+
config = { headers: {}, withCredentials: true };
|
|
61
|
+
if (config.sign?.header)
|
|
62
|
+
if (config.sign?.key)
|
|
52
63
|
if (data)
|
|
53
64
|
if (config?.headers)
|
|
54
|
-
config.headers[
|
|
65
|
+
config.headers[config.sign.header] = HashOperation.SHA256Secret(config.sign.key, data);
|
|
55
66
|
try
|
|
56
67
|
{
|
|
68
|
+
config.onBeforeRequest?.(url, config);
|
|
57
69
|
this.onBeforeRequests.forEach(onBeforeRequest =>
|
|
58
70
|
{
|
|
59
71
|
onBeforeRequest(url, config);
|
|
@@ -63,17 +75,20 @@ export abstract class BaseServer
|
|
|
63
75
|
{
|
|
64
76
|
onAfterRequest(url, response);
|
|
65
77
|
});
|
|
78
|
+
config.onAfterRequest?.(url, response);
|
|
66
79
|
return { response, data: response.data };
|
|
67
80
|
} catch (error)
|
|
68
81
|
{
|
|
82
|
+
let onError = config.onError ?? this.onError;
|
|
83
|
+
let error_verbose = config.error_verbose ?? this.error_verbose;
|
|
84
|
+
let error_suppress = config.error_suppress ?? this.error_suppress;
|
|
69
85
|
if (error instanceof Error)
|
|
70
86
|
{
|
|
71
|
-
if (
|
|
87
|
+
if (onError)
|
|
72
88
|
{
|
|
73
|
-
if (
|
|
89
|
+
if (error_verbose)
|
|
74
90
|
error.message = `Error calling url '${url}' ` + error.message;
|
|
75
|
-
|
|
76
|
-
this.onError(error);
|
|
91
|
+
onError(error);
|
|
77
92
|
}
|
|
78
93
|
else
|
|
79
94
|
{
|
|
@@ -82,52 +97,52 @@ export abstract class BaseServer
|
|
|
82
97
|
}
|
|
83
98
|
if (axios.isAxiosError(error))
|
|
84
99
|
if (error?.response?.data)
|
|
85
|
-
if (!
|
|
100
|
+
if (!error_suppress)
|
|
86
101
|
{
|
|
87
|
-
if (
|
|
102
|
+
if (error_suppress)
|
|
88
103
|
ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
|
|
89
104
|
else
|
|
90
105
|
ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
|
|
91
106
|
}
|
|
92
107
|
}
|
|
93
|
-
if (!
|
|
108
|
+
if (!error_suppress)
|
|
94
109
|
throw error;
|
|
95
110
|
}
|
|
96
111
|
return {} as any;
|
|
97
112
|
}
|
|
98
|
-
public async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?:
|
|
113
|
+
public async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
99
114
|
{
|
|
100
|
-
return await this._request(async (url: string, _?: any, config?:
|
|
115
|
+
return await this._request(async (url: string, _?: any, config?: BaseServerConfig<ReqData>) =>
|
|
101
116
|
{
|
|
102
117
|
return await axios.get(url, config);
|
|
103
118
|
}, sub, query, undefined, config);
|
|
104
119
|
}
|
|
105
|
-
public async _post<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?:
|
|
120
|
+
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
121
|
{
|
|
107
|
-
return await this._request(async (url: string, data: ReqData, config?:
|
|
122
|
+
return await this._request(async (url: string, data: ReqData, config?: BaseServerConfig<ReqData>) =>
|
|
108
123
|
{
|
|
109
124
|
return await axios.post(url, data, config);
|
|
110
|
-
}, sub, query, data, config
|
|
125
|
+
}, sub, query, data, config);
|
|
111
126
|
}
|
|
112
|
-
public async _put<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?:
|
|
127
|
+
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
128
|
{
|
|
114
|
-
return await this._request(async (url: string, data: ReqData, config?:
|
|
129
|
+
return await this._request(async (url: string, data: ReqData, config?: BaseServerConfig<ReqData>) =>
|
|
115
130
|
{
|
|
116
131
|
return await axios.put(url, data, config);
|
|
117
|
-
}, sub, query, data, config
|
|
132
|
+
}, sub, query, data, config);
|
|
118
133
|
}
|
|
119
|
-
public async _patch<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?:
|
|
134
|
+
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
135
|
{
|
|
121
|
-
return await this._request(async (url: string, data: ReqData, config?:
|
|
136
|
+
return await this._request(async (url: string, data: ReqData, config?: BaseServerConfig<ReqData>) =>
|
|
122
137
|
{
|
|
123
138
|
return await axios.patch(url, data, config);
|
|
124
|
-
}, sub, query, data, config
|
|
139
|
+
}, sub, query, data, config);
|
|
125
140
|
}
|
|
126
|
-
public async _delete<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?:
|
|
141
|
+
public async _delete<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: BaseServerConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
127
142
|
{
|
|
128
|
-
return await this._request(async (url: string, _?: any, config?:
|
|
143
|
+
return await this._request(async (url: string, _?: any, config?: BaseServerConfig<ReqData>) =>
|
|
129
144
|
{
|
|
130
145
|
return await axios.delete(url, config);
|
|
131
|
-
}, sub, query, undefined, config
|
|
146
|
+
}, sub, query, undefined, config);
|
|
132
147
|
}
|
|
133
148
|
}
|