UNPKG

@orpc/client

Version:

<div align="center"> <image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" /> </div>

250 lines (243 loc) • 9.71 kB
import { Value, Promisable } from '@orpc/shared'; import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server'; import { BatchResponseMode } from '@orpc/standard-server/batch'; import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.CpCa3si8.mjs'; import { b as ClientContext } from '../shared/client.i2uoJbEp.mjs'; interface BatchLinkPluginGroup<T extends ClientContext> { condition(options: StandardLinkClientInterceptorOptions<T>): boolean; context: T; path?: readonly string[]; input?: unknown; } interface BatchLinkPluginOptions<T extends ClientContext> { groups: readonly [BatchLinkPluginGroup<T>, ...BatchLinkPluginGroup<T>[]]; /** * The maximum number of requests in the batch. * * @default 10 */ maxSize?: Value<Promisable<number>, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>; /** * The batch response mode. * * @default 'streaming' */ mode?: Value<BatchResponseMode, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>; /** * Defines the URL to use for the batch request. * * @default the URL of the first request in the batch + '/__batch__' */ url?: Value<Promisable<string | URL>, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>; /** * The maximum length of the URL. * * @default 2083 */ maxUrlLength?: Value<Promisable<number>, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>; /** * Defines the HTTP headers to use for the batch request. * * @default The same headers of all requests in the batch */ headers?: Value<Promisable<StandardHeaders>, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>; /** * Map the batch request items before sending them. * * @default Removes headers that are duplicated in the batch headers. */ mapRequestItem?: (options: StandardLinkClientInterceptorOptions<T> & { batchUrl: URL; batchHeaders: StandardHeaders; }) => StandardRequest; /** * Exclude a request from the batch. * * @default () => false */ exclude?: (options: StandardLinkClientInterceptorOptions<T>) => boolean; } /** * The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch, * reducing the overhead of sending each one separately. * * @see {@link https://orpc.dev/docs/plugins/batch-requests Batch Requests Plugin Docs} */ declare class BatchLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> { #private; private readonly groups; private readonly maxSize; private readonly batchUrl; private readonly maxUrlLength; private readonly batchHeaders; private readonly mapRequestItem; private readonly exclude; private readonly mode; private pending; order: number; constructor(options: NoInfer<BatchLinkPluginOptions<T>>); init(options: StandardLinkOptions<T>): void; } interface DedupeRequestsPluginGroup<T extends ClientContext> { condition(options: StandardLinkClientInterceptorOptions<T>): boolean; /** * The context used for the rest of the request lifecycle. */ context: T; } interface DedupeRequestsPluginOptions<T extends ClientContext> { /** * To enable deduplication, a request must match at least one defined group. * Requests that fall into the same group are considered for deduplication together. */ groups: readonly [DedupeRequestsPluginGroup<T>, ...DedupeRequestsPluginGroup<T>[]]; /** * Filters requests to dedupe * * @default (({ request }) => request.method === 'GET') */ filter?: (options: StandardLinkClientInterceptorOptions<T>) => boolean; } /** * Prevents duplicate requests by deduplicating similar ones to reduce server load. * * @see {@link https://orpc.dev/docs/plugins/dedupe-requests Dedupe Requests Plugin} */ declare class DedupeRequestsPlugin<T extends ClientContext> implements StandardLinkPlugin<T> { #private; order: number; constructor(options: NoInfer<DedupeRequestsPluginOptions<T>>); init(options: StandardLinkOptions<T>): void; } interface ClientRetryPluginAttemptOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> { lastEventRetry: number | undefined; attemptIndex: number; error: unknown; } interface ClientRetryPluginContext { /** * Maximum retry attempts before throwing * Use `Number.POSITIVE_INFINITY` for infinite retries (e.g., when handling Server-Sent Events). * * @default 0 */ retry?: Value<Promisable<number>, [StandardLinkInterceptorOptions<ClientRetryPluginContext>]>; /** * Delay (in ms) before retrying. * * @default (o) => o.lastEventRetry ?? 2000 */ retryDelay?: Value<Promisable<number>, [ClientRetryPluginAttemptOptions<ClientRetryPluginContext>]>; /** * Determine should retry or not. * * @default true */ shouldRetry?: Value<Promisable<boolean>, [ClientRetryPluginAttemptOptions<ClientRetryPluginContext>]>; /** * The hook called when retrying, and return the unsubscribe function. */ onRetry?: (options: ClientRetryPluginAttemptOptions<ClientRetryPluginContext>) => void | ((isSuccess: boolean) => void); } declare class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error { } interface ClientRetryPluginOptions { default?: ClientRetryPluginContext; } /** * The Client Retry Plugin enables retrying client calls when errors occur. * * @see {@link https://orpc.dev/docs/plugins/client-retry Client Retry Plugin Docs} */ declare class ClientRetryPlugin<T extends ClientRetryPluginContext> implements StandardLinkPlugin<T> { private readonly defaultRetry; private readonly defaultRetryDelay; private readonly defaultShouldRetry; private readonly defaultOnRetry; order: number; constructor(options?: ClientRetryPluginOptions); init(options: StandardLinkOptions<T>): void; } interface RetryAfterPluginOptions<T extends ClientContext> { /** * Override condition to determine whether to retry or not. * * @default ((response) => response.status === 429 || response.status === 503) */ condition?: Value<boolean, [ response: StandardLazyResponse, options: StandardLinkClientInterceptorOptions<T> ]>; /** * Maximum attempts before giving up retries. * * @default 3 */ maxAttempts?: Value<number, [ response: StandardLazyResponse, options: StandardLinkClientInterceptorOptions<T> ]>; /** * Maximum timeout in milliseconds to wait before giving up retries. * * @default 5 * 60 * 1000 (5 minutes) */ timeout?: Value<number, [ response: StandardLazyResponse, options: StandardLinkClientInterceptorOptions<T> ]>; } /** * The Retry After Plugin automatically retries requests based on server `Retry-After` headers. * This is particularly useful for handling rate limiting and temporary server unavailability. * * @see {@link https://orpc.dev/docs/plugins/retry-after Retry After Plugin Docs} */ declare class RetryAfterPlugin<T extends ClientContext> implements StandardLinkPlugin<T> { private readonly condition; private readonly maxAttempts; private readonly timeout; order: number; constructor(options?: RetryAfterPluginOptions<T>); init(options: StandardLinkOptions<T>): void; private parseRetryAfterHeader; private delayExecution; } interface SimpleCsrfProtectionLinkPluginOptions<T extends ClientContext> { /** * The name of the header to check. * * @default 'x-csrf-token' */ headerName?: Value<Promisable<string>, [options: StandardLinkClientInterceptorOptions<T>]>; /** * The value of the header to check. * * @default 'orpc' * */ headerValue?: Value<Promisable<string>, [options: StandardLinkClientInterceptorOptions<T>]>; /** * Exclude a procedure from the plugin. * * @default false */ exclude?: Value<Promisable<boolean>, [options: StandardLinkClientInterceptorOptions<T>]>; } /** * This plugin adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application. * It helps ensure that requests to your procedures originate from JavaScript code, * not from other sources like standard HTML forms or direct browser navigation. * * @see {@link https://orpc.dev/docs/plugins/simple-csrf-protection Simple CSRF Protection Plugin Docs} */ declare class SimpleCsrfProtectionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> { private readonly headerName; private readonly headerValue; private readonly exclude; constructor(options?: SimpleCsrfProtectionLinkPluginOptions<T>); order: number; init(options: StandardLinkOptions<T>): void; } export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, RetryAfterPlugin, SimpleCsrfProtectionLinkPlugin }; export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, RetryAfterPluginOptions, SimpleCsrfProtectionLinkPluginOptions };