@digitaldefiance/i18n-lib 1.3.5 → 1.3.7

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 CHANGED
@@ -2274,6 +2274,16 @@ For issues, questions, or contributions:
2274
2274
 
2275
2275
  ## ChangeLog
2276
2276
 
2277
+ ### Version 1.3.7
2278
+
2279
+ - Add handleable at i18n level
2280
+ - Add TranslatableGenericHandleable at i18n level
2281
+
2282
+ ### Version 1.3.6
2283
+
2284
+ - Simplify LanguageContextSpace and generics related to it
2285
+ - Make plugin engine respect admin context
2286
+
2277
2287
  ### Version 1.3.5
2278
2288
 
2279
2289
  - CommonJS
package/dist/context.d.ts CHANGED
@@ -11,34 +11,34 @@ import { LanguageContextSpace } from './types';
11
11
  * @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
12
12
  * @returns A new I18nContext instance
13
13
  */
14
- export declare function createContext<TLanguage extends string, TTranslationContext extends string = LanguageContextSpace, TContext extends I18nContext<TLanguage, TTranslationContext> = I18nContext<TLanguage, TTranslationContext>>(defaultLanguage: TLanguage, defaultContext: TTranslationContext, defaultCurrencyCode?: CurrencyCode, defaultTimezone?: Timezone, defaultAdminTimezone?: Timezone): TContext;
14
+ export declare function createContext<TLanguage extends string>(defaultLanguage: TLanguage, defaultContext?: LanguageContextSpace, defaultCurrencyCode?: CurrencyCode, defaultTimezone?: Timezone, defaultAdminTimezone?: Timezone): I18nContext<TLanguage>;
15
15
  /**
16
16
  * Sets the language for the given I18n context.
17
17
  * @param context - The I18n context to modify
18
18
  * @param language - The language to set
19
19
  */
20
- export declare function setLanguage<TLanguage extends string, TContext extends string = LanguageContextSpace>(context: I18nContext<TLanguage, TContext>, language: TLanguage): void;
20
+ export declare function setLanguage<TLanguage extends string>(context: I18nContext<TLanguage>, language: TLanguage): void;
21
21
  /**
22
22
  * Sets the admin language for the given I18n context.
23
23
  * @param context - The I18n context to modify
24
24
  * @param language - The admin language to set
25
25
  */
26
- export declare function setAdminLanguage<TLanguage extends string, TContext extends string = LanguageContextSpace>(context: I18nContext<TLanguage, TContext>, language: TLanguage): void;
26
+ export declare function setAdminLanguage<TLanguage extends string>(context: I18nContext<TLanguage>, language: TLanguage): void;
27
27
  /**
28
28
  * Sets the current context for the given I18n context.
29
29
  * @param context - The I18n context to modify
30
30
  * @param languageContext - The language context to set
31
31
  */
32
- export declare function setContext<TLanguage extends string, TContext extends string = LanguageContextSpace>(context: I18nContext<TLanguage, TContext>, languageContext: TContext): void;
32
+ export declare function setContext<TLanguage extends string>(context: I18nContext<TLanguage>, languageContext: LanguageContextSpace): void;
33
33
  /**
34
34
  * Sets the timezone for the given I18n context.
35
35
  * @param context - The I18n context to modify
36
36
  * @param timezone - The timezone to set
37
37
  */
38
- export declare function setTimezone<TLanguage extends string, TContext extends string = LanguageContextSpace>(context: I18nContext<TLanguage, TContext>, timezone: Timezone): void;
38
+ export declare function setTimezone<TLanguage extends string>(context: I18nContext<TLanguage>, timezone: Timezone): void;
39
39
  /**
40
40
  * Sets the admin timezone for the given I18n context.
41
41
  * @param context - The I18n context to modify
42
42
  * @param timezone - The admin timezone to set
43
43
  */
44
- export declare function setAdminTimezone<TLanguage extends string, TContext extends string = LanguageContextSpace>(context: I18nContext<TLanguage, TContext>, timezone: Timezone): void;
44
+ export declare function setAdminTimezone<TLanguage extends string>(context: I18nContext<TLanguage>, timezone: Timezone): void;
package/dist/context.js CHANGED
@@ -17,7 +17,7 @@ const timezone_1 = require("./timezone");
17
17
  * @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
18
18
  * @returns A new I18nContext instance
19
19
  */
20
- function createContext(defaultLanguage, defaultContext, defaultCurrencyCode = new currency_code_1.CurrencyCode('USD'), defaultTimezone = new timezone_1.Timezone('UTC'), defaultAdminTimezone = new timezone_1.Timezone('UTC')) {
20
+ function createContext(defaultLanguage, defaultContext = 'admin', defaultCurrencyCode = new currency_code_1.CurrencyCode('USD'), defaultTimezone = new timezone_1.Timezone('UTC'), defaultAdminTimezone = new timezone_1.Timezone('UTC')) {
21
21
  return {
22
22
  language: defaultLanguage,
23
23
  adminLanguage: defaultLanguage,
@@ -20,7 +20,7 @@ declare global {
20
20
  StringKey: DefaultStringKey;
21
21
  Language: DefaultLanguageCode;
22
22
  LanguageCodes: LanguageCodeCollection<DefaultLanguageCode>;
23
- engine: I18nEngine<I18n.Config['StringKey'], I18n.Config['Language'], Record<any, any>, string>;
23
+ engine: I18nEngine<I18n.Config['StringKey'], I18n.Config['Language'], Record<any, any>>;
24
24
  }
25
25
  }
26
26
  }
@@ -29,4 +29,4 @@ export type Language = I18n.Config['Language'];
29
29
  export type Engine = I18n.Config['engine'];
30
30
  export type DefaultLanguageCodesType = I18n.Config['LanguageCodes'];
31
31
  export declare const getI18nEngine: () => Engine;
32
- export declare const getDefaultI18nEngine: <TConstants extends Record<string, any>, TTranslationContext extends string, TContext extends I18nContext<DefaultLanguageCode, TTranslationContext>>(constants: TConstants, timezone?: Timezone, adminTimezone?: Timezone) => I18nEngine<DefaultStringKey, DefaultLanguageCode, TConstants, TTranslationContext, TContext>;
32
+ export declare const getDefaultI18nEngine: <TConstants extends Record<string, any>, TContext extends I18nContext<DefaultLanguageCode>>(constants: TConstants, timezone?: Timezone, adminTimezone?: Timezone) => I18nEngine<DefaultStringKey, DefaultLanguageCode, TConstants, TContext>;
@@ -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;
@@ -0,0 +1,6 @@
1
+ export interface HandleableErrorOptions {
2
+ cause?: Error;
3
+ handled?: boolean;
4
+ statusCode?: number;
5
+ sourceData?: unknown;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface IHandleable {
2
+ toJSON(): Record<string, unknown>;
3
+ get handled(): boolean;
4
+ set handled(value: boolean);
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -4,11 +4,11 @@ import { LanguageCodeCollection, LanguageContextSpace, MasterStringsCollection }
4
4
  /**
5
5
  * I18n configuration interface
6
6
  */
7
- export interface I18nConfig<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>, TTranslationContext extends string = LanguageContextSpace> {
7
+ export interface I18nConfig<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>> {
8
8
  stringNames: TStringKey[];
9
9
  strings: MasterStringsCollection<TStringKey, TLanguage>;
10
10
  defaultLanguage: TLanguage;
11
- defaultTranslationContext: TTranslationContext;
11
+ defaultTranslationContext: LanguageContextSpace;
12
12
  defaultCurrencyCode: CurrencyCode;
13
13
  languageCodes: LanguageCodeCollection<TLanguage>;
14
14
  languages: TLanguage[];
@@ -4,11 +4,11 @@ import { LanguageContextSpace } from './types';
4
4
  /**
5
5
  * I18n context interface
6
6
  */
7
- export interface I18nContext<TLanguage extends string, TTranslationContext extends string = LanguageContextSpace> {
7
+ export interface I18nContext<TLanguage extends string> {
8
8
  language: TLanguage;
9
9
  adminLanguage: TLanguage;
10
10
  currencyCode: CurrencyCode;
11
- currentContext: TTranslationContext;
11
+ currentContext: LanguageContextSpace;
12
12
  timezone: Timezone;
13
13
  adminTimezone: Timezone;
14
14
  }
@@ -5,7 +5,7 @@ import { EnumLanguageTranslation } from './types';
5
5
  /**
6
6
  * Internationalization engine class
7
7
  */
8
- export declare class I18nEngine<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>, TTranslationContext extends string = string, TContext extends I18nContext<TLanguage, TTranslationContext> = I18nContext<TLanguage, TTranslationContext>> {
8
+ export declare class I18nEngine<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>, TContext extends I18nContext<TLanguage> = I18nContext<TLanguage>> {
9
9
  /**
10
10
  * Registry for enum translations
11
11
  */
@@ -13,7 +13,7 @@ export declare class I18nEngine<TStringKey extends string, TLanguage extends str
13
13
  /**
14
14
  * Configuration for the i18n engine
15
15
  */
16
- readonly config: I18nConfig<TStringKey, TLanguage, TConstants, TTranslationContext>;
16
+ readonly config: I18nConfig<TStringKey, TLanguage, TConstants>;
17
17
  /**
18
18
  * Static instances for semi-singleton pattern
19
19
  */
@@ -40,7 +40,7 @@ export declare class I18nEngine<TStringKey extends string, TLanguage extends str
40
40
  * @param key Optional instance key for the semi-singleton pattern
41
41
  * @throws Error if an instance with the same key already exists
42
42
  */
43
- constructor(config: I18nConfig<TStringKey, TLanguage, TConstants, TTranslationContext>, key?: string, newContext?: () => TContext);
43
+ constructor(config: I18nConfig<TStringKey, TLanguage, TConstants>, key?: string, newContext?: () => TContext);
44
44
  /**
45
45
  * Gets an instance of the I18nEngine by key. If no key is provided, the default instance is returned.
46
46
  * @param key The key of the instance to retrieve
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';
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);
@@ -83,6 +83,7 @@ export declare class PluginI18nEngine<TLanguages extends string> {
83
83
  updateContext(updates: Partial<IActiveContext<TLanguages>>): void;
84
84
  /**
85
85
  * Get current language from global context
86
+ * Respects language context space (admin vs user)
86
87
  */
87
88
  private getCurrentLanguage;
88
89
  /**
@@ -220,10 +220,12 @@ class PluginI18nEngine {
220
220
  }
221
221
  /**
222
222
  * Get current language from global context
223
+ * Respects language context space (admin vs user)
223
224
  */
224
225
  getCurrentLanguage() {
225
226
  const globalContext = global_active_context_1.GlobalActiveContext.getInstance();
226
- return globalContext.getContext(this.contextKey).language;
227
+ const context = globalContext.getContext(this.contextKey);
228
+ return context.currentContext === 'admin' ? context.adminLanguage : context.language;
227
229
  }
228
230
  /**
229
231
  * Set current language
@@ -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;
@@ -32,7 +32,7 @@ export declare abstract class TypedError<TEnum extends Record<string, string>, T
32
32
  readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
33
33
  readonly language?: Language | undefined;
34
34
  readonly otherVars?: Record<string, string | number> | undefined;
35
- constructor(engine: I18nEngine<TStringKey, Language, Record<string, any>, string>, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, language?: Language | undefined, otherVars?: Record<string, string | number> | undefined);
35
+ constructor(engine: I18nEngine<TStringKey, Language, Record<string, any>>, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, language?: Language | undefined, otherVars?: Record<string, string | number> | undefined);
36
36
  }
37
37
  /**
38
38
  * Plugin-based TypedError that works with the new component registration system
package/dist/types.d.ts CHANGED
@@ -4,7 +4,7 @@ import { LanguageDefinition } from './language-definition';
4
4
  /**
5
5
  * Standard language context spaces
6
6
  */
7
- export type LanguageContextSpace = 'admin' | 'user' | 'system' | 'api';
7
+ export type LanguageContextSpace = 'admin' | 'user';
8
8
  /**
9
9
  * Default currency code
10
10
  */
@@ -17,10 +17,6 @@ export declare const DefaultTimezone: string;
17
17
  * Currency position type
18
18
  */
19
19
  export type CurrencyPosition = 'prefix' | 'postfix' | 'infix';
20
- /**
21
- * Custom language context type
22
- */
23
- export type CustomLanguageContext<T extends string = LanguageContextSpace> = T;
24
20
  /**
25
21
  * Collection of localized strings for a specific language
26
22
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitaldefiance/i18n-lib",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "description": "Generic i18n library with enum translation support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",