mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-26 01:40:31 +01:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
977a9e1465 | ||
|
|
2620379e02 | ||
|
|
e4f42d5322 | ||
|
|
ad28a9124e | ||
|
|
9c8509f79c | ||
|
|
2ce29137ce | ||
|
|
e9c5307e23 | ||
|
|
bbdabf544c | ||
|
|
aed10ac5b8 | ||
|
|
4b2c714658 | ||
|
|
43e9fefdbd | ||
|
|
dabda23976 | ||
|
|
af89f32275 | ||
|
|
1f27b871fb |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "trpc-nuxt",
|
"name": "trpc-nuxt",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.1.18",
|
"version": "0.1.24",
|
||||||
"packageManager": "pnpm@7.1.1",
|
"packageManager": "pnpm@7.1.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/module.cjs",
|
"main": "./dist/module.cjs",
|
||||||
@@ -37,6 +37,7 @@
|
|||||||
"@trpc/server": "^9.23.2",
|
"@trpc/server": "^9.23.2",
|
||||||
"@vueuse/core": "^8.5.0",
|
"@vueuse/core": "^8.5.0",
|
||||||
"@vueuse/nuxt": "^8.5.0",
|
"@vueuse/nuxt": "^8.5.0",
|
||||||
|
"dedent": "^0.7.0",
|
||||||
"defu": "^6.0.0",
|
"defu": "^6.0.0",
|
||||||
"h3": "^0.7.8",
|
"h3": "^0.7.8",
|
||||||
"pathe": "^0.3.0",
|
"pathe": "^0.3.0",
|
||||||
|
|||||||
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
@@ -13,6 +13,7 @@ importers:
|
|||||||
'@vueuse/core': ^8.5.0
|
'@vueuse/core': ^8.5.0
|
||||||
'@vueuse/nuxt': ^8.5.0
|
'@vueuse/nuxt': ^8.5.0
|
||||||
bumpp: ^7.1.1
|
bumpp: ^7.1.1
|
||||||
|
dedent: ^0.7.0
|
||||||
defu: ^6.0.0
|
defu: ^6.0.0
|
||||||
eslint: ^8.14.0
|
eslint: ^8.14.0
|
||||||
h3: ^0.7.8
|
h3: ^0.7.8
|
||||||
@@ -30,6 +31,7 @@ importers:
|
|||||||
'@trpc/server': 9.23.4
|
'@trpc/server': 9.23.4
|
||||||
'@vueuse/core': 8.5.0
|
'@vueuse/core': 8.5.0
|
||||||
'@vueuse/nuxt': 8.5.0
|
'@vueuse/nuxt': 8.5.0
|
||||||
|
dedent: 0.7.0
|
||||||
defu: 6.0.0
|
defu: 6.0.0
|
||||||
h3: 0.7.8
|
h3: 0.7.8
|
||||||
pathe: 0.3.0
|
pathe: 0.3.0
|
||||||
@@ -2077,6 +2079,10 @@ packages:
|
|||||||
engines: {node: '>=0.10'}
|
engines: {node: '>=0.10'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/dedent/0.7.0:
|
||||||
|
resolution: {integrity: sha512-Q6fKUPqnAHAyhiUgFU7BUzLiv0kd8saH9al7tnu5Q/okj6dnupxyTgFIBjVzJATdfIAm9NAsvXNzjaKa+bxVyA==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/deep-extend/0.6.0:
|
/deep-extend/0.6.0:
|
||||||
resolution: {integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==}
|
resolution: {integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==}
|
||||||
engines: {node: '>=4.0.0'}
|
engines: {node: '>=4.0.0'}
|
||||||
@@ -3794,7 +3800,7 @@ packages:
|
|||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/is-module/1.0.0:
|
/is-module/1.0.0:
|
||||||
resolution: {integrity: sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE=}
|
resolution: {integrity: sha512-51ypPSPCoTEIN9dy5Oy+h4pShgJmPCygKfyRCISBI+JoWT/2oJvK8QPxmwv7b/p239jXrm9M1mlQbyKJ5A152g==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/is-negative-zero/2.0.2:
|
/is-negative-zero/2.0.2:
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import { join, resolve } from 'pathe'
|
import { join, resolve } from 'pathe'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
|
// @ts-expect-error: No types
|
||||||
|
import dedent from 'dedent'
|
||||||
|
|
||||||
import { addPlugin, addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
|
import { addPlugin, addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
|
||||||
|
|
||||||
@@ -23,7 +25,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-handler')
|
nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-handler')
|
||||||
|
|
||||||
const handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts')
|
const handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts')
|
||||||
const trpcOptionsPath = join(nuxt.options.rootDir, 'server/trpc')
|
const trpcOptionsPath = join(nuxt.options.srcDir, 'server/trpc')
|
||||||
|
|
||||||
// Add vueuse
|
// Add vueuse
|
||||||
nuxt.options.modules.push('@vueuse/nuxt')
|
nuxt.options.modules.push('@vueuse/nuxt')
|
||||||
@@ -53,7 +55,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
filename: 'trpc-handler.ts',
|
filename: 'trpc-handler.ts',
|
||||||
write: true,
|
write: true,
|
||||||
getContents() {
|
getContents() {
|
||||||
return `
|
return dedent`
|
||||||
import { createTRPCHandler } from 'trpc-nuxt/api'
|
import { createTRPCHandler } from 'trpc-nuxt/api'
|
||||||
import * as functions from '${trpcOptionsPath}'
|
import * as functions from '${trpcOptionsPath}'
|
||||||
|
|
||||||
|
|||||||
@@ -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,5 @@
|
|||||||
import * as trpc from '@trpc/client'
|
import * as trpc from '@trpc/client'
|
||||||
|
import { unref } from 'vue'
|
||||||
import { useClientHeaders } from './client'
|
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'
|
||||||
|
|||||||
Reference in New Issue
Block a user