topsyde-utils 1.0.0
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/LICENSE +21 -0
- package/README.md +192 -0
- package/dist/app.d.ts +11 -0
- package/dist/app.js +32 -0
- package/dist/app.js.map +1 -0
- package/dist/consts.d.ts +30 -0
- package/dist/consts.js +34 -0
- package/dist/consts.js.map +1 -0
- package/dist/enums.d.ts +13 -0
- package/dist/enums.js +19 -0
- package/dist/enums.js.map +1 -0
- package/dist/errors.d.ts +37 -0
- package/dist/errors.js +46 -0
- package/dist/errors.js.map +1 -0
- package/dist/guards.d.ts +19 -0
- package/dist/guards.js +42 -0
- package/dist/guards.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +75 -0
- package/dist/index.js.map +1 -0
- package/dist/initializable.d.ts +134 -0
- package/dist/initializable.js +311 -0
- package/dist/initializable.js.map +1 -0
- package/dist/lib.d.ts +67 -0
- package/dist/lib.js +488 -0
- package/dist/lib.js.map +1 -0
- package/dist/router/index.d.ts +5 -0
- package/dist/router/index.js +30 -0
- package/dist/router/index.js.map +1 -0
- package/dist/router/middleware.d.ts +21 -0
- package/dist/router/middleware.js +47 -0
- package/dist/router/middleware.js.map +1 -0
- package/dist/router/route.d.ts +14 -0
- package/dist/router/route.js +22 -0
- package/dist/router/route.js.map +1 -0
- package/dist/router/router.d.ts +20 -0
- package/dist/router/router.js +35 -0
- package/dist/router/router.js.map +1 -0
- package/dist/singleton.d.ts +97 -0
- package/dist/singleton.js +142 -0
- package/dist/singleton.js.map +1 -0
- package/dist/throwable.d.ts +42 -0
- package/dist/throwable.js +74 -0
- package/dist/throwable.js.map +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +55 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
/**
|
2
|
+
* Configuration options for initialization
|
3
|
+
*/
|
4
|
+
export interface InitOptions {
|
5
|
+
/** Number of retry attempts (default: 25) */
|
6
|
+
retries?: number;
|
7
|
+
/** Time in milliseconds between retry attempts (default: 200) */
|
8
|
+
retryInterval?: number;
|
9
|
+
/** Whether to initialize automatically in constructor (default: false) */
|
10
|
+
autoInitialize?: boolean;
|
11
|
+
/** Custom timeout in milliseconds (overrides retries * retryInterval) */
|
12
|
+
timeout?: number;
|
13
|
+
}
|
14
|
+
/**
|
15
|
+
* Events emitted by Initializable instances
|
16
|
+
*/
|
17
|
+
export type InitializableEvent = 'initializing' | 'initialized' | 'failed' | 'timeout';
|
18
|
+
/**
|
19
|
+
* Base class for objects that require asynchronous initialization
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* class Database extends Initializable {
|
23
|
+
* private connection: any;
|
24
|
+
*
|
25
|
+
* constructor() {
|
26
|
+
* super({ retries: 10, retryInterval: 500 });
|
27
|
+
* }
|
28
|
+
*
|
29
|
+
* protected async doInitialize(): Promise<void> {
|
30
|
+
* this.connection = await connectToDatabase();
|
31
|
+
* }
|
32
|
+
* }
|
33
|
+
*
|
34
|
+
* // Usage
|
35
|
+
* const db = new Database();
|
36
|
+
* await db.initialize();
|
37
|
+
* // or check status
|
38
|
+
* if (await db.isInitialized()) {
|
39
|
+
* // use the database
|
40
|
+
* }
|
41
|
+
*/
|
42
|
+
declare class Initializable {
|
43
|
+
/** Number of retry attempts */
|
44
|
+
private retries;
|
45
|
+
/** Time in milliseconds between retry attempts */
|
46
|
+
private retryInterval;
|
47
|
+
/** Whether initialization has completed successfully */
|
48
|
+
private _initialized;
|
49
|
+
/** Whether initialization is in progress */
|
50
|
+
private initializing;
|
51
|
+
/** Whether initialization has failed */
|
52
|
+
private failed;
|
53
|
+
/** Optional timeout in milliseconds */
|
54
|
+
private timeout?;
|
55
|
+
/** Event listeners */
|
56
|
+
private listeners;
|
57
|
+
/** Abort controller for cancellation */
|
58
|
+
private abortController;
|
59
|
+
/**
|
60
|
+
* Creates a new Initializable instance
|
61
|
+
* @param options Configuration options
|
62
|
+
*/
|
63
|
+
constructor(options?: InitOptions);
|
64
|
+
/**
|
65
|
+
* Initialize the object
|
66
|
+
* @returns Promise that resolves when initialization is complete
|
67
|
+
* @throws Error if initialization fails
|
68
|
+
*/
|
69
|
+
initialize(): Promise<void>;
|
70
|
+
/**
|
71
|
+
* Implementation-specific initialization logic
|
72
|
+
* Override this method in subclasses to provide custom initialization
|
73
|
+
*/
|
74
|
+
protected doInitialize(): Promise<void>;
|
75
|
+
/**
|
76
|
+
* Check if the object is initialized
|
77
|
+
* @param waitForIt Whether to wait for initialization to complete
|
78
|
+
* @returns Promise that resolves to true if initialized, false otherwise
|
79
|
+
*/
|
80
|
+
isInitialized(waitForIt?: boolean): Promise<boolean>;
|
81
|
+
/**
|
82
|
+
* Wait for initialization to complete
|
83
|
+
* @returns Promise that resolves when initialization is complete
|
84
|
+
* @throws Error if initialization fails or times out
|
85
|
+
*/
|
86
|
+
private waitForInitialization;
|
87
|
+
/**
|
88
|
+
* Cancel initialization if in progress
|
89
|
+
*/
|
90
|
+
cancel(): void;
|
91
|
+
/**
|
92
|
+
* Reset initialization state
|
93
|
+
* Allows re-initialization after failure
|
94
|
+
*/
|
95
|
+
reset(): void;
|
96
|
+
/**
|
97
|
+
* Register an event listener
|
98
|
+
* @param event Event name
|
99
|
+
* @param callback Function to call when event is emitted
|
100
|
+
* @returns this for chaining
|
101
|
+
*/
|
102
|
+
on(event: InitializableEvent, callback: Function): this;
|
103
|
+
/**
|
104
|
+
* Remove an event listener
|
105
|
+
* @param event Event name
|
106
|
+
* @param callback Function to remove
|
107
|
+
* @returns this for chaining
|
108
|
+
*/
|
109
|
+
off(event: InitializableEvent, callback: Function): this;
|
110
|
+
/**
|
111
|
+
* Emit an event
|
112
|
+
* @param event Event name
|
113
|
+
* @param args Arguments to pass to listeners
|
114
|
+
*/
|
115
|
+
protected emit(event: InitializableEvent, ...args: any[]): void;
|
116
|
+
/**
|
117
|
+
* Get the abort signal for cancellation
|
118
|
+
* Can be passed to fetch or other cancellable operations
|
119
|
+
*/
|
120
|
+
protected get abortSignal(): AbortSignal | undefined;
|
121
|
+
/**
|
122
|
+
* Check if initialization has been completed
|
123
|
+
*/
|
124
|
+
get initialized(): boolean;
|
125
|
+
/**
|
126
|
+
* Check if initialization is in progress
|
127
|
+
*/
|
128
|
+
get isInitializing(): boolean;
|
129
|
+
/**
|
130
|
+
* Check if initialization has failed
|
131
|
+
*/
|
132
|
+
get hasFailed(): boolean;
|
133
|
+
}
|
134
|
+
export default Initializable;
|
@@ -0,0 +1,311 @@
|
|
1
|
+
"use strict";
|
2
|
+
/* class Initializable {
|
3
|
+
retries: number;
|
4
|
+
initialized: boolean;
|
5
|
+
constructor(retries: number = 25) {
|
6
|
+
this.retries = retries;
|
7
|
+
this.initialized = false;
|
8
|
+
this.initialize();
|
9
|
+
}
|
10
|
+
|
11
|
+
protected initialize() {
|
12
|
+
this.initialized = true;
|
13
|
+
}
|
14
|
+
|
15
|
+
protected async isInitialized(): Promise<boolean> {
|
16
|
+
let retries = this.retries; // 5 seconds / 200ms = 25 retries
|
17
|
+
|
18
|
+
while (!this.initialized && retries > 0) {
|
19
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
20
|
+
retries--;
|
21
|
+
}
|
22
|
+
|
23
|
+
if (!this.initialized) {
|
24
|
+
throw new Error("Initialization failed after 5 seconds");
|
25
|
+
}
|
26
|
+
|
27
|
+
return true;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
export default Initializable;
|
32
|
+
*/
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
34
|
+
/**
|
35
|
+
* Base class for objects that require asynchronous initialization
|
36
|
+
*
|
37
|
+
* @example
|
38
|
+
* class Database extends Initializable {
|
39
|
+
* private connection: any;
|
40
|
+
*
|
41
|
+
* constructor() {
|
42
|
+
* super({ retries: 10, retryInterval: 500 });
|
43
|
+
* }
|
44
|
+
*
|
45
|
+
* protected async doInitialize(): Promise<void> {
|
46
|
+
* this.connection = await connectToDatabase();
|
47
|
+
* }
|
48
|
+
* }
|
49
|
+
*
|
50
|
+
* // Usage
|
51
|
+
* const db = new Database();
|
52
|
+
* await db.initialize();
|
53
|
+
* // or check status
|
54
|
+
* if (await db.isInitialized()) {
|
55
|
+
* // use the database
|
56
|
+
* }
|
57
|
+
*/
|
58
|
+
class Initializable {
|
59
|
+
/**
|
60
|
+
* Creates a new Initializable instance
|
61
|
+
* @param options Configuration options
|
62
|
+
*/
|
63
|
+
constructor(options = {}) {
|
64
|
+
/** Whether initialization has completed successfully */
|
65
|
+
this._initialized = false;
|
66
|
+
/** Whether initialization is in progress */
|
67
|
+
this.initializing = false;
|
68
|
+
/** Whether initialization has failed */
|
69
|
+
this.failed = false;
|
70
|
+
/** Event listeners */
|
71
|
+
this.listeners = new Map();
|
72
|
+
/** Abort controller for cancellation */
|
73
|
+
this.abortController = null;
|
74
|
+
this.retries = options.retries ?? 25;
|
75
|
+
this.retryInterval = options.retryInterval ?? 200;
|
76
|
+
this.timeout = options.timeout;
|
77
|
+
if (options.autoInitialize) {
|
78
|
+
// Schedule initialization on next tick to allow subclass construction to complete
|
79
|
+
setTimeout(() => this.initialize(), 0);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
/**
|
83
|
+
* Initialize the object
|
84
|
+
* @returns Promise that resolves when initialization is complete
|
85
|
+
* @throws Error if initialization fails
|
86
|
+
*/
|
87
|
+
async initialize() {
|
88
|
+
// If already initialized, return immediately
|
89
|
+
if (this._initialized) {
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
// If already initializing, wait for it to complete
|
93
|
+
if (this.initializing) {
|
94
|
+
return this.waitForInitialization();
|
95
|
+
}
|
96
|
+
// Start initialization
|
97
|
+
this.initializing = true;
|
98
|
+
this.failed = false;
|
99
|
+
this.abortController = new AbortController();
|
100
|
+
try {
|
101
|
+
// Emit initializing event
|
102
|
+
this.emit('initializing');
|
103
|
+
// Call the implementation-specific initialization
|
104
|
+
await this.doInitialize();
|
105
|
+
// Mark as initialized
|
106
|
+
this._initialized = true;
|
107
|
+
this.initializing = false;
|
108
|
+
// Emit initialized event
|
109
|
+
this.emit('initialized');
|
110
|
+
}
|
111
|
+
catch (error) {
|
112
|
+
// Mark as failed
|
113
|
+
this.failed = true;
|
114
|
+
this.initializing = false;
|
115
|
+
// Emit failed event
|
116
|
+
this.emit('failed', error);
|
117
|
+
// Re-throw the error
|
118
|
+
throw error instanceof Error
|
119
|
+
? error
|
120
|
+
: new Error(`Initialization failed: ${String(error)}`);
|
121
|
+
}
|
122
|
+
finally {
|
123
|
+
this.abortController = null;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
/**
|
127
|
+
* Implementation-specific initialization logic
|
128
|
+
* Override this method in subclasses to provide custom initialization
|
129
|
+
*/
|
130
|
+
async doInitialize() {
|
131
|
+
// Default implementation does nothing
|
132
|
+
// Subclasses should override this method
|
133
|
+
this._initialized = true;
|
134
|
+
}
|
135
|
+
/**
|
136
|
+
* Check if the object is initialized
|
137
|
+
* @param waitForIt Whether to wait for initialization to complete
|
138
|
+
* @returns Promise that resolves to true if initialized, false otherwise
|
139
|
+
*/
|
140
|
+
async isInitialized(waitForIt = false) {
|
141
|
+
// If already initialized, return immediately
|
142
|
+
if (this._initialized) {
|
143
|
+
return true;
|
144
|
+
}
|
145
|
+
// If not waiting or already failed, return current status
|
146
|
+
if (!waitForIt || this.failed) {
|
147
|
+
return this._initialized;
|
148
|
+
}
|
149
|
+
// Wait for initialization to complete
|
150
|
+
try {
|
151
|
+
await this.waitForInitialization();
|
152
|
+
return true;
|
153
|
+
}
|
154
|
+
catch (error) {
|
155
|
+
return false;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
/**
|
159
|
+
* Wait for initialization to complete
|
160
|
+
* @returns Promise that resolves when initialization is complete
|
161
|
+
* @throws Error if initialization fails or times out
|
162
|
+
*/
|
163
|
+
async waitForInitialization() {
|
164
|
+
// If already initialized, return immediately
|
165
|
+
if (this._initialized) {
|
166
|
+
return;
|
167
|
+
}
|
168
|
+
// If not initializing, start initialization
|
169
|
+
if (!this.initializing) {
|
170
|
+
return this.initialize();
|
171
|
+
}
|
172
|
+
const className = this.constructor.name;
|
173
|
+
const maxTime = this.timeout ?? (this.retries * this.retryInterval);
|
174
|
+
let retries = this.retries;
|
175
|
+
// Create a promise that resolves when initialization completes
|
176
|
+
return new Promise((resolve, reject) => {
|
177
|
+
// One-time event listeners for completion
|
178
|
+
const onInitialized = () => {
|
179
|
+
this.off('initialized', onInitialized);
|
180
|
+
this.off('failed', onFailed);
|
181
|
+
this.off('timeout', onTimeout);
|
182
|
+
resolve();
|
183
|
+
};
|
184
|
+
const onFailed = (error) => {
|
185
|
+
this.off('initialized', onInitialized);
|
186
|
+
this.off('failed', onFailed);
|
187
|
+
this.off('timeout', onTimeout);
|
188
|
+
reject(error);
|
189
|
+
};
|
190
|
+
const onTimeout = () => {
|
191
|
+
this.off('initialized', onInitialized);
|
192
|
+
this.off('failed', onFailed);
|
193
|
+
this.off('timeout', onTimeout);
|
194
|
+
reject(new Error(`Initialization of ${className} timed out after ${maxTime}ms`));
|
195
|
+
};
|
196
|
+
// Register event listeners
|
197
|
+
this.on('initialized', onInitialized);
|
198
|
+
this.on('failed', onFailed);
|
199
|
+
this.on('timeout', onTimeout);
|
200
|
+
// Set up polling to check for timeout
|
201
|
+
const checkInterval = setInterval(() => {
|
202
|
+
retries--;
|
203
|
+
if (this._initialized) {
|
204
|
+
clearInterval(checkInterval);
|
205
|
+
// Will be handled by event
|
206
|
+
}
|
207
|
+
else if (retries <= 0) {
|
208
|
+
clearInterval(checkInterval);
|
209
|
+
this.emit('timeout');
|
210
|
+
}
|
211
|
+
}, this.retryInterval);
|
212
|
+
});
|
213
|
+
}
|
214
|
+
/**
|
215
|
+
* Cancel initialization if in progress
|
216
|
+
*/
|
217
|
+
cancel() {
|
218
|
+
if (this.initializing && this.abortController) {
|
219
|
+
this.abortController.abort();
|
220
|
+
this.initializing = false;
|
221
|
+
this.failed = true;
|
222
|
+
this.emit('failed', new Error('Initialization cancelled'));
|
223
|
+
}
|
224
|
+
}
|
225
|
+
/**
|
226
|
+
* Reset initialization state
|
227
|
+
* Allows re-initialization after failure
|
228
|
+
*/
|
229
|
+
reset() {
|
230
|
+
if (this.initializing) {
|
231
|
+
this.cancel();
|
232
|
+
}
|
233
|
+
this._initialized = false;
|
234
|
+
this.initializing = false;
|
235
|
+
this.failed = false;
|
236
|
+
}
|
237
|
+
/**
|
238
|
+
* Register an event listener
|
239
|
+
* @param event Event name
|
240
|
+
* @param callback Function to call when event is emitted
|
241
|
+
* @returns this for chaining
|
242
|
+
*/
|
243
|
+
on(event, callback) {
|
244
|
+
if (!this.listeners.has(event)) {
|
245
|
+
this.listeners.set(event, []);
|
246
|
+
}
|
247
|
+
this.listeners.get(event).push(callback);
|
248
|
+
return this;
|
249
|
+
}
|
250
|
+
/**
|
251
|
+
* Remove an event listener
|
252
|
+
* @param event Event name
|
253
|
+
* @param callback Function to remove
|
254
|
+
* @returns this for chaining
|
255
|
+
*/
|
256
|
+
off(event, callback) {
|
257
|
+
if (this.listeners.has(event)) {
|
258
|
+
const callbacks = this.listeners.get(event);
|
259
|
+
const index = callbacks.indexOf(callback);
|
260
|
+
if (index !== -1) {
|
261
|
+
callbacks.splice(index, 1);
|
262
|
+
}
|
263
|
+
}
|
264
|
+
return this;
|
265
|
+
}
|
266
|
+
/**
|
267
|
+
* Emit an event
|
268
|
+
* @param event Event name
|
269
|
+
* @param args Arguments to pass to listeners
|
270
|
+
*/
|
271
|
+
emit(event, ...args) {
|
272
|
+
if (this.listeners.has(event)) {
|
273
|
+
const callbacks = [...this.listeners.get(event)];
|
274
|
+
callbacks.forEach(callback => {
|
275
|
+
try {
|
276
|
+
callback(...args);
|
277
|
+
}
|
278
|
+
catch (error) {
|
279
|
+
console.error(`Error in ${event} event listener:`, error);
|
280
|
+
}
|
281
|
+
});
|
282
|
+
}
|
283
|
+
}
|
284
|
+
/**
|
285
|
+
* Get the abort signal for cancellation
|
286
|
+
* Can be passed to fetch or other cancellable operations
|
287
|
+
*/
|
288
|
+
get abortSignal() {
|
289
|
+
return this.abortController?.signal;
|
290
|
+
}
|
291
|
+
/**
|
292
|
+
* Check if initialization has been completed
|
293
|
+
*/
|
294
|
+
get initialized() {
|
295
|
+
return this._initialized;
|
296
|
+
}
|
297
|
+
/**
|
298
|
+
* Check if initialization is in progress
|
299
|
+
*/
|
300
|
+
get isInitializing() {
|
301
|
+
return this.initializing;
|
302
|
+
}
|
303
|
+
/**
|
304
|
+
* Check if initialization has failed
|
305
|
+
*/
|
306
|
+
get hasFailed() {
|
307
|
+
return this.failed;
|
308
|
+
}
|
309
|
+
}
|
310
|
+
exports.default = Initializable;
|
311
|
+
//# sourceMappingURL=initializable.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"initializable.js","sourceRoot":"","sources":["../src/initializable.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;;AAwBH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,aAAa;IAyBlB;;;OAGG;IACH,YAAY,UAAuB,EAAE;QAtBrC,wDAAwD;QAChD,iBAAY,GAAY,KAAK,CAAC;QAEtC,4CAA4C;QACpC,iBAAY,GAAY,KAAK,CAAC;QAEtC,wCAAwC;QAChC,WAAM,GAAY,KAAK,CAAC;QAKhC,sBAAsB;QACd,cAAS,GAAwC,IAAI,GAAG,EAAE,CAAC;QAEnE,wCAAwC;QAChC,oBAAe,GAA2B,IAAI,CAAC;QAOtD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;QACrC,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,IAAI,GAAG,CAAC;QAClD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAE/B,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;YAC5B,kFAAkF;YAClF,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,UA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|
|
1
|
+
import * as fs from "fs";
|
2
|
+
declare class Lib {
|
3
|
+
static Log(...args: any): void;
|
4
|
+
static LogObject(object: any, text?: string): void;
|
5
|
+
static Warn(...args: any): void;
|
6
|
+
static $Log(...args: any): void;
|
7
|
+
static secondsToMilliseconds(seconds: number): number;
|
8
|
+
static minutesToMilliseconds(minutes: number): number;
|
9
|
+
static hoursToMilliseconds(hours: number): number;
|
10
|
+
static GetTimestamp(milliseconds_ago: number): Date;
|
11
|
+
static GetDateTimestamp(date: string | Date, format?: string, isUTC?: boolean): number;
|
12
|
+
static calculatePercentage(x: number, y: number): number;
|
13
|
+
static MYSQLTimestamp(): string;
|
14
|
+
static FormatUnixToDate(unix_time_stamp: number, in_milliseconds?: boolean): Date;
|
15
|
+
static FormatDate(date: Date | string, format?: string): string;
|
16
|
+
static DaysBetweenDates(startDate: Date | string | undefined, endDate: Date | string | undefined, format?: string): number;
|
17
|
+
static IsPastDate(date: Date | string, format?: string, debug?: boolean, currentDate?: Date | string): boolean;
|
18
|
+
static IsPastDateFrom(date: Date | string, from: Date | string, format?: string): boolean;
|
19
|
+
static addTimeFromDate(date: Date | string, milliseconds: number): Date;
|
20
|
+
static UUID(minLength?: number): string;
|
21
|
+
static Debounce(callback: (...args: any[]) => void, delay?: number): (...args: any[]) => any;
|
22
|
+
static IsNil(value: any): boolean;
|
23
|
+
static IsPrimitive(value: any): boolean;
|
24
|
+
static EmptyObject(value: any): boolean;
|
25
|
+
static IsNumpty(value: any, _objectsOnly?: boolean): boolean;
|
26
|
+
static IsEmpty(value: any, _objectsOnly?: boolean): boolean;
|
27
|
+
static IsArray(variable: any): boolean;
|
28
|
+
static IsString(variable: any): boolean;
|
29
|
+
static IsNumber(variable: any): boolean;
|
30
|
+
static IsObject(variable: any): boolean;
|
31
|
+
static IsFunction(variable: any): boolean;
|
32
|
+
static IsRegex(variable: any): boolean;
|
33
|
+
static IsBoolean(variable: any): boolean;
|
34
|
+
static GetType(value: any, asTypeOf?: boolean): null | string | boolean;
|
35
|
+
static GetProjectRoot(startDir?: string, rootReference?: string): string;
|
36
|
+
static RunTaskWithTimeout(task: () => Promise<void>, timeout: number): Promise<unknown>;
|
37
|
+
static GetFolderPath(folder: string): string;
|
38
|
+
static GetFilePath(folder: string, file: string): string;
|
39
|
+
static CreateDirectory(folderToCreate: string): Promise<string>;
|
40
|
+
static DeleteDirectory(folderToDelete: string): Promise<void>;
|
41
|
+
static CreateFile(folderPath: string, filePath: string, content: string): Promise<void>;
|
42
|
+
static GetFile(filePathFromRoot: string): fs.ReadStream;
|
43
|
+
static GetFilesInDirectory(directoryPath: string): string[];
|
44
|
+
static DeleteFile(filePathFromRoot: string): Promise<void>;
|
45
|
+
static Timestamp(log?: boolean): string;
|
46
|
+
static RemoveWhitespace(value: string): string;
|
47
|
+
static msToString(ms: number): string;
|
48
|
+
static FormatPhone(phone_number: string): string;
|
49
|
+
static ToMB(bytes: number, as_KB?: boolean, decimalPlaces?: number): string;
|
50
|
+
static ToGB(bytes: number, as_KB?: boolean, decimalPlaces?: number): string;
|
51
|
+
static RetryHandler<T extends (...args: any[]) => any>(func: T, retries?: number, ...args: Parameters<T>): Promise<ReturnType<T>>;
|
52
|
+
static Difference(x: any[], y: any[]): any[];
|
53
|
+
static ReadFileContent(filePath: string): Promise<string>;
|
54
|
+
static measureExecutionTime<T extends (...args: any[]) => any, U extends ReturnType<T>>(func: T, ...args: Parameters<T>): Promise<{
|
55
|
+
result: Awaited<U>;
|
56
|
+
time: number | string;
|
57
|
+
}>;
|
58
|
+
static ToCamelCase(str: string): string;
|
59
|
+
static ToSnakeCase(str: string): string;
|
60
|
+
static ToKebebCase(str: string): string;
|
61
|
+
}
|
62
|
+
export default Lib;
|
63
|
+
export declare class Debug {
|
64
|
+
static Log(...args: any): void;
|
65
|
+
static $Log(...args: any): void;
|
66
|
+
static LogObject(object: any, text?: string): void;
|
67
|
+
}
|