Skip to main content
Version: 11.x

本地链接

localLink终止链接 的一个子类,它允许你直接在应用中进行 tRPC 过程调用,而无需通过 HTTP。

¥localLink is a terminating link that allows you to make tRPC procedure calls directly in your application without going through HTTP.

信息

我们已将其前缀为 unstable_,因为它是一个新 API,但你可以安全地使用它!阅读更多

¥We have prefixed this as unstable_ as it's a new API, but you're safe to use it! Read more.

用法

¥Usage

tsx
import { createTRPCClient, unstable_localLink } from '@trpc/client';
import type { AppRouter } from '../server';
const client = createTRPCClient<AppRouter>({
links: [
unstable_localLink({
router: appRouter,
createContext: async () => {
// Create your context here
return {};
},
onError: (opts) => {
// Log errors here, similarly to how you would in an API route
console.error('Error:', opts.error);
},
}),
],
});
tsx
import { createTRPCClient, unstable_localLink } from '@trpc/client';
import type { AppRouter } from '../server';
const client = createTRPCClient<AppRouter>({
links: [
unstable_localLink({
router: appRouter,
createContext: async () => {
// Create your context here
return {};
},
onError: (opts) => {
// Log errors here, similarly to how you would in an API route
console.error('Error:', opts.error);
},
}),
],
});

特性

¥Features

  • 直接过程调用且无 HTTP 开销

    ¥Direct procedure calls without HTTP overhead

  • 完全支持查询、修改和订阅。

    ¥Full support for queries, mutations, and subscriptions

  • 自动错误处理和转换

    ¥Automatic error handling and transformation

  • 支持中止信号

    ¥Support for abort signals

  • 类型安全的上下文创建

    ¥Type-safe context creation

选项

¥Options

localLink 接受以下选项:

¥The localLink accepts the following options:

ts
type LocalLinkOptions<TRouter extends AnyRouter> = {
router: TRouter;
createContext: () => Promise<inferRouterContext<TRouter>>;
onError?: (opts: ErrorHandlerOptions<inferRouterContext<TRouter>>) => void;
} & TransformerOptions<inferClientTypes<TRouter>>;
ts
type LocalLinkOptions<TRouter extends AnyRouter> = {
router: TRouter;
createContext: () => Promise<inferRouterContext<TRouter>>;
onError?: (opts: ErrorHandlerOptions<inferRouterContext<TRouter>>) => void;
} & TransformerOptions<inferClientTypes<TRouter>>;

router

用于过程调用的 tRPC 路由实例。

¥The tRPC router instance to use for procedure calls.

createContext

为每个过程调用创建上下文的函数。每次请求都会调用此链接,并应返回一个解析为上下文对象的 Promise。

¥A function that creates the context for each procedure call. This is called for each request and should return a promise that resolves to the context object.

onError

可选的错误处理程序,在过程调用期间发生错误时调用。它接收错误、操作类型、路径、输入和上下文。

¥An optional error handler that is called when an error occurs during a procedure call. It receives the error, operation type, path, input, and context.

transformer

用于序列化/反序列化数据的可选输入/输出转换器。

¥Optional input/output transformers for serialization/deserialization of data.

注意

¥Notes

  • 建议在需要直接进行过程调用而无需 HTTP 的情况下使用此链接。

    ¥It's recommended to use this link in scenarios where you need direct procedure calls without HTTP

  • 对于大多数客户端应用,你应该使用 httpLink 或其他基于 HTTP 的链接。

    ¥For most client-side applications, you should use the httpLink or other HTTP-based links instead

  • 此链接支持所有 tRPC 功能,包括查询、修改和订阅。

    ¥The link supports all tRPC features including queries, mutations, and subscriptions

  • 错误处理和转换将自动进行,就像基于 HTTP 的链接一样

    ¥Error handling and transformation are handled automatically, just like with HTTP-based links