feat: add composable for custom client headers

This commit is contained in:
Robert Soriano
2022-05-24 09:46:15 -07:00
parent 48152ead8d
commit 3b5e35ef68
7 changed files with 130 additions and 12 deletions

View File

@@ -4,18 +4,26 @@ import type { router } from '~/server/trpc'
declare type AppRouter = typeof router
export default defineNuxtPlugin(() => {
export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig().public.trpc
const headers = useRequestHeaders()
const client = trpc.createTRPCClient<AppRouter>({
url: `${config.baseURL}${config.trpcURL}`,
headers: useRequestHeaders(),
headers: () => {
let otherHeaders = {}
if (!process.server) {
const key = 'trpc-nuxt-header'
otherHeaders = JSON.parse(localStorage.getItem(key) || JSON.stringify({}))
}
return {
...otherHeaders,
...headers,
}
},
})
return {
provide: {
client,
},
}
nuxtApp.provide('client', client)
})
declare module '#app' {