@freshpointcz/fresh-core 0.0.14 → 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 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';
@@ -291,7 +291,8 @@ declare enum LanguageCode {
291
291
  declare enum PaymentMethod {
292
292
  CODE = 0,
293
293
  CARD = 1,
294
- NONE = 2
294
+ NONE = 2,
295
+ CREDIT = 3
295
296
  }
296
297
 
297
298
  declare enum TransactionType {
@@ -343,6 +344,8 @@ declare function TimestampColumn(options?: ColumnOptions): PropertyDecorator;
343
344
 
344
345
  declare abstract class FreshDao<T extends BaseEntity> {
345
346
  protected abstract repo: Repository<T>;
347
+ protected abstract entity: EntityTarget<T>;
348
+ protected getRepo(manager?: EntityManager, entity?: EntityTarget<T>): Repository<T>;
346
349
  }
347
350
 
348
351
  declare class Category extends FreshEntity {
@@ -360,7 +363,93 @@ declare class Product extends FreshEntity {
360
363
  declare class Subcategory extends FreshEntity {
361
364
  }
362
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
+
363
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>;
364
453
 
365
454
  declare abstract class DataHelper<T> {
366
455
  private _data?;
@@ -465,6 +554,8 @@ declare abstract class FreshJob<T = void> extends Singleton {
465
554
  abstract invoke(): T | Promise<T>;
466
555
  }
467
556
 
557
+ type CardNumber = `${number}${number}${number}${number}`;
558
+
468
559
  declare class ApiError extends Error {
469
560
  private _statusCode;
470
561
  get statusCode(): number;
@@ -550,4 +641,4 @@ interface HealthCheckResult {
550
641
  };
551
642
  }
552
643
 
553
- 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';
@@ -291,7 +291,8 @@ declare enum LanguageCode {
291
291
  declare enum PaymentMethod {
292
292
  CODE = 0,
293
293
  CARD = 1,
294
- NONE = 2
294
+ NONE = 2,
295
+ CREDIT = 3
295
296
  }
296
297
 
297
298
  declare enum TransactionType {
@@ -343,6 +344,8 @@ declare function TimestampColumn(options?: ColumnOptions): PropertyDecorator;
343
344
 
344
345
  declare abstract class FreshDao<T extends BaseEntity> {
345
346
  protected abstract repo: Repository<T>;
347
+ protected abstract entity: EntityTarget<T>;
348
+ protected getRepo(manager?: EntityManager, entity?: EntityTarget<T>): Repository<T>;
346
349
  }
347
350
 
348
351
  declare class Category extends FreshEntity {
@@ -360,7 +363,93 @@ declare class Product extends FreshEntity {
360
363
  declare class Subcategory extends FreshEntity {
361
364
  }
362
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
+
363
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>;
364
453
 
365
454
  declare abstract class DataHelper<T> {
366
455
  private _data?;
@@ -465,6 +554,8 @@ declare abstract class FreshJob<T = void> extends Singleton {
465
554
  abstract invoke(): T | Promise<T>;
466
555
  }
467
556
 
557
+ type CardNumber = `${number}${number}${number}${number}`;
558
+
468
559
  declare class ApiError extends Error {
469
560
  private _statusCode;
470
561
  get statusCode(): number;
@@ -550,4 +641,4 @@ interface HealthCheckResult {
550
641
  };
551
642
  }
552
643
 
553
- 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.js CHANGED
@@ -452,6 +452,14 @@ __export(index_exports, {
452
452
  TransactionType: () => TransactionType,
453
453
  createDeferred: () => createDeferred,
454
454
  freshEslintConfig: () => eslint_config_default,
455
+ hasOwn: () => hasOwn,
456
+ isEnumValue: () => isEnumValue,
457
+ isFlag01: () => isFlag01,
458
+ isMaybe: () => isMaybe,
459
+ isNumber: () => isNumber,
460
+ isNumberInRange: () => isNumberInRange,
461
+ isObject: () => isObject,
462
+ isString: () => isString,
455
463
  isValidCron: () => isValidCron,
456
464
  logger: () => logger
457
465
  });
@@ -1140,6 +1148,7 @@ var PaymentMethod = /* @__PURE__ */ (function(PaymentMethod2) {
1140
1148
  PaymentMethod2[PaymentMethod2["CODE"] = 0] = "CODE";
1141
1149
  PaymentMethod2[PaymentMethod2["CARD"] = 1] = "CARD";
1142
1150
  PaymentMethod2[PaymentMethod2["NONE"] = 2] = "NONE";
1151
+ PaymentMethod2[PaymentMethod2["CREDIT"] = 3] = "CREDIT";
1143
1152
  return PaymentMethod2;
1144
1153
  })({});
1145
1154
 
@@ -1198,6 +1207,9 @@ _ts_decorate3([
1198
1207
 
1199
1208
  // src/database/dao/fresh-dao.ts
1200
1209
  var _FreshDao = class _FreshDao {
1210
+ getRepo(manager, entity = this.entity) {
1211
+ return manager ? manager.getRepository(entity) : this.repo;
1212
+ }
1201
1213
  };
1202
1214
  __name(_FreshDao, "FreshDao");
1203
1215
  var FreshDao = _FreshDao;
@@ -1286,6 +1298,56 @@ Subcategory = _ts_decorate8([
1286
1298
  (0, import_typeorm9.Entity)()
1287
1299
  ], Subcategory);
1288
1300
 
1301
+ // src/common/typeguards/enums.ts
1302
+ function isEnumValue(enumObj, v) {
1303
+ if (!enumObj) {
1304
+ return typeof v === "string" || typeof v === "number";
1305
+ }
1306
+ const values = new Set(Object.values(enumObj));
1307
+ return (typeof v === "string" || typeof v === "number") && values.has(v);
1308
+ }
1309
+ __name(isEnumValue, "isEnumValue");
1310
+
1311
+ // src/common/typeguards/objects.ts
1312
+ function isObject(v) {
1313
+ return typeof v === "object" && v !== null;
1314
+ }
1315
+ __name(isObject, "isObject");
1316
+ function hasOwn(obj, key) {
1317
+ return Object.prototype.hasOwnProperty.call(obj, key);
1318
+ }
1319
+ __name(hasOwn, "hasOwn");
1320
+
1321
+ // src/common/typeguards/primitives.ts
1322
+ function isNumber(v) {
1323
+ return typeof v === "number" && Number.isFinite(v);
1324
+ }
1325
+ __name(isNumber, "isNumber");
1326
+ function isString(v) {
1327
+ return typeof v === "string";
1328
+ }
1329
+ __name(isString, "isString");
1330
+ function isFlag01(v) {
1331
+ return v === 0 || v === 1;
1332
+ }
1333
+ __name(isFlag01, "isFlag01");
1334
+ function isNumberInRange(v, min, max, options) {
1335
+ var _a, _b;
1336
+ if (!isNumber(v)) {
1337
+ return false;
1338
+ }
1339
+ const includeMin = (_a = options == null ? void 0 : options.includeMin) != null ? _a : true;
1340
+ const includeMax = (_b = options == null ? void 0 : options.includeMax) != null ? _b : true;
1341
+ if (includeMin ? v < min : v <= min) {
1342
+ return false;
1343
+ }
1344
+ if (includeMax ? v > max : v >= max) {
1345
+ return false;
1346
+ }
1347
+ return true;
1348
+ }
1349
+ __name(isNumberInRange, "isNumberInRange");
1350
+
1289
1351
  // src/core/data-helper.ts
1290
1352
  var _DataHelper = class _DataHelper {
1291
1353
  constructor(startDataRetrieve = true, deviceIds) {
@@ -1459,6 +1521,12 @@ var _ApiError = class _ApiError extends Error {
1459
1521
  __name(_ApiError, "ApiError");
1460
1522
  var ApiError = _ApiError;
1461
1523
 
1524
+ // src/types/maybe-type.ts
1525
+ function isMaybe(v, inner) {
1526
+ return v === null || inner(v);
1527
+ }
1528
+ __name(isMaybe, "isMaybe");
1529
+
1462
1530
  // src/config/eslint-config.ts
1463
1531
  var import_typescript_eslint = __toESM(require("typescript-eslint"));
1464
1532
  var import_eslint_plugin_prettier = __toESM(require("eslint-plugin-prettier"));
@@ -1670,6 +1738,14 @@ var datasource_default = PG_DATA_SOURCE_OPTIONS;
1670
1738
  TransactionType,
1671
1739
  createDeferred,
1672
1740
  freshEslintConfig,
1741
+ hasOwn,
1742
+ isEnumValue,
1743
+ isFlag01,
1744
+ isMaybe,
1745
+ isNumber,
1746
+ isNumberInRange,
1747
+ isObject,
1748
+ isString,
1673
1749
  isValidCron,
1674
1750
  logger
1675
1751
  });