@nuxt/docs-nightly 4.6.0-29840992.7ccd5a64 → 4.6.0-29841789.b58e27b1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -103,20 +103,19 @@ On Nuxt 5 the same names are auto-imported from `nuxt/server`, so explicit impor
|
|
|
103
103
|
| `handleCors(event, options)` returns `true` when it answered a preflight request. | `handleCors(event, options)` returns the preflight `Response` to return from the handler, or `false`. |
|
|
104
104
|
| `readValidatedBody` and `getValidatedQuery` take a validation function. | They take a [Standard Schema](https://standardschema.dev) or a function. |
|
|
105
105
|
| `createError({ statusCode, statusMessage })` | `createError({ status, statusText })` |
|
|
106
|
-
| `useSession` and
|
|
106
|
+
| h3's `useSession`, `getSession`, `updateSession` and `clearSession` seal an `h3` cookie with the `password` you pass. | They seal a `nuxt-session` cookie, by default with a secret derived from `appSecret`. The two are not interchangeable: a session one issues does not unseal with the other. |
|
|
107
107
|
| `getResponseHeader`, `setResponseHeader`, `appendResponseHeader`, `removeResponseHeader` | `event.res.headers.get()`, `.set()`, `.append()`, `.delete()` |
|
|
108
108
|
| `getMethod(event)`, `getRequestHost(event)` | `event.req.method`, `event.url.host` |
|
|
109
109
|
|
|
110
110
|
### Helpers Outside `nuxt/server`
|
|
111
111
|
|
|
112
|
-
For h3 or Nitro helpers that `nuxt/server` does not provide, such as `readMultipartFormData`, `proxyRequest`, `useStorage` or `defineCachedEventHandler`,
|
|
112
|
+
For a handler that needs h3 or Nitro helpers that `nuxt/server` does not provide, such as `readMultipartFormData`, `proxyRequest`, `useStorage` or `defineCachedEventHandler`, keep importing `defineEventHandler` and those helpers from `h3` (or `nitropack/runtime`) for that handler. Code that does this is tied to the server runtime and needs revisiting on Nuxt 5.
|
|
113
113
|
|
|
114
114
|
```ts [server/api/upload.post.ts]
|
|
115
|
-
import { defineEventHandler,
|
|
116
|
-
import { readMultipartFormData } from 'h3'
|
|
115
|
+
import { defineEventHandler, readMultipartFormData } from 'h3'
|
|
117
116
|
|
|
118
117
|
export default defineEventHandler(async (event) => {
|
|
119
|
-
const parts = await readMultipartFormData(
|
|
118
|
+
const parts = await readMultipartFormData(event)
|
|
120
119
|
return { received: parts?.length ?? 0 }
|
|
121
120
|
})
|
|
122
121
|
```
|
|
@@ -49,7 +49,6 @@ This code can run under `@nuxt/nitro-server` and under `@nuxt/vite-server`, and
|
|
|
49
49
|
| `getRouteRules` | The [route rules](/docs/guide/concepts/rendering#route-rules) matched for the request. |
|
|
50
50
|
| `useRuntimeConfig` | The server's [runtime configuration](/docs/guide/going-further/runtime-config). |
|
|
51
51
|
| `useAppConfig` | The [app config](/docs/directory-structure/app/app-config), as a copy per request when passed the event. |
|
|
52
|
-
| `toNuxtRequestEvent` | The event in the shape the server runtime provides. |
|
|
53
52
|
|
|
54
53
|
Types are exported alongside the utilities: `RequestEvent`, `RequestEventContext`, `NuxtRequestEvent`, `EventHandler`, `AppRouteRules`, `ServerRoutes`, `CorsOptions`, `ValidateResult`, `NuxtError`, `NuxtErrorDetails`, `NuxtErrorJSON`, `NuxtErrorLike`, `Session`, `SessionConfig`, `SessionData`, `SessionEvent`, `SessionManager`, `SessionPassword` and `SessionUpdate`.
|
|
55
54
|
|
|
@@ -87,7 +86,7 @@ export default defineEventHandler(async (event) => {
|
|
|
87
86
|
})
|
|
88
87
|
```
|
|
89
88
|
|
|
90
|
-
`NuxtRequestEvent` is the same request in the shape the configured builder provides, which is h3 v1's `H3Event` under `@nuxt/nitro-server`.
|
|
89
|
+
`NuxtRequestEvent` is the same request in the shape the configured builder provides, which is h3 v1's `H3Event` under `@nuxt/nitro-server`.
|
|
91
90
|
|
|
92
91
|
## Validation
|
|
93
92
|
|
|
@@ -157,31 +156,18 @@ The session travels in the cookie, so it is limited to what a browser will accep
|
|
|
157
156
|
|
|
158
157
|
`nuxt/server` isn't a re-export of h3, so if you need something else that h3 and Nitro offer, such as the server lifecycle (`defineNitroPlugin`, `defineNitroErrorHandler` and the Nitro app hooks), you would import it directly.
|
|
159
158
|
|
|
160
|
-
|
|
159
|
+
For a handler that needs h3's own helpers, such as `readRawBody`, `readMultipartFormData`, `assertMethod`, `proxyRequest`, `fetchWithEvent` or `writeEarlyHints`, or that reads `event.node` or `event.waitUntil()`, import `defineEventHandler` from `h3` for that handler:
|
|
161
160
|
|
|
162
161
|
```ts [server/api/upload.post.ts]
|
|
163
|
-
import { defineEventHandler,
|
|
164
|
-
import { readMultipartFormData } from 'h3'
|
|
162
|
+
import { defineEventHandler, readMultipartFormData } from 'h3'
|
|
165
163
|
|
|
166
164
|
export default defineEventHandler(async (event) => {
|
|
167
|
-
const parts = await readMultipartFormData(
|
|
165
|
+
const parts = await readMultipartFormData(event)
|
|
168
166
|
return { received: parts?.length ?? 0 }
|
|
169
167
|
})
|
|
170
168
|
```
|
|
171
169
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
Nitro's own APIs mostly take no event at all: `defineCachedEventHandler()`, `useStorage()`, `useDatabase()` and tasks.
|
|
175
|
-
|
|
176
|
-
::tip
|
|
177
|
-
If you find you are writing Nitro-specific code, you may simply prefer to import `defineEventHandler` from `h3` instead:
|
|
178
|
-
|
|
179
|
-
```ts [server/api/proxy.ts]
|
|
180
|
-
import { defineEventHandler, proxyRequest } from 'h3'
|
|
181
|
-
|
|
182
|
-
export default defineEventHandler(event => proxyRequest(event, 'https://example.com'))
|
|
183
|
-
```
|
|
184
|
-
::
|
|
170
|
+
Nitro's own APIs mostly take no event at all: `defineCachedEventHandler()`, `useStorage()`, `useDatabase()` and tasks. Import them from `nitropack/runtime`.
|
|
185
171
|
|
|
186
172
|
## What Isn't Portable
|
|
187
173
|
|