mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-24 17:00:32 +01:00
docs: improve docs
This commit is contained in:
44
docs/content/1.get-started/4.tips/2.headers.md
Normal file
44
docs/content/1.get-started/4.tips/2.headers.md
Normal file
@@ -0,0 +1,44 @@
|
||||
---
|
||||
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 {}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user