HTTP 批量链接
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
httpBatchLink 是一个终止链接,它可将多个独立的 tRPC 操作批处理成单个 HTTP 请求,并发送到单个 tRPC 过程。
用法
你可以按如下方式导入并将 httpBatchLink 添加到 links 数组中:
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 来利用批处理功能。以下代码将产生仅一次 HTTP 请求,在服务器端执行仅一次数据库查询:
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 函数接受符合 HTTPBatchLinkOptions 结构的配置对象。
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.io/docs/data-transformers**/transformer?: DataTransformerOptions;/*** Headers to be set on outgoing requests or a callback that of said headers* @see http://trpc.io/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.io/docs/data-transformers**/transformer?: DataTransformerOptions;/*** Headers to be set on outgoing requests or a callback that of said headers* @see http://trpc.io/docs/header*/headers?:| HTTPHeaders| ((opts: { opList: Operation[] }) => HTTPHeaders | Promise<HTTPHeaders>);}
设置最大 URL 长度
发送批量请求时,URL 可能因过长而触发 HTTP 错误,例如 413 Payload Too Large、414 URI Too Long 或 404 Not Found。maxURLLength 选项可限制单个批次中包含的请求数量。
另一种实现方式是
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',}),],});
禁用请求批处理
1. 在服务器端禁用 batching
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:
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
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:
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',}),],};},});