mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-25 17:30:33 +01:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eea5733dcd | ||
|
|
71bbbf2b86 | ||
|
|
2b57ab8791 | ||
|
|
f8edd769f0 | ||
|
|
419ef34de6 | ||
|
|
30c76b5859 | ||
|
|
2575beae5d | ||
|
|
b09d1af30d | ||
|
|
610e441db7 | ||
|
|
959b370729 | ||
|
|
c1c4e67694 | ||
|
|
779221d9e6 |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "trpc-nuxt",
|
"name": "trpc-nuxt",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.1.8",
|
"version": "0.1.14",
|
||||||
"packageManager": "pnpm@7.1.1",
|
"packageManager": "pnpm@7.1.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/module.cjs",
|
"main": "./dist/module.cjs",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'url'
|
|||||||
import { join, resolve } from 'pathe'
|
import { join, resolve } from 'pathe'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
|
|
||||||
import { addPluginTemplate, addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
|
import { addPlugin, addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
|
||||||
|
|
||||||
export interface ModuleOptions {
|
export interface ModuleOptions {
|
||||||
baseURL: string
|
baseURL: string
|
||||||
@@ -20,7 +20,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
},
|
},
|
||||||
async setup(options, nuxt) {
|
async setup(options, nuxt) {
|
||||||
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
|
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
|
||||||
nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-client', '#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.rootDir, 'server/trpc')
|
||||||
@@ -33,7 +33,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
|
|
||||||
nuxt.hook('autoImports:extend', (imports) => {
|
nuxt.hook('autoImports:extend', (imports) => {
|
||||||
imports.push(
|
imports.push(
|
||||||
{ name: 'useClient', from: '#build/trpc-client' },
|
{ name: 'useClient', from: join(runtimeDir, 'client') },
|
||||||
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
|
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -43,26 +43,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
handler: handlerPath,
|
handler: handlerPath,
|
||||||
})
|
})
|
||||||
|
|
||||||
addTemplate({
|
addPlugin(resolve(runtimeDir, 'plugin'))
|
||||||
filename: 'trpc-client.ts',
|
|
||||||
write: true,
|
|
||||||
getContents() {
|
|
||||||
return `
|
|
||||||
export const useClient = () => {
|
|
||||||
const { $client } = useNuxtApp()
|
|
||||||
return $client
|
|
||||||
}
|
|
||||||
`
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
addPluginTemplate({
|
|
||||||
src: resolve(runtimeDir, 'plugin.ts'),
|
|
||||||
write: true,
|
|
||||||
options: {
|
|
||||||
url: `${finalConfig.baseURL}${finalConfig.trpcURL}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
filename: 'trpc-handler.ts',
|
filename: 'trpc-handler.ts',
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import type {
|
|||||||
_Transform,
|
_Transform,
|
||||||
} from 'nuxt/dist/app/composables/asyncData'
|
} from 'nuxt/dist/app/composables/asyncData'
|
||||||
import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProcedureOutput } from '@trpc/server'
|
import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProcedureOutput } from '@trpc/server'
|
||||||
import type { TRPCClientErrorLike } from '@trpc/client'
|
import type { TRPCClient, TRPCClientErrorLike } from '@trpc/client'
|
||||||
import { objectHash } from 'ohash'
|
import { objectHash } from 'ohash'
|
||||||
import { useAsyncData, useState } from '#app'
|
import { useAsyncData, useNuxtApp, useState } from '#app'
|
||||||
// @ts-expect-error: Resolved by Nuxt
|
// @ts-expect-error: Resolved by Nuxt
|
||||||
import type { router } from '~/server/trpc'
|
import type { router } from '~/server/trpc'
|
||||||
|
|
||||||
@@ -58,3 +58,8 @@ export async function useAsyncQuery<
|
|||||||
error: serverError,
|
error: serverError,
|
||||||
} as any
|
} as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useClient(): TRPCClient<AppRouter> {
|
||||||
|
const { $client } = useNuxtApp()
|
||||||
|
return $client
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
import * as trpc from '@trpc/client'
|
import * as trpc from '@trpc/client'
|
||||||
import { defineNuxtPlugin, useRequestHeaders } from '#app'
|
// @ts-expect-error: Resolved by Nuxt
|
||||||
|
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
||||||
import type { router } from '~/server/trpc'
|
import type { router } from '~/server/trpc'
|
||||||
|
|
||||||
const options = JSON.parse('<%= JSON.stringify(options) %>')
|
type AppRouter = typeof router
|
||||||
|
|
||||||
|
const config = useRuntimeConfig().public.trpc
|
||||||
|
const client = trpc.createTRPCClient<AppRouter>({
|
||||||
|
url: `${config.baseURL}${config.trpcURL}`,
|
||||||
|
headers: useRequestHeaders(),
|
||||||
|
})
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
const client = trpc.createTRPCClient<typeof router>({
|
|
||||||
url: options.url as string,
|
|
||||||
headers: useRequestHeaders(),
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
provide: {
|
provide: {
|
||||||
client,
|
client,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
declare module '#app' {
|
||||||
|
interface NuxtApp {
|
||||||
|
$client: trpc.TRPCClient<AppRouter>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user