ts-client-lib 0.0.7
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/README.md +76 -0
- package/auth/TSJWT.d.ts +29 -0
- package/auth/TSJWT.js +44 -0
- package/auth/TSOAuth.d.ts +132 -0
- package/auth/TSOAuth.js +230 -0
- package/devices/TSCordova.d.ts +5 -0
- package/devices/TSCordova.js +52 -0
- package/entities/TSCountries.d.ts +14 -0
- package/entities/TSCountries.js +1188 -0
- package/entities/TSCurrencies.d.ts +35 -0
- package/entities/TSCurrencies.js +604 -0
- package/entities/TSMobilePhones.d.ts +167 -0
- package/entities/TSMobilePhones.js +206 -0
- package/entities/TSMoney.d.ts +149 -0
- package/entities/TSMoney.js +311 -0
- package/entities/currency-amount.d.ts +13 -0
- package/entities/currency-amount.js +43 -0
- package/finance/TSBonus.d.ts +197 -0
- package/finance/TSBonus.js +530 -0
- package/finance/TSKYC.d.ts +563 -0
- package/finance/TSKYC.js +1066 -0
- package/finance/TSTax.d.ts +49 -0
- package/finance/TSTax.js +106 -0
- package/finance/bonus-money.d.ts +41 -0
- package/finance/bonus-money.js +61 -0
- package/games/TSBetSlip.d.ts +72 -0
- package/games/TSBetSlip.js +179 -0
- package/games/TSBetSystem.d.ts +4 -0
- package/games/TSBetSystem.js +48 -0
- package/games/TSLotto.d.ts +35 -0
- package/games/TSLotto.js +205 -0
- package/games/TSPool.d.ts +28 -0
- package/games/TSPool.js +88 -0
- package/package.json +93 -0
- package/utils/TSArray.d.ts +9 -0
- package/utils/TSArray.js +87 -0
- package/utils/TSBoolean.d.ts +4 -0
- package/utils/TSBoolean.js +24 -0
- package/utils/TSCache.d.ts +167 -0
- package/utils/TSCache.js +531 -0
- package/utils/TSDate.d.ts +8 -0
- package/utils/TSDate.js +67 -0
- package/utils/TSHeuristic.d.ts +20 -0
- package/utils/TSHeuristic.js +197 -0
- package/utils/TSLZS.d.ts +42 -0
- package/utils/TSLZS.js +343 -0
- package/utils/TSLog.d.ts +40 -0
- package/utils/TSLog.js +110 -0
- package/utils/TSNumber.d.ts +6 -0
- package/utils/TSNumber.js +68 -0
- package/utils/TSObject.d.ts +29 -0
- package/utils/TSObject.js +312 -0
- package/utils/TSPagination.d.ts +282 -0
- package/utils/TSPagination.js +425 -0
- package/utils/TSPaginationMulti.d.ts +77 -0
- package/utils/TSPaginationMulti.js +356 -0
- package/utils/TSString.d.ts +10 -0
- package/utils/TSString.js +107 -0
- package/utils/TSValidator.d.ts +16 -0
- package/utils/TSValidator.js +74 -0
- package/utils/TSWorker.d.ts +3 -0
- package/utils/TSWorker.js +32 -0
- package/utils/diacritics-removal-map.d.ts +5 -0
- package/utils/diacritics-removal-map.js +341 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
declare class BaseProvider {
|
|
2
|
+
/** Accept keys as array e.g: {blogId:"2",action:"view"} and convert it to unique string */
|
|
3
|
+
generateKey(object: Record<string, unknown>): string;
|
|
4
|
+
/** Check if cache id already exist in saved context */
|
|
5
|
+
alreadyExist(storedContext: unknown[], cacheKey: unknown): boolean;
|
|
6
|
+
generateContextKey(key: string, value: unknown): string;
|
|
7
|
+
/**
|
|
8
|
+
* Get current time (compared to Epoch time) in seconds
|
|
9
|
+
*/
|
|
10
|
+
getCurrentTime(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Return default values
|
|
13
|
+
*/
|
|
14
|
+
getDefault(): {
|
|
15
|
+
prefix: string;
|
|
16
|
+
ttl: number;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Return subscribers
|
|
20
|
+
*
|
|
21
|
+
* @returns {{cacheAdded: Array, cacheRemoved: Array}}
|
|
22
|
+
*/
|
|
23
|
+
getEventSubscribers(): Record<string, ((payload: unknown) => void)[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Dispatch event to subscribers
|
|
26
|
+
*
|
|
27
|
+
* @param event Event name
|
|
28
|
+
* @param object Object will be sent to subscriber
|
|
29
|
+
*/
|
|
30
|
+
dispatchEvent(event: string, object: unknown): void;
|
|
31
|
+
/**
|
|
32
|
+
* Check if x is a function
|
|
33
|
+
*
|
|
34
|
+
* @param x
|
|
35
|
+
* @returns {boolean}
|
|
36
|
+
*/
|
|
37
|
+
isFunction(x: unknown): boolean;
|
|
38
|
+
}
|
|
39
|
+
export declare class TSStorage {
|
|
40
|
+
extra: any;
|
|
41
|
+
support: string;
|
|
42
|
+
constructor(type?: string, extra?: any);
|
|
43
|
+
/** Where persisted values are stored for this instance. */
|
|
44
|
+
private storageKind;
|
|
45
|
+
toggle(key: string, value?: unknown): any;
|
|
46
|
+
storageAvailable(type: string): string;
|
|
47
|
+
private createCookie;
|
|
48
|
+
private readCookie;
|
|
49
|
+
private serialize;
|
|
50
|
+
private deserialize;
|
|
51
|
+
private byteCount;
|
|
52
|
+
}
|
|
53
|
+
declare class StorageProvider extends BaseProvider {
|
|
54
|
+
storage: TSStorage;
|
|
55
|
+
constructor(type?: string, extra?: unknown);
|
|
56
|
+
get(key: unknown): any;
|
|
57
|
+
set(key: unknown, value: unknown, ttl?: number, contexts?: Record<string, unknown>): void;
|
|
58
|
+
removeByKey(key: unknown): void;
|
|
59
|
+
removeByContext(context: Record<string, unknown>): void;
|
|
60
|
+
private clearCachesForIds;
|
|
61
|
+
}
|
|
62
|
+
declare class ArrayProvider extends BaseProvider {
|
|
63
|
+
cacheArray: Record<string, {
|
|
64
|
+
data: unknown;
|
|
65
|
+
ttl: number;
|
|
66
|
+
createdAt: number;
|
|
67
|
+
}>;
|
|
68
|
+
cacheContexts: Record<string, string[]>;
|
|
69
|
+
constructor();
|
|
70
|
+
get(key: unknown): any;
|
|
71
|
+
set(key: unknown, value: unknown, ttl?: number | null, contexts?: Record<string, unknown>): void;
|
|
72
|
+
removeByKey(key: unknown): void;
|
|
73
|
+
removeByContext(context: Record<string, unknown>): void;
|
|
74
|
+
private clearArrayCachesForContext;
|
|
75
|
+
}
|
|
76
|
+
declare class ProviderManager {
|
|
77
|
+
DEFAULT: string;
|
|
78
|
+
storageProvider?: StorageProvider;
|
|
79
|
+
arrayProvider: ArrayProvider;
|
|
80
|
+
constructor(type?: string, extra?: unknown);
|
|
81
|
+
use(provider: string): void;
|
|
82
|
+
getProvider(name?: string): StorageProvider | ArrayProvider;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Class TSCache - Core to work with cache
|
|
86
|
+
*/
|
|
87
|
+
export declare class TSCache {
|
|
88
|
+
/**
|
|
89
|
+
* Default values served as model
|
|
90
|
+
*/
|
|
91
|
+
static model: {
|
|
92
|
+
DEFAULT: {
|
|
93
|
+
prefix: string;
|
|
94
|
+
ttl: number;
|
|
95
|
+
};
|
|
96
|
+
eventSubscribers: Record<string, Array<(payload: unknown) => void>>;
|
|
97
|
+
};
|
|
98
|
+
providerManager: ProviderManager;
|
|
99
|
+
constructor(type?: string, extra?: unknown);
|
|
100
|
+
/**
|
|
101
|
+
* Switch provider. available providers are: 'storage','array'
|
|
102
|
+
*
|
|
103
|
+
* @param provider
|
|
104
|
+
*/
|
|
105
|
+
use(provider: string): any;
|
|
106
|
+
/**
|
|
107
|
+
* Get cache by array key
|
|
108
|
+
*
|
|
109
|
+
* @param key - Array key
|
|
110
|
+
* @returns {string}
|
|
111
|
+
* @example
|
|
112
|
+
* TSCache.get({blogId:"2",action:"view"});
|
|
113
|
+
*/
|
|
114
|
+
get(key: any): any;
|
|
115
|
+
/**
|
|
116
|
+
* Save render for key
|
|
117
|
+
*
|
|
118
|
+
* @param key - Array key
|
|
119
|
+
* @param value - value must be a string
|
|
120
|
+
* @param ttl - Time to live in seconds
|
|
121
|
+
* @param contexts - Contexts
|
|
122
|
+
* @returns {TSCache}
|
|
123
|
+
*/
|
|
124
|
+
set(key: any, value: any, ttl?: number, contexts?: any): any;
|
|
125
|
+
/**
|
|
126
|
+
* Set prefix for cache key (default: _cache)
|
|
127
|
+
*
|
|
128
|
+
* @param prefix
|
|
129
|
+
* @returns {TSCache}
|
|
130
|
+
*/
|
|
131
|
+
setPrefix(prefix: string): any;
|
|
132
|
+
/**
|
|
133
|
+
* Get prefix for cache key
|
|
134
|
+
*
|
|
135
|
+
* @returns {string}
|
|
136
|
+
*/
|
|
137
|
+
getPrefix(): string;
|
|
138
|
+
/**
|
|
139
|
+
* Remove by key from the cache
|
|
140
|
+
*
|
|
141
|
+
* @param key
|
|
142
|
+
* @returns {TSCache}
|
|
143
|
+
*/
|
|
144
|
+
removeByKey(key: any): any;
|
|
145
|
+
/**
|
|
146
|
+
* Remove by context from the cache
|
|
147
|
+
*
|
|
148
|
+
* @param context
|
|
149
|
+
* @returns {TSCache}
|
|
150
|
+
*/
|
|
151
|
+
removeByContext(context: any): any;
|
|
152
|
+
/**
|
|
153
|
+
* Subscribe to an event
|
|
154
|
+
*
|
|
155
|
+
* @param event
|
|
156
|
+
* @param callback
|
|
157
|
+
*/
|
|
158
|
+
on(event: string, callback: (payload: unknown) => void): void;
|
|
159
|
+
/**
|
|
160
|
+
* Unsubscribe to an event
|
|
161
|
+
*
|
|
162
|
+
* @param event
|
|
163
|
+
* @param callback
|
|
164
|
+
*/
|
|
165
|
+
unsubscribe(event: string, callback: (payload: unknown) => void): void;
|
|
166
|
+
}
|
|
167
|
+
export {};
|
package/utils/TSCache.js
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.TSCache = exports.TSStorage = void 0;
|
|
19
|
+
var BaseProvider = /** @class */ (function () {
|
|
20
|
+
function BaseProvider() {
|
|
21
|
+
}
|
|
22
|
+
/** Accept keys as array e.g: {blogId:"2",action:"view"} and convert it to unique string */
|
|
23
|
+
BaseProvider.prototype.generateKey = function (object) {
|
|
24
|
+
var generatedKey = this.getDefault().prefix + '_';
|
|
25
|
+
var keyArray = [];
|
|
26
|
+
for (var key in object) {
|
|
27
|
+
if (Object.hasOwn(object, key)) {
|
|
28
|
+
keyArray.push(key);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
keyArray.sort();
|
|
32
|
+
for (var i = 0; i < keyArray.length; i++) {
|
|
33
|
+
generatedKey += keyArray[i] + '_' + object[keyArray[i]];
|
|
34
|
+
if (i !== (keyArray.length - 1)) {
|
|
35
|
+
generatedKey += '__';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return generatedKey;
|
|
39
|
+
};
|
|
40
|
+
/** Check if cache id already exist in saved context */
|
|
41
|
+
BaseProvider.prototype.alreadyExist = function (storedContext, cacheKey) {
|
|
42
|
+
return storedContext.indexOf(cacheKey) >= 0;
|
|
43
|
+
};
|
|
44
|
+
BaseProvider.prototype.generateContextKey = function (key, value) {
|
|
45
|
+
return this.getDefault().prefix + '_c_' + key + '_' + value; // _c_ stands for _context_
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Get current time (compared to Epoch time) in seconds
|
|
49
|
+
*/
|
|
50
|
+
BaseProvider.prototype.getCurrentTime = function () {
|
|
51
|
+
return Math.floor(new Date().getTime() / 1000);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Return default values
|
|
55
|
+
*/
|
|
56
|
+
BaseProvider.prototype.getDefault = function () {
|
|
57
|
+
return TSCache.model.DEFAULT;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Return subscribers
|
|
61
|
+
*
|
|
62
|
+
* @returns {{cacheAdded: Array, cacheRemoved: Array}}
|
|
63
|
+
*/
|
|
64
|
+
BaseProvider.prototype.getEventSubscribers = function () {
|
|
65
|
+
return TSCache.model.eventSubscribers;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Dispatch event to subscribers
|
|
69
|
+
*
|
|
70
|
+
* @param event Event name
|
|
71
|
+
* @param object Object will be sent to subscriber
|
|
72
|
+
*/
|
|
73
|
+
BaseProvider.prototype.dispatchEvent = function (event, object) {
|
|
74
|
+
var subscribers = this.getEventSubscribers();
|
|
75
|
+
var callbacks = subscribers[event];
|
|
76
|
+
if (!callbacks || callbacks.length < 1) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
for (var i = 0; i < callbacks.length; i++) {
|
|
80
|
+
if (typeof (callbacks[i]) !== 'undefined' && this.isFunction(callbacks[i])) {
|
|
81
|
+
callbacks[i](object);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Check if x is a function
|
|
87
|
+
*
|
|
88
|
+
* @param x
|
|
89
|
+
* @returns {boolean}
|
|
90
|
+
*/
|
|
91
|
+
BaseProvider.prototype.isFunction = function (x) {
|
|
92
|
+
return Object.prototype.toString.call(x) === '[object Function]';
|
|
93
|
+
};
|
|
94
|
+
return BaseProvider;
|
|
95
|
+
}());
|
|
96
|
+
var TSStorage = /** @class */ (function () {
|
|
97
|
+
function TSStorage(type, extra) {
|
|
98
|
+
if (type === void 0) { type = 'localStorage'; }
|
|
99
|
+
if (extra === void 0) { extra = {}; }
|
|
100
|
+
this.extra = extra;
|
|
101
|
+
this.extra = Object.assign({
|
|
102
|
+
serialize: function (data) {
|
|
103
|
+
if (typeof data === 'object') {
|
|
104
|
+
data = JSON.stringify(data);
|
|
105
|
+
}
|
|
106
|
+
return data;
|
|
107
|
+
},
|
|
108
|
+
deserialize: function (data) {
|
|
109
|
+
if (data == null) {
|
|
110
|
+
return data;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return JSON.parse(data);
|
|
114
|
+
}
|
|
115
|
+
catch (_a) {
|
|
116
|
+
return data;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}, this.extra);
|
|
120
|
+
this.support = this.storageAvailable(type);
|
|
121
|
+
}
|
|
122
|
+
/** Where persisted values are stored for this instance. */
|
|
123
|
+
TSStorage.prototype.storageKind = function () {
|
|
124
|
+
if (this.support.localeCompare('localStorage') === 0)
|
|
125
|
+
return 'local';
|
|
126
|
+
if (this.support.localeCompare('sessionStorage') === 0)
|
|
127
|
+
return 'session';
|
|
128
|
+
return 'cookie';
|
|
129
|
+
};
|
|
130
|
+
TSStorage.prototype.toggle = function (key, value) {
|
|
131
|
+
// If value is detected, set new or modify store
|
|
132
|
+
if (typeof value !== 'undefined' && value !== null) {
|
|
133
|
+
var data = this.serialize(value);
|
|
134
|
+
var k_1 = this.storageKind();
|
|
135
|
+
if (k_1 === 'local')
|
|
136
|
+
window.localStorage.setItem(key, data);
|
|
137
|
+
else if (k_1 === 'session')
|
|
138
|
+
window.sessionStorage.setItem(key, data);
|
|
139
|
+
else
|
|
140
|
+
this.createCookie(key, data, 30);
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
if (typeof value === 'undefined') {
|
|
144
|
+
var data = void 0;
|
|
145
|
+
var k_2 = this.storageKind();
|
|
146
|
+
if (k_2 === 'local')
|
|
147
|
+
data = window.localStorage.getItem(key);
|
|
148
|
+
else if (k_2 === 'session')
|
|
149
|
+
data = window.sessionStorage.getItem(key);
|
|
150
|
+
else
|
|
151
|
+
data = this.readCookie(key);
|
|
152
|
+
return this.deserialize(data);
|
|
153
|
+
}
|
|
154
|
+
// value === null: remove
|
|
155
|
+
var k = this.storageKind();
|
|
156
|
+
if (k === 'local')
|
|
157
|
+
setTimeout(function () { return window.localStorage.removeItem(key); }, 90);
|
|
158
|
+
else if (k === 'session')
|
|
159
|
+
setTimeout(function () { return window.sessionStorage.removeItem(key); }, 90);
|
|
160
|
+
else
|
|
161
|
+
this.createCookie(key, '', -1);
|
|
162
|
+
};
|
|
163
|
+
TSStorage.prototype.storageAvailable = function (type) {
|
|
164
|
+
// Use try catch to allow storage to work on private mode
|
|
165
|
+
if (type === 'cookie') {
|
|
166
|
+
return 'cookie';
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
var storage = window[type], x = '__storage_test__';
|
|
170
|
+
storage.setItem(x, x);
|
|
171
|
+
storage.removeItem(x);
|
|
172
|
+
return type;
|
|
173
|
+
}
|
|
174
|
+
catch (_a) {
|
|
175
|
+
return 'cookie';
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
TSStorage.prototype.createCookie = function (key, value, exp) {
|
|
179
|
+
var date = new Date();
|
|
180
|
+
date.setTime(date.getTime() + (exp * 24 * 60 * 60 * 1000));
|
|
181
|
+
var c = "".concat(key, "=").concat(value, "; expires=").concat(date.toUTCString(), "; path=/; SameSite=None; Secure");
|
|
182
|
+
if (this.extra.domain) {
|
|
183
|
+
c += "; domain=.".concat(this.extra.domain);
|
|
184
|
+
}
|
|
185
|
+
if (this.byteCount(c) < 4096) {
|
|
186
|
+
window.document.cookie = c;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
TSStorage.prototype.readCookie = function (key) {
|
|
190
|
+
var nameEQ = key + '=', ca = window.document.cookie.split(';');
|
|
191
|
+
for (var i = 0, max = ca.length; i < max; i++) {
|
|
192
|
+
var c = ca[i];
|
|
193
|
+
while (c.charAt(0) === ' ') {
|
|
194
|
+
c = c.substring(1, c.length);
|
|
195
|
+
}
|
|
196
|
+
if (c.indexOf(nameEQ) === 0) {
|
|
197
|
+
return c.substring(nameEQ.length, c.length);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
};
|
|
202
|
+
TSStorage.prototype.serialize = function (data) {
|
|
203
|
+
return this.extra && this.extra.serialize ? this.extra.serialize(data) : data;
|
|
204
|
+
};
|
|
205
|
+
TSStorage.prototype.deserialize = function (data) {
|
|
206
|
+
return this.extra && this.extra.deserialize ? this.extra.deserialize(data) : data;
|
|
207
|
+
};
|
|
208
|
+
TSStorage.prototype.byteCount = function (s) {
|
|
209
|
+
return encodeURI(s).split(/%..|./).length - 1;
|
|
210
|
+
};
|
|
211
|
+
return TSStorage;
|
|
212
|
+
}());
|
|
213
|
+
exports.TSStorage = TSStorage;
|
|
214
|
+
var StorageProvider = /** @class */ (function (_super) {
|
|
215
|
+
__extends(StorageProvider, _super);
|
|
216
|
+
function StorageProvider(type, extra) {
|
|
217
|
+
if (type === void 0) { type = 'localStorage'; }
|
|
218
|
+
var _this = _super.call(this) || this;
|
|
219
|
+
_this.storage = new TSStorage(type, extra);
|
|
220
|
+
return _this;
|
|
221
|
+
}
|
|
222
|
+
StorageProvider.prototype.get = function (key) {
|
|
223
|
+
var generatedKey = this.generateKey(key), object = this.storage.toggle(generatedKey);
|
|
224
|
+
if (object !== null) {
|
|
225
|
+
// Check if the cache is expired
|
|
226
|
+
if ((this.getCurrentTime() - object.createdAt) >= object.ttl) {
|
|
227
|
+
this.storage.toggle(generatedKey, null);
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
return object.data;
|
|
231
|
+
}
|
|
232
|
+
return null;
|
|
233
|
+
};
|
|
234
|
+
StorageProvider.prototype.set = function (key, value, ttl, contexts) {
|
|
235
|
+
ttl = ttl || this.getDefault().ttl;
|
|
236
|
+
var cacheKey = this.generateKey(key);
|
|
237
|
+
this.storage.toggle(cacheKey, { data: value, ttl: ttl, createdAt: this.getCurrentTime() });
|
|
238
|
+
var ctx = contexts !== null && contexts !== void 0 ? contexts : {};
|
|
239
|
+
for (var context in ctx) {
|
|
240
|
+
if (!Object.hasOwn(ctx, context)) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
var contextKey = this.generateContextKey(context, ctx[context]);
|
|
244
|
+
var storedContext = this.storage.toggle(contextKey);
|
|
245
|
+
if (storedContext !== null && !this.alreadyExist(storedContext, cacheKey)) {
|
|
246
|
+
storedContext.push(cacheKey);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
storedContext = [cacheKey];
|
|
250
|
+
}
|
|
251
|
+
this.storage.toggle(contextKey, storedContext);
|
|
252
|
+
}
|
|
253
|
+
this.dispatchEvent('cacheAdded', {
|
|
254
|
+
key: key, value: value, ttl: ttl, contexts: contexts || null
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
StorageProvider.prototype.removeByKey = function (key) {
|
|
258
|
+
var generatedKey = this.generateKey(key), cache = this.storage.toggle(generatedKey);
|
|
259
|
+
if (cache !== null) {
|
|
260
|
+
this.storage.toggle(generatedKey, null);
|
|
261
|
+
this.dispatchEvent('cacheRemoved', {
|
|
262
|
+
generatedKey: generatedKey, value: cache.data, ttl: cache.ttl
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
StorageProvider.prototype.removeByContext = function (context) {
|
|
267
|
+
for (var key in context) {
|
|
268
|
+
if (!Object.hasOwn(context, key))
|
|
269
|
+
continue;
|
|
270
|
+
var contextKey = this.generateContextKey(key, context[key]);
|
|
271
|
+
var storedContext = this.storage.toggle(contextKey);
|
|
272
|
+
if (storedContext === null)
|
|
273
|
+
return;
|
|
274
|
+
var cacheIds = JSON.parse(storedContext);
|
|
275
|
+
this.clearCachesForIds(contextKey, cacheIds);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
StorageProvider.prototype.clearCachesForIds = function (contextKey, cacheIds) {
|
|
279
|
+
for (var i = 0; i < cacheIds.length; i++) {
|
|
280
|
+
var raw = localStorage.getItem(cacheIds[i]);
|
|
281
|
+
var cache = raw != null ? JSON.parse(raw) : null;
|
|
282
|
+
if (!cache) {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
this.storage.toggle(cacheIds[i], null);
|
|
286
|
+
this.dispatchEvent('cacheRemoved', {
|
|
287
|
+
generatedKey: cacheIds[i], value: cache.data, ttl: cache.ttl
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
this.storage.toggle(contextKey, null);
|
|
291
|
+
};
|
|
292
|
+
return StorageProvider;
|
|
293
|
+
}(BaseProvider));
|
|
294
|
+
var ArrayProvider = /** @class */ (function (_super) {
|
|
295
|
+
__extends(ArrayProvider, _super);
|
|
296
|
+
function ArrayProvider() {
|
|
297
|
+
var _this = _super.call(this) || this;
|
|
298
|
+
_this.cacheArray = {};
|
|
299
|
+
_this.cacheContexts = {};
|
|
300
|
+
return _this;
|
|
301
|
+
}
|
|
302
|
+
ArrayProvider.prototype.get = function (key) {
|
|
303
|
+
var generatedKey = this.generateKey(key);
|
|
304
|
+
if (Object.hasOwn(this.cacheArray, generatedKey)) {
|
|
305
|
+
var object = this.cacheArray[generatedKey];
|
|
306
|
+
// Check if the cache is expired
|
|
307
|
+
if ((this.getCurrentTime() - object.createdAt) >= object.ttl) {
|
|
308
|
+
delete this.cacheArray[generatedKey];
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
return object.data;
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
ArrayProvider.prototype.set = function (key, value, ttl, contexts) {
|
|
318
|
+
var generatedKey = this.generateKey(key);
|
|
319
|
+
ttl = ttl === null || typeof (ttl) === 'undefined' ? this.getDefault().ttl : ttl;
|
|
320
|
+
this.cacheArray[generatedKey] = {
|
|
321
|
+
data: value, ttl: ttl, createdAt: this.getCurrentTime()
|
|
322
|
+
};
|
|
323
|
+
var ctx = contexts !== null && contexts !== void 0 ? contexts : {};
|
|
324
|
+
for (var context in ctx) {
|
|
325
|
+
if (!Object.hasOwn(ctx, context)) {
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
var contextKey = this.generateContextKey(context, ctx[context]);
|
|
329
|
+
var storedContext = Object.hasOwn(this.cacheContexts, contextKey) ? this.cacheContexts[contextKey] : null;
|
|
330
|
+
if (storedContext !== null && !this.alreadyExist(storedContext, generatedKey)) {
|
|
331
|
+
storedContext.push(generatedKey);
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
storedContext = [generatedKey];
|
|
335
|
+
}
|
|
336
|
+
this.cacheContexts[contextKey] = storedContext;
|
|
337
|
+
}
|
|
338
|
+
this.dispatchEvent('cacheAdded', {
|
|
339
|
+
key: key, value: value, ttl: ttl, contexts: contexts || null
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
ArrayProvider.prototype.removeByKey = function (key) {
|
|
343
|
+
var generatedKey = this.generateKey(key);
|
|
344
|
+
if (Object.hasOwn(this.cacheArray, generatedKey)) {
|
|
345
|
+
var cache = this.cacheArray[generatedKey];
|
|
346
|
+
delete this.cacheArray[generatedKey];
|
|
347
|
+
this.dispatchEvent('cacheRemoved', {
|
|
348
|
+
generatedKey: generatedKey, value: cache.data, ttl: cache.ttl
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
ArrayProvider.prototype.removeByContext = function (context) {
|
|
353
|
+
for (var key in context) {
|
|
354
|
+
if (!Object.hasOwn(context, key))
|
|
355
|
+
continue;
|
|
356
|
+
var contextKey = this.generateContextKey(key, context[key]);
|
|
357
|
+
var storedContext = Object.hasOwn(this.cacheContexts, contextKey) ? this.cacheContexts[contextKey] : null;
|
|
358
|
+
if (storedContext === null)
|
|
359
|
+
return;
|
|
360
|
+
this.clearArrayCachesForContext(contextKey, storedContext);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
ArrayProvider.prototype.clearArrayCachesForContext = function (contextKey, storedContext) {
|
|
364
|
+
for (var i = 0; i < storedContext.length; i++) {
|
|
365
|
+
var cache = this.cacheArray[storedContext[i]];
|
|
366
|
+
if (!cache) {
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
delete this.cacheArray[storedContext[i]];
|
|
370
|
+
this.dispatchEvent('cacheRemoved', {
|
|
371
|
+
generatedKey: storedContext[i], value: cache.data, ttl: cache.ttl
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
delete this.cacheContexts[contextKey];
|
|
375
|
+
};
|
|
376
|
+
return ArrayProvider;
|
|
377
|
+
}(BaseProvider));
|
|
378
|
+
var ProviderManager = /** @class */ (function () {
|
|
379
|
+
function ProviderManager(type, extra) {
|
|
380
|
+
if (type === void 0) { type = 'localStorage'; }
|
|
381
|
+
this.DEFAULT = 'storage';
|
|
382
|
+
if (typeof window !== 'undefined') {
|
|
383
|
+
this.storageProvider = new StorageProvider(type, extra);
|
|
384
|
+
}
|
|
385
|
+
this.arrayProvider = new ArrayProvider();
|
|
386
|
+
}
|
|
387
|
+
ProviderManager.prototype.use = function (provider) {
|
|
388
|
+
this.DEFAULT = provider;
|
|
389
|
+
};
|
|
390
|
+
ProviderManager.prototype.getProvider = function (name) {
|
|
391
|
+
var _a;
|
|
392
|
+
var n = name || this.DEFAULT;
|
|
393
|
+
if (typeof window === 'undefined') {
|
|
394
|
+
n = 'array';
|
|
395
|
+
}
|
|
396
|
+
switch (n) {
|
|
397
|
+
case 'storage':
|
|
398
|
+
return (_a = this.storageProvider) !== null && _a !== void 0 ? _a : this.arrayProvider;
|
|
399
|
+
case 'array':
|
|
400
|
+
return this.arrayProvider;
|
|
401
|
+
default:
|
|
402
|
+
return this.arrayProvider;
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
return ProviderManager;
|
|
406
|
+
}());
|
|
407
|
+
/**
|
|
408
|
+
* Class TSCache - Core to work with cache
|
|
409
|
+
*/
|
|
410
|
+
var TSCache = /** @class */ (function () {
|
|
411
|
+
function TSCache(type, extra) {
|
|
412
|
+
if (type === void 0) { type = 'localStorage'; }
|
|
413
|
+
this.providerManager = new ProviderManager(type, extra);
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Switch provider. available providers are: 'storage','array'
|
|
417
|
+
*
|
|
418
|
+
* @param provider
|
|
419
|
+
*/
|
|
420
|
+
TSCache.prototype.use = function (provider) {
|
|
421
|
+
this.providerManager.use(provider);
|
|
422
|
+
return this;
|
|
423
|
+
};
|
|
424
|
+
/**
|
|
425
|
+
* Get cache by array key
|
|
426
|
+
*
|
|
427
|
+
* @param key - Array key
|
|
428
|
+
* @returns {string}
|
|
429
|
+
* @example
|
|
430
|
+
* TSCache.get({blogId:"2",action:"view"});
|
|
431
|
+
*/
|
|
432
|
+
TSCache.prototype.get = function (key) {
|
|
433
|
+
return this.providerManager.getProvider().get(key);
|
|
434
|
+
};
|
|
435
|
+
/**
|
|
436
|
+
* Save render for key
|
|
437
|
+
*
|
|
438
|
+
* @param key - Array key
|
|
439
|
+
* @param value - value must be a string
|
|
440
|
+
* @param ttl - Time to live in seconds
|
|
441
|
+
* @param contexts - Contexts
|
|
442
|
+
* @returns {TSCache}
|
|
443
|
+
*/
|
|
444
|
+
TSCache.prototype.set = function (key, value, ttl, contexts) {
|
|
445
|
+
try {
|
|
446
|
+
this.providerManager.getProvider().set(key, value, ttl, contexts);
|
|
447
|
+
}
|
|
448
|
+
catch (_a) {
|
|
449
|
+
return null;
|
|
450
|
+
}
|
|
451
|
+
return this;
|
|
452
|
+
};
|
|
453
|
+
/**
|
|
454
|
+
* Set prefix for cache key (default: _cache)
|
|
455
|
+
*
|
|
456
|
+
* @param prefix
|
|
457
|
+
* @returns {TSCache}
|
|
458
|
+
*/
|
|
459
|
+
TSCache.prototype.setPrefix = function (prefix) {
|
|
460
|
+
TSCache.model.DEFAULT.prefix = prefix;
|
|
461
|
+
return this;
|
|
462
|
+
};
|
|
463
|
+
/**
|
|
464
|
+
* Get prefix for cache key
|
|
465
|
+
*
|
|
466
|
+
* @returns {string}
|
|
467
|
+
*/
|
|
468
|
+
TSCache.prototype.getPrefix = function () {
|
|
469
|
+
return TSCache.model.DEFAULT.prefix;
|
|
470
|
+
};
|
|
471
|
+
/**
|
|
472
|
+
* Remove by key from the cache
|
|
473
|
+
*
|
|
474
|
+
* @param key
|
|
475
|
+
* @returns {TSCache}
|
|
476
|
+
*/
|
|
477
|
+
TSCache.prototype.removeByKey = function (key) {
|
|
478
|
+
this.providerManager.getProvider().removeByKey(key);
|
|
479
|
+
return this;
|
|
480
|
+
};
|
|
481
|
+
/**
|
|
482
|
+
* Remove by context from the cache
|
|
483
|
+
*
|
|
484
|
+
* @param context
|
|
485
|
+
* @returns {TSCache}
|
|
486
|
+
*/
|
|
487
|
+
TSCache.prototype.removeByContext = function (context) {
|
|
488
|
+
this.providerManager.getProvider().removeByContext(context);
|
|
489
|
+
return this;
|
|
490
|
+
};
|
|
491
|
+
/**
|
|
492
|
+
* Subscribe to an event
|
|
493
|
+
*
|
|
494
|
+
* @param event
|
|
495
|
+
* @param callback
|
|
496
|
+
*/
|
|
497
|
+
TSCache.prototype.on = function (event, callback) {
|
|
498
|
+
var subs = TSCache.model.eventSubscribers;
|
|
499
|
+
if (!subs[event]) {
|
|
500
|
+
subs[event] = [];
|
|
501
|
+
}
|
|
502
|
+
subs[event].push(callback);
|
|
503
|
+
};
|
|
504
|
+
/**
|
|
505
|
+
* Unsubscribe to an event
|
|
506
|
+
*
|
|
507
|
+
* @param event
|
|
508
|
+
* @param callback
|
|
509
|
+
*/
|
|
510
|
+
TSCache.prototype.unsubscribe = function (event, callback) {
|
|
511
|
+
var callbacks = TSCache.model.eventSubscribers[event];
|
|
512
|
+
if (!callbacks) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
for (var i = 0; i < callbacks.length; i++) {
|
|
516
|
+
if (callbacks[i] === callback) {
|
|
517
|
+
delete callbacks[i];
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
/**
|
|
523
|
+
* Default values served as model
|
|
524
|
+
*/
|
|
525
|
+
TSCache.model = {
|
|
526
|
+
DEFAULT: { prefix: '_c', ttl: 604800 },
|
|
527
|
+
eventSubscribers: { cacheAdded: [], cacheRemoved: [] }
|
|
528
|
+
};
|
|
529
|
+
return TSCache;
|
|
530
|
+
}());
|
|
531
|
+
exports.TSCache = TSCache;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class TSDate {
|
|
2
|
+
static utc(type: string, data: number, set?: Array<number>): Date;
|
|
3
|
+
static key(max?: number, date?: Date): string;
|
|
4
|
+
static parse(date: string, data?: number, set?: number[]): string;
|
|
5
|
+
static yyyymmddhhss(date: string | Date | number): Date;
|
|
6
|
+
static format(timestamp: number | undefined, format: string, cb?: (data: any) => any): string;
|
|
7
|
+
static timer(milliseconds: number, format: string, cb?: (data: any) => any): string;
|
|
8
|
+
}
|