namirasoft-core 1.4.27 → 1.4.29
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/EncryptionOperation.js +17 -7
- package/dist/EncryptionOperation.js.map +1 -1
- package/dist/HashOperation.js +17 -7
- package/dist/HashOperation.js.map +1 -1
- package/dist/PhoneOperation.js +17 -7
- package/dist/PhoneOperation.js.map +1 -1
- package/dist/RegexTemplate.d.ts +7 -0
- package/dist/RegexTemplate.js +12 -0
- package/dist/RegexTemplate.js.map +1 -0
- package/dist/SearchOperation.d.ts +1 -1
- package/dist/SearchOperation.js +2 -0
- package/dist/SearchOperation.js.map +1 -1
- package/dist/SetTimeouService.d.ts +7 -0
- package/dist/SetTimeouService.js +20 -0
- package/dist/SetTimeouService.js.map +1 -0
- package/dist/TimeOperation.d.ts +2 -0
- package/dist/TimeOperation.js +6 -0
- package/dist/TimeOperation.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +26 -26
- package/src/BaseDatabaseRow.ts +6 -6
- package/src/BaseMetaColumn.ts +17 -17
- package/src/BaseMetaDatabase.ts +30 -30
- package/src/BaseMetaTable.ts +40 -40
- package/src/BaseServer.ts +113 -113
- package/src/BaseUUID.ts +55 -55
- package/src/CacheService.ts +57 -57
- package/src/ConsoleOperation.ts +68 -68
- package/src/ConvertService.ts +100 -100
- package/src/CookieService.ts +33 -33
- package/src/Countries.ts +486 -486
- package/src/Country.ts +21 -21
- package/src/CountryOperation.ts +98 -98
- package/src/EncodingOperation.ts +12 -12
- package/src/EncryptionOperation.ts +40 -40
- package/src/EnvService.ts +22 -22
- package/src/ErrorOperation.ts +13 -13
- package/src/FileOperation.ts +57 -57
- package/src/FilterItem.ts +128 -128
- package/src/FilterItemColumnType.ts +6 -6
- package/src/FilterItemOperator.ts +51 -51
- package/src/GeoOperation.ts +18 -18
- package/src/HTTPError.ts +8 -8
- package/src/HTTPMethod.ts +6 -6
- package/src/HashOperation.ts +24 -24
- package/src/IStorage.ts +5 -5
- package/src/IStorageCookie.ts +52 -52
- package/src/IStorageJsonFile.ts +45 -45
- package/src/IStorageLocal.ts +16 -16
- package/src/IStorageMemory.ts +17 -17
- package/src/NamingConvention.ts +169 -169
- package/src/ObjectService.ts +27 -27
- package/src/PackageService.ts +76 -76
- package/src/PhoneOperation.ts +8 -8
- package/src/PriceOperation.ts +18 -18
- package/src/SearchOperation.ts +31 -29
- package/src/SetTimeouService.ts +22 -0
- package/src/SortItem.ts +88 -88
- package/src/StringOperation.ts +18 -18
- package/src/TimeOperation.ts +271 -262
- package/src/URLOperation.ts +54 -54
- package/src/VersionOperation.ts +46 -46
- package/src/index.ts +42 -41
package/src/BaseServer.ts
CHANGED
|
@@ -1,114 +1,114 @@
|
|
|
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
|
-
|
|
9
|
-
export abstract class BaseServer
|
|
10
|
-
{
|
|
11
|
-
protected base_url: string;
|
|
12
|
-
public suppressErrors: boolean = false;
|
|
13
|
-
constructor(base_url: string)
|
|
14
|
-
{
|
|
15
|
-
this.base_url = base_url;
|
|
16
|
-
}
|
|
17
|
-
private onBeforeRequests: ((url: string, config?: AxiosRequestConfig) => void)[] = [];
|
|
18
|
-
private onAfterRequests: ((url: string, response: AxiosResponse) => void)[] = [];
|
|
19
|
-
protected abstract onBeforeRequest<ReqData = any>(url: string, config?: AxiosRequestConfig<ReqData>): void;
|
|
20
|
-
protected abstract onAfterRequest<ResData = any>(url: string, response: AxiosResponse<ResData>): void;
|
|
21
|
-
protected abstract onError(error: Error): void;
|
|
22
|
-
addOnBeforeRequest(onBeforeRequest: (url: string, config?: AxiosRequestConfig) => void)
|
|
23
|
-
{
|
|
24
|
-
this.onBeforeRequests.push(onBeforeRequest);
|
|
25
|
-
}
|
|
26
|
-
addOnAfterRequest(onAfterRequest: (url: string, response: AxiosResponse) => void)
|
|
27
|
-
{
|
|
28
|
-
this.onAfterRequests.push(onAfterRequest);
|
|
29
|
-
}
|
|
30
|
-
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 }>
|
|
31
|
-
{
|
|
32
|
-
let url: string = URLOperation.getLink(this.base_url, sub, query);
|
|
33
|
-
if (!config)
|
|
34
|
-
config = { headers: {} };
|
|
35
|
-
if (sign_header)
|
|
36
|
-
if (sign_key)
|
|
37
|
-
if (data)
|
|
38
|
-
if (config?.headers)
|
|
39
|
-
config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
|
|
40
|
-
try
|
|
41
|
-
{
|
|
42
|
-
this.onBeforeRequest(url, config);
|
|
43
|
-
this.onBeforeRequests.forEach(onBeforeRequest =>
|
|
44
|
-
{
|
|
45
|
-
onBeforeRequest(url, config);
|
|
46
|
-
});
|
|
47
|
-
let response: AxiosResponse<ResData> = await onRequest(url, data, config);
|
|
48
|
-
this.onAfterRequest(url, response);
|
|
49
|
-
this.onAfterRequests.forEach(onAfterRequest =>
|
|
50
|
-
{
|
|
51
|
-
onAfterRequest(url, response);
|
|
52
|
-
});
|
|
53
|
-
return { response, data: response.data };
|
|
54
|
-
} catch (error)
|
|
55
|
-
{
|
|
56
|
-
if (error instanceof Error)
|
|
57
|
-
{
|
|
58
|
-
let BASESERVER_ERROR_VERBOSE = new EnvService("BASESERVER_ERROR_VERBOSE", false).getBoolean(false);
|
|
59
|
-
|
|
60
|
-
if (this.onError)
|
|
61
|
-
{
|
|
62
|
-
if (BASESERVER_ERROR_VERBOSE)
|
|
63
|
-
error.message = `Error calling url '${url}' ` + error.message;
|
|
64
|
-
this.onError(error);
|
|
65
|
-
}
|
|
66
|
-
else
|
|
67
|
-
{
|
|
68
|
-
ConsoleOperation.warning("onError function has not been properly set.");
|
|
69
|
-
ConsoleOperation.error(error?.message);
|
|
70
|
-
}
|
|
71
|
-
if (axios.isAxiosError(error))
|
|
72
|
-
if (error?.response?.data)
|
|
73
|
-
if (!this.suppressErrors)
|
|
74
|
-
{
|
|
75
|
-
if (BASESERVER_ERROR_VERBOSE)
|
|
76
|
-
ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
|
|
77
|
-
else
|
|
78
|
-
ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (!this.suppressErrors)
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
return {} as any;
|
|
85
|
-
}
|
|
86
|
-
protected async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
87
|
-
{
|
|
88
|
-
return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
|
|
89
|
-
{
|
|
90
|
-
return await axios.get(url, config);
|
|
91
|
-
}, sub, query, undefined, config);
|
|
92
|
-
}
|
|
93
|
-
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 }>
|
|
94
|
-
{
|
|
95
|
-
return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
|
|
96
|
-
{
|
|
97
|
-
return await axios.post(url, data, config);
|
|
98
|
-
}, sub, query, data, config, sign_header, sign_key);
|
|
99
|
-
}
|
|
100
|
-
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 }>
|
|
101
|
-
{
|
|
102
|
-
return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
|
|
103
|
-
{
|
|
104
|
-
return await axios.put(url, data, config);
|
|
105
|
-
}, sub, query, data, config, sign_header, sign_key);
|
|
106
|
-
}
|
|
107
|
-
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 }>
|
|
108
|
-
{
|
|
109
|
-
return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
|
|
110
|
-
{
|
|
111
|
-
return await axios.delete(url, config);
|
|
112
|
-
}, sub, query, undefined, config, sign_header, sign_key);
|
|
113
|
-
}
|
|
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
|
+
|
|
9
|
+
export abstract class BaseServer
|
|
10
|
+
{
|
|
11
|
+
protected base_url: string;
|
|
12
|
+
public suppressErrors: boolean = false;
|
|
13
|
+
constructor(base_url: string)
|
|
14
|
+
{
|
|
15
|
+
this.base_url = base_url;
|
|
16
|
+
}
|
|
17
|
+
private onBeforeRequests: ((url: string, config?: AxiosRequestConfig) => void)[] = [];
|
|
18
|
+
private onAfterRequests: ((url: string, response: AxiosResponse) => void)[] = [];
|
|
19
|
+
protected abstract onBeforeRequest<ReqData = any>(url: string, config?: AxiosRequestConfig<ReqData>): void;
|
|
20
|
+
protected abstract onAfterRequest<ResData = any>(url: string, response: AxiosResponse<ResData>): void;
|
|
21
|
+
protected abstract onError(error: Error): void;
|
|
22
|
+
addOnBeforeRequest(onBeforeRequest: (url: string, config?: AxiosRequestConfig) => void)
|
|
23
|
+
{
|
|
24
|
+
this.onBeforeRequests.push(onBeforeRequest);
|
|
25
|
+
}
|
|
26
|
+
addOnAfterRequest(onAfterRequest: (url: string, response: AxiosResponse) => void)
|
|
27
|
+
{
|
|
28
|
+
this.onAfterRequests.push(onAfterRequest);
|
|
29
|
+
}
|
|
30
|
+
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 }>
|
|
31
|
+
{
|
|
32
|
+
let url: string = URLOperation.getLink(this.base_url, sub, query);
|
|
33
|
+
if (!config)
|
|
34
|
+
config = { headers: {} };
|
|
35
|
+
if (sign_header)
|
|
36
|
+
if (sign_key)
|
|
37
|
+
if (data)
|
|
38
|
+
if (config?.headers)
|
|
39
|
+
config.headers[sign_header] = HashOperation.SHA256Secret(sign_key, data);
|
|
40
|
+
try
|
|
41
|
+
{
|
|
42
|
+
this.onBeforeRequest(url, config);
|
|
43
|
+
this.onBeforeRequests.forEach(onBeforeRequest =>
|
|
44
|
+
{
|
|
45
|
+
onBeforeRequest(url, config);
|
|
46
|
+
});
|
|
47
|
+
let response: AxiosResponse<ResData> = await onRequest(url, data, config);
|
|
48
|
+
this.onAfterRequest(url, response);
|
|
49
|
+
this.onAfterRequests.forEach(onAfterRequest =>
|
|
50
|
+
{
|
|
51
|
+
onAfterRequest(url, response);
|
|
52
|
+
});
|
|
53
|
+
return { response, data: response.data };
|
|
54
|
+
} catch (error)
|
|
55
|
+
{
|
|
56
|
+
if (error instanceof Error)
|
|
57
|
+
{
|
|
58
|
+
let BASESERVER_ERROR_VERBOSE = new EnvService("BASESERVER_ERROR_VERBOSE", false).getBoolean(false);
|
|
59
|
+
|
|
60
|
+
if (this.onError)
|
|
61
|
+
{
|
|
62
|
+
if (BASESERVER_ERROR_VERBOSE)
|
|
63
|
+
error.message = `Error calling url '${url}' ` + error.message;
|
|
64
|
+
this.onError(error);
|
|
65
|
+
}
|
|
66
|
+
else
|
|
67
|
+
{
|
|
68
|
+
ConsoleOperation.warning("onError function has not been properly set.");
|
|
69
|
+
ConsoleOperation.error(error?.message);
|
|
70
|
+
}
|
|
71
|
+
if (axios.isAxiosError(error))
|
|
72
|
+
if (error?.response?.data)
|
|
73
|
+
if (!this.suppressErrors)
|
|
74
|
+
{
|
|
75
|
+
if (BASESERVER_ERROR_VERBOSE)
|
|
76
|
+
ErrorOperation.throwHTTP(error.response.status, `Error calling url '${url}' ` + JSON.stringify(error.response.data));
|
|
77
|
+
else
|
|
78
|
+
ErrorOperation.throwHTTP(error.response.status, JSON.stringify(error.response.data));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!this.suppressErrors)
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
return {} as any;
|
|
85
|
+
}
|
|
86
|
+
protected async _get<ResData = any, ReqData = any>(sub: string, query?: { [name: string]: ParsedNameValue }, config?: AxiosRequestConfig<ReqData>): Promise<{ response: AxiosResponse<ResData>, data: ResData }>
|
|
87
|
+
{
|
|
88
|
+
return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
|
|
89
|
+
{
|
|
90
|
+
return await axios.get(url, config);
|
|
91
|
+
}, sub, query, undefined, config);
|
|
92
|
+
}
|
|
93
|
+
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 }>
|
|
94
|
+
{
|
|
95
|
+
return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
|
|
96
|
+
{
|
|
97
|
+
return await axios.post(url, data, config);
|
|
98
|
+
}, sub, query, data, config, sign_header, sign_key);
|
|
99
|
+
}
|
|
100
|
+
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 }>
|
|
101
|
+
{
|
|
102
|
+
return await this._request(async (url: string, data: ReqData, config?: AxiosRequestConfig<ReqData>) =>
|
|
103
|
+
{
|
|
104
|
+
return await axios.put(url, data, config);
|
|
105
|
+
}, sub, query, data, config, sign_header, sign_key);
|
|
106
|
+
}
|
|
107
|
+
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 }>
|
|
108
|
+
{
|
|
109
|
+
return await this._request(async (url: string, _?: any, config?: AxiosRequestConfig<ReqData>) =>
|
|
110
|
+
{
|
|
111
|
+
return await axios.delete(url, config);
|
|
112
|
+
}, sub, query, undefined, config, sign_header, sign_key);
|
|
113
|
+
}
|
|
114
114
|
}
|
package/src/BaseUUID.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { v4 } from "uuid";
|
|
2
|
-
|
|
3
|
-
export class BaseUUID
|
|
4
|
-
{
|
|
5
|
-
private static SIZE = 20;
|
|
6
|
-
static isValid(id: any)
|
|
7
|
-
{
|
|
8
|
-
let value = id + "";
|
|
9
|
-
if (value.length != BaseUUID.SIZE)
|
|
10
|
-
return false;
|
|
11
|
-
let parts = id.split("@")[0].split("-");
|
|
12
|
-
if (parts.length < 2)
|
|
13
|
-
return false;
|
|
14
|
-
return /^[a-z][a-z][a-z]$/.test(parts[0]);
|
|
15
|
-
}
|
|
16
|
-
static getShort(id: string)
|
|
17
|
-
{
|
|
18
|
-
let parts = id.split("@")[0].split("-");
|
|
19
|
-
let ans = [];
|
|
20
|
-
for (let index = 0; index < parts.length - 1; index++)
|
|
21
|
-
{
|
|
22
|
-
const part = parts[index];
|
|
23
|
-
if (/^[a-z][a-z][a-z]$/.test(part))
|
|
24
|
-
ans.push(part);
|
|
25
|
-
else
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
return ans.join("-");
|
|
29
|
-
}
|
|
30
|
-
short: string;
|
|
31
|
-
constructor(short: string)
|
|
32
|
-
{
|
|
33
|
-
this.short = short;
|
|
34
|
-
}
|
|
35
|
-
static uuid(lenght: number = 16): string
|
|
36
|
-
{
|
|
37
|
-
return v4().replace(/-/g, '').substring(0, lenght);
|
|
38
|
-
}
|
|
39
|
-
static isChild(id: string): boolean
|
|
40
|
-
{
|
|
41
|
-
let index = id.indexOf("@")
|
|
42
|
-
return index != -1;
|
|
43
|
-
}
|
|
44
|
-
static changeToChild(owner_id: string, name: string): string
|
|
45
|
-
{
|
|
46
|
-
let index = owner_id.indexOf("@")
|
|
47
|
-
if (index != -1)
|
|
48
|
-
return owner_id.substring(0, index + 1) + name.substring(0, BaseUUID.SIZE - index - 1).padStart(BaseUUID.SIZE - index - 1, '-');
|
|
49
|
-
return "";
|
|
50
|
-
}
|
|
51
|
-
new()
|
|
52
|
-
{
|
|
53
|
-
let ans = this.short + "-" + BaseUUID.uuid();
|
|
54
|
-
return ans.substring(0, BaseUUID.SIZE);
|
|
55
|
-
}
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
|
|
3
|
+
export class BaseUUID
|
|
4
|
+
{
|
|
5
|
+
private static SIZE = 20;
|
|
6
|
+
static isValid(id: any)
|
|
7
|
+
{
|
|
8
|
+
let value = id + "";
|
|
9
|
+
if (value.length != BaseUUID.SIZE)
|
|
10
|
+
return false;
|
|
11
|
+
let parts = id.split("@")[0].split("-");
|
|
12
|
+
if (parts.length < 2)
|
|
13
|
+
return false;
|
|
14
|
+
return /^[a-z][a-z][a-z]$/.test(parts[0]);
|
|
15
|
+
}
|
|
16
|
+
static getShort(id: string)
|
|
17
|
+
{
|
|
18
|
+
let parts = id.split("@")[0].split("-");
|
|
19
|
+
let ans = [];
|
|
20
|
+
for (let index = 0; index < parts.length - 1; index++)
|
|
21
|
+
{
|
|
22
|
+
const part = parts[index];
|
|
23
|
+
if (/^[a-z][a-z][a-z]$/.test(part))
|
|
24
|
+
ans.push(part);
|
|
25
|
+
else
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
return ans.join("-");
|
|
29
|
+
}
|
|
30
|
+
short: string;
|
|
31
|
+
constructor(short: string)
|
|
32
|
+
{
|
|
33
|
+
this.short = short;
|
|
34
|
+
}
|
|
35
|
+
static uuid(lenght: number = 16): string
|
|
36
|
+
{
|
|
37
|
+
return v4().replace(/-/g, '').substring(0, lenght);
|
|
38
|
+
}
|
|
39
|
+
static isChild(id: string): boolean
|
|
40
|
+
{
|
|
41
|
+
let index = id.indexOf("@")
|
|
42
|
+
return index != -1;
|
|
43
|
+
}
|
|
44
|
+
static changeToChild(owner_id: string, name: string): string
|
|
45
|
+
{
|
|
46
|
+
let index = owner_id.indexOf("@")
|
|
47
|
+
if (index != -1)
|
|
48
|
+
return owner_id.substring(0, index + 1) + name.substring(0, BaseUUID.SIZE - index - 1).padStart(BaseUUID.SIZE - index - 1, '-');
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
new()
|
|
52
|
+
{
|
|
53
|
+
let ans = this.short + "-" + BaseUUID.uuid();
|
|
54
|
+
return ans.substring(0, BaseUUID.SIZE);
|
|
55
|
+
}
|
|
56
56
|
};
|
package/src/CacheService.ts
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { IStorage } from "./IStorage";
|
|
2
|
-
|
|
3
|
-
interface NSCachStorage<DataType>
|
|
4
|
-
{
|
|
5
|
-
expires_at: Date;
|
|
6
|
-
data: DataType;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class CacheService<DataType>
|
|
10
|
-
{
|
|
11
|
-
private name: string;
|
|
12
|
-
private duration_minutes: number;
|
|
13
|
-
private storage: IStorage;
|
|
14
|
-
private getFromSource: () => Promise<DataType>;
|
|
15
|
-
private onExpired: () => void;
|
|
16
|
-
constructor(name: string, duration_minutes: number, storage: IStorage, getFromSource: () => Promise<DataType>, onExpired: () => void)
|
|
17
|
-
{
|
|
18
|
-
this.name = name;
|
|
19
|
-
this.duration_minutes = duration_minutes;
|
|
20
|
-
this.storage = storage;
|
|
21
|
-
this.getFromSource = getFromSource;
|
|
22
|
-
this.onExpired = onExpired;
|
|
23
|
-
}
|
|
24
|
-
async get(): Promise<DataType>
|
|
25
|
-
{
|
|
26
|
-
let ans: DataType;
|
|
27
|
-
let value = this.storage.get(this.name, "");
|
|
28
|
-
if (value)
|
|
29
|
-
{
|
|
30
|
-
let cache = JSON.parse(value) as NSCachStorage<DataType>;
|
|
31
|
-
if (new Date(cache.expires_at) > new Date())
|
|
32
|
-
return cache.data;
|
|
33
|
-
}
|
|
34
|
-
ans = await this.getFromSource();
|
|
35
|
-
this.set(ans);
|
|
36
|
-
return ans;
|
|
37
|
-
}
|
|
38
|
-
set(data: DataType)
|
|
39
|
-
{
|
|
40
|
-
let expires_at = new Date();
|
|
41
|
-
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes);
|
|
42
|
-
let value = JSON.stringify({ data, expires_at });
|
|
43
|
-
this.storage.set(this.name, value);
|
|
44
|
-
//
|
|
45
|
-
let sleep = expires_at.getTime() - new Date().getTime();
|
|
46
|
-
if (sleep <= 0)
|
|
47
|
-
sleep = 5 * 1000;
|
|
48
|
-
setTimeout(() =>
|
|
49
|
-
{
|
|
50
|
-
if (this.onExpired)
|
|
51
|
-
this.onExpired();
|
|
52
|
-
}, sleep);
|
|
53
|
-
}
|
|
54
|
-
del()
|
|
55
|
-
{
|
|
56
|
-
this.storage.del(this.name);
|
|
57
|
-
}
|
|
1
|
+
import { IStorage } from "./IStorage";
|
|
2
|
+
|
|
3
|
+
interface NSCachStorage<DataType>
|
|
4
|
+
{
|
|
5
|
+
expires_at: Date;
|
|
6
|
+
data: DataType;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class CacheService<DataType>
|
|
10
|
+
{
|
|
11
|
+
private name: string;
|
|
12
|
+
private duration_minutes: number;
|
|
13
|
+
private storage: IStorage;
|
|
14
|
+
private getFromSource: () => Promise<DataType>;
|
|
15
|
+
private onExpired: () => void;
|
|
16
|
+
constructor(name: string, duration_minutes: number, storage: IStorage, getFromSource: () => Promise<DataType>, onExpired: () => void)
|
|
17
|
+
{
|
|
18
|
+
this.name = name;
|
|
19
|
+
this.duration_minutes = duration_minutes;
|
|
20
|
+
this.storage = storage;
|
|
21
|
+
this.getFromSource = getFromSource;
|
|
22
|
+
this.onExpired = onExpired;
|
|
23
|
+
}
|
|
24
|
+
async get(): Promise<DataType>
|
|
25
|
+
{
|
|
26
|
+
let ans: DataType;
|
|
27
|
+
let value = this.storage.get(this.name, "");
|
|
28
|
+
if (value)
|
|
29
|
+
{
|
|
30
|
+
let cache = JSON.parse(value) as NSCachStorage<DataType>;
|
|
31
|
+
if (new Date(cache.expires_at) > new Date())
|
|
32
|
+
return cache.data;
|
|
33
|
+
}
|
|
34
|
+
ans = await this.getFromSource();
|
|
35
|
+
this.set(ans);
|
|
36
|
+
return ans;
|
|
37
|
+
}
|
|
38
|
+
set(data: DataType)
|
|
39
|
+
{
|
|
40
|
+
let expires_at = new Date();
|
|
41
|
+
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes);
|
|
42
|
+
let value = JSON.stringify({ data, expires_at });
|
|
43
|
+
this.storage.set(this.name, value);
|
|
44
|
+
//
|
|
45
|
+
let sleep = expires_at.getTime() - new Date().getTime();
|
|
46
|
+
if (sleep <= 0)
|
|
47
|
+
sleep = 5 * 1000;
|
|
48
|
+
setTimeout(() =>
|
|
49
|
+
{
|
|
50
|
+
if (this.onExpired)
|
|
51
|
+
this.onExpired();
|
|
52
|
+
}, sleep);
|
|
53
|
+
}
|
|
54
|
+
del()
|
|
55
|
+
{
|
|
56
|
+
this.storage.del(this.name);
|
|
57
|
+
}
|
|
58
58
|
}
|
package/src/ConsoleOperation.ts
CHANGED
|
@@ -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
|
}
|