From 20f31712c1e1e11e57df7d08191459eec2af7e4f Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Wed, 9 Mar 2022 14:41:24 +0100 Subject: [PATCH] feat(nuxt3): auto generate runtime config type declarations (#3573) Co-authored-by: Rodrigo Mesquita Co-authored-by: Daniel Roe Co-authored-by: Rodrigo Mesquita <30835404+rrmesquita@users.noreply.github.com> --- docs/package.json | 2 +- package.json | 2 +- packages/bridge/package.json | 1 + packages/kit/package.json | 2 +- packages/nuxt3/package.json | 1 + packages/nuxt3/src/core/templates.ts | 17 +++++++ packages/schema/src/types/config.ts | 4 +- test/fixtures/basic/nuxt.config.ts | 5 +- test/fixtures/basic/types.ts | 9 ++++ yarn.lock | 76 +++++++++++++++++----------- 10 files changed, 84 insertions(+), 35 deletions(-) diff --git a/docs/package.json b/docs/package.json index 970cbad7c8..f0128db61b 100644 --- a/docs/package.json +++ b/docs/package.json @@ -18,7 +18,7 @@ "pathe": "^0.2.0", "rimraf": "^3.0.2", "scule": "^0.2.1", - "untyped": "^0.3.0", + "untyped": "^0.4.2", "vue-mq": "^1.0.1", "vue-plausible": "^1.3.1" }, diff --git a/package.json b/package.json index 611b1843b5..be511bf52a 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "jiti": "^1.13.0", "nitropack-dev": "link:../nitropack", "nuxt3": "workspace:./packages/nuxt3", - "unbuild": "^0.6.9" + "unbuild": "^0.7.0" }, "devDependencies": { "@iconify-json/carbon": "^1.1.1", diff --git a/packages/bridge/package.json b/packages/bridge/package.json index c06656fc64..c2715a838a 100644 --- a/packages/bridge/package.json +++ b/packages/bridge/package.json @@ -58,6 +58,7 @@ "ufo": "^0.7.9", "unplugin": "^0.3.2", "unplugin-vue2-script-setup": "0.9.2", + "untyped": "^0.4.2", "vite": "^2.8.2", "vite-plugin-vue2": "^1.9.3", "vue-template-compiler": "^2.6.14" diff --git a/packages/kit/package.json b/packages/kit/package.json index fe656adc5d..60127d6bfa 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -29,7 +29,7 @@ "scule": "^0.2.1", "semver": "^7.3.5", "unctx": "^1.0.2", - "untyped": "^0.3.0" + "untyped": "^0.4.2" }, "devDependencies": { "@types/lodash.template": "^4", diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 896f3c0175..9f266add3d 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -59,6 +59,7 @@ "scule": "^0.2.1", "ufo": "^0.7.11", "unplugin": "^0.4.0", + "untyped": "^0.4.2", "vue": "^3.2.31", "vue-router": "^4.0.13" }, diff --git a/packages/nuxt3/src/core/templates.ts b/packages/nuxt3/src/core/templates.ts index 976a1733f1..2c5f8bb7e8 100644 --- a/packages/nuxt3/src/core/templates.ts +++ b/packages/nuxt3/src/core/templates.ts @@ -3,6 +3,7 @@ import type { Nuxt, NuxtApp } from '@nuxt/schema' import { genArrayFromRaw, genDynamicImport, genExport, genImport, genString } from 'knitwork' import { isAbsolute, join, relative } from 'pathe' +import { resolveSchema, generateTypes } from 'untyped' import escapeRE from 'escape-string-regexp' export interface TemplateContext { @@ -128,6 +129,22 @@ export const schemaTemplate = { ` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends NuxtModule ? Partial : Record` ), ' }', + generateTypes(resolveSchema(nuxt.options.publicRuntimeConfig), + { + interfaceName: 'PublicRuntimeConfig', + addExport: false, + addDefaults: false, + allowExtraKeys: false, + indentation: 2 + }), + generateTypes(resolveSchema(nuxt.options.privateRuntimeConfig), { + interfaceName: 'PrivateRuntimeConfig', + addExport: false, + addDefaults: false, + indentation: 2, + allowExtraKeys: false, + defaultDescrption: 'This value is only accessible from server-side.' + }), '}' ].join('\n') } diff --git a/packages/schema/src/types/config.ts b/packages/schema/src/types/config.ts index 5b6468c23a..ac8765132c 100644 --- a/packages/schema/src/types/config.ts +++ b/packages/schema/src/types/config.ts @@ -17,4 +17,6 @@ export interface PublicRuntimeConfig extends Record { } export interface PrivateRuntimeConfig extends PublicRuntimeConfig { } type _RuntimeConfig = PublicRuntimeConfig & Partial -export interface RuntimeConfig extends _RuntimeConfig { } +export interface RuntimeConfig extends _RuntimeConfig { + [key: string]: any +} diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index a3fb219710..466eb42e3b 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -8,7 +8,10 @@ export default defineNuxtConfig({ output: { dir: process.env.NITRO_OUTPUT_DIR } }, publicRuntimeConfig: { - testConfig: '123' + testConfig: 123 + }, + privateRuntimeConfig: { + privateConfig: 'secret_key' }, modules: ['~/modules/example'], hooks: { diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 78e6c0f1f9..20561e1231 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -79,3 +79,12 @@ describe('modules', () => { defineNuxtConfig({ undeclaredKey: { other: false } }) }) }) + +describe('runtimeConfig', () => { + it('generated runtimeConfig types', () => { + const runtimeConfig = useRuntimeConfig() + expectTypeOf(runtimeConfig.testConfig).toMatchTypeOf() + expectTypeOf(runtimeConfig.privateConfig).toMatchTypeOf() + expectTypeOf(runtimeConfig.unknown).toMatchTypeOf() + }) +}) diff --git a/yarn.lock b/yarn.lock index b4a8b53a99..b695276a57 100644 --- a/yarn.lock +++ b/yarn.lock @@ -42,7 +42,7 @@ __metadata: languageName: node linkType: hard -"@babel/code-frame@npm:^7.0.0, @babel/code-frame@npm:^7.10.4, @babel/code-frame@npm:^7.16.0, @babel/code-frame@npm:^7.16.7": +"@babel/code-frame@npm:^7.0.0, @babel/code-frame@npm:^7.10.4, @babel/code-frame@npm:^7.16.7": version: 7.16.7 resolution: "@babel/code-frame@npm:7.16.7" dependencies: @@ -2683,6 +2683,7 @@ __metadata: unbuild: latest unplugin: ^0.3.2 unplugin-vue2-script-setup: 0.9.2 + untyped: ^0.4.2 vite: ^2.8.2 vite-plugin-vue2: ^1.9.3 vue: ^2 @@ -2985,7 +2986,7 @@ __metadata: semver: ^7.3.5 unbuild: latest unctx: ^1.0.2 - untyped: ^0.3.0 + untyped: ^0.4.2 languageName: unknown linkType: soft @@ -14517,7 +14518,7 @@ __metadata: languageName: node linkType: hard -"mkdist@npm:^0.3.9": +"mkdist@npm:^0.3.10": version: 0.3.10 resolution: "mkdist@npm:0.3.10" dependencies: @@ -15496,6 +15497,7 @@ __metadata: ufo: ^0.7.11 unbuild: latest unplugin: ^0.4.0 + untyped: ^0.4.2 vue: ^3.2.31 vue-meta: next vue-router: ^4.0.13 @@ -19052,19 +19054,19 @@ __metadata: languageName: node linkType: hard -"rollup-plugin-dts@npm:^4.1.0": - version: 4.1.0 - resolution: "rollup-plugin-dts@npm:4.1.0" +"rollup-plugin-dts@npm:^4.2.0": + version: 4.2.0 + resolution: "rollup-plugin-dts@npm:4.2.0" dependencies: - "@babel/code-frame": ^7.16.0 - magic-string: ^0.25.7 + "@babel/code-frame": ^7.16.7 + magic-string: ^0.26.1 peerDependencies: rollup: ^2.55 - typescript: ~4.1 || ~4.2 || ~4.3 || ~4.4 || ~4.5 + typescript: ^4.1 dependenciesMeta: "@babel/code-frame": optional: true - checksum: add2715d4906dccb8bbad63e02668303eee553b68e2ae8c7c686abc5ea7936ef316db43845eb12bf978002f461e43b00ecfe61bd2ffedd6c570e12a289de7c18 + checksum: 2b8394bf62c3903a68722266999adf3e85768bf1a7e93286841b466afaabf5cb30501f71d40075c3393cc33defa6ed13d3276bae0d8cb7a87a95d06f7a93c04d languageName: node linkType: hard @@ -20957,7 +20959,7 @@ __metadata: languageName: node linkType: hard -"typescript@npm:^4.3.4, typescript@npm:^4.5.5, typescript@npm:^4.6.2": +"typescript@npm:^4.3.4, typescript@npm:^4.6.2": version: 4.6.2 resolution: "typescript@npm:4.6.2" bin: @@ -20967,7 +20969,7 @@ __metadata: languageName: node linkType: hard -"typescript@patch:typescript@^4.3.4#~builtin, typescript@patch:typescript@^4.5.5#~builtin, typescript@patch:typescript@^4.6.2#~builtin": +"typescript@patch:typescript@^4.3.4#~builtin, typescript@patch:typescript@^4.6.2#~builtin": version: 4.6.2 resolution: "typescript@patch:typescript@npm%3A4.6.2#~builtin::version=4.6.2&hash=bda367" bin: @@ -21065,40 +21067,40 @@ __metadata: languageName: node linkType: hard -"unbuild@npm:^0.6.9": - version: 0.6.9 - resolution: "unbuild@npm:0.6.9" +"unbuild@npm:^0.7.0": + version: 0.7.0 + resolution: "unbuild@npm:0.7.0" dependencies: "@rollup/plugin-alias": ^3.1.9 - "@rollup/plugin-commonjs": ^21.0.1 + "@rollup/plugin-commonjs": ^21.0.2 "@rollup/plugin-json": ^4.1.0 "@rollup/plugin-node-resolve": ^13.1.3 - "@rollup/plugin-replace": ^3.0.1 - "@rollup/pluginutils": ^4.1.2 - chalk: ^5.0.0 + "@rollup/plugin-replace": ^4.0.0 + "@rollup/pluginutils": ^4.2.0 + chalk: ^5.0.1 consola: ^2.15.3 defu: ^5.0.1 - esbuild: ^0.14.14 + esbuild: ^0.14.25 hookable: ^5.1.1 - jiti: ^1.12.14 - magic-string: ^0.25.7 + jiti: ^1.13.0 + magic-string: ^0.26.1 mkdirp: ^1.0.4 - mkdist: ^0.3.9 - mlly: ^0.4.1 + mkdist: ^0.3.10 + mlly: ^0.4.3 mri: ^1.2.0 pathe: ^0.2.0 pkg-types: ^0.3.2 - pretty-bytes: ^5.6.0 + pretty-bytes: ^6.0.0 rimraf: ^3.0.2 - rollup: ^2.66.1 - rollup-plugin-dts: ^4.1.0 + rollup: ^2.70.0 + rollup-plugin-dts: ^4.2.0 rollup-plugin-esbuild: ^4.8.2 scule: ^0.2.1 - typescript: ^4.5.5 - untyped: ^0.3.0 + typescript: ^4.6.2 + untyped: ^0.4.0 bin: unbuild: dist/cli.mjs - checksum: f3db7b040007247ffd7e1ba66e895bb6fd85a5f1e0f8253b862b6ef0f289d0d394e9cc8fee5edbdef901fcfaecafa2cea259b314919f2c2ca7855e22cdc0cdd6 + checksum: 085af2e17825859e50979909c127e38a149abcc2f75ca57d5e47d1699d042ccbac696fadf6808b24e6ee7ac361915933efbc6452f5102fbb856997ff89fe473b languageName: node linkType: hard @@ -21390,6 +21392,20 @@ __metadata: languageName: node linkType: hard +"untyped@npm:^0.4.0": + version: 0.4.0 + resolution: "untyped@npm:0.4.0" + checksum: f2557963fb93c2a055e18c3b0d06bcab2e1539c024824946053ded03e275a062c8540e1f00d90c12e5fc3f78c75a46e11ac83d83ac827b2113868489855e791f + languageName: node + linkType: hard + +"untyped@npm:^0.4.2": + version: 0.4.2 + resolution: "untyped@npm:0.4.2" + checksum: 51468cf8cb114795d08986cd37ff37f51a039a1b8c82147d3e7a12edd8515b446c54304728e5c290161249a599e72bc90851bbe6ae90c68778ad30642b6cbcfa + languageName: node + linkType: hard + "upath@npm:^1.1.1": version: 1.2.0 resolution: "upath@npm:1.2.0"