1- import ConfigKey from "./configKey " ;
2- import ConfigValue from "./configValue " ;
1+ import { ReforgeLogLevel } from "./logger " ;
2+ import { TypedFrontEndConfigurationRaw , ConfigEvaluationMetadata } from "./types " ;
33
4- export type RawConfigWithoutTypes = { [ key : string ] : any } ;
5-
6- export type ConfigEvaluationMetadata = {
7- configRowIndex : number ;
8- conditionalValueIndex : number ;
9- type : string ;
10- configId : string ;
11- } ;
4+ export type RawConfigWithoutTypes = Record < string , any > ;
125
136type APIKeyMetadata = {
147 id : string | number ;
158} ;
169
10+ // TODO: Why is this definition different from the one in ./types.ts?
1711type Duration = {
1812 definition : string ;
1913 millis : number ;
2014} ;
2115
22- type Value = {
23- [ key : string ] : number | string | string [ ] | boolean | Duration ;
24- } ;
16+ export interface IntRange {
17+ /** if empty treat as Number.MIN_VALUE. Inclusive */
18+ start ?: bigint | undefined ;
19+ /** if empty treat as Number.MAX_VALUE. Exclusive */
20+ end ?: bigint | undefined ;
21+ }
22+
23+ export enum ProvidedSource {
24+ EnvVar = "ENV_VAR" ,
25+ }
26+ export interface Provided {
27+ source ?: ProvidedSource | undefined ;
28+ /** eg MY_ENV_VAR */
29+ lookup ?: string | undefined ;
30+ }
31+
32+ export enum SchemaType {
33+ UNKNOWN = 0 ,
34+ ZOD = 1 ,
35+ JSON_SCHEMA = 2 ,
36+ }
37+
38+ export interface Schema {
39+ schema : string ;
40+ schemaType : SchemaType ;
41+ }
42+
43+ export interface WeightedValue {
44+ /** out of 1000 */
45+ weight : number ;
46+ // eslint-disable-next-line no-use-before-define
47+ value : ConfigValue | undefined ;
48+ }
49+
50+ export enum LimitResponse_LimitPolicyNames {
51+ SecondlyRolling = 1 ,
52+ MinutelyRolling = 3 ,
53+ HourlyRolling = 5 ,
54+ DailyRolling = 7 ,
55+ MonthlyRolling = 8 ,
56+ Infinite = 9 ,
57+ YearlyRolling = 10 ,
58+ }
59+
60+ export enum LimitDefinition_SafetyLevel {
61+ L4_BEST_EFFORT = 4 ,
62+ L5_BOMBPROOF = 5 ,
63+ }
64+
65+ export interface LimitDefinition {
66+ policyName : LimitResponse_LimitPolicyNames ;
67+ limit : number ;
68+ burst : number ;
69+ accountId : number ;
70+ lastModified : number ;
71+ returnable : boolean ;
72+ /** [default = L4_BEST_EFFORT]; // Overridable by request */
73+ safetyLevel : LimitDefinition_SafetyLevel ;
74+ }
75+ export interface WeightedValues {
76+ weightedValues : WeightedValue [ ] ;
77+ hashByPropertyName ?: string | undefined ;
78+ }
79+
80+ export type ConfigValue =
81+ | {
82+ int : number | undefined ;
83+ }
84+ | {
85+ string : string | undefined ;
86+ }
87+ | {
88+ bytes : Buffer | undefined ;
89+ }
90+ | {
91+ double : number | undefined ;
92+ }
93+ | {
94+ bool : boolean | undefined ;
95+ }
96+ | {
97+ weightedValues ?: WeightedValues | undefined ;
98+ }
99+ | {
100+ limitDefinition ?: LimitDefinition | undefined ;
101+ }
102+ | {
103+ logLevel : ReforgeLogLevel | undefined ;
104+ }
105+ | {
106+ stringList : string [ ] | undefined ;
107+ }
108+ | {
109+ intRange : IntRange | undefined ;
110+ }
111+ | {
112+ provided : Provided | undefined ;
113+ }
114+ | {
115+ duration : Duration | undefined ;
116+ }
117+ | {
118+ json : string | undefined ;
119+ }
120+ | {
121+ schema : Schema | undefined ;
122+ }
123+ | {
124+ /** don't log or telemetry this value */
125+ confidential : boolean | undefined ;
126+ }
127+ | {
128+ /** key name to decrypt with */
129+ decryptWith : string | undefined ;
130+ } ;
25131
26132type Evaluation = {
27- value : Value ;
133+ value : ConfigValue ;
28134 configEvaluationMetadata : {
29135 configRowIndex : string | number ;
30136 conditionalValueIndex : string | number ;
@@ -47,7 +153,11 @@ const parseRawMetadata = (metadata: any) => ({
47153 configId : metadata . id ,
48154} ) ;
49155
50- const valueFor = ( value : Value , type : string , key : string ) : ConfigValue => {
156+ const valueFor = < K extends keyof TypedFrontEndConfigurationRaw > (
157+ value : ConfigValue ,
158+ type : keyof ConfigValue ,
159+ key : K
160+ ) : TypedFrontEndConfigurationRaw [ K ] => {
51161 const rawValue = value [ type ] ;
52162
53163 switch ( type ) {
@@ -77,7 +187,7 @@ export const parseEvaluationPayload = (payload: EvaluationPayload) => {
77187 Object . keys ( payload . evaluations ) . forEach ( ( key ) => {
78188 const evaluation = payload . evaluations [ key ] ;
79189
80- const type = Object . keys ( evaluation . value ) [ 0 ] ;
190+ const type = Object . keys ( evaluation . value ) [ 0 ] as keyof ConfigValue ;
81191
82192 // eslint-disable-next-line no-use-before-define
83193 configs [ key ] = new Config (
@@ -98,30 +208,32 @@ const parseRawConfigWithoutTypes = (payload: RawConfigWithoutTypes) => {
98208 // eslint-disable-next-line no-use-before-define
99209 const configs = { } as { [ key : string ] : Config } ;
100210 Object . keys ( payload ) . forEach ( ( key ) => {
101- const type = typeof payload [ key ] ;
211+ const type = typeof payload [ key ] as keyof ConfigValue ;
102212 // eslint-disable-next-line no-use-before-define
103213 configs [ key ] = new Config ( key , valueFor ( { [ type ] : payload [ key ] } , type , key ) , type ) ;
104214 } ) ;
105215
106216 return configs ;
107217} ;
108218
109- export class Config {
110- key : ConfigKey ;
219+ export class Config <
220+ K extends keyof TypedFrontEndConfigurationRaw = keyof TypedFrontEndConfigurationRaw ,
221+ > {
222+ key : K ;
111223
112- value : ConfigValue ;
224+ value : TypedFrontEndConfigurationRaw [ K ] ;
113225
114- rawValue : Value | undefined ;
226+ rawValue : ConfigValue | undefined ;
115227
116- type : string ;
228+ type : keyof ConfigValue ;
117229
118230 configEvaluationMetadata : ConfigEvaluationMetadata | undefined ;
119231
120232 constructor (
121- key : ConfigKey ,
122- value : ConfigValue ,
123- type : string ,
124- rawValue ?: Value ,
233+ key : K ,
234+ value : TypedFrontEndConfigurationRaw [ K ] ,
235+ type : keyof ConfigValue ,
236+ rawValue ?: ConfigValue ,
125237 metadata ?: ConfigEvaluationMetadata
126238 ) {
127239 this . key = key ;
0 commit comments