@freshpointcz/fresh-core 0.0.13 → 0.0.15
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/index.d.mts +220 -8
- package/dist/index.d.ts +220 -8
- package/dist/index.js +170 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +162 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import dayjs, { Dayjs } from 'dayjs';
|
|
2
2
|
import { Logger } from 'winston';
|
|
3
|
-
import { BaseEntity, ColumnOptions, Repository, DataSourceOptions } from 'typeorm';
|
|
3
|
+
import { BaseEntity, ColumnOptions, Repository, EntityTarget, EntityManager, DataSourceOptions } from 'typeorm';
|
|
4
4
|
import { Job } from 'node-schedule';
|
|
5
5
|
import * as eslint from 'eslint';
|
|
6
6
|
import * as typescript_eslint_dist_compatibility_types from 'typescript-eslint/dist/compatibility-types';
|
|
@@ -58,11 +58,62 @@ declare const logger: Logger;
|
|
|
58
58
|
|
|
59
59
|
declare function isValidCron(expr: string): boolean;
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Abstract base class implementing a per-subclass Singleton pattern.
|
|
63
|
+
*
|
|
64
|
+
* Each subclass of `Singleton` can have exactly one instance.
|
|
65
|
+
* Instantiation is guarded at runtime: calling `new` multiple times
|
|
66
|
+
* will always return the first created instance for that subclass.
|
|
67
|
+
*
|
|
68
|
+
* Instances are stored internally in a static map keyed by constructor
|
|
69
|
+
* function, allowing independent singleton instances per subclass.
|
|
70
|
+
*
|
|
71
|
+
* ⚠️ Important:
|
|
72
|
+
* - Subclasses MUST NOT override the constructor unless they fully
|
|
73
|
+
* understand the consequences.
|
|
74
|
+
* - Initialization logic must be placed in {@link onInit}, not the constructor.
|
|
75
|
+
* - The constructor may return an existing instance, which is legal in JS
|
|
76
|
+
* but mildly unsettling.
|
|
77
|
+
*/
|
|
61
78
|
declare abstract class Singleton {
|
|
79
|
+
/**
|
|
80
|
+
* Internal registry of singleton instances.
|
|
81
|
+
* One instance per subclass constructor.
|
|
82
|
+
*/
|
|
62
83
|
private static instances;
|
|
63
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Creates or returns the singleton instance for the subclass.
|
|
86
|
+
*
|
|
87
|
+
* If an instance already exists for the subclass, that instance is
|
|
88
|
+
* returned instead of creating a new one.
|
|
89
|
+
*
|
|
90
|
+
* @param callOnInit - Whether to call {@link onInit} for a newly
|
|
91
|
+
* created instance. Ignored if the instance already exists.
|
|
92
|
+
*/
|
|
93
|
+
constructor(callOnInit?: boolean);
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves the singleton instance for the calling subclass.
|
|
96
|
+
*
|
|
97
|
+
* If no instance exists, a new one is created using the provided
|
|
98
|
+
* constructor arguments.
|
|
99
|
+
*
|
|
100
|
+
* Intended to be used by subclasses as a protected factory method.
|
|
101
|
+
*
|
|
102
|
+
* @typeParam T - The concrete subclass type.
|
|
103
|
+
* @param args - Arguments forwarded to the subclass constructor.
|
|
104
|
+
* @returns The singleton instance of the subclass.
|
|
105
|
+
*/
|
|
64
106
|
protected static getInstance<T>(this: new (...args: any[]) => T, ...args: ConstructorParameters<any>): T;
|
|
65
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Lifecycle hook called once when the singleton instance is first created.
|
|
109
|
+
*
|
|
110
|
+
* Subclasses must implement this method instead of relying on the
|
|
111
|
+
* constructor for initialization logic.
|
|
112
|
+
*
|
|
113
|
+
* This method is guaranteed to be called at most once per subclass
|
|
114
|
+
* instance.
|
|
115
|
+
*/
|
|
116
|
+
protected abstract onInit(): void | Promise<void>;
|
|
66
117
|
}
|
|
67
118
|
|
|
68
119
|
type Status = "ok" | "error" | "not-authorized" | "not-authenticated" | "internal-server-error" | "validation-error";
|
|
@@ -240,7 +291,8 @@ declare enum LanguageCode {
|
|
|
240
291
|
declare enum PaymentMethod {
|
|
241
292
|
CODE = 0,
|
|
242
293
|
CARD = 1,
|
|
243
|
-
NONE = 2
|
|
294
|
+
NONE = 2,
|
|
295
|
+
CREDIT = 3
|
|
244
296
|
}
|
|
245
297
|
|
|
246
298
|
declare enum TransactionType {
|
|
@@ -292,6 +344,8 @@ declare function TimestampColumn(options?: ColumnOptions): PropertyDecorator;
|
|
|
292
344
|
|
|
293
345
|
declare abstract class FreshDao<T extends BaseEntity> {
|
|
294
346
|
protected abstract repo: Repository<T>;
|
|
347
|
+
protected abstract entity: EntityTarget<T>;
|
|
348
|
+
protected getRepo(manager?: EntityManager, entity?: EntityTarget<T>): Repository<T>;
|
|
295
349
|
}
|
|
296
350
|
|
|
297
351
|
declare class Category extends FreshEntity {
|
|
@@ -309,7 +363,93 @@ declare class Product extends FreshEntity {
|
|
|
309
363
|
declare class Subcategory extends FreshEntity {
|
|
310
364
|
}
|
|
311
365
|
|
|
366
|
+
/**
|
|
367
|
+
* Runtime validation for enum values.
|
|
368
|
+
* If a runtime enum object is provided, the value must match one of its values
|
|
369
|
+
* If no enum object is provided, this falls back to accepting `string | number`
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* isEnumValue(TransactionType, 0) // true
|
|
373
|
+
* isEnumValue(TransactionType, 99) // false
|
|
374
|
+
* isEnumValue(undefined, 99) // true
|
|
375
|
+
* isEnumValue(undefined, "99") // true
|
|
376
|
+
*/
|
|
377
|
+
declare function isEnumValue<E extends Record<string, string | number>>(enumObj: E | undefined, v: unknown): v is E[keyof E];
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Non-null object typeguard
|
|
381
|
+
* @usage Use for narrowing `unknown` type and to check that a value is a non-null object
|
|
382
|
+
* @description
|
|
383
|
+
* WARNING: Arrays and functions are technically objects in JS,
|
|
384
|
+
* but this guard intentionally allows them only as plain objects
|
|
385
|
+
* via structural usage.
|
|
386
|
+
*/
|
|
387
|
+
declare function isObject(v: unknown): v is Record<string, unknown>;
|
|
388
|
+
/**
|
|
389
|
+
* Wrapper around `Object.prototype.hasOwnProperty`.
|
|
390
|
+
* @usage Use this instead of `key in obj`
|
|
391
|
+
*/
|
|
392
|
+
declare function hasOwn(obj: Record<string, unknown>, key: string): boolean;
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Checks that a value is a finite number
|
|
396
|
+
*
|
|
397
|
+
* Rejects numbers like:
|
|
398
|
+
* - NaN
|
|
399
|
+
* - Infinity / -Infinity
|
|
400
|
+
*
|
|
401
|
+
* @usage numeric fields validation
|
|
402
|
+
*/
|
|
403
|
+
declare function isNumber(v: unknown): v is number;
|
|
404
|
+
/**
|
|
405
|
+
* Checks that a value is a string
|
|
406
|
+
*/
|
|
407
|
+
declare function isString(v: unknown): v is string;
|
|
408
|
+
/**
|
|
409
|
+
* Validates a numeric boolean flag represented as 0 or 1
|
|
410
|
+
*/
|
|
411
|
+
declare function isFlag01(v: unknown): v is 0 | 1;
|
|
412
|
+
/**
|
|
413
|
+
* Checks that a value is a finite number within a given range
|
|
414
|
+
* By default, the range is inclusive on both ends
|
|
415
|
+
*
|
|
416
|
+
* @param v - Value to validate
|
|
417
|
+
* @param min - Minimum allowed value
|
|
418
|
+
* @param max - Maximum allowed value
|
|
419
|
+
* @param options - Range behavior configuration
|
|
420
|
+
* @param options.includeMin - Whether `min` is allowed (default: true)
|
|
421
|
+
* @param options.includeMax - Whether `max` is allowed (default: true)
|
|
422
|
+
*
|
|
423
|
+
* @returns `true` if the value is a finite number within the range
|
|
424
|
+
* @example
|
|
425
|
+
* isNumberInRange(5, 1, 10); // true
|
|
426
|
+
* isNumberInRange(1, 1, 10); // true
|
|
427
|
+
* isNumberInRange(1, 1, 10, { includeMin: false }); // false
|
|
428
|
+
* isNumberInRange(10, 1, 10, { includeMax: false }); // false
|
|
429
|
+
* isNumberInRange("5", 1, 10); // false
|
|
430
|
+
* isNumberInRange(NaN, 1, 10); // false
|
|
431
|
+
*/
|
|
432
|
+
declare function isNumberInRange(v: unknown, min: number, max: number, options?: {
|
|
433
|
+
includeMin?: boolean;
|
|
434
|
+
includeMax?: boolean;
|
|
435
|
+
}): v is number;
|
|
436
|
+
|
|
312
437
|
type Maybe<T> = T | null;
|
|
438
|
+
/**
|
|
439
|
+
* Validates a value that may be `null` or given type
|
|
440
|
+
*
|
|
441
|
+
* WARNING:
|
|
442
|
+
* `undefined` is NOT accepted by this function.
|
|
443
|
+
* If you need `undefined` support, it must be handled
|
|
444
|
+
* before calling this guard or explicitly allowed elsewhere.
|
|
445
|
+
*
|
|
446
|
+
* @typeParam T - The underlying non-nullable type
|
|
447
|
+
* @param v - Value to validate
|
|
448
|
+
* @param inner - Typeguard function for the underlying type `T`
|
|
449
|
+
*
|
|
450
|
+
* @returns `true` if the value is `null` or satisfies the inner typeguard
|
|
451
|
+
*/
|
|
452
|
+
declare function isMaybe<T>(v: unknown, inner: (x: unknown) => x is T): v is Maybe<T>;
|
|
313
453
|
|
|
314
454
|
declare abstract class DataHelper<T> {
|
|
315
455
|
private _data?;
|
|
@@ -324,26 +464,98 @@ declare abstract class DataHelper<T> {
|
|
|
324
464
|
abstract startDataRetrieval(): Promise<Maybe<T>>;
|
|
325
465
|
}
|
|
326
466
|
|
|
467
|
+
/**
|
|
468
|
+
* Abstract base class for scheduled singleton jobs.
|
|
469
|
+
*
|
|
470
|
+
* Combines:
|
|
471
|
+
* - a per-subclass Singleton lifecycle
|
|
472
|
+
* - cron-based scheduling via `node-schedule`
|
|
473
|
+
* - a predictable execution entry point via {@link invoke}
|
|
474
|
+
*
|
|
475
|
+
* Each concrete job:
|
|
476
|
+
* - exists exactly once per process
|
|
477
|
+
* - may or may not be scheduled (depending on `jobEnabled`)
|
|
478
|
+
* - is responsible only for business logic, not scheduling mechanics
|
|
479
|
+
*
|
|
480
|
+
* The job is scheduled immediately during construction if enabled.
|
|
481
|
+
* Initialization logging is handled internally.
|
|
482
|
+
*
|
|
483
|
+
* @typeParam T - Return type of the job execution.
|
|
484
|
+
*
|
|
485
|
+
* ⚠️ Design notes for future maintainers:
|
|
486
|
+
* - Do NOT override the constructor unless you enjoy debugging time-based bugs.
|
|
487
|
+
* - All job logic belongs in {@link invoke}.
|
|
488
|
+
* - Rescheduling requires calling {@link reschedule} with valid cron.
|
|
489
|
+
*/
|
|
327
490
|
declare abstract class FreshJob<T = void> extends Singleton {
|
|
491
|
+
/** Human-readable job identifier (used for logs and errors). */
|
|
328
492
|
private _jobName;
|
|
493
|
+
/**
|
|
494
|
+
* Cron expression defining when the job runs.
|
|
495
|
+
*
|
|
496
|
+
* Must contain numeric cron fields only.
|
|
497
|
+
* Example: `0 5 * * 4`
|
|
498
|
+
*
|
|
499
|
+
* Timezone is always forced to `"Europe/Prague"`.
|
|
500
|
+
* If you need another timezone, this class is not your friend.
|
|
501
|
+
*/
|
|
329
502
|
private _cronExpression;
|
|
503
|
+
/** Scheduled job instance, or `null` if the job is disabled. */
|
|
330
504
|
private _job;
|
|
331
505
|
/**
|
|
332
|
-
*
|
|
333
|
-
*
|
|
506
|
+
* Creates and optionally schedules a singleton cron job.
|
|
507
|
+
*
|
|
508
|
+
* @param jobName - Logical name of the job (used in logs and errors).
|
|
509
|
+
* @param cronExpression - Cron expression consisting of numeric fields only.
|
|
510
|
+
* Example: `0 5 * * 4`
|
|
511
|
+
* The timezone `"Europe/Prague"` is applied automatically.
|
|
512
|
+
*
|
|
513
|
+
* @param jobEnabled - Whether the job should be scheduled immediately.
|
|
514
|
+
* If `false`, the instance exists but no cron trigger is registered.
|
|
515
|
+
*
|
|
516
|
+
* @throws Error If the cron expression is invalid.
|
|
517
|
+
* @throws Error If the job cannot be scheduled.
|
|
334
518
|
*/
|
|
335
519
|
constructor(jobName: string, cronExpression: string, jobEnabled: boolean);
|
|
336
|
-
/**
|
|
520
|
+
/**
|
|
521
|
+
* Initialization hook.
|
|
522
|
+
*
|
|
523
|
+
* Called once during construction and used primarily for logging.
|
|
524
|
+
* Can also be reused by subclasses if manual rescheduling is implemented.
|
|
525
|
+
*
|
|
526
|
+
* @param rescheduled - Indicates whether the job was reconfigured
|
|
527
|
+
* after initial creation.
|
|
528
|
+
*/
|
|
337
529
|
protected onInit(rescheduled?: boolean): void;
|
|
530
|
+
/** @returns The logical name of the job. */
|
|
338
531
|
get jobName(): string;
|
|
532
|
+
/** Allows subclasses to rename the job if absolutely necessary. */
|
|
339
533
|
protected set jobName(value: string);
|
|
534
|
+
/** @returns The cron expression used to schedule the job. */
|
|
340
535
|
get cronExpression(): string;
|
|
536
|
+
/**
|
|
537
|
+
* Allows subclasses to update the cron expression internally.
|
|
538
|
+
* Does reschedule of job if exists
|
|
539
|
+
*/
|
|
341
540
|
protected set cronExpression(value: string);
|
|
541
|
+
/** @returns The underlying scheduled job instance, or `null` if disabled. */
|
|
342
542
|
protected get job(): Job | null;
|
|
543
|
+
/** Allows subclasses to manage the scheduled job instance. */
|
|
343
544
|
protected set job(value: Job | null);
|
|
545
|
+
protected reschedule(newCronExpression: string): boolean;
|
|
546
|
+
/**
|
|
547
|
+
* Job execution entry point.
|
|
548
|
+
*
|
|
549
|
+
* This method is called by the scheduler when the cron rule matches.
|
|
550
|
+
* All business logic belongs here.
|
|
551
|
+
*
|
|
552
|
+
* @returns The result of the job execution, optionally wrapped in a Promise.
|
|
553
|
+
*/
|
|
344
554
|
abstract invoke(): T | Promise<T>;
|
|
345
555
|
}
|
|
346
556
|
|
|
557
|
+
type CardNumber = `${number}${number}${number}${number}`;
|
|
558
|
+
|
|
347
559
|
declare class ApiError extends Error {
|
|
348
560
|
private _statusCode;
|
|
349
561
|
get statusCode(): number;
|
|
@@ -429,4 +641,4 @@ interface HealthCheckResult {
|
|
|
429
641
|
};
|
|
430
642
|
}
|
|
431
643
|
|
|
432
|
-
export { AMOUNT_UNIT, ActionCommandCode, ApiError, Category, DataHelper, DateUtils, type Deferred, DepotPoolStatus, Device, FreshDao, FreshEntity, FreshHyperEntity, FreshJob, FreshTranslationBase, type HealthCheckResult, HttpStatus, LanguageCode, Manufacturer, type Maybe, PaymentMethod, PG_DATA_SOURCE_OPTIONS as PgDataSourceOptions, Product, SinglePromiseWaiter, Singleton, type Status, StatusDto, Subcategory, TimestampColumn, TransactionType, createDeferred, FRESH_ESLINT_CONFIG as freshEslintConfig, isValidCron, logger };
|
|
644
|
+
export { AMOUNT_UNIT, ActionCommandCode, ApiError, type CardNumber, Category, DataHelper, DateUtils, type Deferred, DepotPoolStatus, Device, FreshDao, FreshEntity, FreshHyperEntity, FreshJob, FreshTranslationBase, type HealthCheckResult, HttpStatus, LanguageCode, Manufacturer, type Maybe, PaymentMethod, PG_DATA_SOURCE_OPTIONS as PgDataSourceOptions, Product, SinglePromiseWaiter, Singleton, type Status, StatusDto, Subcategory, TimestampColumn, TransactionType, createDeferred, FRESH_ESLINT_CONFIG as freshEslintConfig, hasOwn, isEnumValue, isFlag01, isMaybe, isNumber, isNumberInRange, isObject, isString, isValidCron, logger };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import dayjs, { Dayjs } from 'dayjs';
|
|
2
2
|
import { Logger } from 'winston';
|
|
3
|
-
import { BaseEntity, ColumnOptions, Repository, DataSourceOptions } from 'typeorm';
|
|
3
|
+
import { BaseEntity, ColumnOptions, Repository, EntityTarget, EntityManager, DataSourceOptions } from 'typeorm';
|
|
4
4
|
import { Job } from 'node-schedule';
|
|
5
5
|
import * as eslint from 'eslint';
|
|
6
6
|
import * as typescript_eslint_dist_compatibility_types from 'typescript-eslint/dist/compatibility-types';
|
|
@@ -58,11 +58,62 @@ declare const logger: Logger;
|
|
|
58
58
|
|
|
59
59
|
declare function isValidCron(expr: string): boolean;
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Abstract base class implementing a per-subclass Singleton pattern.
|
|
63
|
+
*
|
|
64
|
+
* Each subclass of `Singleton` can have exactly one instance.
|
|
65
|
+
* Instantiation is guarded at runtime: calling `new` multiple times
|
|
66
|
+
* will always return the first created instance for that subclass.
|
|
67
|
+
*
|
|
68
|
+
* Instances are stored internally in a static map keyed by constructor
|
|
69
|
+
* function, allowing independent singleton instances per subclass.
|
|
70
|
+
*
|
|
71
|
+
* ⚠️ Important:
|
|
72
|
+
* - Subclasses MUST NOT override the constructor unless they fully
|
|
73
|
+
* understand the consequences.
|
|
74
|
+
* - Initialization logic must be placed in {@link onInit}, not the constructor.
|
|
75
|
+
* - The constructor may return an existing instance, which is legal in JS
|
|
76
|
+
* but mildly unsettling.
|
|
77
|
+
*/
|
|
61
78
|
declare abstract class Singleton {
|
|
79
|
+
/**
|
|
80
|
+
* Internal registry of singleton instances.
|
|
81
|
+
* One instance per subclass constructor.
|
|
82
|
+
*/
|
|
62
83
|
private static instances;
|
|
63
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Creates or returns the singleton instance for the subclass.
|
|
86
|
+
*
|
|
87
|
+
* If an instance already exists for the subclass, that instance is
|
|
88
|
+
* returned instead of creating a new one.
|
|
89
|
+
*
|
|
90
|
+
* @param callOnInit - Whether to call {@link onInit} for a newly
|
|
91
|
+
* created instance. Ignored if the instance already exists.
|
|
92
|
+
*/
|
|
93
|
+
constructor(callOnInit?: boolean);
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves the singleton instance for the calling subclass.
|
|
96
|
+
*
|
|
97
|
+
* If no instance exists, a new one is created using the provided
|
|
98
|
+
* constructor arguments.
|
|
99
|
+
*
|
|
100
|
+
* Intended to be used by subclasses as a protected factory method.
|
|
101
|
+
*
|
|
102
|
+
* @typeParam T - The concrete subclass type.
|
|
103
|
+
* @param args - Arguments forwarded to the subclass constructor.
|
|
104
|
+
* @returns The singleton instance of the subclass.
|
|
105
|
+
*/
|
|
64
106
|
protected static getInstance<T>(this: new (...args: any[]) => T, ...args: ConstructorParameters<any>): T;
|
|
65
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Lifecycle hook called once when the singleton instance is first created.
|
|
109
|
+
*
|
|
110
|
+
* Subclasses must implement this method instead of relying on the
|
|
111
|
+
* constructor for initialization logic.
|
|
112
|
+
*
|
|
113
|
+
* This method is guaranteed to be called at most once per subclass
|
|
114
|
+
* instance.
|
|
115
|
+
*/
|
|
116
|
+
protected abstract onInit(): void | Promise<void>;
|
|
66
117
|
}
|
|
67
118
|
|
|
68
119
|
type Status = "ok" | "error" | "not-authorized" | "not-authenticated" | "internal-server-error" | "validation-error";
|
|
@@ -240,7 +291,8 @@ declare enum LanguageCode {
|
|
|
240
291
|
declare enum PaymentMethod {
|
|
241
292
|
CODE = 0,
|
|
242
293
|
CARD = 1,
|
|
243
|
-
NONE = 2
|
|
294
|
+
NONE = 2,
|
|
295
|
+
CREDIT = 3
|
|
244
296
|
}
|
|
245
297
|
|
|
246
298
|
declare enum TransactionType {
|
|
@@ -292,6 +344,8 @@ declare function TimestampColumn(options?: ColumnOptions): PropertyDecorator;
|
|
|
292
344
|
|
|
293
345
|
declare abstract class FreshDao<T extends BaseEntity> {
|
|
294
346
|
protected abstract repo: Repository<T>;
|
|
347
|
+
protected abstract entity: EntityTarget<T>;
|
|
348
|
+
protected getRepo(manager?: EntityManager, entity?: EntityTarget<T>): Repository<T>;
|
|
295
349
|
}
|
|
296
350
|
|
|
297
351
|
declare class Category extends FreshEntity {
|
|
@@ -309,7 +363,93 @@ declare class Product extends FreshEntity {
|
|
|
309
363
|
declare class Subcategory extends FreshEntity {
|
|
310
364
|
}
|
|
311
365
|
|
|
366
|
+
/**
|
|
367
|
+
* Runtime validation for enum values.
|
|
368
|
+
* If a runtime enum object is provided, the value must match one of its values
|
|
369
|
+
* If no enum object is provided, this falls back to accepting `string | number`
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* isEnumValue(TransactionType, 0) // true
|
|
373
|
+
* isEnumValue(TransactionType, 99) // false
|
|
374
|
+
* isEnumValue(undefined, 99) // true
|
|
375
|
+
* isEnumValue(undefined, "99") // true
|
|
376
|
+
*/
|
|
377
|
+
declare function isEnumValue<E extends Record<string, string | number>>(enumObj: E | undefined, v: unknown): v is E[keyof E];
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Non-null object typeguard
|
|
381
|
+
* @usage Use for narrowing `unknown` type and to check that a value is a non-null object
|
|
382
|
+
* @description
|
|
383
|
+
* WARNING: Arrays and functions are technically objects in JS,
|
|
384
|
+
* but this guard intentionally allows them only as plain objects
|
|
385
|
+
* via structural usage.
|
|
386
|
+
*/
|
|
387
|
+
declare function isObject(v: unknown): v is Record<string, unknown>;
|
|
388
|
+
/**
|
|
389
|
+
* Wrapper around `Object.prototype.hasOwnProperty`.
|
|
390
|
+
* @usage Use this instead of `key in obj`
|
|
391
|
+
*/
|
|
392
|
+
declare function hasOwn(obj: Record<string, unknown>, key: string): boolean;
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Checks that a value is a finite number
|
|
396
|
+
*
|
|
397
|
+
* Rejects numbers like:
|
|
398
|
+
* - NaN
|
|
399
|
+
* - Infinity / -Infinity
|
|
400
|
+
*
|
|
401
|
+
* @usage numeric fields validation
|
|
402
|
+
*/
|
|
403
|
+
declare function isNumber(v: unknown): v is number;
|
|
404
|
+
/**
|
|
405
|
+
* Checks that a value is a string
|
|
406
|
+
*/
|
|
407
|
+
declare function isString(v: unknown): v is string;
|
|
408
|
+
/**
|
|
409
|
+
* Validates a numeric boolean flag represented as 0 or 1
|
|
410
|
+
*/
|
|
411
|
+
declare function isFlag01(v: unknown): v is 0 | 1;
|
|
412
|
+
/**
|
|
413
|
+
* Checks that a value is a finite number within a given range
|
|
414
|
+
* By default, the range is inclusive on both ends
|
|
415
|
+
*
|
|
416
|
+
* @param v - Value to validate
|
|
417
|
+
* @param min - Minimum allowed value
|
|
418
|
+
* @param max - Maximum allowed value
|
|
419
|
+
* @param options - Range behavior configuration
|
|
420
|
+
* @param options.includeMin - Whether `min` is allowed (default: true)
|
|
421
|
+
* @param options.includeMax - Whether `max` is allowed (default: true)
|
|
422
|
+
*
|
|
423
|
+
* @returns `true` if the value is a finite number within the range
|
|
424
|
+
* @example
|
|
425
|
+
* isNumberInRange(5, 1, 10); // true
|
|
426
|
+
* isNumberInRange(1, 1, 10); // true
|
|
427
|
+
* isNumberInRange(1, 1, 10, { includeMin: false }); // false
|
|
428
|
+
* isNumberInRange(10, 1, 10, { includeMax: false }); // false
|
|
429
|
+
* isNumberInRange("5", 1, 10); // false
|
|
430
|
+
* isNumberInRange(NaN, 1, 10); // false
|
|
431
|
+
*/
|
|
432
|
+
declare function isNumberInRange(v: unknown, min: number, max: number, options?: {
|
|
433
|
+
includeMin?: boolean;
|
|
434
|
+
includeMax?: boolean;
|
|
435
|
+
}): v is number;
|
|
436
|
+
|
|
312
437
|
type Maybe<T> = T | null;
|
|
438
|
+
/**
|
|
439
|
+
* Validates a value that may be `null` or given type
|
|
440
|
+
*
|
|
441
|
+
* WARNING:
|
|
442
|
+
* `undefined` is NOT accepted by this function.
|
|
443
|
+
* If you need `undefined` support, it must be handled
|
|
444
|
+
* before calling this guard or explicitly allowed elsewhere.
|
|
445
|
+
*
|
|
446
|
+
* @typeParam T - The underlying non-nullable type
|
|
447
|
+
* @param v - Value to validate
|
|
448
|
+
* @param inner - Typeguard function for the underlying type `T`
|
|
449
|
+
*
|
|
450
|
+
* @returns `true` if the value is `null` or satisfies the inner typeguard
|
|
451
|
+
*/
|
|
452
|
+
declare function isMaybe<T>(v: unknown, inner: (x: unknown) => x is T): v is Maybe<T>;
|
|
313
453
|
|
|
314
454
|
declare abstract class DataHelper<T> {
|
|
315
455
|
private _data?;
|
|
@@ -324,26 +464,98 @@ declare abstract class DataHelper<T> {
|
|
|
324
464
|
abstract startDataRetrieval(): Promise<Maybe<T>>;
|
|
325
465
|
}
|
|
326
466
|
|
|
467
|
+
/**
|
|
468
|
+
* Abstract base class for scheduled singleton jobs.
|
|
469
|
+
*
|
|
470
|
+
* Combines:
|
|
471
|
+
* - a per-subclass Singleton lifecycle
|
|
472
|
+
* - cron-based scheduling via `node-schedule`
|
|
473
|
+
* - a predictable execution entry point via {@link invoke}
|
|
474
|
+
*
|
|
475
|
+
* Each concrete job:
|
|
476
|
+
* - exists exactly once per process
|
|
477
|
+
* - may or may not be scheduled (depending on `jobEnabled`)
|
|
478
|
+
* - is responsible only for business logic, not scheduling mechanics
|
|
479
|
+
*
|
|
480
|
+
* The job is scheduled immediately during construction if enabled.
|
|
481
|
+
* Initialization logging is handled internally.
|
|
482
|
+
*
|
|
483
|
+
* @typeParam T - Return type of the job execution.
|
|
484
|
+
*
|
|
485
|
+
* ⚠️ Design notes for future maintainers:
|
|
486
|
+
* - Do NOT override the constructor unless you enjoy debugging time-based bugs.
|
|
487
|
+
* - All job logic belongs in {@link invoke}.
|
|
488
|
+
* - Rescheduling requires calling {@link reschedule} with valid cron.
|
|
489
|
+
*/
|
|
327
490
|
declare abstract class FreshJob<T = void> extends Singleton {
|
|
491
|
+
/** Human-readable job identifier (used for logs and errors). */
|
|
328
492
|
private _jobName;
|
|
493
|
+
/**
|
|
494
|
+
* Cron expression defining when the job runs.
|
|
495
|
+
*
|
|
496
|
+
* Must contain numeric cron fields only.
|
|
497
|
+
* Example: `0 5 * * 4`
|
|
498
|
+
*
|
|
499
|
+
* Timezone is always forced to `"Europe/Prague"`.
|
|
500
|
+
* If you need another timezone, this class is not your friend.
|
|
501
|
+
*/
|
|
329
502
|
private _cronExpression;
|
|
503
|
+
/** Scheduled job instance, or `null` if the job is disabled. */
|
|
330
504
|
private _job;
|
|
331
505
|
/**
|
|
332
|
-
*
|
|
333
|
-
*
|
|
506
|
+
* Creates and optionally schedules a singleton cron job.
|
|
507
|
+
*
|
|
508
|
+
* @param jobName - Logical name of the job (used in logs and errors).
|
|
509
|
+
* @param cronExpression - Cron expression consisting of numeric fields only.
|
|
510
|
+
* Example: `0 5 * * 4`
|
|
511
|
+
* The timezone `"Europe/Prague"` is applied automatically.
|
|
512
|
+
*
|
|
513
|
+
* @param jobEnabled - Whether the job should be scheduled immediately.
|
|
514
|
+
* If `false`, the instance exists but no cron trigger is registered.
|
|
515
|
+
*
|
|
516
|
+
* @throws Error If the cron expression is invalid.
|
|
517
|
+
* @throws Error If the job cannot be scheduled.
|
|
334
518
|
*/
|
|
335
519
|
constructor(jobName: string, cronExpression: string, jobEnabled: boolean);
|
|
336
|
-
/**
|
|
520
|
+
/**
|
|
521
|
+
* Initialization hook.
|
|
522
|
+
*
|
|
523
|
+
* Called once during construction and used primarily for logging.
|
|
524
|
+
* Can also be reused by subclasses if manual rescheduling is implemented.
|
|
525
|
+
*
|
|
526
|
+
* @param rescheduled - Indicates whether the job was reconfigured
|
|
527
|
+
* after initial creation.
|
|
528
|
+
*/
|
|
337
529
|
protected onInit(rescheduled?: boolean): void;
|
|
530
|
+
/** @returns The logical name of the job. */
|
|
338
531
|
get jobName(): string;
|
|
532
|
+
/** Allows subclasses to rename the job if absolutely necessary. */
|
|
339
533
|
protected set jobName(value: string);
|
|
534
|
+
/** @returns The cron expression used to schedule the job. */
|
|
340
535
|
get cronExpression(): string;
|
|
536
|
+
/**
|
|
537
|
+
* Allows subclasses to update the cron expression internally.
|
|
538
|
+
* Does reschedule of job if exists
|
|
539
|
+
*/
|
|
341
540
|
protected set cronExpression(value: string);
|
|
541
|
+
/** @returns The underlying scheduled job instance, or `null` if disabled. */
|
|
342
542
|
protected get job(): Job | null;
|
|
543
|
+
/** Allows subclasses to manage the scheduled job instance. */
|
|
343
544
|
protected set job(value: Job | null);
|
|
545
|
+
protected reschedule(newCronExpression: string): boolean;
|
|
546
|
+
/**
|
|
547
|
+
* Job execution entry point.
|
|
548
|
+
*
|
|
549
|
+
* This method is called by the scheduler when the cron rule matches.
|
|
550
|
+
* All business logic belongs here.
|
|
551
|
+
*
|
|
552
|
+
* @returns The result of the job execution, optionally wrapped in a Promise.
|
|
553
|
+
*/
|
|
344
554
|
abstract invoke(): T | Promise<T>;
|
|
345
555
|
}
|
|
346
556
|
|
|
557
|
+
type CardNumber = `${number}${number}${number}${number}`;
|
|
558
|
+
|
|
347
559
|
declare class ApiError extends Error {
|
|
348
560
|
private _statusCode;
|
|
349
561
|
get statusCode(): number;
|
|
@@ -429,4 +641,4 @@ interface HealthCheckResult {
|
|
|
429
641
|
};
|
|
430
642
|
}
|
|
431
643
|
|
|
432
|
-
export { AMOUNT_UNIT, ActionCommandCode, ApiError, Category, DataHelper, DateUtils, type Deferred, DepotPoolStatus, Device, FreshDao, FreshEntity, FreshHyperEntity, FreshJob, FreshTranslationBase, type HealthCheckResult, HttpStatus, LanguageCode, Manufacturer, type Maybe, PaymentMethod, PG_DATA_SOURCE_OPTIONS as PgDataSourceOptions, Product, SinglePromiseWaiter, Singleton, type Status, StatusDto, Subcategory, TimestampColumn, TransactionType, createDeferred, FRESH_ESLINT_CONFIG as freshEslintConfig, isValidCron, logger };
|
|
644
|
+
export { AMOUNT_UNIT, ActionCommandCode, ApiError, type CardNumber, Category, DataHelper, DateUtils, type Deferred, DepotPoolStatus, Device, FreshDao, FreshEntity, FreshHyperEntity, FreshJob, FreshTranslationBase, type HealthCheckResult, HttpStatus, LanguageCode, Manufacturer, type Maybe, PaymentMethod, PG_DATA_SOURCE_OPTIONS as PgDataSourceOptions, Product, SinglePromiseWaiter, Singleton, type Status, StatusDto, Subcategory, TimestampColumn, TransactionType, createDeferred, FRESH_ESLINT_CONFIG as freshEslintConfig, hasOwn, isEnumValue, isFlag01, isMaybe, isNumber, isNumberInRange, isObject, isString, isValidCron, logger };
|