@delicious-simplicity/typekey
Version:
TypeScript library for generating cache keys with type safety
25 lines (24 loc) • 1 kB
TypeScript
import { KeyDictionaryError } from './error';
type KeyParams = Record<string, string | number | boolean | null | undefined>;
type KeyPrefixConfig = Record<string, string[]>;
type KeyPrefixParams<T extends KeyPrefixConfig> = {
[K in keyof T]: Record<T[K][number], KeyParams[T[K][number]]>;
};
type KeyDictionaryOptionsBase = {
maxKeyLength?: number;
throwOnMaxLengthViolation?: false;
};
type KeyDictionaryOptionsStrict = {
maxKeyLength: number;
throwOnMaxLengthViolation: true;
};
type KeyDictionaryOptions = KeyDictionaryOptionsBase | KeyDictionaryOptionsStrict;
declare class KeyDictionary<T extends KeyPrefixConfig> {
private prefixParamsMap;
private maxKeyLength;
private throwOnMaxLengthViolation;
constructor(config: T, options?: KeyDictionaryOptions);
private hashKeyParts;
generateKey<K extends keyof T>(prefix: K, params: KeyPrefixParams<T>[K]): string;
}
export { KeyDictionary, KeyDictionaryError, type KeyDictionaryOptions, type KeyPrefixConfig };