@powfix/core-js 0.13.10 → 0.13.12
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/src/utils/DateUtils.d.ts +1 -1
- package/dist/src/utils/DateUtils.js +2 -2
- package/dist/src/utils/backend/base/SequelizeUtils.d.ts +25 -2
- package/dist/src/utils/browser.d.ts +1 -0
- package/dist/src/utils/browser.js +1 -0
- package/dist/src/utils/try-catch/TryCatch.d.ts +24 -0
- package/dist/src/utils/try-catch/TryCatch.js +78 -0
- package/dist/src/utils/try-catch/index.d.ts +1 -0
- package/dist/src/utils/try-catch/index.js +17 -0
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MomentInput, RelativeTimeSpec } from "moment";
|
|
2
2
|
export declare class DateUtils {
|
|
3
|
-
static relativeDate: (input: MomentInput,
|
|
3
|
+
static relativeDate: (input: MomentInput, base?: MomentInput) => string;
|
|
4
4
|
static setLocale: (language: DateUtils.Locale) => void;
|
|
5
5
|
private static getRelativeTimeSpec;
|
|
6
6
|
}
|
|
@@ -8,14 +8,14 @@ const moment_1 = __importDefault(require("moment"));
|
|
|
8
8
|
class DateUtils {
|
|
9
9
|
}
|
|
10
10
|
exports.DateUtils = DateUtils;
|
|
11
|
-
DateUtils.relativeDate = (input,
|
|
11
|
+
DateUtils.relativeDate = (input, base = Date.now()) => {
|
|
12
12
|
// Create new moment instance to apply new locale
|
|
13
13
|
const datetime = moment_1.default.isMoment(input) ? (0, moment_1.default)(input.toDate()) : (0, moment_1.default)(input);
|
|
14
14
|
if (!datetime.isValid()) {
|
|
15
15
|
console.error('core-js:DateUtils.relativeDate() datetime is invalid', datetime);
|
|
16
16
|
return '<INVALID DATE>';
|
|
17
17
|
}
|
|
18
|
-
return datetime.from(
|
|
18
|
+
return datetime.from(base);
|
|
19
19
|
};
|
|
20
20
|
DateUtils.setLocale = (language) => {
|
|
21
21
|
const relativeTime = DateUtils.getRelativeTimeSpec(language);
|
|
@@ -2,11 +2,34 @@ import { ModelAttributeColumnOptions } from "sequelize";
|
|
|
2
2
|
interface UuidColumnOptions extends Omit<ModelAttributeColumnOptions, 'type'> {
|
|
3
3
|
columnName: string;
|
|
4
4
|
}
|
|
5
|
+
type GetIncludeKeyPatterns<Model> = Model extends {
|
|
6
|
+
_id: string;
|
|
7
|
+
} ? `${string}Uuid` | 'uuid' : `${string}Uuid`;
|
|
8
|
+
/**
|
|
9
|
+
* Extracts keys from a type `Model` that end with "Uuid".
|
|
10
|
+
* It also includes any keys provided in the optional type `CustomKeys`.
|
|
11
|
+
* @template Model The type to extract keys from.
|
|
12
|
+
* @template CustomKeys An optional type representing additional keys to include. Defaults to `never`.
|
|
13
|
+
*/
|
|
14
|
+
type ExtractUuidKeys<Model, CustomKeys = never> = {
|
|
15
|
+
[Property in keyof Model]: Property extends GetIncludeKeyPatterns<Model> ? Property : never;
|
|
16
|
+
}[keyof Model] | CustomKeys;
|
|
17
|
+
type ExtractAdditionalKeys<Model> = {
|
|
18
|
+
[Property in keyof Model]: Property extends GetIncludeKeyPatterns<Model> ? never : Model[Property] extends string | null ? Property : never;
|
|
19
|
+
}[keyof Model];
|
|
20
|
+
/**
|
|
21
|
+
* Extracts all UUID-related keys from a `Model`, including those within related objects.
|
|
22
|
+
* It first ensures all properties of the `Model` are required, then it flattens the UUID keys of related objects
|
|
23
|
+
* using `InjectRelationKeys` and finally extracts the keys ending with "Uuid" using `ExtractKeys`.
|
|
24
|
+
* It also includes any keys provided in the optional `AdditionalKeys` type.
|
|
25
|
+
* @template Model The main model type.
|
|
26
|
+
*/
|
|
27
|
+
export type UuidKeys<Model> = ExtractUuidKeys<Model>;
|
|
5
28
|
export declare class SequelizeUtils {
|
|
6
29
|
static decimal2Number(value: any): number | null | undefined;
|
|
7
30
|
static getPrimaryUuidColumn: () => Partial<ModelAttributeColumnOptions>;
|
|
8
|
-
static getForeignUuidColumn: ({ columnName, allowNull }: {
|
|
9
|
-
columnName: string;
|
|
31
|
+
static getForeignUuidColumn: <Model = never, AdditionalKeys extends Exclude<ExtractAdditionalKeys<Model>, { [Property in keyof Model]: Property extends GetIncludeKeyPatterns<Model> ? Property : never; }[keyof Model]> = never>({ columnName, allowNull }: {
|
|
32
|
+
columnName: Model extends undefined ? string : AdditionalKeys | { [Property in keyof Model]: Property extends GetIncludeKeyPatterns<Model> ? Property : never; }[keyof Model];
|
|
10
33
|
allowNull: boolean;
|
|
11
34
|
}) => Partial<ModelAttributeColumnOptions>;
|
|
12
35
|
static getUuidColumn: ({ columnName, ...overrideOptions }: UuidColumnOptions) => Partial<ModelAttributeColumnOptions>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a utility class for handling synchronous and asynchronous operations with a consistent try-catch pattern.
|
|
3
|
+
* It returns a tuple where the first element is the error (or null on success) and the second element is the result (or undefined on error).
|
|
4
|
+
*/
|
|
5
|
+
export declare class TryCatch {
|
|
6
|
+
/**
|
|
7
|
+
* Executes a synchronous function within a try-catch block.
|
|
8
|
+
* @template T The return type of the function.
|
|
9
|
+
* @template E The type of the error that might be thrown (defaults to Error).
|
|
10
|
+
* @param fn The synchronous function to execute.
|
|
11
|
+
*@param disableLogging console.error is default way of logging on error. You can disable it by setting it true
|
|
12
|
+
* @returns A tuple containing the result of the function and an error (null if successful).
|
|
13
|
+
*/
|
|
14
|
+
static function<T, E = Error>(fn: () => T, disableLogging?: boolean): readonly [null, T] | readonly [E, undefined];
|
|
15
|
+
/**
|
|
16
|
+
* Executes an asynchronous function (Promise) within a try-catch block.
|
|
17
|
+
* @template T The resolved value type of the Promise.
|
|
18
|
+
* @template E The type of the error that might be rejected (defaults to Error).
|
|
19
|
+
* @param fn The Promise to await.
|
|
20
|
+
* @param disableLogging console.error is default way of logging on error. You can disable it by setting it true
|
|
21
|
+
* @returns A tuple containing the resolved value of the Promise and an error (null if successful).
|
|
22
|
+
*/
|
|
23
|
+
static asyncFunction<T, E = Error>(fn: Promise<T>, disableLogging?: boolean): Promise<readonly [null, Awaited<T>] | readonly [E, undefined]>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TryCatch = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Provides a utility class for handling synchronous and asynchronous operations with a consistent try-catch pattern.
|
|
15
|
+
* It returns a tuple where the first element is the error (or null on success) and the second element is the result (or undefined on error).
|
|
16
|
+
*/
|
|
17
|
+
class TryCatch {
|
|
18
|
+
/**
|
|
19
|
+
* Executes a synchronous function within a try-catch block.
|
|
20
|
+
* @template T The return type of the function.
|
|
21
|
+
* @template E The type of the error that might be thrown (defaults to Error).
|
|
22
|
+
* @param fn The synchronous function to execute.
|
|
23
|
+
*@param disableLogging console.error is default way of logging on error. You can disable it by setting it true
|
|
24
|
+
* @returns A tuple containing the result of the function and an error (null if successful).
|
|
25
|
+
*/
|
|
26
|
+
static function(fn, disableLogging) {
|
|
27
|
+
try {
|
|
28
|
+
return [
|
|
29
|
+
null,
|
|
30
|
+
fn(),
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
if (!disableLogging) {
|
|
35
|
+
console.error("ERROR in TryCatch: ", e);
|
|
36
|
+
}
|
|
37
|
+
if (e == null) {
|
|
38
|
+
e = new Error("Unknown error");
|
|
39
|
+
}
|
|
40
|
+
return [
|
|
41
|
+
e,
|
|
42
|
+
undefined,
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Executes an asynchronous function (Promise) within a try-catch block.
|
|
48
|
+
* @template T The resolved value type of the Promise.
|
|
49
|
+
* @template E The type of the error that might be rejected (defaults to Error).
|
|
50
|
+
* @param fn The Promise to await.
|
|
51
|
+
* @param disableLogging console.error is default way of logging on error. You can disable it by setting it true
|
|
52
|
+
* @returns A tuple containing the resolved value of the Promise and an error (null if successful).
|
|
53
|
+
*/
|
|
54
|
+
static asyncFunction(fn, disableLogging) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
try {
|
|
57
|
+
const data = yield fn;
|
|
58
|
+
return [
|
|
59
|
+
null,
|
|
60
|
+
data,
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
if (!disableLogging) {
|
|
65
|
+
console.error("ERROR in TryCatch: ", e);
|
|
66
|
+
}
|
|
67
|
+
if (e == null) {
|
|
68
|
+
e = new Error("Unknown error");
|
|
69
|
+
}
|
|
70
|
+
return [
|
|
71
|
+
e,
|
|
72
|
+
undefined,
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.TryCatch = TryCatch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./TryCatch";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./TryCatch"), exports);
|