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