|
| 1 | +import type { ContextAttributes, Environment, FlagKey, PrecomputedFlag } from '../interfaces' |
| 2 | +import { FormatEnum } from '../interfaces' |
| 3 | + |
| 4 | +// Base interface for all configuration responses |
| 5 | +interface IBasePrecomputedConfigurationResponse { |
| 6 | + readonly format: FormatEnum.PRECOMPUTED |
| 7 | + readonly obfuscated: boolean |
| 8 | + readonly createdAt: string |
| 9 | + readonly environment?: Environment |
| 10 | + readonly subjectKey: string |
| 11 | + readonly subjectAttributes?: ContextAttributes |
| 12 | +} |
| 13 | + |
| 14 | +export interface IPrecomputedConfigurationResponse extends IBasePrecomputedConfigurationResponse { |
| 15 | + readonly obfuscated: false // Always false |
| 16 | + readonly flags: Record<FlagKey, PrecomputedFlag> |
| 17 | +} |
| 18 | + |
| 19 | +export interface IPrecomputedConfiguration { |
| 20 | + // JSON encoded configuration response (obfuscated or unobfuscated) |
| 21 | + readonly response: string |
| 22 | + readonly subjectKey: string |
| 23 | + readonly subjectAttributes?: ContextAttributes |
| 24 | +} |
| 25 | + |
| 26 | +export function createPrecomputedConfiguration( |
| 27 | + response: string, |
| 28 | + subjectKey: string, |
| 29 | + subjectAttributes?: ContextAttributes |
| 30 | +): IPrecomputedConfiguration { |
| 31 | + return { |
| 32 | + response, |
| 33 | + subjectKey, |
| 34 | + subjectAttributes, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export function createUnobfuscatedPrecomputedConfiguration( |
| 39 | + subjectKey: string, |
| 40 | + flags: Record<FlagKey, PrecomputedFlag>, |
| 41 | + subjectAttributes?: ContextAttributes, |
| 42 | + environment?: Environment |
| 43 | +): IPrecomputedConfiguration { |
| 44 | + const response = createPrecomputedConfigurationResponse(subjectKey, flags, subjectAttributes, environment) |
| 45 | + return createPrecomputedConfiguration(JSON.stringify(response), subjectKey, subjectAttributes) |
| 46 | +} |
| 47 | + |
| 48 | +export function createPrecomputedConfigurationResponse( |
| 49 | + subjectKey: string, |
| 50 | + flags: Record<FlagKey, PrecomputedFlag>, |
| 51 | + subjectAttributes?: ContextAttributes, |
| 52 | + environment?: Environment |
| 53 | +): IPrecomputedConfigurationResponse { |
| 54 | + return { |
| 55 | + format: FormatEnum.PRECOMPUTED, |
| 56 | + obfuscated: false, |
| 57 | + createdAt: new Date().toISOString(), |
| 58 | + subjectKey, |
| 59 | + subjectAttributes, |
| 60 | + environment, |
| 61 | + flags, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// "Wire" in the name means "in-transit"/"file" format. |
| 66 | +// In-memory representation may differ significantly and is up to SDKs. |
| 67 | +export interface IConfigurationWire { |
| 68 | + /** |
| 69 | + * Version field should be incremented for breaking format changes. |
| 70 | + * For example, removing required fields or changing field type/meaning. |
| 71 | + */ |
| 72 | + readonly version: number |
| 73 | + readonly precomputed?: IPrecomputedConfiguration |
| 74 | +} |
| 75 | + |
| 76 | +export function createConfigurationWireV1(precomputed?: IPrecomputedConfiguration): IConfigurationWire { |
| 77 | + return { |
| 78 | + version: 1, |
| 79 | + precomputed, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export const configurationWireV1 = { |
| 84 | + fromString(str: string): IConfigurationWire { |
| 85 | + return JSON.parse(str) as IConfigurationWire |
| 86 | + }, |
| 87 | + |
| 88 | + precomputed(precomputedConfig: IPrecomputedConfiguration): IConfigurationWire { |
| 89 | + return createConfigurationWireV1(precomputedConfig) |
| 90 | + }, |
| 91 | + |
| 92 | + empty(): IConfigurationWire { |
| 93 | + return createConfigurationWireV1() |
| 94 | + }, |
| 95 | + |
| 96 | + toString(config: IConfigurationWire): string { |
| 97 | + return JSON.stringify(config) |
| 98 | + }, |
| 99 | +} |
0 commit comments