@digitaldefiance/i18n-lib 1.3.6 → 1.3.8
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/README.md +9 -0
- package/dist/handleable.d.ts +13 -0
- package/dist/handleable.js +56 -0
- package/dist/i-handleable-error-options.d.ts +6 -0
- package/dist/i-handleable-error-options.js +2 -0
- package/dist/i-handleable.d.ts +5 -0
- package/dist/i-handleable.js +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/translatable-handleable-generic.d.ts +28 -0
- package/dist/translatable-handleable-generic.js +40 -0
- package/dist/typed-handleable.d.ts +14 -0
- package/dist/typed-handleable.js +40 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2274,6 +2274,15 @@ For issues, questions, or contributions:
|
|
|
2274
2274
|
|
|
2275
2275
|
## ChangeLog
|
|
2276
2276
|
|
|
2277
|
+
### Version 1.3.8
|
|
2278
|
+
|
|
2279
|
+
- Add TypedHandleable
|
|
2280
|
+
|
|
2281
|
+
### Version 1.3.7
|
|
2282
|
+
|
|
2283
|
+
- Add handleable at i18n level
|
|
2284
|
+
- Add TranslatableGenericHandleable at i18n level
|
|
2285
|
+
|
|
2277
2286
|
### Version 1.3.6
|
|
2278
2287
|
|
|
2279
2288
|
- Simplify LanguageContextSpace and generics related to it
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { HandleableErrorOptions } from './i-handleable-error-options';
|
|
2
|
+
import { IHandleable } from './i-handleable';
|
|
3
|
+
export declare class HandleableError extends Error implements IHandleable {
|
|
4
|
+
readonly cause?: Error;
|
|
5
|
+
readonly statusCode: number;
|
|
6
|
+
readonly sourceData?: unknown;
|
|
7
|
+
private _handled;
|
|
8
|
+
constructor(source: Error, options?: HandleableErrorOptions);
|
|
9
|
+
get handled(): boolean;
|
|
10
|
+
set handled(value: boolean);
|
|
11
|
+
private serializeValue;
|
|
12
|
+
toJSON(): Record<string, unknown>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HandleableError = void 0;
|
|
4
|
+
class HandleableError extends Error {
|
|
5
|
+
constructor(source, options) {
|
|
6
|
+
super(source.message);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
this.cause = options?.cause ?? source;
|
|
9
|
+
this.statusCode = options?.statusCode ?? 500;
|
|
10
|
+
this._handled = options?.handled ?? false;
|
|
11
|
+
this.sourceData = options?.sourceData;
|
|
12
|
+
// Capture stack trace - prioritize source stack, then capture new one
|
|
13
|
+
if (source.stack) {
|
|
14
|
+
this.stack = source.stack;
|
|
15
|
+
}
|
|
16
|
+
else if (Error.captureStackTrace) {
|
|
17
|
+
Error.captureStackTrace(this, this.constructor);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
this.stack = new Error().stack;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
get handled() {
|
|
24
|
+
return this._handled;
|
|
25
|
+
}
|
|
26
|
+
set handled(value) {
|
|
27
|
+
this._handled = value;
|
|
28
|
+
}
|
|
29
|
+
serializeValue(value) {
|
|
30
|
+
if (value && typeof value === 'object' && 'toJSON' in value && typeof value.toJSON === 'function') {
|
|
31
|
+
return value.toJSON();
|
|
32
|
+
}
|
|
33
|
+
if (value instanceof Error) {
|
|
34
|
+
return value.message;
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
return value.map(item => this.serializeValue(item));
|
|
38
|
+
}
|
|
39
|
+
if (value && typeof value === 'object') {
|
|
40
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, this.serializeValue(v)]));
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
toJSON() {
|
|
45
|
+
return {
|
|
46
|
+
name: this.name,
|
|
47
|
+
message: this.message,
|
|
48
|
+
statusCode: this.statusCode,
|
|
49
|
+
handled: this.handled,
|
|
50
|
+
stack: this.stack,
|
|
51
|
+
cause: this.serializeValue(this.cause),
|
|
52
|
+
...(this.sourceData ? { sourceData: this.serializeValue(this.sourceData) } : {}),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.HandleableError = HandleableError;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,9 @@ export * from './currency-format';
|
|
|
12
12
|
export * from './default-config';
|
|
13
13
|
export * from './enum-registry';
|
|
14
14
|
export * from './global-active-context';
|
|
15
|
+
export * from './handleable';
|
|
16
|
+
export * from './i-handleable';
|
|
17
|
+
export * from './i-handleable-error-options';
|
|
15
18
|
export * from './i-global-active-context';
|
|
16
19
|
export * from './i18n-context';
|
|
17
20
|
export * from './i18n-engine';
|
|
@@ -20,6 +23,7 @@ export * from './template';
|
|
|
20
23
|
export * from './timezone';
|
|
21
24
|
export * from './translatable-generic-error';
|
|
22
25
|
export * from './typed-error';
|
|
26
|
+
export * from './typed-handleable';
|
|
23
27
|
export * from './types';
|
|
24
28
|
export * from './utils';
|
|
25
29
|
export * from './validation-config';
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,9 @@ __exportStar(require("./currency-format"), exports);
|
|
|
31
31
|
__exportStar(require("./default-config"), exports);
|
|
32
32
|
__exportStar(require("./enum-registry"), exports);
|
|
33
33
|
__exportStar(require("./global-active-context"), exports);
|
|
34
|
+
__exportStar(require("./handleable"), exports);
|
|
35
|
+
__exportStar(require("./i-handleable"), exports);
|
|
36
|
+
__exportStar(require("./i-handleable-error-options"), exports);
|
|
34
37
|
__exportStar(require("./i-global-active-context"), exports);
|
|
35
38
|
__exportStar(require("./i18n-context"), exports);
|
|
36
39
|
__exportStar(require("./i18n-engine"), exports);
|
|
@@ -39,6 +42,7 @@ __exportStar(require("./template"), exports);
|
|
|
39
42
|
__exportStar(require("./timezone"), exports);
|
|
40
43
|
__exportStar(require("./translatable-generic-error"), exports);
|
|
41
44
|
__exportStar(require("./typed-error"), exports);
|
|
45
|
+
__exportStar(require("./typed-handleable"), exports);
|
|
42
46
|
__exportStar(require("./types"), exports);
|
|
43
47
|
__exportStar(require("./utils"), exports);
|
|
44
48
|
__exportStar(require("./validation-config"), exports);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IHandleable } from './i-handleable';
|
|
2
|
+
import { TranslatableGenericError } from './translatable-generic-error';
|
|
3
|
+
/**
|
|
4
|
+
* Generic translatable error that works with any plugin engine and component
|
|
5
|
+
*/
|
|
6
|
+
export declare class TranslatableHandleableGenericError<TStringKey extends string = string, TLanguage extends string = string> extends TranslatableGenericError<TStringKey, TLanguage> implements IHandleable {
|
|
7
|
+
private _handled;
|
|
8
|
+
readonly cause?: Error;
|
|
9
|
+
readonly statusCode: number;
|
|
10
|
+
readonly sourceData?: unknown;
|
|
11
|
+
/**
|
|
12
|
+
* Create a translatable error
|
|
13
|
+
* @param componentId - The component ID to translate from
|
|
14
|
+
* @param stringKey - The translation key
|
|
15
|
+
* @param variables - Variables for interpolation
|
|
16
|
+
* @param language - Optional language override
|
|
17
|
+
* @param metadata - Additional error metadata
|
|
18
|
+
* @param instanceKey - Optional engine instance key
|
|
19
|
+
*/
|
|
20
|
+
constructor(componentId: string, stringKey: TStringKey, variables?: Record<string, string | number>, language?: TLanguage, metadata?: Record<string, any>, instanceKey?: string, handleableOptions?: {
|
|
21
|
+
statusCode?: number;
|
|
22
|
+
cause?: Error;
|
|
23
|
+
sourceData?: unknown;
|
|
24
|
+
});
|
|
25
|
+
get handled(): boolean;
|
|
26
|
+
set handled(value: boolean);
|
|
27
|
+
toJSON(): Record<string, unknown>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TranslatableHandleableGenericError = void 0;
|
|
4
|
+
const translatable_generic_error_1 = require("./translatable-generic-error");
|
|
5
|
+
/**
|
|
6
|
+
* Generic translatable error that works with any plugin engine and component
|
|
7
|
+
*/
|
|
8
|
+
class TranslatableHandleableGenericError extends translatable_generic_error_1.TranslatableGenericError {
|
|
9
|
+
/**
|
|
10
|
+
* Create a translatable error
|
|
11
|
+
* @param componentId - The component ID to translate from
|
|
12
|
+
* @param stringKey - The translation key
|
|
13
|
+
* @param variables - Variables for interpolation
|
|
14
|
+
* @param language - Optional language override
|
|
15
|
+
* @param metadata - Additional error metadata
|
|
16
|
+
* @param instanceKey - Optional engine instance key
|
|
17
|
+
*/
|
|
18
|
+
constructor(componentId, stringKey, variables, language, metadata, instanceKey, handleableOptions) {
|
|
19
|
+
super(componentId, stringKey, variables, language, metadata, instanceKey);
|
|
20
|
+
this._handled = false;
|
|
21
|
+
this.statusCode = handleableOptions?.statusCode ?? 500;
|
|
22
|
+
this.cause = handleableOptions?.cause;
|
|
23
|
+
this.sourceData = handleableOptions?.sourceData;
|
|
24
|
+
}
|
|
25
|
+
get handled() {
|
|
26
|
+
return this._handled;
|
|
27
|
+
}
|
|
28
|
+
set handled(value) {
|
|
29
|
+
this._handled = value;
|
|
30
|
+
}
|
|
31
|
+
toJSON() {
|
|
32
|
+
return {
|
|
33
|
+
statusCode: this.statusCode,
|
|
34
|
+
message: this.message,
|
|
35
|
+
cause: this.cause,
|
|
36
|
+
sourceData: this.sourceData,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.TranslatableHandleableGenericError = TranslatableHandleableGenericError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HandleableErrorOptions } from './i-handleable-error-options';
|
|
2
|
+
import { IHandleable } from './i-handleable';
|
|
3
|
+
import { HandleableError } from './handleable';
|
|
4
|
+
import { CompleteReasonMap, TranslationEngine } from './typed-error';
|
|
5
|
+
import { Language } from './default-config';
|
|
6
|
+
export declare class TypedHandleableError<TEnum extends Record<string, string>, TStringKey extends string> extends HandleableError implements IHandleable {
|
|
7
|
+
readonly type: TEnum[keyof TEnum];
|
|
8
|
+
readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
|
|
9
|
+
readonly engine: TranslationEngine<TStringKey>;
|
|
10
|
+
readonly language?: Language;
|
|
11
|
+
readonly otherVars?: Record<string, string | number>;
|
|
12
|
+
constructor(type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, engine: TranslationEngine<TStringKey>, language?: Language, otherVars?: Record<string, string | number>, options?: HandleableErrorOptions);
|
|
13
|
+
toJSON(): Record<string, unknown>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedHandleableError = void 0;
|
|
4
|
+
const handleable_1 = require("./handleable");
|
|
5
|
+
const i18n_engine_1 = require("./i18n-engine");
|
|
6
|
+
const core_string_key_1 = require("./core-string-key");
|
|
7
|
+
class TypedHandleableError extends handleable_1.HandleableError {
|
|
8
|
+
constructor(type, reasonMap, engine, language, otherVars, options) {
|
|
9
|
+
const key = reasonMap[type];
|
|
10
|
+
if (!key) {
|
|
11
|
+
const coreEngine = i18n_engine_1.I18nEngine.getInstance();
|
|
12
|
+
throw new Error(coreEngine.translate(core_string_key_1.CoreStringKey.Error_MissingTranslationKeyTemplate, {
|
|
13
|
+
stringKey: key,
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
let message = String(type);
|
|
17
|
+
try {
|
|
18
|
+
const keyString = key;
|
|
19
|
+
const translated = engine.translate(keyString, otherVars, language);
|
|
20
|
+
message = String(translated || type);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
message = String(type);
|
|
24
|
+
}
|
|
25
|
+
super(new Error(message), options);
|
|
26
|
+
this.type = type;
|
|
27
|
+
this.reasonMap = reasonMap;
|
|
28
|
+
this.language = language;
|
|
29
|
+
this.otherVars = otherVars;
|
|
30
|
+
this.engine = engine;
|
|
31
|
+
}
|
|
32
|
+
toJSON() {
|
|
33
|
+
const baseJson = super.toJSON();
|
|
34
|
+
return {
|
|
35
|
+
...baseJson,
|
|
36
|
+
type: this.type,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.TypedHandleableError = TypedHandleableError;
|