mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-26 09:50:31 +01:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c8509f79c | ||
|
|
2ce29137ce | ||
|
|
e9c5307e23 | ||
|
|
bbdabf544c | ||
|
|
aed10ac5b8 | ||
|
|
4b2c714658 | ||
|
|
43e9fefdbd | ||
|
|
dabda23976 | ||
|
|
af89f32275 | ||
|
|
1f27b871fb | ||
|
|
281e4c05a0 | ||
|
|
38ac520b97 |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "trpc-nuxt",
|
"name": "trpc-nuxt",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.1.17",
|
"version": "0.1.23",
|
||||||
"packageManager": "pnpm@7.1.1",
|
"packageManager": "pnpm@7.1.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/module.cjs",
|
"main": "./dist/module.cjs",
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const client = useClient()
|
const client = useClient()
|
||||||
const headers = useClientHeader({ authorization: 'asdada' })
|
const headers = useClientHeaders()
|
||||||
const { data: todos, pending, error, refresh } = await useAsyncQuery(['getTodos'])
|
const { data: todos, pending, error, refresh } = await useAsyncQuery(['getTodos'])
|
||||||
|
|
||||||
console.log(headers.value)
|
|
||||||
|
|
||||||
const addHeader = () => {
|
const addHeader = () => {
|
||||||
// headers.value.cookie = 'counter=69'
|
// headers.value.cookie = 'counter=69'
|
||||||
console.log(headers.value)
|
console.log(headers.value)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
imports.push(
|
imports.push(
|
||||||
{ name: 'useClient', from: join(runtimeDir, 'client') },
|
{ name: 'useClient', from: join(runtimeDir, 'client') },
|
||||||
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
|
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
|
||||||
{ name: 'useClientHeader', from: join(runtimeDir, 'client') },
|
{ name: 'useClientHeaders', from: join(runtimeDir, 'client') },
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ import type {
|
|||||||
import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProcedureOutput } from '@trpc/server'
|
import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProcedureOutput } from '@trpc/server'
|
||||||
import type { TRPCClient, TRPCClientErrorLike } from '@trpc/client'
|
import type { TRPCClient, TRPCClientErrorLike } from '@trpc/client'
|
||||||
import { objectHash } from 'ohash'
|
import { objectHash } from 'ohash'
|
||||||
import type { MaybeRef } from '@vueuse/core'
|
import type { Ref } from 'vue'
|
||||||
import { useStorage } from '@vueuse/core'
|
import { useStorage } from '@vueuse/core'
|
||||||
import { useAsyncData, useNuxtApp, useState } from '#app'
|
import { useAsyncData, useNuxtApp, useState } from '#app'
|
||||||
import type { router } from '~/server/trpc'
|
import type { router } from '~/server/trpc'
|
||||||
|
|
||||||
|
type MaybeRef<T> = T | Ref<T>
|
||||||
|
|
||||||
type AppRouter = typeof router
|
type AppRouter = typeof router
|
||||||
|
|
||||||
export type inferProcedures<
|
export type inferProcedures<
|
||||||
@@ -29,13 +31,6 @@ export type TError = TRPCClientErrorLike<AppRouter>
|
|||||||
|
|
||||||
export type TQueryValues = inferProcedures<AppRouter['_def']['queries']>
|
export type TQueryValues = inferProcedures<AppRouter['_def']['queries']>
|
||||||
|
|
||||||
/**
|
|
||||||
* Additional header properties to pass to tRPC client.
|
|
||||||
*
|
|
||||||
* @see https://trpc.io/docs/vanilla
|
|
||||||
* @param pathAndInput tRPC client path and input.
|
|
||||||
* @param options Options to pass to useAsyncData.
|
|
||||||
*/
|
|
||||||
export async function useAsyncQuery<
|
export async function useAsyncQuery<
|
||||||
TPath extends keyof TQueryValues & string,
|
TPath extends keyof TQueryValues & string,
|
||||||
TOutput extends TQueryValues[TPath]['output'] = TQueryValues[TPath]['output'],
|
TOutput extends TQueryValues[TPath]['output'] = TQueryValues[TPath]['output'],
|
||||||
@@ -68,22 +63,11 @@ export async function useAsyncQuery<
|
|||||||
} as any
|
} as any
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* tRPC Client.
|
|
||||||
*
|
|
||||||
* @see https://trpc.io/docs/vanilla
|
|
||||||
*/
|
|
||||||
export function useClient(): TRPCClient<AppRouter> {
|
export function useClient(): TRPCClient<AppRouter> {
|
||||||
const { $client } = useNuxtApp()
|
const { $client } = useNuxtApp()
|
||||||
return $client
|
return $client
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export function useClientHeaders(initialValue: MaybeRef<Record<string, any>> = {}): Ref<Record<string, any>> {
|
||||||
* Additional header properties to pass to tRPC client.
|
return useStorage('trpc-nuxt-header', initialValue)
|
||||||
*
|
|
||||||
* @see https://github.com/trpc/trpc/discussions/1686
|
|
||||||
* @param initialValue
|
|
||||||
*/
|
|
||||||
export function useClientHeaders(initialValue?: MaybeRef<Record<string, any>>) {
|
|
||||||
return useStorage('trpc-nuxt-header', initialValue || {})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import * as trpc from '@trpc/client'
|
import * as trpc from '@trpc/client'
|
||||||
|
import { unref } from 'vue'
|
||||||
|
import { useClientHeaders } from './client'
|
||||||
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
||||||
import type { router } from '~/server/trpc'
|
import type { router } from '~/server/trpc'
|
||||||
|
|
||||||
@@ -7,17 +9,12 @@ declare type AppRouter = typeof router
|
|||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin((nuxtApp) => {
|
||||||
const config = useRuntimeConfig().public.trpc
|
const config = useRuntimeConfig().public.trpc
|
||||||
const headers = useRequestHeaders()
|
const headers = useRequestHeaders()
|
||||||
|
const otherHeaders = useClientHeaders()
|
||||||
const client = trpc.createTRPCClient<AppRouter>({
|
const client = trpc.createTRPCClient<AppRouter>({
|
||||||
url: `${config.baseURL}${config.trpcURL}`,
|
url: `${config.baseURL}${config.trpcURL}`,
|
||||||
headers: () => {
|
headers: () => {
|
||||||
let otherHeaders = {}
|
|
||||||
if (!process.server) {
|
|
||||||
const key = 'trpc-nuxt-header'
|
|
||||||
otherHeaders = JSON.parse(localStorage.getItem(key) || JSON.stringify({}))
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...otherHeaders,
|
...unref(otherHeaders),
|
||||||
...headers,
|
...headers,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user