@digitaldefiance/i18n-lib 1.0.27 → 1.0.29
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/typed-error.d.ts +12 -4
- package/dist/typed-error.js +3 -0
- package/dist/utils.d.ts +23 -0
- package/dist/utils.js +32 -0
- package/package.json +1 -1
package/dist/typed-error.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { Language, StringKey } from './default-config';
|
|
2
2
|
import { I18nEngine } from './i18n-engine';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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<
|
|
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 };
|
package/dist/typed-error.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypedError = void 0;
|
|
4
4
|
const default_config_1 = require("./default-config");
|
|
5
|
+
/**
|
|
6
|
+
* Type-safe TypedError that ensures complete enum coverage
|
|
7
|
+
*/
|
|
5
8
|
class TypedError extends Error {
|
|
6
9
|
constructor(engine, type, reasonMap, language, otherVars) {
|
|
7
10
|
const key = reasonMap[type];
|
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
|
+
}
|