namirasoft-core 1.4.85 → 1.4.86

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.
Files changed (51) hide show
  1. package/dist/URLOperation.d.ts +1 -0
  2. package/dist/URLOperation.js +10 -0
  3. package/dist/URLOperation.js.map +1 -1
  4. package/package.json +27 -27
  5. package/src/BaseDatabaseRow.ts +6 -6
  6. package/src/BaseMetaColumn.ts +17 -17
  7. package/src/BaseMetaDatabase.ts +30 -30
  8. package/src/BaseMetaTable.ts +40 -40
  9. package/src/BaseServer.ts +125 -125
  10. package/src/BaseUUID.ts +68 -68
  11. package/src/ByteOperation.ts +57 -57
  12. package/src/CacheService.ts +75 -75
  13. package/src/ConsoleOperation.ts +68 -68
  14. package/src/ConvertService.ts +118 -118
  15. package/src/CookieService.ts +33 -33
  16. package/src/Countries.ts +486 -486
  17. package/src/Country.ts +21 -21
  18. package/src/CountryOperation.ts +98 -98
  19. package/src/EncodingOperation.ts +12 -12
  20. package/src/EncryptionOperation.ts +40 -40
  21. package/src/EnvService.ts +22 -22
  22. package/src/ErrorOperation.ts +13 -13
  23. package/src/FileOperation.ts +60 -60
  24. package/src/FilterItem.ts +117 -117
  25. package/src/FilterItemColumnType.ts +9 -9
  26. package/src/FilterItemOperator.ts +52 -52
  27. package/src/GeoOperation.ts +18 -18
  28. package/src/HTTPError.ts +8 -8
  29. package/src/HTTPMethod.ts +6 -6
  30. package/src/HashOperation.ts +24 -24
  31. package/src/IStorage.ts +5 -5
  32. package/src/IStorageCookie.ts +52 -52
  33. package/src/IStorageJsonFile.ts +45 -45
  34. package/src/IStorageLocal.ts +16 -16
  35. package/src/IStorageMemoryDedicated.ts +25 -25
  36. package/src/IStorageMemoryShared.ts +21 -21
  37. package/src/IStorageSession.ts +16 -16
  38. package/src/NamingConvention.ts +191 -191
  39. package/src/ObjectService.ts +27 -27
  40. package/src/PackageService.ts +76 -76
  41. package/src/PhoneOperation.ts +8 -8
  42. package/src/PriceOperation.ts +20 -20
  43. package/src/SearchOperation.ts +31 -31
  44. package/src/SetTimeouService.ts +32 -32
  45. package/src/SortItem.ts +88 -88
  46. package/src/StringOperation.ts +22 -22
  47. package/src/TimeOperation.ts +299 -299
  48. package/src/TimeUnitOperation.ts +70 -70
  49. package/src/URLOperation.ts +66 -54
  50. package/src/VersionOperation.ts +46 -46
  51. package/src/index.ts +46 -46
@@ -10,4 +10,5 @@ export declare abstract class URLOperation {
10
10
  static getLink(domain: string, sub: string, query?: {
11
11
  [name: string]: ParsedNameValue;
12
12
  }): string;
13
+ static isEqual(a: string, b: string): boolean;
13
14
  }
@@ -43,6 +43,16 @@ class URLOperation {
43
43
  static getLink(domain, sub, query) {
44
44
  return this.merge(domain, this.getSub(sub, query));
45
45
  }
46
+ static isEqual(a, b) {
47
+ try {
48
+ const url_a = new URL(a);
49
+ const url_b = new URL(b);
50
+ return url_a.origin === url_b.origin && url_a.pathname === url_b.pathname;
51
+ }
52
+ catch (e) {
53
+ return a == b;
54
+ }
55
+ }
46
56
  }
47
57
  exports.URLOperation = URLOperation;
48
58
  //# sourceMappingURL=URLOperation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"URLOperation.js","sourceRoot":"","sources":["../src/URLOperation.ts"],"names":[],"mappings":";;;AAEA,MAAsB,YAAY;IAE9B,MAAM,CAAC,QAAQ,CAAC,KAA2C;QAEvD,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,IAAI,KAAK;YACL,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EACpC,CAAC;gBACG,IAAI,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;gBACvB,IAAI,KAAK,IAAI,IAAI,EACjB,CAAC;oBACG,IAAI,KAAK;wBACL,GAAG,IAAI,GAAG,CAAC;;wBAEX,GAAG,IAAI,GAAG,CAAC;oBACf,IAAI,KAAK,CAAC,OAAO,CAAC,KAA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AAM,CAAC,MAAM,CAAC,GAAW,EAAE,KAA2C;QAElE,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;IACjD,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,MAAc,EAAE,GAAW,EAAE,KAA2C;QAEnF,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;IACvD,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,CAAS,EAAE,CAAS;QAE/B,IACA,CAAC;YACG,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC;YACzB,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC;YACzB,OAAO,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,QAAQ,KAAK,KAAK,CAAC,QAAQ,CAAC;QAC9E,CAAC;QAAC,OAAO,CAAC,EACV,CAAC;YACG,OAAO,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;IACL,CAAC;CACJ;AAhED,oCAgEC"}
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
- {
2
- "name": "namirasoft-core",
3
- "title": "Namirasoft Core NPM Package",
4
- "description": "Namira Software Corporation Core NPM Package",
5
- "icon": "logo.png",
6
- "logo": "https://static.namirasoft.com/image/namirasoft/site/logo/base.png",
7
- "language": "ts",
8
- "framework": "npm",
9
- "application": "package",
10
- "private": false,
11
- "version": "1.4.85",
12
- "author": "Amir Abolhasani",
13
- "license": "MIT",
14
- "main": "./dist/index.js",
15
- "types": "./dist/index.d.ts",
16
- "scripts": {
17
- "build": ""
18
- },
19
- "dependencies": {
20
- "@types/node": "^24.3.0",
21
- "async-mutex": "^0.5.0",
22
- "axios": "^1.11.0",
23
- "buffer": "^6.0.3",
24
- "moment": "^2.30.1",
25
- "phone": "^3.1.67",
26
- "uuid": "^11.1.0"
27
- }
1
+ {
2
+ "name": "namirasoft-core",
3
+ "title": "Namirasoft Core NPM Package",
4
+ "description": "Namira Software Corporation Core NPM Package",
5
+ "icon": "logo.png",
6
+ "logo": "https://static.namirasoft.com/image/namirasoft/site/logo/base.png",
7
+ "language": "ts",
8
+ "framework": "npm",
9
+ "application": "package",
10
+ "private": false,
11
+ "version": "1.4.86",
12
+ "author": "Amir Abolhasani",
13
+ "license": "MIT",
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "scripts": {
17
+ "build": ""
18
+ },
19
+ "dependencies": {
20
+ "@types/node": "^24.3.0",
21
+ "async-mutex": "^0.5.0",
22
+ "axios": "^1.11.0",
23
+ "buffer": "^6.0.3",
24
+ "moment": "^2.30.1",
25
+ "phone": "^3.1.67",
26
+ "uuid": "^11.1.0"
27
+ }
28
28
  }
@@ -1,7 +1,7 @@
1
- export type BaseDatabaseRow<ID> =
2
- {
3
- id: ID;
4
- createdAt: string;
5
- updatedAt: string;
6
- deletedAt: string;
1
+ export type BaseDatabaseRow<ID> =
2
+ {
3
+ id: ID;
4
+ createdAt: string;
5
+ updatedAt: string;
6
+ deletedAt: string;
7
7
  }
@@ -1,18 +1,18 @@
1
- import { BaseMetaTable } from "./BaseMetaTable";
2
-
3
- export class BaseMetaColumn
4
- {
5
- table: BaseMetaTable;
6
- name: string;
7
- text: string;
8
- type: string; // todo
9
- required: boolean;
10
- constructor(table: BaseMetaTable, name: string, text: string, type: string, required: boolean)
11
- {
12
- this.table = table;
13
- this.name = name;
14
- this.text = text;
15
- this.type = type;
16
- this.required = required;
17
- }
1
+ import { BaseMetaTable } from "./BaseMetaTable";
2
+
3
+ export class BaseMetaColumn
4
+ {
5
+ table: BaseMetaTable;
6
+ name: string;
7
+ text: string;
8
+ type: string; // todo
9
+ required: boolean;
10
+ constructor(table: BaseMetaTable, name: string, text: string, type: string, required: boolean)
11
+ {
12
+ this.table = table;
13
+ this.name = name;
14
+ this.text = text;
15
+ this.type = type;
16
+ this.required = required;
17
+ }
18
18
  }
@@ -1,31 +1,31 @@
1
- import { BaseMetaTable } from "./BaseMetaTable";
2
-
3
- export abstract class BaseMetaDatabase
4
- {
5
- public tables: { [name: string]: BaseMetaTable } = {};
6
- forEachTable<T, MT extends BaseMetaTable>(handler: (table: MT) => T | null): T | null
7
- {
8
- for (const key in this.tables)
9
- {
10
- const element = this.tables[key];
11
- let ans = handler(element as MT);
12
- if (ans != null)
13
- return ans;
14
- }
15
- return null;
16
- }
17
- getTables<MT extends BaseMetaTable>(): MT[]
18
- {
19
- let ans: MT[] = [];
20
- this.forEachTable((table) =>
21
- {
22
- ans.push(table as MT);
23
- return null;
24
- });
25
- return ans;
26
- }
27
- hasTable(name: string): boolean
28
- {
29
- return this.tables[name] != null;
30
- }
1
+ import { BaseMetaTable } from "./BaseMetaTable";
2
+
3
+ export abstract class BaseMetaDatabase
4
+ {
5
+ public tables: { [name: string]: BaseMetaTable } = {};
6
+ forEachTable<T, MT extends BaseMetaTable>(handler: (table: MT) => T | null): T | null
7
+ {
8
+ for (const key in this.tables)
9
+ {
10
+ const element = this.tables[key];
11
+ let ans = handler(element as MT);
12
+ if (ans != null)
13
+ return ans;
14
+ }
15
+ return null;
16
+ }
17
+ getTables<MT extends BaseMetaTable>(): MT[]
18
+ {
19
+ let ans: MT[] = [];
20
+ this.forEachTable((table) =>
21
+ {
22
+ ans.push(table as MT);
23
+ return null;
24
+ });
25
+ return ans;
26
+ }
27
+ hasTable(name: string): boolean
28
+ {
29
+ return this.tables[name] != null;
30
+ }
31
31
  };
@@ -1,41 +1,41 @@
1
- import { BaseMetaColumn } from "./BaseMetaColumn";
2
- import { BaseMetaDatabase } from "./BaseMetaDatabase";
3
-
4
- export class BaseMetaTable
5
- {
6
- database: BaseMetaDatabase | null;
7
- name: string;
8
- text: string;
9
- public columns: { [name: string]: BaseMetaColumn } = {};
10
- constructor(database: BaseMetaDatabase | null, name: string, text: string)
11
- {
12
- this.database = database;
13
- this.name = name;
14
- this.text = text;
15
- }
16
- forEachColumn<T, MC extends BaseMetaColumn>(handler: (column: MC) => T | null): T | null
17
- {
18
- for (const key in this.columns)
19
- {
20
- const element = this.columns[key];
21
- let ans = handler(element as MC);
22
- if (ans != null)
23
- return ans;
24
- }
25
- return null;
26
- }
27
- getColumns<MC extends BaseMetaColumn>(): MC[]
28
- {
29
- let ans: MC[] = [];
30
- this.forEachColumn((column) =>
31
- {
32
- ans.push(column as MC);
33
- return null;
34
- });
35
- return ans;
36
- }
37
- hasColumn(name: string): boolean
38
- {
39
- return this.columns[name] != null;
40
- }
1
+ import { BaseMetaColumn } from "./BaseMetaColumn";
2
+ import { BaseMetaDatabase } from "./BaseMetaDatabase";
3
+
4
+ export class BaseMetaTable
5
+ {
6
+ database: BaseMetaDatabase | null;
7
+ name: string;
8
+ text: string;
9
+ public columns: { [name: string]: BaseMetaColumn } = {};
10
+ constructor(database: BaseMetaDatabase | null, name: string, text: string)
11
+ {
12
+ this.database = database;
13
+ this.name = name;
14
+ this.text = text;
15
+ }
16
+ forEachColumn<T, MC extends BaseMetaColumn>(handler: (column: MC) => T | null): T | null
17
+ {
18
+ for (const key in this.columns)
19
+ {
20
+ const element = this.columns[key];
21
+ let ans = handler(element as MC);
22
+ if (ans != null)
23
+ return ans;
24
+ }
25
+ return null;
26
+ }
27
+ getColumns<MC extends BaseMetaColumn>(): MC[]
28
+ {
29
+ let ans: MC[] = [];
30
+ this.forEachColumn((column) =>
31
+ {
32
+ ans.push(column as MC);
33
+ return null;
34
+ });
35
+ return ans;
36
+ }
37
+ hasColumn(name: string): boolean
38
+ {
39
+ return this.columns[name] != null;
40
+ }
41
41
  }
package/src/BaseServer.ts CHANGED
@@ -1,126 +1,126 @@
1
- import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
2
- import { HashOperation } from "./HashOperation";
3
- import { URLOperation } from "./URLOperation";
4
- import { ErrorOperation } from "./ErrorOperation";
5
- import { ParsedNameValue } from "./ParsedNameValue";
6
- import { ConsoleOperation } from "./ConsoleOperation";
7
- import { EnvService } from "./EnvService";
8
- import { HTTPError } from "./HTTPError";
9
-
10
- export abstract class BaseServer
11
- {
12
- public static isErrorCode(e: any, code: number): boolean
13
- {
14
- if (axios.isAxiosError(e))
15
- if (e.response)
16
- if (e.response?.status === code)
17
- return true;
18
- if (e instanceof HTTPError)
19
- if (e.code === code)
20
- return true;
21
- return false
22
- }
23
-
24
- protected base_url: string;
25
- private onBeforeRequests: ((url: string, config?: AxiosRequestConfig) => void)[] = [];
26
- private onAfterRequests: ((url: string, res: AxiosResponse) => void)[] = [];
27
- protected abstract onError(error: Error): void;
28
- public suppressErrors: boolean = false;
29
- public suppressOnErrors: boolean = false;
30
- public verboseOnErrors: boolean;
31
-
32
- constructor(base_url: string)
33
- {
34
- this.base_url = base_url;
35
- this.verboseOnErrors = new EnvService("BASESERVER_ERROR_VERBOSE", false).getBoolean(false);
36
- }
37
- public addOnBeforeRequest(onBeforeRequest: (url: string, config?: AxiosRequestConfig) => void)
38
- {
39
- this.onBeforeRequests.push(onBeforeRequest);
40
- }
41
- public addOnAfterRequest(onAfterRequest: (url: string, res: AxiosResponse) => void)
42
- {
43
- this.onAfterRequests.push(onAfterRequest);
44
- }
45
- private async _request<ResData = any, ReqData = any>(onRequest: (url: string, data?: any, config?: AxiosRequestConfig<ReqData>) => Promise<AxiosResponse<ResData>>, sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
46
- {
47
- let url: string = URLOperation.getLink(this.base_url, sub, query);
48
- if (!config)
49
- config = { headers: {} };
50
- if (sign_header)
51
- if (sign_key)
52
- if (data)
53
- if (config?.headers)
54
- config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
55
- try
56
- {
57
- this.onBeforeRequests.forEach(onBeforeRequest =>
58
- {
59
- onBeforeRequest(url, config);
60
- });
61
- let response: AxiosResponse<ResData> = await onRequest(url, data, config);
62
- this.onAfterRequests.forEach(onAfterRequest =>
63
- {
64
- onAfterRequest(url, response);
65
- });
66
- return { response, data: response.data };
67
- } catch (error)
68
- {
69
- if (error instanceof Error)
70
- {
71
- if (this.onError)
72
- {
73
- if (this.verboseOnErrors)
74
- error.message = `Error calling url '${url}' ` + error.message;
75
- if (!this.suppressOnErrors)
76
- this.onError(error);
77
- }
78
- else
79
- {
80
- ConsoleOperation.warning("onError function has not been properly set.");
81
- ConsoleOperation.error(error?.message);
82
- }
83
- if (axios.isAxiosError(error))
84
- if (error?.response?.data)
85
- if (!this.suppressErrors)
86
- {
87
- if (this.verboseOnErrors)
88
- ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
89
- else
90
- ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
91
- }
92
- }
93
- if (!this.suppressErrors)
94
- throw error;
95
- }
96
- return {} as any;
97
- }
98
- public async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
99
- {
100
- return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
101
- {
102
- return await axios.get(url, config);
103
- }, sub, query, undefined, config);
104
- }
105
- public async _post<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
106
- {
107
- return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
108
- {
109
- return await axios.post(url, data, config);
110
- }, sub, query, data, config, sign_header, sign_key);
111
- }
112
- public async _put<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
113
- {
114
- return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
115
- {
116
- return await axios.put(url, data, config);
117
- }, sub, query, data, config, sign_header, sign_key);
118
- }
119
- public async _delete<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
120
- {
121
- return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
122
- {
123
- return await axios.delete(url, config);
124
- }, sub, query, undefined, config, sign_header, sign_key);
125
- }
1
+ import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
2
+ import { HashOperation } from "./HashOperation";
3
+ import { URLOperation } from "./URLOperation";
4
+ import { ErrorOperation } from "./ErrorOperation";
5
+ import { ParsedNameValue } from "./ParsedNameValue";
6
+ import { ConsoleOperation } from "./ConsoleOperation";
7
+ import { EnvService } from "./EnvService";
8
+ import { HTTPError } from "./HTTPError";
9
+
10
+ export abstract class BaseServer
11
+ {
12
+ public static isErrorCode(e: any, code: number): boolean
13
+ {
14
+ if (axios.isAxiosError(e))
15
+ if (e.response)
16
+ if (e.response?.status === code)
17
+ return true;
18
+ if (e instanceof HTTPError)
19
+ if (e.code === code)
20
+ return true;
21
+ return false
22
+ }
23
+
24
+ protected base_url: string;
25
+ private onBeforeRequests: ((url: string, config?: AxiosRequestConfig) => void)[] = [];
26
+ private onAfterRequests: ((url: string, res: AxiosResponse) => void)[] = [];
27
+ protected abstract onError(error: Error): void;
28
+ public suppressErrors: boolean = false;
29
+ public suppressOnErrors: boolean = false;
30
+ public verboseOnErrors: boolean;
31
+
32
+ constructor(base_url: string)
33
+ {
34
+ this.base_url = base_url;
35
+ this.verboseOnErrors = new EnvService("BASESERVER_ERROR_VERBOSE", false).getBoolean(false);
36
+ }
37
+ public addOnBeforeRequest(onBeforeRequest: (url: string, config?: AxiosRequestConfig) => void)
38
+ {
39
+ this.onBeforeRequests.push(onBeforeRequest);
40
+ }
41
+ public addOnAfterRequest(onAfterRequest: (url: string, res: AxiosResponse) => void)
42
+ {
43
+ this.onAfterRequests.push(onAfterRequest);
44
+ }
45
+ private async _request<ResData = any, ReqData = any>(onRequest: (url: string, data?: any, config?: AxiosRequestConfig<ReqData>) => Promise<AxiosResponse<ResData>>, sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
46
+ {
47
+ let url: string = URLOperation.getLink(this.base_url, sub, query);
48
+ if (!config)
49
+ config = { headers: {} };
50
+ if (sign_header)
51
+ if (sign_key)
52
+ if (data)
53
+ if (config?.headers)
54
+ config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
55
+ try
56
+ {
57
+ this.onBeforeRequests.forEach(onBeforeRequest =>
58
+ {
59
+ onBeforeRequest(url, config);
60
+ });
61
+ let response: AxiosResponse<ResData> = await onRequest(url, data, config);
62
+ this.onAfterRequests.forEach(onAfterRequest =>
63
+ {
64
+ onAfterRequest(url, response);
65
+ });
66
+ return { response, data: response.data };
67
+ } catch (error)
68
+ {
69
+ if (error instanceof Error)
70
+ {
71
+ if (this.onError)
72
+ {
73
+ if (this.verboseOnErrors)
74
+ error.message = `Error calling url '${url}' ` + error.message;
75
+ if (!this.suppressOnErrors)
76
+ this.onError(error);
77
+ }
78
+ else
79
+ {
80
+ ConsoleOperation.warning("onError function has not been properly set.");
81
+ ConsoleOperation.error(error?.message);
82
+ }
83
+ if (axios.isAxiosError(error))
84
+ if (error?.response?.data)
85
+ if (!this.suppressErrors)
86
+ {
87
+ if (this.verboseOnErrors)
88
+ ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
89
+ else
90
+ ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
91
+ }
92
+ }
93
+ if (!this.suppressErrors)
94
+ throw error;
95
+ }
96
+ return {} as any;
97
+ }
98
+ public async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
99
+ {
100
+ return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
101
+ {
102
+ return await axios.get(url, config);
103
+ }, sub, query, undefined, config);
104
+ }
105
+ public async _post<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
106
+ {
107
+ return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
108
+ {
109
+ return await axios.post(url, data, config);
110
+ }, sub, query, data, config, sign_header, sign_key);
111
+ }
112
+ public async _put<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, data?: ReqData, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
113
+ {
114
+ return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
115
+ {
116
+ return await axios.put(url, data, config);
117
+ }, sub, query, data, config, sign_header, sign_key);
118
+ }
119
+ public async _delete<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>, sign_header?: string, sign_key?: string): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
120
+ {
121
+ return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
122
+ {
123
+ return await axios.delete(url, config);
124
+ }, sub, query, undefined, config, sign_header, sign_key);
125
+ }
126
126
  }