@freshpointcz/fresh-core 0.0.13 → 0.0.14

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 CHANGED
@@ -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
- constructor();
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
- protected abstract onInit(): void;
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";
@@ -324,23 +375,93 @@ declare abstract class DataHelper<T> {
324
375
  abstract startDataRetrieval(): Promise<Maybe<T>>;
325
376
  }
326
377
 
378
+ /**
379
+ * Abstract base class for scheduled singleton jobs.
380
+ *
381
+ * Combines:
382
+ * - a per-subclass Singleton lifecycle
383
+ * - cron-based scheduling via `node-schedule`
384
+ * - a predictable execution entry point via {@link invoke}
385
+ *
386
+ * Each concrete job:
387
+ * - exists exactly once per process
388
+ * - may or may not be scheduled (depending on `jobEnabled`)
389
+ * - is responsible only for business logic, not scheduling mechanics
390
+ *
391
+ * The job is scheduled immediately during construction if enabled.
392
+ * Initialization logging is handled internally.
393
+ *
394
+ * @typeParam T - Return type of the job execution.
395
+ *
396
+ * ⚠️ Design notes for future maintainers:
397
+ * - Do NOT override the constructor unless you enjoy debugging time-based bugs.
398
+ * - All job logic belongs in {@link invoke}.
399
+ * - Rescheduling requires calling {@link reschedule} with valid cron.
400
+ */
327
401
  declare abstract class FreshJob<T = void> extends Singleton {
402
+ /** Human-readable job identifier (used for logs and errors). */
328
403
  private _jobName;
404
+ /**
405
+ * Cron expression defining when the job runs.
406
+ *
407
+ * Must contain numeric cron fields only.
408
+ * Example: `0 5 * * 4`
409
+ *
410
+ * Timezone is always forced to `"Europe/Prague"`.
411
+ * If you need another timezone, this class is not your friend.
412
+ */
329
413
  private _cronExpression;
414
+ /** Scheduled job instance, or `null` if the job is disabled. */
330
415
  private _job;
331
416
  /**
332
- * @param cronExpression must be cron of just numbers ex.: `0 5 * * 4`
333
- * By default timezone is added " Europe/Prague"
417
+ * Creates and optionally schedules a singleton cron job.
418
+ *
419
+ * @param jobName - Logical name of the job (used in logs and errors).
420
+ * @param cronExpression - Cron expression consisting of numeric fields only.
421
+ * Example: `0 5 * * 4`
422
+ * The timezone `"Europe/Prague"` is applied automatically.
423
+ *
424
+ * @param jobEnabled - Whether the job should be scheduled immediately.
425
+ * If `false`, the instance exists but no cron trigger is registered.
426
+ *
427
+ * @throws Error If the cron expression is invalid.
428
+ * @throws Error If the job cannot be scheduled.
334
429
  */
335
430
  constructor(jobName: string, cronExpression: string, jobEnabled: boolean);
336
- /** Just logging */
431
+ /**
432
+ * Initialization hook.
433
+ *
434
+ * Called once during construction and used primarily for logging.
435
+ * Can also be reused by subclasses if manual rescheduling is implemented.
436
+ *
437
+ * @param rescheduled - Indicates whether the job was reconfigured
438
+ * after initial creation.
439
+ */
337
440
  protected onInit(rescheduled?: boolean): void;
441
+ /** @returns The logical name of the job. */
338
442
  get jobName(): string;
443
+ /** Allows subclasses to rename the job if absolutely necessary. */
339
444
  protected set jobName(value: string);
445
+ /** @returns The cron expression used to schedule the job. */
340
446
  get cronExpression(): string;
447
+ /**
448
+ * Allows subclasses to update the cron expression internally.
449
+ * Does reschedule of job if exists
450
+ */
341
451
  protected set cronExpression(value: string);
452
+ /** @returns The underlying scheduled job instance, or `null` if disabled. */
342
453
  protected get job(): Job | null;
454
+ /** Allows subclasses to manage the scheduled job instance. */
343
455
  protected set job(value: Job | null);
456
+ protected reschedule(newCronExpression: string): boolean;
457
+ /**
458
+ * Job execution entry point.
459
+ *
460
+ * This method is called by the scheduler when the cron rule matches.
461
+ * All business logic belongs here.
462
+ *
463
+ * @returns The result of the job execution, optionally wrapped in a Promise.
464
+ */
344
465
  abstract invoke(): T | Promise<T>;
345
466
  }
346
467
 
package/dist/index.d.ts CHANGED
@@ -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
- constructor();
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
- protected abstract onInit(): void;
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";
@@ -324,23 +375,93 @@ declare abstract class DataHelper<T> {
324
375
  abstract startDataRetrieval(): Promise<Maybe<T>>;
325
376
  }
326
377
 
378
+ /**
379
+ * Abstract base class for scheduled singleton jobs.
380
+ *
381
+ * Combines:
382
+ * - a per-subclass Singleton lifecycle
383
+ * - cron-based scheduling via `node-schedule`
384
+ * - a predictable execution entry point via {@link invoke}
385
+ *
386
+ * Each concrete job:
387
+ * - exists exactly once per process
388
+ * - may or may not be scheduled (depending on `jobEnabled`)
389
+ * - is responsible only for business logic, not scheduling mechanics
390
+ *
391
+ * The job is scheduled immediately during construction if enabled.
392
+ * Initialization logging is handled internally.
393
+ *
394
+ * @typeParam T - Return type of the job execution.
395
+ *
396
+ * ⚠️ Design notes for future maintainers:
397
+ * - Do NOT override the constructor unless you enjoy debugging time-based bugs.
398
+ * - All job logic belongs in {@link invoke}.
399
+ * - Rescheduling requires calling {@link reschedule} with valid cron.
400
+ */
327
401
  declare abstract class FreshJob<T = void> extends Singleton {
402
+ /** Human-readable job identifier (used for logs and errors). */
328
403
  private _jobName;
404
+ /**
405
+ * Cron expression defining when the job runs.
406
+ *
407
+ * Must contain numeric cron fields only.
408
+ * Example: `0 5 * * 4`
409
+ *
410
+ * Timezone is always forced to `"Europe/Prague"`.
411
+ * If you need another timezone, this class is not your friend.
412
+ */
329
413
  private _cronExpression;
414
+ /** Scheduled job instance, or `null` if the job is disabled. */
330
415
  private _job;
331
416
  /**
332
- * @param cronExpression must be cron of just numbers ex.: `0 5 * * 4`
333
- * By default timezone is added " Europe/Prague"
417
+ * Creates and optionally schedules a singleton cron job.
418
+ *
419
+ * @param jobName - Logical name of the job (used in logs and errors).
420
+ * @param cronExpression - Cron expression consisting of numeric fields only.
421
+ * Example: `0 5 * * 4`
422
+ * The timezone `"Europe/Prague"` is applied automatically.
423
+ *
424
+ * @param jobEnabled - Whether the job should be scheduled immediately.
425
+ * If `false`, the instance exists but no cron trigger is registered.
426
+ *
427
+ * @throws Error If the cron expression is invalid.
428
+ * @throws Error If the job cannot be scheduled.
334
429
  */
335
430
  constructor(jobName: string, cronExpression: string, jobEnabled: boolean);
336
- /** Just logging */
431
+ /**
432
+ * Initialization hook.
433
+ *
434
+ * Called once during construction and used primarily for logging.
435
+ * Can also be reused by subclasses if manual rescheduling is implemented.
436
+ *
437
+ * @param rescheduled - Indicates whether the job was reconfigured
438
+ * after initial creation.
439
+ */
337
440
  protected onInit(rescheduled?: boolean): void;
441
+ /** @returns The logical name of the job. */
338
442
  get jobName(): string;
443
+ /** Allows subclasses to rename the job if absolutely necessary. */
339
444
  protected set jobName(value: string);
445
+ /** @returns The cron expression used to schedule the job. */
340
446
  get cronExpression(): string;
447
+ /**
448
+ * Allows subclasses to update the cron expression internally.
449
+ * Does reschedule of job if exists
450
+ */
341
451
  protected set cronExpression(value: string);
452
+ /** @returns The underlying scheduled job instance, or `null` if disabled. */
342
453
  protected get job(): Job | null;
454
+ /** Allows subclasses to manage the scheduled job instance. */
343
455
  protected set job(value: Job | null);
456
+ protected reschedule(newCronExpression: string): boolean;
457
+ /**
458
+ * Job execution entry point.
459
+ *
460
+ * This method is called by the scheduler when the cron rule matches.
461
+ * All business logic belongs here.
462
+ *
463
+ * @returns The result of the job execution, optionally wrapped in a Promise.
464
+ */
344
465
  abstract invoke(): T | Promise<T>;
345
466
  }
346
467
 
package/dist/index.js CHANGED
@@ -746,14 +746,38 @@ __name(isValidCron, "isValidCron");
746
746
 
747
747
  // src/common/patterns/singleton.ts
748
748
  var _Singleton = class _Singleton {
749
- constructor() {
749
+ /**
750
+ * Creates or returns the singleton instance for the subclass.
751
+ *
752
+ * If an instance already exists for the subclass, that instance is
753
+ * returned instead of creating a new one.
754
+ *
755
+ * @param callOnInit - Whether to call {@link onInit} for a newly
756
+ * created instance. Ignored if the instance already exists.
757
+ */
758
+ constructor(callOnInit = true) {
750
759
  const ctor = this.constructor;
751
760
  const existing = _Singleton.instances.get(ctor);
752
761
  if (existing) {
753
762
  return existing;
754
763
  }
755
764
  _Singleton.instances.set(ctor, this);
765
+ if (callOnInit) {
766
+ void this.onInit();
767
+ }
756
768
  }
769
+ /**
770
+ * Retrieves the singleton instance for the calling subclass.
771
+ *
772
+ * If no instance exists, a new one is created using the provided
773
+ * constructor arguments.
774
+ *
775
+ * Intended to be used by subclasses as a protected factory method.
776
+ *
777
+ * @typeParam T - The concrete subclass type.
778
+ * @param args - Arguments forwarded to the subclass constructor.
779
+ * @returns The singleton instance of the subclass.
780
+ */
757
781
  static getInstance(...args) {
758
782
  let instance = _Singleton.instances.get(this);
759
783
  if (!instance) {
@@ -764,6 +788,10 @@ var _Singleton = class _Singleton {
764
788
  }
765
789
  };
766
790
  __name(_Singleton, "Singleton");
791
+ /**
792
+ * Internal registry of singleton instances.
793
+ * One instance per subclass constructor.
794
+ */
767
795
  __publicField(_Singleton, "instances", /* @__PURE__ */ new Map());
768
796
  var Singleton = _Singleton;
769
797
 
@@ -1299,13 +1327,34 @@ var DataHelper = _DataHelper;
1299
1327
  var import_node_schedule = require("node-schedule");
1300
1328
  var _FreshJob = class _FreshJob extends Singleton {
1301
1329
  /**
1302
- * @param cronExpression must be cron of just numbers ex.: `0 5 * * 4`
1303
- * By default timezone is added " Europe/Prague"
1330
+ * Creates and optionally schedules a singleton cron job.
1331
+ *
1332
+ * @param jobName - Logical name of the job (used in logs and errors).
1333
+ * @param cronExpression - Cron expression consisting of numeric fields only.
1334
+ * Example: `0 5 * * 4`
1335
+ * The timezone `"Europe/Prague"` is applied automatically.
1336
+ *
1337
+ * @param jobEnabled - Whether the job should be scheduled immediately.
1338
+ * If `false`, the instance exists but no cron trigger is registered.
1339
+ *
1340
+ * @throws Error If the cron expression is invalid.
1341
+ * @throws Error If the job cannot be scheduled.
1304
1342
  */
1305
1343
  constructor(jobName, cronExpression, jobEnabled) {
1306
- super();
1344
+ super(false);
1345
+ /** Human-readable job identifier (used for logs and errors). */
1307
1346
  __publicField(this, "_jobName");
1347
+ /**
1348
+ * Cron expression defining when the job runs.
1349
+ *
1350
+ * Must contain numeric cron fields only.
1351
+ * Example: `0 5 * * 4`
1352
+ *
1353
+ * Timezone is always forced to `"Europe/Prague"`.
1354
+ * If you need another timezone, this class is not your friend.
1355
+ */
1308
1356
  __publicField(this, "_cronExpression");
1357
+ /** Scheduled job instance, or `null` if the job is disabled. */
1309
1358
  __publicField(this, "_job", null);
1310
1359
  this._jobName = jobName;
1311
1360
  if (!isValidCron(cronExpression)) {
@@ -1325,28 +1374,68 @@ var _FreshJob = class _FreshJob extends Singleton {
1325
1374
  }
1326
1375
  this.onInit();
1327
1376
  }
1328
- /** Just logging */
1377
+ /**
1378
+ * Initialization hook.
1379
+ *
1380
+ * Called once during construction and used primarily for logging.
1381
+ * Can also be reused by subclasses if manual rescheduling is implemented.
1382
+ *
1383
+ * @param rescheduled - Indicates whether the job was reconfigured
1384
+ * after initial creation.
1385
+ */
1329
1386
  onInit(rescheduled) {
1330
1387
  console.log(`Job ${this.jobName} ${rescheduled ? "rescheduled" : "initialized"} with cron rule: "${this.cronExpression}"`);
1331
1388
  }
1389
+ /** @returns The logical name of the job. */
1332
1390
  get jobName() {
1333
1391
  return this._jobName;
1334
1392
  }
1393
+ /** Allows subclasses to rename the job if absolutely necessary. */
1335
1394
  set jobName(value) {
1336
1395
  this._jobName = value;
1337
1396
  }
1397
+ /** @returns The cron expression used to schedule the job. */
1338
1398
  get cronExpression() {
1339
1399
  return this._cronExpression;
1340
1400
  }
1401
+ /**
1402
+ * Allows subclasses to update the cron expression internally.
1403
+ * Does reschedule of job if exists
1404
+ */
1341
1405
  set cronExpression(value) {
1406
+ if (!isValidCron(value)) {
1407
+ throw new Error(`Job ${this._jobName} cannot be re-scheduled with invalid cron: "${value}"`);
1408
+ }
1342
1409
  this._cronExpression = value;
1343
1410
  }
1411
+ /** @returns The underlying scheduled job instance, or `null` if disabled. */
1344
1412
  get job() {
1345
1413
  return this._job;
1346
1414
  }
1415
+ /** Allows subclasses to manage the scheduled job instance. */
1347
1416
  set job(value) {
1348
1417
  this._job = value;
1349
1418
  }
1419
+ reschedule(newCronExpression) {
1420
+ let result = false;
1421
+ if (!isValidCron(newCronExpression)) {
1422
+ throw new Error(`Job ${this._jobName} cannot be re-scheduled with invalid cron: "${newCronExpression}"`);
1423
+ }
1424
+ this._cronExpression = newCronExpression;
1425
+ if (this._job) {
1426
+ const rescheduleSuccess = this._job.reschedule({
1427
+ rule: this._cronExpression,
1428
+ tz: "Europe/Prague"
1429
+ });
1430
+ if (!rescheduleSuccess) {
1431
+ throw new Error(`Job ${this._jobName} could not be re-scheduled`);
1432
+ } else {
1433
+ this.onInit(true);
1434
+ }
1435
+ result = rescheduleSuccess;
1436
+ }
1437
+ return result;
1438
+ }
1350
1439
  };
1351
1440
  __name(_FreshJob, "FreshJob");
1352
1441
  var FreshJob = _FreshJob;