Compare commits

...

10 Commits

Author SHA1 Message Date
Robert Soriano
e9c5307e23 release v0.1.22 2022-05-27 01:10:10 -07:00
Robert Soriano
bbdabf544c fix: type inference bug with vueuse 2022-05-27 01:10:06 -07:00
Robert Soriano
aed10ac5b8 release v0.1.21 2022-05-27 00:56:58 -07:00
Robert Soriano
4b2c714658 fix: remove module export 2022-05-27 00:56:53 -07:00
Robert Soriano
43e9fefdbd release v0.1.20 2022-05-26 13:48:57 -07:00
Robert Soriano
dabda23976 export module id 2022-05-26 13:48:53 -07:00
Robert Soriano
af89f32275 release v0.1.19 2022-05-26 07:27:20 -07:00
Robert Soriano
1f27b871fb fix: unref import from vue 2022-05-26 07:21:42 -07:00
Robert Soriano
281e4c05a0 release v0.1.18 2022-05-24 10:01:23 -07:00
Robert Soriano
38ac520b97 fix: server storage and incorrect imports 2022-05-24 10:01:19 -07:00
5 changed files with 12 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
{
"name": "trpc-nuxt",
"type": "module",
"version": "0.1.17",
"version": "0.1.22",
"packageManager": "pnpm@7.1.1",
"license": "MIT",
"main": "./dist/module.cjs",

View File

@@ -1,10 +1,8 @@
<script setup lang="ts">
const client = useClient()
const headers = useClientHeader({ authorization: 'asdada' })
const headers = useClientHeaders()
const { data: todos, pending, error, refresh } = await useAsyncQuery(['getTodos'])
console.log(headers.value)
const addHeader = () => {
// headers.value.cookie = 'counter=69'
console.log(headers.value)

View File

@@ -38,7 +38,7 @@ export default defineNuxtModule<ModuleOptions>({
imports.push(
{ name: 'useClient', from: join(runtimeDir, 'client') },
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
{ name: 'useClientHeader', from: join(runtimeDir, 'client') },
{ name: 'useClientHeaders', from: join(runtimeDir, 'client') },
)
})

View File

@@ -8,11 +8,12 @@ import type {
import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProcedureOutput } from '@trpc/server'
import type { TRPCClient, TRPCClientErrorLike } from '@trpc/client'
import { objectHash } from 'ohash'
import type { MaybeRef } from '@vueuse/core'
import { useStorage } from '@vueuse/core'
import type { Ref } from 'vue'
import { useAsyncData, useNuxtApp, useState } from '#app'
import type { router } from '~/server/trpc'
type MaybeRef<T> = T | Ref<T>
type AppRouter = typeof router
export type inferProcedures<
@@ -29,13 +30,6 @@ export type TError = TRPCClientErrorLike<AppRouter>
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<
TPath extends keyof TQueryValues & string,
TOutput extends TQueryValues[TPath]['output'] = TQueryValues[TPath]['output'],
@@ -68,22 +62,11 @@ export async function useAsyncQuery<
} as any
}
/**
* tRPC Client.
*
* @see https://trpc.io/docs/vanilla
*/
export function useClient(): TRPCClient<AppRouter> {
const { $client } = useNuxtApp()
return $client
}
/**
* Additional header properties to pass to tRPC client.
*
* @see https://github.com/trpc/trpc/discussions/1686
* @param initialValue
*/
export function useClientHeaders(initialValue?: MaybeRef<Record<string, any>>) {
return useStorage('trpc-nuxt-header', initialValue || {})
export function useClientHeaders(initialValue: MaybeRef<Record<string, any>> = {}): Ref<Record<string, any>> {
return useStorage('trpc-nuxt-header', initialValue)
}

View File

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