namirasoft-core 1.2.13 → 1.2.14

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/.gitlab-ci.yml CHANGED
@@ -1,14 +1,14 @@
1
- stages:
2
- - npm-publish
3
-
4
- publish:
5
- stage: npm-publish
6
- script:
7
- - rm -rf node_modules
8
- - rm -rf dist
9
- - ncu -u
10
- - npm i
11
- - tsc
12
- - npm publish
13
- only:
1
+ stages:
2
+ - npm-publish
3
+
4
+ publish:
5
+ stage: npm-publish
6
+ script:
7
+ - rm -rf node_modules
8
+ - rm -rf dist
9
+ - ncu -u
10
+ - npm i
11
+ - tsc
12
+ - npm publish
13
+ only:
14
14
  - master
@@ -27,15 +27,23 @@ exports.EncryptionOperation = void 0;
27
27
  const crypto = __importStar(require("node:crypto"));
28
28
  class EncryptionOperation {
29
29
  static encrypt(algorithm, secret, message) {
30
- const iv = Buffer.alloc(16, 0);
30
+ const iv = crypto.randomBytes(16);
31
31
  const cipher = crypto.createCipheriv(algorithm, secret, iv);
32
- return cipher.update(JSON.stringify(message), 'utf8', 'hex') + cipher.final('hex');
32
+ const encrypted = Buffer.concat([cipher.update(JSON.stringify(message), 'utf8'), cipher.final()]);
33
+ const authTag = cipher.getAuthTag();
34
+ const ivHex = iv.toString('hex');
35
+ const authTagHex = authTag.toString('hex');
36
+ const encryptedHex = encrypted.toString('hex');
37
+ return [ivHex, authTagHex, encryptedHex].join(";");
33
38
  }
34
39
  static decrypt(algorithm, secret, message) {
35
- let encryptedText = Buffer.from(message, 'hex');
36
- const iv = Buffer.alloc(16, 0);
40
+ const [ivHex, authTagHex, encryptedHex] = message.split(';');
41
+ const iv = Buffer.from(ivHex, 'hex');
42
+ const authTag = Buffer.from(authTagHex, 'hex');
43
+ const encrypte = Buffer.from(encryptedHex, 'hex');
37
44
  const decipher = crypto.createDecipheriv(algorithm, secret, iv);
38
- let decrypted = decipher.update(encryptedText);
45
+ decipher.setAuthTag(authTag);
46
+ let decrypted = decipher.update(encrypte);
39
47
  decrypted = Buffer.concat([decrypted, decipher.final()]);
40
48
  return JSON.parse(decrypted.toString());
41
49
  }
@@ -1 +1 @@
1
- {"version":3,"file":"EncryptionOperation.js","sourceRoot":"","sources":["../src/EncryptionOperation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAsC;AAEtC,MAAa,mBAAmB;IAEpB,MAAM,CAAC,OAAO,CAAI,SAAiB,EAAE,MAAc,EAAE,OAAU;QAEnE,MAAM,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QAC/B,MAAM,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QAC5D,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvF,CAAC;IACO,MAAM,CAAC,OAAO,CAAI,SAAiB,EAAE,MAAc,EAAE,OAAe;QAExE,IAAI,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAEhD,MAAM,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QAC/B,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QAEhE,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAC/C,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QACzD,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC5C,CAAC;IACD,MAAM,CAAC,gBAAgB,CAAI,MAAc,EAAE,OAAU;QAEjD,OAAO,mBAAmB,CAAC,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvE,CAAC;IACD,MAAM,CAAC,gBAAgB,CAAI,MAAc,EAAE,OAAe;QAEtD,OAAO,mBAAmB,CAAC,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvE,CAAC;CACJ;AA3BD,kDA2BC"}
1
+ {"version":3,"file":"EncryptionOperation.js","sourceRoot":"","sources":["../src/EncryptionOperation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAsC;AAEtC,MAAa,mBAAmB;IAEpB,MAAM,CAAC,OAAO,CAAI,SAAiB,EAAE,MAAc,EAAE,OAAU;QAEnE,MAAM,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QAC5D,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAClG,MAAM,OAAO,GAAI,MAAc,CAAC,UAAU,EAAE,CAAC;QAC7C,MAAM,KAAK,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC3C,MAAM,YAAY,GAAG,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC/C,OAAO,CAAC,KAAK,EAAE,UAAU,EAAE,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACvD,CAAC;IACO,MAAM,CAAC,OAAO,CAAI,SAAiB,EAAE,MAAc,EAAE,OAAe;QAExE,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,YAAY,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC7D,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACrC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC/C,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QAElD,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QAC/D,QAAgB,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAEtC,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC1C,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QACzD,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC5C,CAAC;IACD,MAAM,CAAC,gBAAgB,CAAI,MAAc,EAAE,OAAU;QAEjD,OAAO,mBAAmB,CAAC,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvE,CAAC;IACD,MAAM,CAAC,gBAAgB,CAAI,MAAc,EAAE,OAAe;QAEtD,OAAO,mBAAmB,CAAC,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvE,CAAC;CACJ;AAnCD,kDAmCC"}
@@ -0,0 +1,4 @@
1
+ export declare class SignOperation {
2
+ static sign(secret: string, message: any): string;
3
+ static isValid(key: string, values: any, signature: string): boolean;
4
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SignOperation = void 0;
4
+ const HashOperation_1 = require("./HashOperation");
5
+ class SignOperation {
6
+ static sign(secret, message) {
7
+ return HashOperation_1.HashOperation.sha256Secret(secret, message);
8
+ }
9
+ static isValid(key, values, signature) {
10
+ return this.sign(key, values).toLowerCase() === signature.toLowerCase();
11
+ }
12
+ }
13
+ exports.SignOperation = SignOperation;
14
+ //# sourceMappingURL=SignOperation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SignOperation.js","sourceRoot":"","sources":["../src/SignOperation.ts"],"names":[],"mappings":";;;AAAA,mDAAgD;AAEhD,MAAa,aAAa;IAEtB,MAAM,CAAC,IAAI,CAAC,MAAc,EAAE,OAAY;QAEpC,OAAO,6BAAa,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,GAAW,EAAE,MAAW,EAAE,SAAiB;QAEtD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,WAAW,EAAE,KAAK,SAAS,CAAC,WAAW,EAAE,CAAC;IAC5E,CAAC;CACJ;AAVD,sCAUC"}
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
- {
2
- "name": "namirasoft-core",
3
- "description": "Namira Software Corporation Core NPM Package",
4
- "version": "1.2.13",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "scripts": {},
8
- "author": "Amir Abolhasani",
9
- "license": "MIT",
10
- "dependencies": {
11
- "@types/node": "^20.11.24",
12
- "axios": "^1.6.7",
13
- "moment": "^2.30.1",
14
- "phone": "^3.1.42"
15
- }
1
+ {
2
+ "name": "namirasoft-core",
3
+ "description": "Namira Software Corporation Core NPM Package",
4
+ "version": "1.2.14",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "scripts": {},
8
+ "author": "Amir Abolhasani",
9
+ "license": "MIT",
10
+ "dependencies": {
11
+ "@types/node": "^20.11.25",
12
+ "axios": "^1.6.7",
13
+ "moment": "^2.30.1",
14
+ "phone": "^3.1.42"
15
+ }
16
16
  }
@@ -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
  }
package/src/BaseServer.ts CHANGED
@@ -1,73 +1,73 @@
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
-
7
- export abstract class BaseServer
8
- {
9
- protected domain: string = "";
10
- constructor(domain: string)
11
- {
12
- this.domain = domain;
13
- }
14
- protected abstract onBeforeRequest<ReqData = any>(url: string, config?: AxiosRequestConfig<ReqData>): void;
15
- protected abstract onAfterRequest<ResData = any>(url: string, response: AxiosResponse<ResData>): void;
16
- protected abstract onError(error: Error): void;
17
- 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 }>
18
- {
19
- let url: string = URLOperation.getLink(this.domain, sub, query);
20
- if (!config)
21
- config = { headers: {} };
22
- if (sign_header)
23
- if (sign_key)
24
- if (data)
25
- if (config?.headers)
26
- config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
27
- try
28
- {
29
- this.onBeforeRequest(url, config);
30
- let response: AxiosResponse<ResData> = await onRequest(url, data, config);
31
- this.onAfterRequest(url, response);
32
- return { response, data: response.data };
33
- } catch (error)
34
- {
35
- if (error instanceof Error)
36
- {
37
- this.onError(error);
38
- if (axios.isAxiosError(error))
39
- if (error?.response?.data)
40
- ErrorOperation.throwHTTP(error.response.status, error.response.data);
41
- }
42
- throw error;
43
- }
44
- }
45
- protected async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
46
- {
47
- return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
48
- {
49
- return await axios.get(url, config);
50
- }, sub, query, undefined, config);
51
- }
52
- 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 }>
53
- {
54
- return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
55
- {
56
- return await axios.post(url, data, config);
57
- }, sub, query, data, config, sign_header, sign_key);
58
- }
59
- 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 }>
60
- {
61
- return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
62
- {
63
- return await axios.put(url, data, config);
64
- }, sub, query, data, config, sign_header, sign_key);
65
- }
66
- 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 }>
67
- {
68
- return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
69
- {
70
- return await axios.delete(url, config);
71
- }, sub, query, undefined, config, sign_header, sign_key);
72
- }
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
+
7
+ export abstract class BaseServer
8
+ {
9
+ protected domain: string = "";
10
+ constructor(domain: string)
11
+ {
12
+ this.domain = domain;
13
+ }
14
+ protected abstract onBeforeRequest<ReqData = any>(url: string, config?: AxiosRequestConfig<ReqData>): void;
15
+ protected abstract onAfterRequest<ResData = any>(url: string, response: AxiosResponse<ResData>): void;
16
+ protected abstract onError(error: Error): void;
17
+ 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 }>
18
+ {
19
+ let url: string = URLOperation.getLink(this.domain, sub, query);
20
+ if (!config)
21
+ config = { headers: {} };
22
+ if (sign_header)
23
+ if (sign_key)
24
+ if (data)
25
+ if (config?.headers)
26
+ config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
27
+ try
28
+ {
29
+ this.onBeforeRequest(url, config);
30
+ let response: AxiosResponse<ResData> = await onRequest(url, data, config);
31
+ this.onAfterRequest(url, response);
32
+ return { response, data: response.data };
33
+ } catch (error)
34
+ {
35
+ if (error instanceof Error)
36
+ {
37
+ this.onError(error);
38
+ if (axios.isAxiosError(error))
39
+ if (error?.response?.data)
40
+ ErrorOperation.throwHTTP(error.response.status, error.response.data);
41
+ }
42
+ throw error;
43
+ }
44
+ }
45
+ protected async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
46
+ {
47
+ return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
48
+ {
49
+ return await axios.get(url, config);
50
+ }, sub, query, undefined, config);
51
+ }
52
+ 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 }>
53
+ {
54
+ return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
55
+ {
56
+ return await axios.post(url, data, config);
57
+ }, sub, query, data, config, sign_header, sign_key);
58
+ }
59
+ 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 }>
60
+ {
61
+ return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
62
+ {
63
+ return await axios.put(url, data, config);
64
+ }, sub, query, data, config, sign_header, sign_key);
65
+ }
66
+ 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 }>
67
+ {
68
+ return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
69
+ {
70
+ return await axios.delete(url, config);
71
+ }, sub, query, undefined, config, sign_header, sign_key);
72
+ }
73
73
  }
@@ -1,99 +1,99 @@
1
- import { ErrorOperation } from "./ErrorOperation";
2
- import { StringOperation } from "./StringOperation";
3
-
4
- export abstract class ConvertService
5
- {
6
- repair: boolean = true;
7
- private mandatory: boolean;
8
- constructor(mandatory: boolean = false)
9
- {
10
- this.mandatory = mandatory;
11
- }
12
- abstract getNullString(): string | null;
13
- private checkMandatory<D>(value: D): D
14
- {
15
- if (this.mandatory)
16
- if (value == null || value == undefined)
17
- this.onMandatoryError();
18
- return value;
19
- }
20
- protected onMandatoryError(): void
21
- {
22
- ErrorOperation.throwHTTP(500, "Object value can not be converted.");
23
- }
24
- getString(_default: string = ""): string
25
- {
26
- let ans = this.checkMandatory(this.getNullString()) ?? _default;
27
- if (this.repair)
28
- ans = StringOperation.repair(ans);
29
- return ans;
30
- }
31
- getNullInt(): number | null
32
- {
33
- let str = this.getString();
34
- if (str == null)
35
- return null;
36
- let ans = parseInt(str);
37
- if (isNaN(ans))
38
- return null;
39
- return ans;
40
- }
41
- getInt(_default: number = 0): number
42
- {
43
- return this.checkMandatory(this.getNullInt()) ?? _default;
44
- }
45
- getNullFloat(): number | null
46
- {
47
- let str = this.getString();
48
- if (str == null)
49
- return null;
50
- let ans = parseFloat(str);
51
- if (isNaN(ans))
52
- return null;
53
- return ans;
54
- }
55
- getFloat(_default: number = 0): number
56
- {
57
- return this.checkMandatory(this.getNullFloat()) ?? _default;
58
- }
59
- getNullBoolean(): boolean | null
60
- {
61
- let str = this.getString();
62
- if (str == null)
63
- return null;
64
- let ans = str.toLowerCase();
65
- if (ans == "true")
66
- return true;
67
- if (ans == "false")
68
- return false;
69
- return null;
70
- }
71
- getBoolean(_default: boolean = false): boolean
72
- {
73
- return this.checkMandatory(this.getNullBoolean()) ?? _default;
74
- }
75
- getNullEnum<T>(enumObj: { [s: string]: T }): T | null
76
- {
77
- let str = this.getString();
78
- return Object.values(enumObj).find((enumValue) => enumValue === str) as T | null;
79
- }
80
- getEnum<T>(enumObj: { [s: string]: T }, _default: T): T
81
- {
82
- return this.checkMandatory(this.getNullEnum<T>(enumObj)) ?? _default;
83
- }
84
- getStringArray(delimiter: string = ","): string[]
85
- {
86
- let ans: string[] = this.getString("").split(delimiter);
87
- ans = ans.map(v => `${v}`);
88
- ans = ans.filter(x => x);
89
- return ans;
90
- }
91
- getIntArray(delimiter: string = ","): number[]
92
- {
93
- return this.getStringArray(delimiter).map(x => parseInt(x)).filter(x => !isNaN(x));
94
- }
95
- getFloatArray(delimiter: string = ","): number[]
96
- {
97
- return this.getStringArray(delimiter).map(x => parseFloat(x)).filter(x => !isNaN(x));
98
- }
1
+ import { ErrorOperation } from "./ErrorOperation";
2
+ import { StringOperation } from "./StringOperation";
3
+
4
+ export abstract class ConvertService
5
+ {
6
+ repair: boolean = true;
7
+ private mandatory: boolean;
8
+ constructor(mandatory: boolean = false)
9
+ {
10
+ this.mandatory = mandatory;
11
+ }
12
+ abstract getNullString(): string | null;
13
+ private checkMandatory<D>(value: D): D
14
+ {
15
+ if (this.mandatory)
16
+ if (value == null || value == undefined)
17
+ this.onMandatoryError();
18
+ return value;
19
+ }
20
+ protected onMandatoryError(): void
21
+ {
22
+ ErrorOperation.throwHTTP(500, "Object value can not be converted.");
23
+ }
24
+ getString(_default: string = ""): string
25
+ {
26
+ let ans = this.checkMandatory(this.getNullString()) ?? _default;
27
+ if (this.repair)
28
+ ans = StringOperation.repair(ans);
29
+ return ans;
30
+ }
31
+ getNullInt(): number | null
32
+ {
33
+ let str = this.getString();
34
+ if (str == null)
35
+ return null;
36
+ let ans = parseInt(str);
37
+ if (isNaN(ans))
38
+ return null;
39
+ return ans;
40
+ }
41
+ getInt(_default: number = 0): number
42
+ {
43
+ return this.checkMandatory(this.getNullInt()) ?? _default;
44
+ }
45
+ getNullFloat(): number | null
46
+ {
47
+ let str = this.getString();
48
+ if (str == null)
49
+ return null;
50
+ let ans = parseFloat(str);
51
+ if (isNaN(ans))
52
+ return null;
53
+ return ans;
54
+ }
55
+ getFloat(_default: number = 0): number
56
+ {
57
+ return this.checkMandatory(this.getNullFloat()) ?? _default;
58
+ }
59
+ getNullBoolean(): boolean | null
60
+ {
61
+ let str = this.getString();
62
+ if (str == null)
63
+ return null;
64
+ let ans = str.toLowerCase();
65
+ if (ans == "true")
66
+ return true;
67
+ if (ans == "false")
68
+ return false;
69
+ return null;
70
+ }
71
+ getBoolean(_default: boolean = false): boolean
72
+ {
73
+ return this.checkMandatory(this.getNullBoolean()) ?? _default;
74
+ }
75
+ getNullEnum<T>(enumObj: { [s: string]: T }): T | null
76
+ {
77
+ let str = this.getString();
78
+ return Object.values(enumObj).find((enumValue) => enumValue === str) as T | null;
79
+ }
80
+ getEnum<T>(enumObj: { [s: string]: T }, _default: T): T
81
+ {
82
+ return this.checkMandatory(this.getNullEnum<T>(enumObj)) ?? _default;
83
+ }
84
+ getStringArray(delimiter: string = ","): string[]
85
+ {
86
+ let ans: string[] = this.getString("").split(delimiter);
87
+ ans = ans.map(v => `${v}`);
88
+ ans = ans.filter(x => x);
89
+ return ans;
90
+ }
91
+ getIntArray(delimiter: string = ","): number[]
92
+ {
93
+ return this.getStringArray(delimiter).map(x => parseInt(x)).filter(x => !isNaN(x));
94
+ }
95
+ getFloatArray(delimiter: string = ","): number[]
96
+ {
97
+ return this.getStringArray(delimiter).map(x => parseFloat(x)).filter(x => !isNaN(x));
98
+ }
99
99
  }