@@ -2,7 +2,6 @@ import * as fs from "fs";
2
2
import * as TOML from "@iarna/toml" ;
3
3
import {
4
4
constructTSModuleGlob ,
5
- constructType ,
6
5
constructTypeKey ,
7
6
generateImportSpecifier ,
8
7
isValidIdentifier ,
@@ -65,43 +64,6 @@ describe("constructTSModuleGlob() should return a valid TS glob ", () => {
65
64
} ) ;
66
65
} ) ;
67
66
68
- describe ( "constructType" , ( ) => {
69
- it ( "should return a valid type" , ( ) => {
70
- expect ( constructType ( "valid" , "string" ) ) . toBe ( "valid: string;" ) ;
71
- expect ( constructType ( "valid123" , "string" ) ) . toBe ( "valid123: string;" ) ;
72
- expect ( constructType ( "valid_123" , "string" ) ) . toBe ( "valid_123: string;" ) ;
73
- expect ( constructType ( "valid_123_" , "string" ) ) . toBe ( "valid_123_: string;" ) ;
74
- expect ( constructType ( "_valid_123_" , "string" ) ) . toBe ( "_valid_123_: string;" ) ;
75
- expect ( constructType ( "_valid_123_" , "string" ) ) . toBe ( "_valid_123_: string;" ) ;
76
-
77
- expect ( constructType ( "123invalid" , "string" ) ) . toBe ( '"123invalid": string;' ) ;
78
- expect ( constructType ( "invalid-123" , "string" ) ) . toBe (
79
- '"invalid-123": string;'
80
- ) ;
81
- expect ( constructType ( "invalid 123" , "string" ) ) . toBe (
82
- '"invalid 123": string;'
83
- ) ;
84
-
85
- expect ( constructType ( "valid" , 'a"' , false ) ) . toBe ( 'valid: "a\\"";' ) ;
86
- expect ( constructType ( "valid" , "a\\" , false ) ) . toBe ( 'valid: "a\\\\";' ) ;
87
- expect ( constructType ( "valid" , "a\\b" , false ) ) . toBe ( 'valid: "a\\\\b";' ) ;
88
- expect ( constructType ( "valid" , 'a\\b"' , false ) ) . toBe ( 'valid: "a\\\\b\\"";' ) ;
89
-
90
- expect ( constructType ( "valid" , 1 ) ) . toBe ( "valid: 1;" ) ;
91
- expect ( constructType ( "valid" , 12345 ) ) . toBe ( "valid: 12345;" ) ;
92
- expect ( constructType ( "valid" , true ) ) . toBe ( "valid: true;" ) ;
93
- expect ( constructType ( "valid" , false ) ) . toBe ( "valid: false;" ) ;
94
- } ) ;
95
- } ) ;
96
-
97
- describe ( "constructType with multiline strings" , ( ) => {
98
- it ( "should correctly escape newlines in string values" , ( ) => {
99
- const multilineString = "This is a\nmulti-line\nstring" ;
100
- const expected = `valid: "This is a\\nmulti-line\\nstring";` ;
101
- expect ( constructType ( "valid" , multilineString , false ) ) . toBe ( expected ) ;
102
- } ) ;
103
- } ) ;
104
-
105
67
describe ( "generateImportSpecifier" , ( ) => {
106
68
it ( "should generate a relative import specifier" , ( ) => {
107
69
expect ( generateImportSpecifier ( "/app/types.ts" , "/app/index.ts" ) ) . toBe (
@@ -602,6 +564,35 @@ describe("generateTypes()", () => {
602
564
` ) ;
603
565
} ) ;
604
566
567
+ it ( "should allow opting out of strict-vars" , async ( ) => {
568
+ fs . writeFileSync (
569
+ "./wrangler.toml" ,
570
+ TOML . stringify ( {
571
+ vars : {
572
+ varStr : "A from wrangler toml" ,
573
+ varArrNum : [ 1 , 2 , 3 ] ,
574
+ varArrMix : [ 1 , "two" , 3 , true ] ,
575
+ varObj : { test : true } ,
576
+ } ,
577
+ } as TOML . JsonMap ) ,
578
+ "utf-8"
579
+ ) ;
580
+
581
+ await runWrangler ( "types --strict-vars=false" ) ;
582
+
583
+ expect ( std . out ) . toMatchInlineSnapshot ( `
584
+ "Generating project types...
585
+
586
+ interface Env {
587
+ varStr: string;
588
+ varArrNum: number[];
589
+ varArrMix: (boolean|number|string)[];
590
+ varObj: object;
591
+ }
592
+ "
593
+ ` ) ;
594
+ } ) ;
595
+
605
596
it ( "should override vars with secrets" , async ( ) => {
606
597
fs . writeFileSync (
607
598
"./wrangler.toml" ,
@@ -634,6 +625,110 @@ describe("generateTypes()", () => {
634
625
` ) ;
635
626
} ) ;
636
627
628
+ it ( "various different types of vars" , async ( ) => {
629
+ fs . writeFileSync (
630
+ "./wrangler.toml" ,
631
+ TOML . stringify ( {
632
+ vars : {
633
+ "var-a" : '"a\\""' ,
634
+ "var-a-1" : '"a\\\\"' ,
635
+ "var-a-b" : '"a\\\\b"' ,
636
+ "var-a-b-" : '"a\\\\b\\""' ,
637
+ 1 : 1 ,
638
+ 12345 : 12345 ,
639
+ true : true ,
640
+ false : false ,
641
+ "multi\nline\nvar" : "this\nis\na\nmulti\nline\nvariable!" ,
642
+ } ,
643
+ } as TOML . JsonMap ) ,
644
+ "utf-8"
645
+ ) ;
646
+ await runWrangler ( "types" ) ;
647
+
648
+ expect ( std . out ) . toMatchInlineSnapshot ( `
649
+ "Generating project types...
650
+
651
+ interface Env {
652
+ \\"1\\": 1;
653
+ \\"12345\\": 12345;
654
+ \\"var-a\\": \\"/\\"a///\\"/\\"\\";
655
+ \\"var-a-1\\": \\"/\\"a/////\\"\\";
656
+ \\"var-a-b\\": \\"/\\"a////b/\\"\\";
657
+ \\"var-a-b-\\": \\"/\\"a////b///\\"/\\"\\";
658
+ true: true;
659
+ false: false;
660
+ \\"multi
661
+ line
662
+ var\\": \\"this/nis/na/nmulti/nline/nvariable!\\";
663
+ }
664
+ "
665
+ ` ) ;
666
+ } ) ;
667
+
668
+ describe ( "vars present in multiple environments" , ( ) => {
669
+ beforeEach ( ( ) => {
670
+ fs . writeFileSync (
671
+ "./wrangler.toml" ,
672
+ TOML . stringify ( {
673
+ vars : {
674
+ MY_VAR : "a var" ,
675
+ MY_VAR_A : "A (dev)" ,
676
+ MY_VAR_B : { value : "B (dev)" } ,
677
+ MY_VAR_C : [ "a" , "b" , "c" ] ,
678
+ } ,
679
+ env : {
680
+ production : {
681
+ vars : {
682
+ MY_VAR : "a var" ,
683
+ MY_VAR_A : "A (prod)" ,
684
+ MY_VAR_B : { value : "B (prod)" } ,
685
+ MY_VAR_C : [ 1 , 2 , 3 ] ,
686
+ } ,
687
+ } ,
688
+ staging : {
689
+ vars : {
690
+ MY_VAR_A : "A (stag)" ,
691
+ } ,
692
+ } ,
693
+ } ,
694
+ } as TOML . JsonMap ) ,
695
+ "utf-8"
696
+ ) ;
697
+ } ) ;
698
+
699
+ it ( "should produce string and union types for variables (default)" , async ( ) => {
700
+ await runWrangler ( "types" ) ;
701
+
702
+ expect ( std . out ) . toMatchInlineSnapshot ( `
703
+ "Generating project types...
704
+
705
+ interface Env {
706
+ MY_VAR: \\"a var\\";
707
+ MY_VAR_A: \\"A (dev)\\" | \\"A (prod)\\" | \\"A (stag)\\";
708
+ MY_VAR_C: [\\"a\\",\\"b\\",\\"c\\"] | [1,2,3];
709
+ MY_VAR_B: {\\"value\\":\\"B (dev)\\"} | {\\"value\\":\\"B (prod)\\"};
710
+ }
711
+ "
712
+ ` ) ;
713
+ } ) ;
714
+
715
+ it ( "should produce non-strict types for variables (with --strict-vars=false)" , async ( ) => {
716
+ await runWrangler ( "types --strict-vars=false" ) ;
717
+
718
+ expect ( std . out ) . toMatchInlineSnapshot ( `
719
+ "Generating project types...
720
+
721
+ interface Env {
722
+ MY_VAR: string;
723
+ MY_VAR_A: string;
724
+ MY_VAR_C: string[] | number[];
725
+ MY_VAR_B: object;
726
+ }
727
+ "
728
+ ` ) ;
729
+ } ) ;
730
+ } ) ;
731
+
637
732
describe ( "customization" , ( ) => {
638
733
describe ( "env" , ( ) => {
639
734
it ( "should allow the user to customize the interface name" , async ( ) => {
@@ -768,7 +863,7 @@ describe("generateTypes()", () => {
768
863
} ) ;
769
864
} ) ;
770
865
771
- it ( "should allow multiple customization to be applied together" , async ( ) => {
866
+ it ( "should allow multiple customizations to be applied together" , async ( ) => {
772
867
fs . writeFileSync (
773
868
"./wrangler.toml" ,
774
869
TOML . stringify ( {
0 commit comments