mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-15 04:29:34 +01:00
45 lines
903 B
Markdown
45 lines
903 B
Markdown
---
|
|
title: Headers
|
|
---
|
|
|
|
# Headers
|
|
|
|
We can use the built-in [useRequestHeaders](https://v3.nuxtjs.org/api/composables/use-request-headers/) to set outgoing request headers:
|
|
|
|
::alert{type="info"}
|
|
[createTRPCNuxtClient](/get-started/client/create) has this feature by default.
|
|
::
|
|
|
|
```ts [plugins/client.ts]
|
|
export default defineNuxtPlugin(() => {
|
|
const headers = useRequestHeaders()
|
|
|
|
const client = createTRPCProxyClient<AppRouter>({
|
|
links: [
|
|
httpBatchLink({
|
|
// headers need to be a function so it gets called dynamically
|
|
// every HTTP request
|
|
headers() {
|
|
// You can add more custom headers here
|
|
return headers
|
|
}
|
|
}),
|
|
],
|
|
})
|
|
|
|
return {
|
|
provide: {
|
|
client,
|
|
},
|
|
}
|
|
})
|
|
```
|
|
|
|
```ts [server/trpc/context.ts]
|
|
export function createContext (event: H3Event) {
|
|
console.log('cookies', parseCookies(event))
|
|
|
|
return {}
|
|
}
|
|
```
|