Skip to content

Commit b17f486

Browse files
authored
fix(types): type safe runtime config on server (#7)
1 parent a3650f8 commit b17f486

File tree

14 files changed

+144
-19559
lines changed

14 files changed

+144
-19559
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ const config = useSafeRuntimeConfig()
142142
</script>
143143
```
144144

145+
You can also use the same composable in server code:
146+
147+
```ts
148+
// server/utils/config.ts
149+
export function getPrivateConfig() {
150+
const config = useSafeRuntimeConfig()
151+
return {
152+
secretKey: config.secretKey,
153+
apiBase: config.public.apiBase,
154+
}
155+
}
156+
```
157+
145158
## Configuration Options
146159

147160
| Option | Type | Default | Description |
@@ -289,7 +302,7 @@ The rule includes auto-fix support — run `eslint --fix` to automatically repla
289302

290303
## Type Safety
291304

292-
Types are auto-generated at build time from your schema's JSON Schema representation. The `useSafeRuntimeConfig()` composable returns a fully typed object — no manual generics needed:
305+
Types are auto-generated at build time from your schema's JSON Schema representation. The `useSafeRuntimeConfig()` composable returns a fully typed object in both app and server contexts (`app.vue`, `server/api`, `server/utils`) — no manual generics needed:
293306

294307
```ts
295308
const config = useSafeRuntimeConfig()

docs/1.guide/3.type-safety.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ icon: ph:shield-check
44

55
# Type Safety
66

7-
The module generates TypeScript types from your schema, making `useSafeRuntimeConfig()` fully typed without manual generics.
7+
The module generates TypeScript types from your schema, making `useSafeRuntimeConfig()` fully typed without manual generics in both app and server code.
88

99
## How It Works
1010

@@ -56,7 +56,7 @@ interface SafeRuntimeConfig {
5656

5757
## Server vs Client
5858

59-
On the server, you get the full config. On the client, only `public` properties are available:
59+
On the server (including `server/api` and `server/utils`), you get the full config. On the client, only `public` properties are available:
6060

6161
```ts
6262
// Server
@@ -69,6 +69,17 @@ config.secretKey // undefined (private keys not exposed)
6969
config.public.apiBase // available
7070
```
7171

72+
```ts
73+
// server/utils/config.ts
74+
export function getServerConfig() {
75+
const config = useSafeRuntimeConfig()
76+
return {
77+
secretKey: config.secretKey,
78+
apiBase: config.public.apiBase,
79+
}
80+
}
81+
```
82+
7283
## Generated Types Location
7384

7485
Types are stored in `.nuxt/types/safe-runtime-config.d.ts` and automatically included via Nuxt's type augmentation.

0 commit comments

Comments
 (0)