mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-02-02 05:08:01 +01:00
update docs
This commit is contained in:
42
docs/content/1.get-started/3.tips/5.headers.md
Normal file
42
docs/content/1.get-started/3.tips/5.headers.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
---
|
||||||
|
title: Headers
|
||||||
|
---
|
||||||
|
|
||||||
|
# Headers
|
||||||
|
|
||||||
|
We can use the built-in `useRequestHeaders` to access/pass headers to any future internal requests during SSR.
|
||||||
|
|
||||||
|
```ts [plugins/client.ts]
|
||||||
|
import { httpBatchLink, createTRPCProxyClient } from '@trpc/client'
|
||||||
|
import type { AppRouter } from '@/server/trpc/routers'
|
||||||
|
|
||||||
|
export default defineNuxtPlugin(() => {
|
||||||
|
const headers = useRequestHeaders()
|
||||||
|
|
||||||
|
const client = createTRPCProxyClient<AppRouter>({
|
||||||
|
links: [
|
||||||
|
httpBatchLink({
|
||||||
|
headers() {
|
||||||
|
return {
|
||||||
|
...unref(headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
provide: {
|
||||||
|
client,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [server/trpc/context.ts]
|
||||||
|
export function createContext (event: H3Event) {
|
||||||
|
console.log('cookies', parseCookies(event))
|
||||||
|
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -4,12 +4,6 @@ import type { inferRouterOutputs } from '@trpc/server';
|
|||||||
import type { AppRouter } from '~~/server/trpc/routers';
|
import type { AppRouter } from '~~/server/trpc/routers';
|
||||||
|
|
||||||
const { $client } = useNuxtApp()
|
const { $client } = useNuxtApp()
|
||||||
// const headers = useClientHeaders()
|
|
||||||
|
|
||||||
// const addHeader = () => {
|
|
||||||
// headers.value.authorization = 'Bearer abcdefghijklmnop'
|
|
||||||
// console.log(headers.value)
|
|
||||||
// }
|
|
||||||
|
|
||||||
const addTodo = async () => {
|
const addTodo = async () => {
|
||||||
const title = Math.random().toString(36).slice(2, 7)
|
const title = Math.random().toString(36).slice(2, 7)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import superjson from 'superjson'
|
|||||||
import type { AppRouter } from '~~/server/trpc/routers'
|
import type { AppRouter } from '~~/server/trpc/routers'
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
|
const headers = useRequestHeaders()
|
||||||
const client = createTRPCProxyClient<AppRouter>({
|
const client = createTRPCProxyClient<AppRouter>({
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
links: [
|
links: [
|
||||||
@@ -13,7 +14,12 @@ export default defineNuxtPlugin(() => {
|
|||||||
(opts.direction === 'down' && opts.result instanceof Error)
|
(opts.direction === 'down' && opts.result instanceof Error)
|
||||||
}),
|
}),
|
||||||
httpBatchLink({
|
httpBatchLink({
|
||||||
url: 'http://localhost:3000/api/trpc'
|
url: 'http://localhost:3000/api/trpc',
|
||||||
|
headers () {
|
||||||
|
return {
|
||||||
|
...unref(headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ export type Context = inferAsyncReturnType<typeof createContext>
|
|||||||
* @link https://trpc.io/docs/context
|
* @link https://trpc.io/docs/context
|
||||||
*/
|
*/
|
||||||
export function createContext (
|
export function createContext (
|
||||||
opts: H3Event
|
event: H3Event
|
||||||
) {
|
) {
|
||||||
// for API-response caching see https://trpc.io/docs/caching
|
// for API-response caching see https://trpc.io/docs/caching
|
||||||
|
console.log('cookies', parseCookies(event))
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user