@digitaldefiance/i18n-lib 1.0.27 → 1.0.28

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.
@@ -1,9 +1,17 @@
1
1
  import { Language, StringKey } from './default-config';
2
2
  import { I18nEngine } from './i18n-engine';
3
- export declare abstract class TypedError<T extends string | number> extends Error {
4
- readonly type: T;
5
- readonly reasonMap: Record<T, StringKey>;
3
+ /**
4
+ * Type constraint to ensure reasonMap has entries for all enum values
5
+ */
6
+ type CompleteReasonMap<TEnum extends Record<string, string | number>, TStringKey extends string> = Record<TEnum[keyof TEnum], TStringKey>;
7
+ /**
8
+ * Type-safe TypedError that ensures complete enum coverage
9
+ */
10
+ export declare abstract class TypedError<TEnum extends Record<string, string>, TStringKey extends string = StringKey> extends Error {
11
+ readonly type: TEnum[keyof TEnum];
12
+ readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
6
13
  readonly language?: Language | undefined;
7
14
  readonly otherVars?: Record<string, string | number> | undefined;
8
- constructor(engine: I18nEngine<StringKey, Language, Record<string, any>, string>, type: T, reasonMap: Record<T, StringKey>, language?: Language | undefined, otherVars?: Record<string, string | number> | undefined);
15
+ 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);
9
16
  }
17
+ export type { CompleteReasonMap };
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypedError = void 0;
4
- const default_config_1 = require("./default-config");
4
+ /**
5
+ * Type-safe TypedError that ensures complete enum coverage
6
+ */
5
7
  class TypedError extends Error {
6
8
  constructor(engine, type, reasonMap, language, otherVars) {
7
9
  const key = reasonMap[type];
8
- if (!key)
9
- throw new Error(engine.translate(default_config_1.DefaultStringKey.Error_MissingTranslationKeyTemplate, { type }, language));
10
10
  super(engine.translate(key, otherVars, language));
11
11
  this.type = type;
12
12
  this.reasonMap = reasonMap;
package/dist/utils.d.ts CHANGED
@@ -31,6 +31,16 @@ export declare function toStringKey<TStringKey extends string>(...parts: (string
31
31
  * @returns The constructed string key
32
32
  */
33
33
  export declare function toStringKeyFromEnum<TStringKey extends string>(value: string, ...parts: string[]): TStringKey;
34
+ /**
35
+ * Type that constructs string keys from enum values with prefixes and optional template suffix
36
+ */
37
+ type BuildStringKey<TEnumValue extends string, TPrefixes extends readonly string[], TIsTemplate extends boolean = false> = TPrefixes extends readonly [] ? `${TEnumValue}${TIsTemplate extends true ? 'Template' : ''}` : TPrefixes extends readonly [infer First, ...infer Rest] ? First extends string ? Rest extends readonly string[] ? `${First}_${BuildStringKey<TEnumValue, Rest, TIsTemplate>}` : never : never : never;
38
+ /**
39
+ * Type that maps all enum values to their corresponding string keys
40
+ */
41
+ type ReasonMapFromEnum<TEnum extends Record<string, string>, TStringKey extends string, TPrefixes extends readonly string[] = [], TTemplateKeys extends Set<TEnum[keyof TEnum]> = never> = {
42
+ [K in TEnum[keyof TEnum]]: BuildStringKey<K, TPrefixes, TTemplateKeys extends Set<K> ? true : false> extends TStringKey ? BuildStringKey<K, TPrefixes, TTemplateKeys extends Set<K> ? true : false> : never;
43
+ };
34
44
  /**
35
45
  * Builds a reason map from an enum object, mapping each enum value to a string key.
36
46
  * @param enumObj - The enum object
@@ -39,3 +49,16 @@ export declare function toStringKeyFromEnum<TStringKey extends string>(value: st
39
49
  * @returns The constructed reason map
40
50
  */
41
51
  export declare function buildReasonMap<TEnum extends Record<string, string>, TStringKey extends string>(enumObj: TEnum, prefixes?: string[], templateKeys?: Set<TEnum[keyof TEnum]>): Record<TEnum[keyof TEnum], TStringKey>;
52
+ /**
53
+ * Type-safe version of buildReasonMap that ensures all enum values are mapped and all string keys exist
54
+ */
55
+ export declare function buildTypeSafeReasonMap<TEnum extends Record<string, string>, TStringKey extends string, TPrefixes extends readonly string[] = [], TTemplateKeys extends Set<TEnum[keyof TEnum]> = never>(enumObj: TEnum, prefixes: TPrefixes, templateKeys?: TTemplateKeys): ReasonMapFromEnum<TEnum, TStringKey, TPrefixes, TTemplateKeys>;
56
+ /**
57
+ * Validates that a reason map has entries for all enum values
58
+ */
59
+ export declare function validateReasonMap<TEnum extends Record<string, string>, TStringKey extends string>(enumObj: TEnum, reasonMap: Partial<Record<TEnum[keyof TEnum], TStringKey>>): reasonMap is Record<TEnum[keyof TEnum], TStringKey>;
60
+ /**
61
+ * Creates a complete reason map ensuring all enum values are covered
62
+ */
63
+ export declare function createCompleteReasonMap<TEnum extends Record<string, string>, TStringKey extends string>(enumObj: TEnum, prefixes?: readonly string[], templateKeys?: Set<TEnum[keyof TEnum]>): Record<TEnum[keyof TEnum], TStringKey>;
64
+ export {};
package/dist/utils.js CHANGED
@@ -9,6 +9,9 @@ exports.isValidTimezone = isValidTimezone;
9
9
  exports.toStringKey = toStringKey;
10
10
  exports.toStringKeyFromEnum = toStringKeyFromEnum;
11
11
  exports.buildReasonMap = buildReasonMap;
12
+ exports.buildTypeSafeReasonMap = buildTypeSafeReasonMap;
13
+ exports.validateReasonMap = validateReasonMap;
14
+ exports.createCompleteReasonMap = createCompleteReasonMap;
12
15
  const moment_timezone_1 = __importDefault(require("moment-timezone"));
13
16
  /**
14
17
  * Replaces variables in a string with their corresponding values from vars or constants.
@@ -87,3 +90,32 @@ function buildReasonMap(enumObj, prefixes = [], templateKeys) {
87
90
  });
88
91
  return map;
89
92
  }
93
+ /**
94
+ * Type-safe version of buildReasonMap that ensures all enum values are mapped and all string keys exist
95
+ */
96
+ function buildTypeSafeReasonMap(enumObj, prefixes, templateKeys) {
97
+ const map = {};
98
+ Object.values(enumObj).forEach(value => {
99
+ const baseKey = [...prefixes, value].join('_');
100
+ const finalKey = templateKeys?.has(value) ? baseKey + 'Template' : baseKey;
101
+ map[value] = finalKey;
102
+ });
103
+ return map;
104
+ }
105
+ /**
106
+ * Validates that a reason map has entries for all enum values
107
+ */
108
+ function validateReasonMap(enumObj, reasonMap) {
109
+ return Object.values(enumObj).every(value => value in reasonMap && reasonMap[value] !== undefined);
110
+ }
111
+ /**
112
+ * Creates a complete reason map ensuring all enum values are covered
113
+ */
114
+ function createCompleteReasonMap(enumObj, prefixes = [], templateKeys) {
115
+ const map = buildReasonMap(enumObj, [...prefixes], templateKeys);
116
+ if (!validateReasonMap(enumObj, map)) {
117
+ const missing = Object.values(enumObj).filter(value => !(value in map));
118
+ throw new Error(`Missing reason map entries for: ${missing.join(', ')}`);
119
+ }
120
+ return map;
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitaldefiance/i18n-lib",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "description": "Generic i18n library with enum translation support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",