UNPKG

@trpc/client

Version:

The tRPC client library

71 lines (57 loc) • 1.93 kB
# HTTP Link > A terminating link that sends each tRPC call as its own HTTP request From https://trpc.io/docs/client/links/httpLink `httpLink` is a [**terminating link**](https://trpc.io/docs/client/links#the-terminating-link) that sends a tRPC operation to a tRPC procedure over HTTP. `httpLink` supports both POST and GET requests. ## Usage You can import and add the `httpLink` to the `links` array as such: ```ts no-check title="client/index.ts" import { createTRPCClient, httpLink } from '@trpc/client'; import type { AppRouter } from './server'; const client = createTRPCClient<AppRouter>({ links: [ httpLink({ url: 'http://localhost:3000', // transformer, }), ], }); ``` ## `httpLink` Options The `httpLink` function takes an options object that has the `HTTPLinkOptions` shape. ```ts no-check export interface HTTPLinkOptions { url: string | URL; /** * Add ponyfill for fetch */ fetch?: typeof fetch; /** * 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'; /** * The character between a procedure path's segments in URLs. Must match * the server's `pathSeparator`. * @default '.' * @see https://trpc.io/docs/rpc#path-separator */ pathSeparator?: '.' | '_' | '~' | '-'; } ``` ## Reference You can check out the source code for this link on [GitHub.](https://github.com/trpc/trpc/blob/main/packages/client/src/links/httpLink.ts)