Aller au contenu principal
Version : 11.x

Lien HTTP

Traduction Bêta Non Officielle

Cette page a été traduite par PageTurner AI (bêta). Non approuvée officiellement par le projet. Vous avez trouvé une erreur ? Signaler un problème →

httpLink est un lien terminal qui envoie une opération tRPC à une procédure tRPC via HTTP.

httpLink prend en charge les requêtes POST et GET.

Utilisation

Vous pouvez importer et ajouter le httpLink au tableau links comme suit :

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

La fonction httpLink accepte un objet d'options conforme au type HTTPLinkOptions.

ts
export interface HTTPLinkOptions {
url: string;
/**
* Add ponyfill for fetch
*/
fetch?: typeof fetch;
/**
* Add ponyfill for AbortController
*/
AbortController?: typeof AbortController | null;
/**
* Data transformer
* @see https://trpc.io/docs/v11/data-transformers
**/
transformer?: DataTransformerOptions;
/**
* Headers to be set on outgoing requests or a callback that of said headers
* @see http://trpc.io/docs/v10/header
*/
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';
}
ts
export interface HTTPLinkOptions {
url: string;
/**
* Add ponyfill for fetch
*/
fetch?: typeof fetch;
/**
* Add ponyfill for AbortController
*/
AbortController?: typeof AbortController | null;
/**
* Data transformer
* @see https://trpc.io/docs/v11/data-transformers
**/
transformer?: DataTransformerOptions;
/**
* Headers to be set on outgoing requests or a callback that of said headers
* @see http://trpc.io/docs/v10/header
*/
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';
}

Référence

Vous pouvez consulter le code source de ce lien sur GitHub.