@@ -120,7 +120,7 @@ export async function typesHandler(
120120
121121 const configBindingsWithSecrets = {
122122 kv_namespaces : config . kv_namespaces ?? [ ] ,
123- vars : getVarsInfo ( args ) ,
123+ vars : collectAllVars ( args ) ,
124124 wasm_modules : config . wasm_modules ,
125125 text_blobs : {
126126 ...config . text_blobs ,
@@ -152,8 +152,7 @@ export async function typesHandler(
152152 configBindingsWithSecrets ,
153153 config ,
154154 envInterface ,
155- outputPath ,
156- args . strictVars
155+ outputPath
157156 ) ;
158157}
159158
@@ -207,16 +206,15 @@ export function generateImportSpecifier(from: string, to: string) {
207206
208207type Secrets = Record < string , string > ;
209208
210- type ConfigToDTS = Partial < Omit < Config , "vars" > > & { vars : VarsInfo } & {
209+ type ConfigToDTS = Partial < Omit < Config , "vars" > > & { vars : VarTypes } & {
211210 secrets : Secrets ;
212211} ;
213212
214213async function generateTypes (
215214 configToDTS : ConfigToDTS ,
216215 config : Config ,
217216 envInterface : string ,
218- outputPath : string ,
219- strictVars : boolean
217+ outputPath : string
220218) {
221219 const configContainsEntrypoint =
222220 config . main !== undefined || ! ! config . site ?. [ "entry-point" ] ;
@@ -265,41 +263,10 @@ async function generateTypes(
265263 const vars = Object . entries ( configToDTS . vars ) . filter (
266264 ( [ key ] ) => ! ( key in configToDTS . secrets )
267265 ) ;
268- for ( const [ varName , varInfo ] of vars ) {
269- const varValueTypes = new Set (
270- varInfo
271- . map ( ( { value : varValue } ) => {
272- if ( ! strictVars ) {
273- if ( Array . isArray ( varValue ) ) {
274- const typesInArray = [
275- ...new Set ( varValue . map ( ( item ) => typeof item ) ) ,
276- ] . sort ( ) ;
277- if ( typesInArray . length === 1 ) {
278- return `${ typesInArray [ 0 ] } []` ;
279- }
280- return `(${ typesInArray . join ( "|" ) } )[]` ;
281- }
282- return typeof varValue ;
283- }
284- if ( typeof varValue === "number" || typeof varValue === "boolean" ) {
285- return `${ varValue } ` ;
286- }
287- if ( typeof varValue === "string" || typeof varValue === "object" ) {
288- return JSON . stringify ( varValue ) ;
289- }
290- return "unknown" ;
291- } )
292- . filter ( Boolean )
293- ) as Set < string > ;
294-
295- const typeKey = constructTypeKey ( varName ) ;
296- const constructedValues = [ ...varValueTypes ] ;
297-
266+ for ( const [ varName , varValues ] of vars ) {
298267 envTypeStructure . push ( [
299- typeKey ,
300- constructedValues . length === 1
301- ? constructedValues [ 0 ]
302- : constructedValues . join ( " | " ) ,
268+ constructTypeKey ( varName ) ,
269+ varValues . length === 1 ? varValues [ 0 ] : varValues . join ( " | " ) ,
303270 ] ) ;
304271 }
305272 }
@@ -608,29 +575,74 @@ type TSConfig = {
608575 } ;
609576} ;
610577
611- type VarValue = Config [ "vars" ] [ string ] ;
612-
613- type VarInfoValue = { value : VarValue ; env ?: string } ;
614-
615- type VarsInfo = Record < string , VarInfoValue [ ] > ;
578+ type VarTypes = Record < string , string [ ] > ;
616579
617- function getVarsInfo (
580+ /**
581+ * Collects all the vars types across all the environments defined in the config file
582+ *
583+ * @param args all the CLI arguments passed to the `types` command
584+ * @returns an object which keys are the variable names and values are arrays containing all the computed types for such variables
585+ */
586+ function collectAllVars (
618587 args : StrictYargsOptionsToInterface < typeof typesOptions >
619- ) : VarsInfo {
620- const varsInfo : VarsInfo = { } ;
621- const { rawConfig } = experimental_readRawConfig ( args ) ;
588+ ) : Record < string , string [ ] > {
589+ const varsInfo : Record < string , Set < string > > = { } ;
622590
623- function collectVars ( vars : RawEnvironment [ "vars" ] , envName ?: string ) {
591+ // Collects onto the `varsInfo` object the vars and values for a specific environment
592+ function collectEnvironmentVars ( vars : RawEnvironment [ "vars" ] ) {
624593 Object . entries ( vars ?? { } ) . forEach ( ( [ key , value ] ) => {
625- varsInfo [ key ] ??= [ ] ;
626- varsInfo [ key ] . push ( { value, env : envName } ) ;
594+ varsInfo [ key ] ??= new Set ( ) ;
595+
596+ if ( ! args . strictVars ) {
597+ // when strict-vars is false we basically only want the plain "typeof" values
598+ varsInfo [ key ] . add (
599+ Array . isArray ( value ) ? typeofArray ( value ) : typeof value
600+ ) ;
601+ return ;
602+ }
603+
604+ if ( typeof value === "number" || typeof value === "boolean" ) {
605+ varsInfo [ key ] . add ( `${ value } ` ) ;
606+ return ;
607+ }
608+ if ( typeof value === "string" || typeof value === "object" ) {
609+ varsInfo [ key ] . add ( JSON . stringify ( value ) ) ;
610+ return ;
611+ }
612+
613+ // let's fallback to a safe `unknown` if we couldn't detect the type
614+ varsInfo [ key ] . add ( "unknown" ) ;
627615 } ) ;
628616 }
629617
630- collectVars ( rawConfig . vars ) ;
631- Object . entries ( rawConfig . env ?? { } ) . forEach ( ( [ envName , env ] ) => {
632- collectVars ( env . vars , envName ) ;
618+ const { rawConfig } = experimental_readRawConfig ( args ) ;
619+ collectEnvironmentVars ( rawConfig . vars ) ;
620+ Object . entries ( rawConfig . env ?? { } ) . forEach ( ( [ _envName , env ] ) => {
621+ collectEnvironmentVars ( env . vars ) ;
633622 } ) ;
634623
635- return varsInfo ;
624+ return Object . fromEntries (
625+ Object . entries ( varsInfo ) . map ( ( [ key , value ] ) => [ key , [ ...value ] ] )
626+ ) ;
627+ }
628+
629+ /**
630+ * Given an array it returns a string representing the types present in such array
631+ *
632+ * e.g.
633+ * `[1, 2, 3]` returns `number[]`,
634+ * `[1, 2, 'three']` returns `(number|string)[]`,
635+ * `['false', true]` returns `(string|boolean)[]`,
636+ *
637+ * @param array the target array
638+ * @returns the string representing the types of such array
639+ */
640+ function typeofArray ( array : unknown [ ] ) : string {
641+ const typesInArray = [ ...new Set ( array . map ( ( item ) => typeof item ) ) ] . sort ( ) ;
642+
643+ if ( typesInArray . length === 1 ) {
644+ return `${ typesInArray [ 0 ] } []` ;
645+ }
646+
647+ return `(${ typesInArray . join ( "|" ) } )[]` ;
636648}
0 commit comments