/
githubmirror
/
trpc
Обзор
Документация
Войти
/
githubmirror
/
trpc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
www/docs/client/vanilla/setup.mdx
97 строк
3 KB
Nick Lucas
feat: Tanstack Intent Skills (#7252)
21 мар 2026, 02:18
Не верифицирован
21 мар 2026, 02:18
e39a654
Код
Авторство
О чём код?
--- id: setup title: Set up a tRPC Client sidebar_label: Setup slug: /client/vanilla/setup --- import TabItem from '@theme/TabItem'; import Tabs from '@theme/Tabs'; ### 1. Install the tRPC Client library Use your preferred package manager to install the `@trpc/client` library, and also install `@trpc/server` which contains some required types. import { InstallSnippet } from '@site/src/components/InstallSnippet'; <InstallSnippet pkgs="@trpc/server @trpc/client" /> :::tip AI Agents If you use an AI coding agent, install tRPC skills for better code generation: ```bash npx @tanstack/intent@latest install ``` ::: ### 2. Import your App Router import ImportAppRouter from '../../partials/_import-approuter.mdx'; <ImportAppRouter /> ### 3. Initialize the tRPC client Create a tRPC client with the `createTRPCClient` method, and add a `links` array with a [terminating link](../links/overview.md#the-terminating-link) pointing at your API. To learn more about tRPC links, [click here](../links/overview.md). ```ts twoslash title='client.ts' // @filename: server.ts import { initTRPC } from '@trpc/server'; const t = initTRPC.create(); export const appRouter = t.router({}); export type AppRouter = typeof appRouter; // @filename: client.ts declare function getAuthCookie(): string; // ---cut--- import { createTRPCClient, httpBatchLink } from '@trpc/client'; import type { AppRouter } from './server'; const client = createTRPCClient<AppRouter>({ links: [ httpBatchLink({ url: 'http://localhost:3000/trpc', // You can pass any HTTP headers you wish here async headers() { return { authorization: getAuthCookie(), }; }, }), ], }); ``` ### 4. Use the tRPC Client Under the hood this creates a typed [JavaScript Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) which allows you to interact with your tRPC API in a fully type-safe way: ```ts twoslash title='client.ts' // @target: esnext // @filename: server.ts import { initTRPC } from '@trpc/server'; import { z } from 'zod'; const t = initTRPC.create(); const appRouter = t.router({ getUser: t.procedure.input(z.string()).query(({ input }) => ({ id: input, name: 'Bilbo' })), createUser: t.procedure.input(z.object({ name: z.string() })).mutation(({ input }) => ({ id: 'id_frodo', ...input })), }); export type AppRouter = typeof appRouter; // @filename: client.ts import { createTRPCClient, httpBatchLink } from '@trpc/client'; import type { AppRouter } from './server'; const client = createTRPCClient<AppRouter>({ links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })], }); // ---cut--- const bilbo = await client.getUser.query('id_bilbo'); // => { id: 'id_bilbo', name: 'Bilbo' }; const frodo = await client.createUser.mutate({ name: 'Frodo' }); // => { id: 'id_frodo', name: 'Frodo' }; ``` You're all set!