namirasoft-core 1.3.67 → 1.3.68

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 (48) hide show
  1. package/dist/ConsoleColor.d.ts +9 -0
  2. package/dist/ConsoleColor.js +38 -0
  3. package/dist/ConsoleColor.js.map +10 -0
  4. package/dist/ConsoleColorService.d.ts +9 -0
  5. package/dist/ConsoleColorService.js +41 -0
  6. package/dist/ConsoleColorService.js.map +10 -0
  7. package/dist/PriceOperation.d.ts +6 -0
  8. package/dist/PriceOperation.js +19 -0
  9. package/dist/PriceOperation.js.map +1 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -1
  13. package/package.json +3 -3
  14. package/src/BaseDatabaseRow.ts +6 -6
  15. package/src/BaseMetaColumn.ts +13 -13
  16. package/src/BaseMetaTable.ts +24 -24
  17. package/src/BaseServer.ts +83 -83
  18. package/src/ConsoleOperation.ts +68 -68
  19. package/src/ConvertService.ts +100 -100
  20. package/src/Countries.ts +486 -486
  21. package/src/Country.ts +21 -21
  22. package/src/CountryOperation.ts +98 -98
  23. package/src/EncodingOperation.ts +10 -10
  24. package/src/EncryptionOperation.ts +40 -40
  25. package/src/ErrorOperation.ts +13 -13
  26. package/src/FileOperation.ts +57 -57
  27. package/src/FilterItem.ts +105 -105
  28. package/src/FilterItemColumnType.ts +6 -6
  29. package/src/FilterItemOperator.ts +51 -51
  30. package/src/GeoOperation.ts +18 -18
  31. package/src/HTTPError.ts +8 -8
  32. package/src/HTTPMethod.ts +6 -6
  33. package/src/HashOperation.ts +24 -24
  34. package/src/IStorage.ts +5 -5
  35. package/src/IStorageCookie.ts +52 -52
  36. package/src/IStorageLocal.ts +16 -16
  37. package/src/IStorageMemory.ts +17 -17
  38. package/src/NamingConvention.ts +107 -107
  39. package/src/ObjectService.ts +23 -23
  40. package/src/PackageService.ts +76 -76
  41. package/src/PhoneOperation.ts +8 -8
  42. package/src/PriceOperation.ts +19 -0
  43. package/src/RegexTemplate.ts +7 -7
  44. package/src/StringOperation.ts +18 -18
  45. package/src/TimeOperation.ts +262 -262
  46. package/src/URLOperation.ts +54 -54
  47. package/src/VersionOperation.ts +46 -46
  48. package/src/index.ts +34 -33
@@ -0,0 +1,9 @@
1
+ export declare class ConsoleOperation
2
+ {
3
+ private static colors;
4
+ static log(message: string): string;
5
+ static info(message: string): string;
6
+ static success(message: string): string;
7
+ static warning(message: string): string;
8
+ static error(message: string): string;
9
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConsoleOperation = void 0;
4
+ class ConsoleOperation
5
+ {
6
+ static log(message)
7
+ {
8
+ return (this.colors.white + message + this.colors.reset);
9
+ }
10
+ static info(message)
11
+ {
12
+ return (this.colors.cyan + message + this.colors.reset);
13
+ }
14
+ static success(message)
15
+ {
16
+ return (this.colors.green + message + this.colors.reset);
17
+ }
18
+ static warning(message)
19
+ {
20
+ return (this.colors.yellow + message + this.colors.reset);
21
+ }
22
+ static error(message)
23
+ {
24
+ return (this.colors.red + message + this.colors.reset);
25
+ }
26
+ }
27
+ exports.ConsoleOperation = ConsoleOperation;
28
+ ConsoleOperation.colors = {
29
+ reset: '\x1b[0m',
30
+ red: '\x1b[31m',
31
+ green: '\x1b[32m',
32
+ yellow: '\x1b[33m',
33
+ blue: '\x1b[34m',
34
+ magenta: '\x1b[35m',
35
+ cyan: '\x1b[36m',
36
+ white: '\x1b[37m',
37
+ };
38
+ //# sourceMappingURL=ConsoleOperation.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "file": "ConsoleOperation.js",
4
+ "sourceRoot": "",
5
+ "sources": [
6
+ "../src/ConsoleOperation.ts"
7
+ ],
8
+ "names": [],
9
+ "mappings": ";;;AAAA,MAAa,YAAY;IAYrB,MAAM,CAAC,GAAG,CAAC,OAAe;QAEtB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7D,CAAC;IACD,MAAM,CAAC,IAAI,CAAC,OAAe;QAEvB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5D,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,OAAe;QAE1B,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7D,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,OAAe;QAE1B,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9D,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,OAAe;QAExB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC;;AA/BL,oCAgCC;AA9BkB,mBAAM,GAAG;IACpB,KAAK,EAAE,SAAS;IAChB,GAAG,EAAE,UAAU;IACf,KAAK,EAAE,UAAU;IACjB,MAAM,EAAE,UAAU;IAClB,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,UAAU;IACnB,IAAI,EAAE,UAAU;IAChB,KAAK,EAAE,UAAU;CACpB,CAAC"
10
+ }
@@ -0,0 +1,9 @@
1
+ export declare class ConsoleOperationService
2
+ {
3
+ private colors;
4
+ log(message: string): string;
5
+ info(message: string): string;
6
+ success(message: string): string;
7
+ warning(message: string): string;
8
+ error(message: string): string;
9
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConsoleOperationService = void 0;
4
+ class ConsoleOperationService
5
+ {
6
+ constructor()
7
+ {
8
+ this.colors = {
9
+ reset: '\x1b[0m',
10
+ red: '\x1b[31m',
11
+ green: '\x1b[32m',
12
+ yellow: '\x1b[33m',
13
+ blue: '\x1b[34m',
14
+ magenta: '\x1b[35m',
15
+ cyan: '\x1b[36m',
16
+ white: '\x1b[37m',
17
+ };
18
+ }
19
+ log(message)
20
+ {
21
+ return (this.colors.white + message + this.colors.reset);
22
+ }
23
+ info(message)
24
+ {
25
+ return (this.colors.cyan + message + this.colors.reset);
26
+ }
27
+ success(message)
28
+ {
29
+ return (this.colors.green + message + this.colors.reset);
30
+ }
31
+ warning(message)
32
+ {
33
+ return (this.colors.yellow + message + this.colors.reset);
34
+ }
35
+ error(message)
36
+ {
37
+ return (this.colors.red + message + this.colors.reset);
38
+ }
39
+ }
40
+ exports.ConsoleOperationService = ConsoleOperationService;
41
+ //# sourceMappingURL=ConsoleOperationService.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "file": "ConsoleOperationService.js",
4
+ "sourceRoot": "",
5
+ "sources": [
6
+ "../src/ConsoleOperationService.ts"
7
+ ],
8
+ "names": [],
9
+ "mappings": ";;;AAAA,MAAa,mBAAmB;IAAhC;QAEY,WAAM,GAAG;YACb,KAAK,EAAE,SAAS;YAChB,GAAG,EAAE,UAAU;YACf,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,UAAU;YAClB,IAAI,EAAE,UAAU;YAChB,OAAO,EAAE,UAAU;YACnB,IAAI,EAAE,UAAU;YAChB,KAAK,EAAE,UAAU;SACpB,CAAC;IAqBN,CAAC;IApBG,GAAG,CAAC,OAAe;QAEf,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,CAAC,OAAe;QAEhB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,CAAC,OAAe;QAEnB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,CAAC,OAAe;QAEnB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9D,CAAC;IACD,KAAK,CAAC,OAAe;QAEjB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC;CACJ;AAhCD,kDAgCC"
10
+ }
@@ -0,0 +1,6 @@
1
+ export declare class PriceOperation {
2
+ static toCent(value: number): number;
3
+ static toNormal(value: number): number;
4
+ static fromCent(value: number): number;
5
+ static fromNormal(value: number): number;
6
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PriceOperation = void 0;
4
+ class PriceOperation {
5
+ static toCent(value) {
6
+ return Math.round(value / 1000);
7
+ }
8
+ static toNormal(value) {
9
+ return Math.round(value / 1000) / 100;
10
+ }
11
+ static fromCent(value) {
12
+ return Math.floor(value * 1000);
13
+ }
14
+ static fromNormal(value) {
15
+ return Math.floor(value * 100 * 1000);
16
+ }
17
+ }
18
+ exports.PriceOperation = PriceOperation;
19
+ //# sourceMappingURL=PriceOperation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PriceOperation.js","sourceRoot":"","sources":["../src/PriceOperation.ts"],"names":[],"mappings":";;;AAAA,MAAa,cAAc;IAEvB,MAAM,CAAC,MAAM,CAAC,KAAa;QAEvB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IACpC,CAAC;IACD,MAAM,CAAC,QAAQ,CAAC,KAAa;QAEzB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC;IAC1C,CAAC;IACD,MAAM,CAAC,QAAQ,CAAC,KAAa;QAEzB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IACpC,CAAC;IACD,MAAM,CAAC,UAAU,CAAC,KAAa;QAE3B,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,GAAG,IAAI,CAAC,CAAC;IAC1C,CAAC;CACJ;AAlBD,wCAkBC"}
package/dist/index.d.ts CHANGED
@@ -27,6 +27,7 @@ export * from "./ObjectService";
27
27
  export * from "./PackageService";
28
28
  export * from "./ParsedNameValue";
29
29
  export * from "./PhoneOperation";
30
+ export * from "./PriceOperation";
30
31
  export * from "./RegexTemplate";
31
32
  export * from "./StringOperation";
32
33
  export * from "./TimeOperation";
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@ __exportStar(require("./ObjectService"), exports);
43
43
  __exportStar(require("./PackageService"), exports);
44
44
  __exportStar(require("./ParsedNameValue"), exports);
45
45
  __exportStar(require("./PhoneOperation"), exports);
46
+ __exportStar(require("./PriceOperation"), exports);
46
47
  __exportStar(require("./RegexTemplate"), exports);
47
48
  __exportStar(require("./StringOperation"), exports);
48
49
  __exportStar(require("./TimeOperation"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,mDAAiC;AACjC,kDAAgC;AAChC,+CAA6B;AAC7B,qDAAmC;AACnC,8CAA4B;AAC5B,mDAAiC;AACjC,4CAA0B;AAC1B,qDAAmC;AACnC,sDAAoC;AACpC,wDAAsC;AACtC,mDAAiC;AACjC,kDAAgC;AAChC,+CAA6B;AAC7B,yDAAuC;AACvC,uDAAqC;AACrC,iDAA+B;AAC/B,kDAAgC;AAChC,8CAA4B;AAC5B,+CAA6B;AAC7B,6CAA2B;AAC3B,mDAAiC;AACjC,kDAAgC;AAChC,mDAAiC;AACjC,qDAAmC;AACnC,kDAAgC;AAChC,mDAAiC;AACjC,oDAAkC;AAClC,mDAAiC;AACjC,kDAAgC;AAChC,oDAAkC;AAClC,kDAAgC;AAChC,iDAA+B;AAC/B,qDAAmC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,mDAAiC;AACjC,kDAAgC;AAChC,+CAA6B;AAC7B,qDAAmC;AACnC,8CAA4B;AAC5B,mDAAiC;AACjC,4CAA0B;AAC1B,qDAAmC;AACnC,sDAAoC;AACpC,wDAAsC;AACtC,mDAAiC;AACjC,kDAAgC;AAChC,+CAA6B;AAC7B,yDAAuC;AACvC,uDAAqC;AACrC,iDAA+B;AAC/B,kDAAgC;AAChC,8CAA4B;AAC5B,+CAA6B;AAC7B,6CAA2B;AAC3B,mDAAiC;AACjC,kDAAgC;AAChC,mDAAiC;AACjC,qDAAmC;AACnC,kDAAgC;AAChC,mDAAiC;AACjC,oDAAkC;AAClC,mDAAiC;AACjC,mDAAiC;AACjC,kDAAgC;AAChC,oDAAkC;AAClC,kDAAgC;AAChC,iDAA+B;AAC/B,qDAAmC"}
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "framework": "npm",
9
9
  "application": "package",
10
10
  "private": false,
11
- "version": "1.3.67",
11
+ "version": "1.3.68",
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": "^20.14.7",
20
+ "@types/node": "^20.14.9",
21
21
  "axios": "^1.7.2",
22
22
  "moment": "^2.30.1",
23
- "phone": "^3.1.45"
23
+ "phone": "^3.1.46"
24
24
  }
25
25
  }
@@ -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,14 +1,14 @@
1
- export class BaseMetaColumn
2
- {
3
- name: string;
4
- text: string;
5
- type: string;
6
- required: boolean;
7
- constructor(name: string, text: string, type: string, required: boolean)
8
- {
9
- this.name = name;
10
- this.text = text;
11
- this.type = type;
12
- this.required = required;
13
- }
1
+ export class BaseMetaColumn
2
+ {
3
+ name: string;
4
+ text: string;
5
+ type: string;
6
+ required: boolean;
7
+ constructor(name: string, text: string, type: string, required: boolean)
8
+ {
9
+ this.name = name;
10
+ this.text = text;
11
+ this.type = type;
12
+ this.required = required;
13
+ }
14
14
  }
@@ -1,25 +1,25 @@
1
- import { BaseMetaColumn } from "./BaseMetaColumn";
2
-
3
- export class BaseMetaTable
4
- {
5
- name: string;
6
- text: string;
7
- constructor(name: string, text: string)
8
- {
9
- this.name = name;
10
- this.text = text;
11
- }
12
- public getColumns(): BaseMetaColumn[]
13
- {
14
- let ans: BaseMetaColumn[] = [];
15
- let columns = (this as any).columns;
16
- if (columns)
17
- for (let key of Object.keys(columns))
18
- {
19
- let object = columns[key];
20
- if (object instanceof BaseMetaColumn)
21
- ans.push(object);
22
- }
23
- return ans;
24
- }
1
+ import { BaseMetaColumn } from "./BaseMetaColumn";
2
+
3
+ export class BaseMetaTable
4
+ {
5
+ name: string;
6
+ text: string;
7
+ constructor(name: string, text: string)
8
+ {
9
+ this.name = name;
10
+ this.text = text;
11
+ }
12
+ public getColumns(): BaseMetaColumn[]
13
+ {
14
+ let ans: BaseMetaColumn[] = [];
15
+ let columns = (this as any).columns;
16
+ if (columns)
17
+ for (let key of Object.keys(columns))
18
+ {
19
+ let object = columns[key];
20
+ if (object instanceof BaseMetaColumn)
21
+ ans.push(object);
22
+ }
23
+ return ans;
24
+ }
25
25
  }
package/src/BaseServer.ts CHANGED
@@ -1,84 +1,84 @@
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
-
8
- export abstract class BaseServer
9
- {
10
- protected domain: string;
11
- public suppressErrors: boolean = false;
12
- constructor(domain: string)
13
- {
14
- this.domain = domain;
15
- }
16
- protected abstract onBeforeRequest<ReqData = any>(url: string, config?: AxiosRequestConfig<ReqData>): void;
17
- protected abstract onAfterRequest<ResData = any>(url: string, response: AxiosResponse<ResData>): void;
18
- protected abstract onError(error: Error): void;
19
- 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 }>
20
- {
21
- let url: string = URLOperation.getLink(this.domain, sub, query);
22
- if (!config)
23
- config = { headers: {} };
24
- if (sign_header)
25
- if (sign_key)
26
- if (data)
27
- if (config?.headers)
28
- config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
29
- try
30
- {
31
- this.onBeforeRequest(url, config);
32
- let response: AxiosResponse<ResData> = await onRequest(url, data, config);
33
- this.onAfterRequest(url, response);
34
- return { response, data: response.data };
35
- } catch (error)
36
- {
37
- if (error instanceof Error)
38
- {
39
- if (this.onError)
40
- this.onError(error);
41
- else
42
- {
43
- ConsoleOperation.warning("onError function has not been properly set.");
44
- ConsoleOperation.error(error?.message);
45
- }
46
- if (axios.isAxiosError(error))
47
- if (error?.response?.data)
48
- if (!this.suppressErrors)
49
- ErrorOperation.throwHTTP(error.response.status, error.response.data);
50
- }
51
- if (!this.suppressErrors)
52
- throw error;
53
- }
54
- return {} as any;
55
- }
56
- protected async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
57
- {
58
- return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
59
- {
60
- return await axios.get(url, config);
61
- }, sub, query, undefined, config);
62
- }
63
- protected 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 }>
64
- {
65
- return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
66
- {
67
- return await axios.post(url, data, config);
68
- }, sub, query, data, config, sign_header, sign_key);
69
- }
70
- protected 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 }>
71
- {
72
- return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
73
- {
74
- return await axios.put(url, data, config);
75
- }, sub, query, data, config, sign_header, sign_key);
76
- }
77
- protected 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 }>
78
- {
79
- return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
80
- {
81
- return await axios.delete(url, config);
82
- }, sub, query, undefined, config, sign_header, sign_key);
83
- }
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
+
8
+ export abstract class BaseServer
9
+ {
10
+ protected domain: string;
11
+ public suppressErrors: boolean = false;
12
+ constructor(domain: string)
13
+ {
14
+ this.domain = domain;
15
+ }
16
+ protected abstract onBeforeRequest<ReqData = any>(url: string, config?: AxiosRequestConfig<ReqData>): void;
17
+ protected abstract onAfterRequest<ResData = any>(url: string, response: AxiosResponse<ResData>): void;
18
+ protected abstract onError(error: Error): void;
19
+ 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 }>
20
+ {
21
+ let url: string = URLOperation.getLink(this.domain, sub, query);
22
+ if (!config)
23
+ config = { headers: {} };
24
+ if (sign_header)
25
+ if (sign_key)
26
+ if (data)
27
+ if (config?.headers)
28
+ config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
29
+ try
30
+ {
31
+ this.onBeforeRequest(url, config);
32
+ let response: AxiosResponse<ResData> = await onRequest(url, data, config);
33
+ this.onAfterRequest(url, response);
34
+ return { response, data: response.data };
35
+ } catch (error)
36
+ {
37
+ if (error instanceof Error)
38
+ {
39
+ if (this.onError)
40
+ this.onError(error);
41
+ else
42
+ {
43
+ ConsoleOperation.warning("onError function has not been properly set.");
44
+ ConsoleOperation.error(error?.message);
45
+ }
46
+ if (axios.isAxiosError(error))
47
+ if (error?.response?.data)
48
+ if (!this.suppressErrors)
49
+ ErrorOperation.throwHTTP(error.response.status, error.response.data);
50
+ }
51
+ if (!this.suppressErrors)
52
+ throw error;
53
+ }
54
+ return {} as any;
55
+ }
56
+ protected async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
57
+ {
58
+ return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
59
+ {
60
+ return await axios.get(url, config);
61
+ }, sub, query, undefined, config);
62
+ }
63
+ protected 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 }>
64
+ {
65
+ return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
66
+ {
67
+ return await axios.post(url, data, config);
68
+ }, sub, query, data, config, sign_header, sign_key);
69
+ }
70
+ protected 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 }>
71
+ {
72
+ return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
73
+ {
74
+ return await axios.put(url, data, config);
75
+ }, sub, query, data, config, sign_header, sign_key);
76
+ }
77
+ protected 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 }>
78
+ {
79
+ return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
80
+ {
81
+ return await axios.delete(url, config);
82
+ }, sub, query, undefined, config, sign_header, sign_key);
83
+ }
84
84
  }
@@ -1,69 +1,69 @@
1
- export class ConsoleOperation
2
- {
3
- public static colors = {
4
- reset: '\x1b[0m',
5
- red: '\x1b[31m',
6
- green: '\x1b[32m',
7
- yellow: '\x1b[33m',
8
- blue: '\x1b[34m',
9
- magenta: '\x1b[35m',
10
- cyan: '\x1b[36m',
11
- white: '\x1b[37m',
12
- };
13
- static formatLogColor(message: string)
14
- {
15
- return (ConsoleOperation.colors.white + message + ConsoleOperation.colors.reset);
16
- }
17
- static formatInfoColor(message: string)
18
- {
19
- return (ConsoleOperation.colors.cyan + message + ConsoleOperation.colors.reset);
20
- }
21
- static formatTraceColor(message: string)
22
- {
23
- return (ConsoleOperation.colors.blue + message + ConsoleOperation.colors.reset);
24
- }
25
- static formatDebugColor(message: string)
26
- {
27
- return (ConsoleOperation.colors.magenta + message + ConsoleOperation.colors.reset);
28
- }
29
- static formatSuccessColor(message: string)
30
- {
31
- return (ConsoleOperation.colors.green + message + ConsoleOperation.colors.reset);
32
- }
33
- static formatWarningColor(message: string)
34
- {
35
- return (ConsoleOperation.colors.yellow + message + ConsoleOperation.colors.reset);
36
- }
37
- static formatErrorColor(message: string)
38
- {
39
- return (ConsoleOperation.colors.red + message + ConsoleOperation.colors.reset);
40
- }
41
- static log(message: string)
42
- {
43
- console.log(ConsoleOperation.formatLogColor(message));
44
- }
45
- static info(message: string)
46
- {
47
- console.info(ConsoleOperation.formatInfoColor(message));
48
- }
49
- static trace(message: string)
50
- {
51
- console.trace(ConsoleOperation.formatTraceColor(message));
52
- }
53
- static debug(message: string)
54
- {
55
- console.debug(ConsoleOperation.formatDebugColor(message));
56
- }
57
- static success(message: string)
58
- {
59
- console.info(ConsoleOperation.formatSuccessColor(message));
60
- }
61
- static warning(message: string)
62
- {
63
- console.warn(ConsoleOperation.formatWarningColor(message));
64
- }
65
- static error(message: string)
66
- {
67
- console.error(ConsoleOperation.formatErrorColor(message));
68
- }
1
+ export class ConsoleOperation
2
+ {
3
+ public static colors = {
4
+ reset: '\x1b[0m',
5
+ red: '\x1b[31m',
6
+ green: '\x1b[32m',
7
+ yellow: '\x1b[33m',
8
+ blue: '\x1b[34m',
9
+ magenta: '\x1b[35m',
10
+ cyan: '\x1b[36m',
11
+ white: '\x1b[37m',
12
+ };
13
+ static formatLogColor(message: string)
14
+ {
15
+ return (ConsoleOperation.colors.white + message + ConsoleOperation.colors.reset);
16
+ }
17
+ static formatInfoColor(message: string)
18
+ {
19
+ return (ConsoleOperation.colors.cyan + message + ConsoleOperation.colors.reset);
20
+ }
21
+ static formatTraceColor(message: string)
22
+ {
23
+ return (ConsoleOperation.colors.blue + message + ConsoleOperation.colors.reset);
24
+ }
25
+ static formatDebugColor(message: string)
26
+ {
27
+ return (ConsoleOperation.colors.magenta + message + ConsoleOperation.colors.reset);
28
+ }
29
+ static formatSuccessColor(message: string)
30
+ {
31
+ return (ConsoleOperation.colors.green + message + ConsoleOperation.colors.reset);
32
+ }
33
+ static formatWarningColor(message: string)
34
+ {
35
+ return (ConsoleOperation.colors.yellow + message + ConsoleOperation.colors.reset);
36
+ }
37
+ static formatErrorColor(message: string)
38
+ {
39
+ return (ConsoleOperation.colors.red + message + ConsoleOperation.colors.reset);
40
+ }
41
+ static log(message: string)
42
+ {
43
+ console.log(ConsoleOperation.formatLogColor(message));
44
+ }
45
+ static info(message: string)
46
+ {
47
+ console.info(ConsoleOperation.formatInfoColor(message));
48
+ }
49
+ static trace(message: string)
50
+ {
51
+ console.trace(ConsoleOperation.formatTraceColor(message));
52
+ }
53
+ static debug(message: string)
54
+ {
55
+ console.debug(ConsoleOperation.formatDebugColor(message));
56
+ }
57
+ static success(message: string)
58
+ {
59
+ console.info(ConsoleOperation.formatSuccessColor(message));
60
+ }
61
+ static warning(message: string)
62
+ {
63
+ console.warn(ConsoleOperation.formatWarningColor(message));
64
+ }
65
+ static error(message: string)
66
+ {
67
+ console.error(ConsoleOperation.formatErrorColor(message));
68
+ }
69
69
  }