@procore/core-http
Version:
A HTTP Client
77 lines (71 loc) • 3.17 kB
text/typescript
import * as up_fetch from 'up-fetch';
import { FetcherOptions, up } from 'up-fetch';
export { ResponseError, ValidationError, isJsonifiable, isResponseError, isValidationError } from 'up-fetch';
import { SystemEvents } from '@procore/web-sdk-events';
type Plugin = Pick<FetcherOptions<typeof fetch, any, any, any>, 'onError' | 'onRequest' | 'onSuccess'>;
/**
* Built-in authentication plugin that handles authentication-related responses.
* Currently detects step-up authentication requirements (401 with www-authenticate header).
* Future: Will also handle regular auth checks (401 and 403).
*/
declare const authPlugin: Plugin;
type CreateClientOptions = {
errorReportingApiKey: string;
fetchFn?: Parameters<typeof up>[0];
defaults?: Parameters<typeof up>[1];
plugins?: Plugin[];
defaultErrorHandling?: boolean;
systemEvents?: SystemEvents;
};
declare function createClient({ fetchFn, defaults, plugins, defaultErrorHandling, errorReportingApiKey, systemEvents, }: CreateClientOptions): (input: RequestInfo | URL, options?: Omit<any, "body" | "headers" | "method"> & {
baseUrl?: string;
body?: unknown;
headers?: HeadersInit | {
[x: string]: string | number | null | undefined;
};
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "CONNECT" | "OPTIONS" | "TRACE" | "HEAD" | (string & {});
onError?: (error: unknown, request: Request) => void;
onRequest?: (request: Request) => void | Promise<void>;
onRequestStreaming?: (event: {
chunk: Uint8Array;
totalBytes: number;
transferredBytes: number;
}, request: Request) => void | Promise<void>;
onResponseStreaming?: (event: {
chunk: Uint8Array;
totalBytes: number | undefined;
transferredBytes: number;
}, response: Response) => void | Promise<void>;
onRetry?: (context: {
response: Response | undefined;
error: unknown;
request: Request;
attempt: number;
}) => void;
onSuccess?: (data: any, request: Request) => void;
params?: {
[x: string]: any;
};
parseRejected?: (response: Response, request: Request) => any;
parseResponse?: ((response: Response, request: Request) => unknown) | undefined;
reject?: (response: Response) => boolean | Promise<boolean>;
retry?: up_fetch.RetryOptions;
schema?: up_fetch.StandardSchemaV1<unknown, any> | undefined;
serializeBody?: ((body: unknown) => BodyInit | null | undefined) | undefined;
serializeParams?: (params: {
[x: string]: any;
}) => string;
signal?: AbortSignal;
timeout?: number;
} & {
defaultErrorHandling?: boolean;
}) => Promise<any>;
interface RequestParams extends RequestInit {
baseUrl?: string;
errorReportingApiKey?: string;
/** Pass to receive error reporting events when errorReportingApiKey is set. */
systemEvents?: SystemEvents;
}
declare function request(url: string, requestParams?: RequestParams): Promise<any>;
declare function requestJSON<T>(url: string, requestParams?: RequestParams): Promise<T>;
export { type Plugin, type RequestParams, authPlugin, createClient, request, requestJSON };