diff --git a/apps/server/src/api/index.ts b/apps/server/src/api/index.ts deleted file mode 100644 index d276e39..0000000 --- a/apps/server/src/api/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { orpc } from './client' -export * from './types' diff --git a/apps/server/src/api/server.ts b/apps/server/src/api/server.ts index 34af679..23e220b 100644 --- a/apps/server/src/api/server.ts +++ b/apps/server/src/api/server.ts @@ -1,7 +1,5 @@ import { implement } from '@orpc/server' import { contract } from './contract' +import type { Context } from './types' -// biome-ignore lint/complexity/noBannedTypes: 暂无 context -export type ORPCContext = {} - -export const os = implement(contract).$context() +export const os = implement(contract).$context() diff --git a/apps/server/src/api/types.ts b/apps/server/src/api/types.ts index e3e814c..da71390 100644 --- a/apps/server/src/api/types.ts +++ b/apps/server/src/api/types.ts @@ -5,6 +5,8 @@ import type { } from '@orpc/contract' import type { contract } from './contract' +// biome-ignore lint/complexity/noBannedTypes: 暂无 context +export type Context = {} export type Contract = typeof contract export type RouterClient = ContractRouterClient export type RouterInputs = InferContractRouterInputs diff --git a/apps/server/src/lib/orpc/client.ts b/apps/server/src/lib/orpc/client.ts new file mode 100644 index 0000000..8861026 --- /dev/null +++ b/apps/server/src/lib/orpc/client.ts @@ -0,0 +1,24 @@ +import { createORPCClient } from '@orpc/client' +import { RPCLink } from '@orpc/client/fetch' +import { createRouterClient } from '@orpc/server' +import { createIsomorphicFn } from '@tanstack/react-start' +import { getRequestHeaders } from '@tanstack/react-start/server' +import { router } from '@/api/routers' +import type { RouterClient } from '@/api/types' + +const getORPCClient = createIsomorphicFn() + .server(() => + createRouterClient(router, { + context: () => ({ + headers: getRequestHeaders(), + }), + }), + ) + .client(() => { + const link = new RPCLink({ + url: `${window.location.origin}/api/rpc`, + }) + return createORPCClient(link) + }) + +export const client: RouterClient = getORPCClient() diff --git a/apps/server/src/api/client.ts b/apps/server/src/lib/orpc/query-client.ts similarity index 51% rename from apps/server/src/api/client.ts rename to apps/server/src/lib/orpc/query-client.ts index d011a07..8d8fe83 100644 --- a/apps/server/src/api/client.ts +++ b/apps/server/src/lib/orpc/query-client.ts @@ -1,28 +1,5 @@ -import { createORPCClient } from '@orpc/client' -import { RPCLink } from '@orpc/client/fetch' -import { createRouterClient } 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 './routers' -import type { RouterClient } from './types' - -const getORPCClient = createIsomorphicFn() - .server(() => - createRouterClient(router, { - context: () => ({ - headers: getRequestHeaders(), - }), - }), - ) - .client(() => { - const link = new RPCLink({ - url: `${window.location.origin}/api/rpc`, - }) - return createORPCClient(link) - }) - -const client: RouterClient = getORPCClient() +import { client } from './client' export const orpc = createTanstackQueryUtils(client, { experimental_defaults: { diff --git a/apps/server/src/routes/index.tsx b/apps/server/src/routes/index.tsx index dc3b074..97fd6cb 100644 --- a/apps/server/src/routes/index.tsx +++ b/apps/server/src/routes/index.tsx @@ -4,7 +4,7 @@ import { isTauri } from '@tauri-apps/api/core' import { getCurrentWindow } from '@tauri-apps/api/window' import type { ChangeEventHandler, FormEventHandler } from 'react' import { useEffect, useState } from 'react' -import { orpc } from '@/api' +import { orpc } from '@/lib/orpc/query-client' export const Route = createFileRoute('/')({ component: Todos,