@@ -1660,6 +1660,257 @@ describe("generate types", () => {
16601660 ` ) ;
16611661 } ) ;
16621662
1663+ it ( "should generate types from config `secrets.required`" , async ( {
1664+ expect,
1665+ } ) => {
1666+ fs . writeFileSync (
1667+ "./wrangler.jsonc" ,
1668+ JSON . stringify ( {
1669+ secrets : { required : [ "API_KEY" , "DB_PASSWORD" ] } ,
1670+ } ) ,
1671+ "utf-8"
1672+ ) ;
1673+
1674+ await runWrangler ( "types --include-runtime=false" ) ;
1675+
1676+ expect ( std . out ) . toMatchInlineSnapshot ( `
1677+ "
1678+ ⛅️ wrangler x.x.x
1679+ ──────────────────
1680+ Generating project types...
1681+
1682+ declare namespace Cloudflare {
1683+ interface Env {
1684+ API_KEY: string;
1685+ DB_PASSWORD: string;
1686+ }
1687+ }
1688+ interface Env extends Cloudflare.Env {}
1689+
1690+ ────────────────────────────────────────────────────────────
1691+ ✨ Types written to worker-configuration.d.ts
1692+
1693+ 📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
1694+ "
1695+ ` ) ;
1696+ } ) ;
1697+
1698+ it ( "should ignore .dev.vars when config `secrets` is defined" , async ( {
1699+ expect,
1700+ } ) => {
1701+ fs . writeFileSync (
1702+ "./wrangler.jsonc" ,
1703+ JSON . stringify ( {
1704+ secrets : { required : [ "API_KEY" ] } ,
1705+ } ) ,
1706+ "utf-8"
1707+ ) ;
1708+
1709+ fs . writeFileSync (
1710+ ".dev.vars" ,
1711+ 'DEV_VARS_SECRET="should not appear"\n' ,
1712+ "utf8"
1713+ ) ;
1714+
1715+ await runWrangler ( "types --include-runtime=false" ) ;
1716+
1717+ expect ( std . out ) . toMatchInlineSnapshot ( `
1718+ "
1719+ ⛅️ wrangler x.x.x
1720+ ──────────────────
1721+ Generating project types...
1722+
1723+ declare namespace Cloudflare {
1724+ interface Env {
1725+ API_KEY: string;
1726+ }
1727+ }
1728+ interface Env extends Cloudflare.Env {}
1729+
1730+ ────────────────────────────────────────────────────────────
1731+ ✨ Types written to worker-configuration.d.ts
1732+
1733+ 📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
1734+ "
1735+ ` ) ;
1736+ } ) ;
1737+
1738+ it ( "should ignore .env when config `secrets` is defined" , async ( {
1739+ expect,
1740+ } ) => {
1741+ fs . writeFileSync (
1742+ "./wrangler.jsonc" ,
1743+ JSON . stringify ( {
1744+ secrets : { required : [ "API_KEY" ] } ,
1745+ } ) ,
1746+ "utf-8"
1747+ ) ;
1748+
1749+ fs . writeFileSync ( ".env" , 'DOT_ENV_SECRET="should not appear"\n' , "utf8" ) ;
1750+
1751+ await runWrangler ( "types --include-runtime=false" ) ;
1752+
1753+ expect ( std . out ) . toMatchInlineSnapshot ( `
1754+ "
1755+ ⛅️ wrangler x.x.x
1756+ ──────────────────
1757+ Generating project types...
1758+
1759+ declare namespace Cloudflare {
1760+ interface Env {
1761+ API_KEY: string;
1762+ }
1763+ }
1764+ interface Env extends Cloudflare.Env {}
1765+
1766+ ────────────────────────────────────────────────────────────
1767+ ✨ Types written to worker-configuration.d.ts
1768+
1769+ 📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
1770+ "
1771+ ` ) ;
1772+ } ) ;
1773+
1774+ it ( "should generate types for per-env config `secrets`" , async ( {
1775+ expect,
1776+ } ) => {
1777+ fs . writeFileSync (
1778+ "./wrangler.jsonc" ,
1779+ JSON . stringify ( {
1780+ secrets : { required : [ "API_KEY" ] } ,
1781+ env : {
1782+ staging : {
1783+ secrets : {
1784+ required : [ "API_KEY" , "DEBUG_TOKEN" ] ,
1785+ } ,
1786+ } ,
1787+ production : {
1788+ secrets : {
1789+ required : [ "API_KEY" , "PROD_DB_PASSWORD" ] ,
1790+ } ,
1791+ } ,
1792+ } ,
1793+ } ) ,
1794+ "utf-8"
1795+ ) ;
1796+
1797+ await runWrangler ( "types --include-runtime=false" ) ;
1798+
1799+ expect ( std . out ) . toMatchInlineSnapshot ( `
1800+ "
1801+ ⛅️ wrangler x.x.x
1802+ ──────────────────
1803+ Generating project types...
1804+
1805+ declare namespace Cloudflare {
1806+ interface StagingEnv {
1807+ API_KEY: string;
1808+ DEBUG_TOKEN: string;
1809+ }
1810+ interface ProductionEnv {
1811+ API_KEY: string;
1812+ PROD_DB_PASSWORD: string;
1813+ }
1814+ interface Env {
1815+ API_KEY: string;
1816+ DEBUG_TOKEN?: string;
1817+ PROD_DB_PASSWORD?: string;
1818+ }
1819+ }
1820+ interface Env extends Cloudflare.Env {}
1821+
1822+ ────────────────────────────────────────────────────────────
1823+ ✨ Types written to worker-configuration.d.ts
1824+
1825+ 📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
1826+ "
1827+ ` ) ;
1828+ } ) ;
1829+
1830+ it ( "should not fall back to .dev.vars for envs without `secrets` when another env declares `secrets`" , async ( {
1831+ expect,
1832+ } ) => {
1833+ fs . writeFileSync (
1834+ "./wrangler.jsonc" ,
1835+ JSON . stringify ( {
1836+ env : {
1837+ staging : {
1838+ secrets : { required : [ "STAGING_KEY" ] } ,
1839+ } ,
1840+ production : { } ,
1841+ } ,
1842+ } ) ,
1843+ "utf-8"
1844+ ) ;
1845+
1846+ fs . writeFileSync (
1847+ ".dev.vars" ,
1848+ 'FALLBACK_SECRET="should not appear"\n' ,
1849+ "utf8"
1850+ ) ;
1851+
1852+ await runWrangler ( "types --include-runtime=false" ) ;
1853+
1854+ expect ( std . out ) . toMatchInlineSnapshot ( `
1855+ "
1856+ ⛅️ wrangler x.x.x
1857+ ──────────────────
1858+ Generating project types...
1859+
1860+ declare namespace Cloudflare {
1861+ interface StagingEnv {
1862+ STAGING_KEY: string;
1863+ }
1864+ interface ProductionEnv {}
1865+ interface Env {
1866+ STAGING_KEY?: string;
1867+ }
1868+ }
1869+ interface Env extends Cloudflare.Env {}
1870+
1871+ ────────────────────────────────────────────────────────────
1872+ ✨ Types written to worker-configuration.d.ts
1873+
1874+ 📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
1875+ "
1876+ ` ) ;
1877+ } ) ;
1878+
1879+ it ( "should exclude .dev.vars keys when `secrets` is declared without `required`" , async ( {
1880+ expect,
1881+ } ) => {
1882+ fs . writeFileSync (
1883+ "./wrangler.jsonc" ,
1884+ JSON . stringify ( {
1885+ secrets : { } ,
1886+ } ) ,
1887+ "utf-8"
1888+ ) ;
1889+
1890+ fs . writeFileSync ( ".dev.vars" , 'SHOULD_NOT_APPEAR="hidden"\n' , "utf8" ) ;
1891+
1892+ await runWrangler ( "types --include-runtime=false" ) ;
1893+
1894+ expect ( std . out ) . toMatchInlineSnapshot ( `
1895+ "
1896+ ⛅️ wrangler x.x.x
1897+ ──────────────────
1898+ Generating project types...
1899+
1900+ declare namespace Cloudflare {
1901+ interface Env {
1902+ }
1903+ }
1904+ interface Env extends Cloudflare.Env {}
1905+
1906+ ────────────────────────────────────────────────────────────
1907+ ✨ Types written to worker-configuration.d.ts
1908+
1909+ 📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
1910+ "
1911+ ` ) ;
1912+ } ) ;
1913+
16631914 it ( "various different types of vars" , async ( { expect } ) => {
16641915 fs . writeFileSync (
16651916 "./wrangler.jsonc" ,
@@ -2328,8 +2579,8 @@ describe("generate types", () => {
23282579 MY_SECRET: string;
23292580 }
23302581 interface Env {
2331- MY_SECRET: string;
23322582 KV_STAGING?: KVNamespace;
2583+ MY_SECRET: string;
23332584 }
23342585 }
23352586 interface Env extends Cloudflare.Env {}
0 commit comments