Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): useRequestHeader utility #24781

Merged
merged 19 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/3.api/2.composables/use-request-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: "useRequestHeader"
description: "Use useRequestHeader to access a certain incoming request header."
links:
- label: Source
icon: i-simple-icons-github
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/ssr.ts
size: xs
---

You can use the built-in [`useRequestHeader`](/docs/api/composables/use-request-header) composable to access any incoming request header within your pages, components, and plugins.

```ts
// Get the cookie request header
const cookie = useRequestHeader('cookie')
GalacticHypernova marked this conversation as resolved.
Show resolved Hide resolved
```

::callout
In the browser, `useRequestHeader` will return an empty string.
GalacticHypernova marked this conversation as resolved.
Show resolved Hide resolved
::

## Example

We can use `useRequestHeader` to easily figure out if a user is authorized or not.

The example below reads the `authorization` request header to find out if a person can access a restricted resource.

```ts [middleware/authorized-only.ts]
export default defineNuxtRouteMiddleware((to, from) => {
if(!useRequestHeader('authorization')){
GalacticHypernova marked this conversation as resolved.
Show resolved Hide resolved
return navigateTo('/not-authorized')
}
})
```
9 changes: 8 additions & 1 deletion packages/nuxt/src/app/composables/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { H3Event } from 'h3'
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeaders } from 'h3'
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders } from 'h3'
import type { NuxtApp } from '../nuxt'
import { useNuxtApp } from '../nuxt'

Expand All @@ -17,6 +17,13 @@ export function useRequestHeaders (include?: any[]) {
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
}

export function useRequestHeader(include: string) {
if (import.meta.client) { return '' }
const event = useRequestEvent()
const header = event ? getRequestHeader(event, include) : ''
return header
GalacticHypernova marked this conversation as resolved.
Show resolved Hide resolved
}

export function useRequestFetch (): typeof global.$fetch {
if (import.meta.client) {
return globalThis.$fetch
Expand Down