Skip to content

Commit 708bb18

Browse files
committed
fix: resolve private runtime correctly from .env, close #424
1 parent 8c287a0 commit 708bb18

8 files changed

Lines changed: 58 additions & 4 deletions

File tree

packages/devtools-kit/src/_types/rpc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { InstallModuleReturn } from './server-ctx'
1212
export interface ServerFunctions {
1313
// Static RPCs (can be provide on production build in the future)
1414
getServerConfig(): NuxtOptions
15+
getServerRuntimeConfig(): Record<string, any>
1516
getModuleOptions(): ModuleOptions
1617
getComponents(): Component[]
1718
getComponentsRelationships(): Promise<ComponentRelationship[]>

packages/devtools/client/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NUXT_API_KEY=my-api-key

packages/devtools/client/composables/state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export function useServerConfig() {
3030
return useAsyncState('getServerConfig', () => rpc.getServerConfig())
3131
}
3232

33+
export function useServerRuntimeConfig() {
34+
return useAsyncState('getServerRuntimeConfig', () => rpc.getServerRuntimeConfig())
35+
}
36+
3337
export function useModuleOptions() {
3438
return useAsyncState('getModuleOptions', () => rpc.getModuleOptions())
3539
}

packages/devtools/client/nuxt.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export default defineNuxtConfig({
3636
fixture2: 'from nuxt.config.ts',
3737
},
3838
runtimeConfig: {
39-
fixture3: 'private runtime config from nuxt.config.ts',
40-
public: {
39+
'fixture3': 'private runtime config from nuxt.config.ts',
40+
'api-key': 'null',
41+
'public': {
4142
fixture4: 'public runtime config from nuxt.config.ts',
4243
},
4344
},

packages/devtools/client/pages/modules/runtime-configs.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ definePageMeta({
1111
})
1212
1313
const client = useClient()
14-
const serverConfig = useServerConfig()
14+
const runtimeConfig = useServerRuntimeConfig()
1515
const payload = computed(() => client.value?.nuxt.payload)
1616
1717
const privateConfig = computed(() => {
1818
const clone = {
19-
...serverConfig.value?.runtimeConfig,
19+
...runtimeConfig.value,
2020
}
2121
delete clone.public
2222
delete clone.app

packages/devtools/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@nuxt/kit": "^3.7.4",
4545
"birpc": "^0.2.14",
4646
"consola": "^3.2.3",
47+
"destr": "^2.0.1",
4748
"error-stack-parser-es": "^0.1.1",
4849
"execa": "^7.2.0",
4950
"fast-glob": "^3.3.1",
@@ -65,6 +66,7 @@
6566
"perfect-debounce": "^1.0.0",
6667
"pkg-types": "^1.0.3",
6768
"rc9": "^2.1.1",
69+
"scule": "^1.0.0",
6870
"semver": "^7.5.4",
6971
"simple-git": "^3.20.0",
7072
"sirv": "^2.0.3",

packages/devtools/src/server-rpc/general.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { resolveBuiltinPresets } from 'unimport'
55
import { resolve } from 'pathe'
66
import { colors } from 'consola/utils'
77
import { logger } from '@nuxt/kit'
8+
import type { Nitro } from 'nitropack'
9+
import destr from 'destr'
10+
import { snakeCase } from 'scule'
811

912
import type { ModuleOptions } from '@nuxt/schema'
1013
import type { AutoImportsWithMetadata, HookInfo, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
@@ -75,6 +78,42 @@ export function setupGeneralRPC({ nuxt, options, refresh, openInEditorHooks }: N
7578
getServerConfig() {
7679
return nuxt.options
7780
},
81+
getServerRuntimeConfig() {
82+
// Ported from https://github.com/unjs/nitro/blob/88e79fcdb2a024c96a3d1fd272d0acbff0405013/src/runtime/config.ts#L31
83+
// Since this operation happends on the Nitro runtime
84+
const ENV_PREFIX = 'NITRO_'
85+
const ENV_PREFIX_ALT = 'NUXT_'
86+
87+
function _getEnv(key: string) {
88+
const envKey = snakeCase(key).toUpperCase()
89+
return destr(process.env[ENV_PREFIX + envKey] ?? process.env[ENV_PREFIX_ALT + envKey])
90+
}
91+
92+
function _isObject(input: unknown) {
93+
return typeof input === 'object' && !Array.isArray(input)
94+
}
95+
96+
function _applyEnv(obj: any, parentKey = '') {
97+
for (const key in obj) {
98+
const subKey = parentKey ? `${parentKey}_${key}` : key
99+
const envValue = _getEnv(subKey)
100+
if (_isObject(obj[key])) {
101+
if (_isObject(envValue))
102+
obj[key] = { ...obj[key], ...(envValue as any) }
103+
104+
_applyEnv(obj[key], subKey)
105+
}
106+
else {
107+
obj[key] = envValue ?? obj[key]
108+
}
109+
}
110+
return obj
111+
}
112+
113+
const runtime = { ...nuxt.options.runtimeConfig }
114+
_applyEnv(runtime)
115+
return runtime
116+
},
78117
getModuleOptions(): ModuleOptions {
79118
return options
80119
},

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)