mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 20:19:33 +01:00
feat: use globalThis.$fetch to call API handler directly on server-side
This commit is contained in:
@@ -5,7 +5,7 @@ import Module from '../src/module'
|
||||
export default defineNuxtConfig({
|
||||
modules: [Module],
|
||||
runtimeConfig: {
|
||||
baseURL: 'http://localhost:3000',
|
||||
baseURL: '',
|
||||
},
|
||||
typescript: {
|
||||
strict: true,
|
||||
|
||||
@@ -16,7 +16,7 @@ export default defineNuxtModule<ModuleOptions>({
|
||||
configKey: 'trpc',
|
||||
},
|
||||
defaults: {
|
||||
baseURL: 'http://localhost:3000',
|
||||
baseURL: '',
|
||||
endpoint: '/trpc',
|
||||
},
|
||||
async setup(options, nuxt) {
|
||||
|
||||
@@ -10,14 +10,21 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
const config = useRuntimeConfig().public.trpc
|
||||
const headers = useRequestHeaders()
|
||||
const otherHeaders = useClientHeaders()
|
||||
|
||||
const baseURL = process.server ? '' : config.baseURL
|
||||
const client = trpc.createTRPCClient<AppRouter>({
|
||||
url: `${config.baseURL}${config.endpoint}`,
|
||||
url: `${baseURL}${config.endpoint}`,
|
||||
headers: () => {
|
||||
return {
|
||||
...unref(otherHeaders),
|
||||
...headers,
|
||||
}
|
||||
},
|
||||
fetch: (input, options) =>
|
||||
globalThis.$fetch.raw(input.toString(), options).then(response => ({
|
||||
...response,
|
||||
json: () => Promise.resolve(response._data),
|
||||
}))
|
||||
})
|
||||
|
||||
nuxtApp.provide('client', client)
|
||||
|
||||
Reference in New Issue
Block a user