33 lines
1 KiB
TypeScript
33 lines
1 KiB
TypeScript
import { createTRPCReact } from '@trpc/react-query'
|
|
import { createTRPCClient, httpBatchLink } from '@trpc/client'
|
|
import type { AppRouter } from '@backend/trpc/router'
|
|
import { BASE_API_URL as DEFAULT_API_URL } from '@packages/shared'
|
|
|
|
// const BASE_API_URL = 'http://192.168.100.126:8787'
|
|
export const BASE_API_URL =
|
|
(import.meta.env?.VITE_API_URL as string | undefined) || DEFAULT_API_URL
|
|
|
|
export const trpc = createTRPCReact<AppRouter>()
|
|
|
|
export function getTrpcClient() {
|
|
return createTRPCClient<AppRouter>({
|
|
links: [
|
|
httpBatchLink({
|
|
url: `${BASE_API_URL}/api/trpc`,
|
|
headers: async () => {
|
|
try {
|
|
const token = localStorage.getItem('auth_token')
|
|
const headers: Record<string, string> = {
|
|
'caller-interface': 'web',
|
|
}
|
|
if (token) {
|
|
headers.Authorization = `Bearer ${token}`
|
|
}
|
|
return headers
|
|
} catch {}
|
|
return { 'caller-interface': 'web' }
|
|
},
|
|
}),
|
|
],
|
|
})
|
|
}
|