fix(nuxt): sync `setResponseStatus` signature with h3 (#19987)

This commit is contained in:
Daniel Roe 2023-03-31 15:02:26 +01:00 committed by GitHub
parent 575f950cfc
commit 822202239c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View File

@ -10,11 +10,13 @@ Nuxt provides composables and utilities for first-class server-side-rendering su
`setResponseStatus` can only be called within component setup functions, plugins, and route middleware.
```js
const event = useRequestEvent()
// Set the status code to 404 for a custom 404 page
setResponseStatus(404)
setResponseStatus(event, 404)
// Set the status message as well
setResponseStatus(404, 'Page Not Found')
setResponseStatus(event, 404, 'Page Not Found')
```
::alert{icon=👉}

View File

@ -111,7 +111,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
// Let vue-router handle internal redirects within middleware
// to prevent the navigation happening after response is sent
if (isProcessingMiddleware() && !isExternal) {
setResponseStatus(options?.redirectCode || 302)
setResponseStatus(nuxtApp.ssrContext.event, options?.redirectCode || 302)
return to
}
const redirectLocation = isExternal ? toPath : joinURL(useRuntimeConfig().app.baseURL, router.resolve(to).fullPath || '/')

View File

@ -25,7 +25,13 @@ export function useRequestFetch (): typeof global.$fetch {
return event?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
}
export function setResponseStatus (code: number, message?: string) {
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
/** @deprecated Pass `event` as first option. */
export function setResponseStatus (code: number, message?: string): void
export function setResponseStatus (arg1: H3Event | number | undefined, arg2?: number | string, arg3?: string) {
if (process.client) { return }
_setResponseStatus(useRequestEvent(), code, message)
if (arg1 && typeof arg1 !== 'number') {
return _setResponseStatus(arg1, arg2 as number | undefined, arg3)
}
return _setResponseStatus(useRequestEvent(), arg1, arg2 as string | undefined)
}