namirasoft-core 1.3.48 → 1.3.49

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/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "framework": "npm",
9
9
  "application": "package",
10
10
  "private": false,
11
- "version": "1.3.48",
11
+ "version": "1.3.49",
12
12
  "author": "Amir Abolhasani",
13
13
  "license": "MIT",
14
14
  "main": "./dist/index.js",
@@ -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,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
  }
@@ -1,101 +1,101 @@
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 (!process.env.NAMIRASOFT_MUTE)
16
- if (this.mandatory)
17
- if (value == null || value == undefined)
18
- this.onMandatoryError();
19
- return value;
20
- }
21
- protected onMandatoryError(): void
22
- {
23
- if (!process.env.NAMIRASOFT_MUTE)
24
- ErrorOperation.throwHTTP(500, "Object value can not be converted.");
25
- }
26
- getString(_default: string = ""): string
27
- {
28
- let ans = this.checkMandatory(this.getNullString()) ?? _default;
29
- if (this.repair)
30
- ans = StringOperation.repair(ans);
31
- return ans;
32
- }
33
- getNullInt(): number | null
34
- {
35
- let str = this.getString();
36
- if (str == null)
37
- return null;
38
- let ans = parseInt(str);
39
- if (isNaN(ans))
40
- return null;
41
- return ans;
42
- }
43
- getInt(_default: number = 0): number
44
- {
45
- return this.checkMandatory(this.getNullInt()) ?? _default;
46
- }
47
- getNullFloat(): number | null
48
- {
49
- let str = this.getString();
50
- if (str == null)
51
- return null;
52
- let ans = parseFloat(str);
53
- if (isNaN(ans))
54
- return null;
55
- return ans;
56
- }
57
- getFloat(_default: number = 0): number
58
- {
59
- return this.checkMandatory(this.getNullFloat()) ?? _default;
60
- }
61
- getNullBoolean(): boolean | null
62
- {
63
- let str = this.getString();
64
- if (str == null)
65
- return null;
66
- let ans = str.toLowerCase();
67
- if (ans == "true")
68
- return true;
69
- if (ans == "false")
70
- return false;
71
- return null;
72
- }
73
- getBoolean(_default: boolean = false): boolean
74
- {
75
- return this.checkMandatory(this.getNullBoolean()) ?? _default;
76
- }
77
- getNullEnum<T>(enumObj: { [s: string]: T }): T | null
78
- {
79
- let str = this.getString();
80
- return Object.values(enumObj).find((enumValue) => enumValue === str) as T | null;
81
- }
82
- getEnum<T>(enumObj: { [s: string]: T }, _default: T): T
83
- {
84
- return this.checkMandatory(this.getNullEnum<T>(enumObj)) ?? _default;
85
- }
86
- getStringArray(delimiter: string = ","): string[]
87
- {
88
- let ans: string[] = this.getString("").split(delimiter);
89
- ans = ans.map(v => `${v}`);
90
- ans = ans.filter(x => x);
91
- return ans;
92
- }
93
- getIntArray(delimiter: string = ","): number[]
94
- {
95
- return this.getStringArray(delimiter).map(x => parseInt(x)).filter(x => !isNaN(x));
96
- }
97
- getFloatArray(delimiter: string = ","): number[]
98
- {
99
- return this.getStringArray(delimiter).map(x => parseFloat(x)).filter(x => !isNaN(x));
100
- }
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 (!process.env.NAMIRASOFT_MUTE)
16
+ if (this.mandatory)
17
+ if (value == null || value == undefined)
18
+ this.onMandatoryError();
19
+ return value;
20
+ }
21
+ protected onMandatoryError(): void
22
+ {
23
+ if (!process.env.NAMIRASOFT_MUTE)
24
+ ErrorOperation.throwHTTP(500, "Object value can not be converted.");
25
+ }
26
+ getString(_default: string = ""): string
27
+ {
28
+ let ans = this.checkMandatory(this.getNullString()) ?? _default;
29
+ if (this.repair)
30
+ ans = StringOperation.repair(ans);
31
+ return ans;
32
+ }
33
+ getNullInt(): number | null
34
+ {
35
+ let str = this.getString();
36
+ if (str == null)
37
+ return null;
38
+ let ans = parseInt(str);
39
+ if (isNaN(ans))
40
+ return null;
41
+ return ans;
42
+ }
43
+ getInt(_default: number = 0): number
44
+ {
45
+ return this.checkMandatory(this.getNullInt()) ?? _default;
46
+ }
47
+ getNullFloat(): number | null
48
+ {
49
+ let str = this.getString();
50
+ if (str == null)
51
+ return null;
52
+ let ans = parseFloat(str);
53
+ if (isNaN(ans))
54
+ return null;
55
+ return ans;
56
+ }
57
+ getFloat(_default: number = 0): number
58
+ {
59
+ return this.checkMandatory(this.getNullFloat()) ?? _default;
60
+ }
61
+ getNullBoolean(): boolean | null
62
+ {
63
+ let str = this.getString();
64
+ if (str == null)
65
+ return null;
66
+ let ans = str.toLowerCase();
67
+ if (ans == "true")
68
+ return true;
69
+ if (ans == "false")
70
+ return false;
71
+ return null;
72
+ }
73
+ getBoolean(_default: boolean = false): boolean
74
+ {
75
+ return this.checkMandatory(this.getNullBoolean()) ?? _default;
76
+ }
77
+ getNullEnum<T>(enumObj: { [s: string]: T }): T | null
78
+ {
79
+ let str = this.getString();
80
+ return Object.values(enumObj).find((enumValue) => enumValue === str) as T | null;
81
+ }
82
+ getEnum<T>(enumObj: { [s: string]: T }, _default: T): T
83
+ {
84
+ return this.checkMandatory(this.getNullEnum<T>(enumObj)) ?? _default;
85
+ }
86
+ getStringArray(delimiter: string = ","): string[]
87
+ {
88
+ let ans: string[] = this.getString("").split(delimiter);
89
+ ans = ans.map(v => `${v}`);
90
+ ans = ans.filter(x => x);
91
+ return ans;
92
+ }
93
+ getIntArray(delimiter: string = ","): number[]
94
+ {
95
+ return this.getStringArray(delimiter).map(x => parseInt(x)).filter(x => !isNaN(x));
96
+ }
97
+ getFloatArray(delimiter: string = ","): number[]
98
+ {
99
+ return this.getStringArray(delimiter).map(x => parseFloat(x)).filter(x => !isNaN(x));
100
+ }
101
101
  }