HTTP 链接
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
httpLink 是一个终止链接,用于通过 HTTP 将 tRPC 操作发送到 tRPC 过程。
httpLink 同时支持 POST 和 GET 请求。
用法
你可以按如下方式导入 httpLink 并将其添加到 links 数组中:
client/index.tstsimport { createTRPCProxyClient, httpLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCProxyClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',}),],});
client/index.tstsimport { createTRPCProxyClient, httpLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCProxyClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',}),],});
httpLink 选项
httpLink 函数接受一个符合 HTTPLinkOptions 类型的配置对象。
tsexport interface HTTPLinkOptions {url: string;/*** Add ponyfill for fetch*/fetch?: typeof fetch;/*** Add ponyfill for AbortController*/AbortController?: typeof AbortController | null;/*** Headers to be set on outgoing requests or a callback that of said headers* @see http://trpc.io/docs/v10/header*/headers?:| HTTPHeaders| ((opts: { op: Operation }) => HTTPHeaders | Promise<HTTPHeaders>);}
tsexport interface HTTPLinkOptions {url: string;/*** Add ponyfill for fetch*/fetch?: typeof fetch;/*** Add ponyfill for AbortController*/AbortController?: typeof AbortController | null;/*** Headers to be set on outgoing requests or a callback that of said headers* @see http://trpc.io/docs/v10/header*/headers?:| HTTPHeaders| ((opts: { op: Operation }) => HTTPHeaders | Promise<HTTPHeaders>);}
参考文档
你可以在 GitHub 上查看该链接的源代码。