Skip to main content
Version: 10.x

HTTP 链接

httpLink 是通过 HTTP 将 tRPC 操作发送到 tRPC 过程的 终止链接

¥httpLink is a terminating link that sends a tRPC operation to a tRPC procedure over HTTP.

httpLink 支持 POST 和 GET 请求。

¥httpLink supports both POST and GET requests.

用法

¥Usage

你可以将 httpLink 导入并添加到 links 数组中,如下所示:

¥You can import and add the httpLink to the links array as such:

client/index.ts
ts
import { createTRPCProxyClient, httpLink } from '@trpc/client';
import type { AppRouter } from '../server';
const client = createTRPCProxyClient<AppRouter>({
links: [
httpLink({
url: 'http://localhost:3000',
}),
],
});
client/index.ts
ts
import { createTRPCProxyClient, httpLink } from '@trpc/client';
import type { AppRouter } from '../server';
const client = createTRPCProxyClient<AppRouter>({
links: [
httpLink({
url: 'http://localhost:3000',
}),
],
});

¥httpLink Options

httpLink 函数采用具有 HTTPLinkOptions 形状的选项对象。

¥The httpLink function takes an options object that has the HTTPLinkOptions shape.

ts
export 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
* @link http://trpc.nodejs.cn/docs/v10/header
*/
headers?:
| HTTPHeaders
| ((opts: { op: Operation }) => HTTPHeaders | Promise<HTTPHeaders>);
}
ts
export 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
* @link http://trpc.nodejs.cn/docs/v10/header
*/
headers?:
| HTTPHeaders
| ((opts: { op: Operation }) => HTTPHeaders | Promise<HTTPHeaders>);
}

参考

¥Reference

你可以在 GitHub。 上查看此链接的源代码

¥You can check out the source code for this link on GitHub.