HTTP 批量链接
httpBatchLink 是一个 终止链接,它将一组单独的 tRPC 操作批处理为发送到单个 tRPC 过程的单个 HTTP 请求。
¥httpBatchLink is a terminating link that batches an array of individual tRPC operations into a single HTTP request that's sent to a single tRPC procedure.
用法
¥Usage
你可以将 httpBatchLink 导入并添加到 links 数组中,如下所示:
¥You can import and add the httpBatchLink to the links array as such:
client/index.tstsimport { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpBatchLink({url: 'http://localhost:3000',}),// transformer,],});
client/index.tstsimport { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpBatchLink({url: 'http://localhost:3000',}),// transformer,],});
之后,你可以通过在 Promise.all.txt 文件中设置所有程序来使用批处理。下面的代码将生成一个 HTTP 请求,并在服务器上生成一个数据库查询:
¥After that, you can make use of batching by setting all your procedures in a Promise.all. The code below will produce exactly one HTTP request and on the server exactly one database query:
tsconst somePosts = await Promise.all([trpc.post.byId.query(1),trpc.post.byId.query(2),trpc.post.byId.query(3),]);
tsconst somePosts = await Promise.all([trpc.post.byId.query(1),trpc.post.byId.query(2),trpc.post.byId.query(3),]);
httpBatchLink 选项
¥httpBatchLink Options
httpBatchLink 函数采用具有 HTTPBatchLinkOptions 形状的选项对象。
¥The httpBatchLink function takes an options object that has the HTTPBatchLinkOptions shape.
tsexport interface HTTPBatchLinkOptions extends HTTPLinkOptions {/*** Maximum length of HTTP URL allowed before operations are split into multiple requests* @default Infinity*/maxURLLength?: number;/*** Maximum number of operations allowed in a single batch request* @default Infinity*/maxItems?: number;}export interface HTTPLinkOptions {url: string;/*** Add ponyfill for fetch*/fetch?: typeof fetch;/*** Add ponyfill for AbortController*/AbortController?: typeof AbortController | null;/*** Data transformer* @see https://trpc.nodejs.cn/docs/data-transformers**/transformer?: DataTransformerOptions;/*** Headers to be set on outgoing requests or a callback that of said headers* @see http://trpc.nodejs.cn/docs/header*/headers?:| HTTPHeaders| ((opts: { opList: Operation[] }) => HTTPHeaders | Promise<HTTPHeaders>);}
tsexport interface HTTPBatchLinkOptions extends HTTPLinkOptions {/*** Maximum length of HTTP URL allowed before operations are split into multiple requests* @default Infinity*/maxURLLength?: number;/*** Maximum number of operations allowed in a single batch request* @default Infinity*/maxItems?: number;}export interface HTTPLinkOptions {url: string;/*** Add ponyfill for fetch*/fetch?: typeof fetch;/*** Add ponyfill for AbortController*/AbortController?: typeof AbortController | null;/*** Data transformer* @see https://trpc.nodejs.cn/docs/data-transformers**/transformer?: DataTransformerOptions;/*** Headers to be set on outgoing requests or a callback that of said headers* @see http://trpc.nodejs.cn/docs/header*/headers?:| HTTPHeaders| ((opts: { opList: Operation[] }) => HTTPHeaders | Promise<HTTPHeaders>);}
设置最大 URL 长度
¥Setting a maximum URL length
发送批量请求时,有时 URL 可能会变得太大,导致 HTTP 错误,例如 413 Payload Too Large、414 URI Too Long 和 404 Not Found。maxURLLength 选项将限制可以批量发送的请求数量。
¥When sending batch requests, sometimes the URL can become too large causing HTTP errors like 413 Payload Too Large, 414 URI Too Long, and 404 Not Found. The maxURLLength option will limit the number of requests that can be sent together in a batch.
另一种方法是
¥An alternative way of doing this is to
client/index.tstsimport { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpBatchLink({url: 'http://localhost:3000',maxURLLength: 2083, // a suitable size// alternatively, you can make all RPC-calls to be called with POST// methodOverride: 'POST',}),],});
client/index.tstsimport { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpBatchLink({url: 'http://localhost:3000',maxURLLength: 2083, // a suitable size// alternatively, you can make all RPC-calls to be called with POST// methodOverride: 'POST',}),],});
禁用请求批处理
¥Disabling request batching
1. 在你的服务器上禁用 batching:
¥ Disable batching on your server:
server.tstsimport { createHTTPServer } from '@trpc/server/adapters/standalone';createHTTPServer({// [...]// 👇 disable batchingallowBatching: false,});
server.tstsimport { createHTTPServer } from '@trpc/server/adapters/standalone';createHTTPServer({// [...]// 👇 disable batchingallowBatching: false,});
或者,如果你使用的是 Next.js:
¥or, if you're using Next.js:
pages/api/trpc/[trpc].tstsexport default trpcNext.createNextApiHandler({// [...]// 👇 disable batchingallowBatching: false,});
pages/api/trpc/[trpc].tstsexport default trpcNext.createNextApiHandler({// [...]// 👇 disable batchingallowBatching: false,});
2. 在 tRPC 客户端中将 httpBatchLink 替换为 httpLink
¥ Replace httpBatchLink with httpLink in your tRPC Client
client/index.tstsimport { createTRPCClient, httpLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',}),],});
client/index.tstsimport { createTRPCClient, httpLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',}),],});
或者,如果你使用的是 Next.js:
¥or, if you're using Next.js:
utils/trpc.tstsximport type { AppRouter } from '@/server/routers/app';import { httpLink } from '@trpc/client';import { createTRPCNext } from '@trpc/next';export const trpc = createTRPCNext<AppRouter>({config() {return {links: [httpLink({url: '/api/trpc',}),],};},});
utils/trpc.tstsximport type { AppRouter } from '@/server/routers/app';import { httpLink } from '@trpc/client';import { createTRPCNext } from '@trpc/next';export const trpc = createTRPCNext<AppRouter>({config() {return {links: [httpLink({url: '/api/trpc',}),],};},});