Files
fullstack-starter/src/orpc/client.ts
imbytecat 1d8479c063 refactor: 统一客户端类型定义并增强类型一致性
- 更新客户端类型以使用定义的 APIClient 类型并确保类型一致性
- 添加类型定义以导出基于路由配置的客户端类型。
2026-01-18 03:26:26 +08:00

28 lines
853 B
TypeScript

import { createORPCClient } from '@orpc/client'
import { RPCLink } from '@orpc/client/fetch'
import { createRouterClient, type RouterClient } from '@orpc/server'
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
import { createIsomorphicFn } from '@tanstack/react-start'
import { getRequestHeaders } from '@tanstack/react-start/server'
import { router } from './router'
import type { APIClient } from './types'
const getORPCClient = createIsomorphicFn()
.server(() =>
createRouterClient(router, {
context: () => ({
headers: getRequestHeaders(),
}),
}),
)
.client(() => {
const link = new RPCLink({
url: `${window.location.origin}/api/rpc`,
})
return createORPCClient<APIClient>(link)
})
const client: APIClient = getORPCClient()
export const orpc = createTanstackQueryUtils(client)