@coolio/http
Version:
HTTP networking client
135 lines (134 loc) • 5.73 kB
TypeScript
import * as qs from 'qs';
import { BodyParser, BodySerializer, HttpHeaders, HttpInterceptor, HttpRequestHandler, HttpRequestOptions, HttpResponse } from './httpClient.types';
/**
* Default request timeout - 5 minutes.
*/
export declare const DEFAULT_REQUEST_TIMEOUT_MS: number;
/**
* A set of configuration options, which allows {@link HttpClient} to perform requests and process responses.
*
* @typeparam T Common body shape defined by {@link bodyParser}.
*/
export interface HttpClientConfig<T = HttpResponse> {
/**
* An implementation of request handler, which handles "low-level" HTTP communication.
* Result of executing a request via RequestHandler is a normalized object,
* which can be further processed by interceptors and HttpClient.
*
* - For Node.js environment, use {@link httpRequestHandler}
* - For browser environments, use {@link fetchRequestHandler}
* - For React Native and any other environment, use {@link xhrRequestHandler}
*/
requestHandler: HttpRequestHandler;
/**
* Headers that will be passed to all requests.
* To add headers dynamically, you can use an interceptor instead.
*/
headers?: HttpHeaders;
/**
* A utility that parses and normalizes body of a response received from server.
* Can be used to decode JSON object, URL-encoded body or plain text.
* Built-in {@link bodyParser} supports case-conversion.
*/
bodyParser?: BodyParser<T>;
/**
* A utility that parses and normalizes body of a request sent to server.
* Can be used to encode JSON object, URL-encoded body or plain text.
* Built-in {@link bodySerializer} supports case-conversion.
*/
bodySerializer?: BodySerializer;
/**
* URL that is applied to all requests without specified protocol and domain.
*/
baseUrl?: string;
/**
* Allow to parse query options in a different way than the standard one.
*/
queryParserOptions?: qs.IParseOptions;
/**
* Allow to serialize query options in a different way than the standard one.
*/
querySerializerOptions?: qs.IStringifyOptions;
/**
* Standard timeout, triggered when server does not respond with headers within specified period of time.
*
* @default {@link DEFAULT_REQUEST_TIMEOUT_MS}
*/
requestTimeout?: number;
}
/**
* Base class in Coolio http package, which allows to perform API calls.
*
* @typeparam T Common body shape defined by bodyParser passed in {@link HttpClientConfig}.
*/
export declare class HttpClient<T = unknown> {
private readonly handle;
private readonly headers?;
private readonly interceptors;
private readonly bodyParser;
private readonly bodySerializer;
private readonly queryParserOptions?;
private readonly querySerializerOptions?;
private readonly baseUrl?;
private readonly defaultRequestTimeout;
constructor(config: HttpClientConfig<T>);
/**
* Adds an interceptor to the client. Interceptor can be written either as class or as a function,
* which may mutate request options and post-process response from server.
* Multiple interceptors can be added to a single HttpClient. They can perform as:
* - cache
* - error handler
* - authorizer
* - logger
* - auto-retry
* - redirection handler
*
* @param interceptor Interceptor that will process every request/response in this HttpClient.
*/
addInterceptor: (interceptor: HttpInterceptor) => this;
/**
* Performs a GET request.
*
* @param uri Address of HTTP endpoint
* @param options Additional {@link HttpOptions} passed with request
*/
get: <Body_1 extends T = any>(uri: string, options?: Partial<HttpRequestOptions> | undefined) => Promise<HttpResponse<Body_1>>;
/**
* Performs a POST request.
*
* @param uri Address of HTTP endpoint
* @param options Additional {@link HttpOptions} passed with request
*/
post: <Body_1 extends T = any>(uri: string, options?: Partial<HttpRequestOptions> | undefined) => Promise<HttpResponse<Body_1>>;
/**
* Performs a PUT request.
*
* @param uri Address of HTTP endpoint
* @param options Additional {@link HttpOptions} passed with request
*/
put: <Body_1 extends T = any>(uri: string, options?: Partial<HttpRequestOptions> | undefined) => Promise<HttpResponse<Body_1>>;
/**
* Performs a PATCH request.
*
* @param uri Address of HTTP endpoint
* @param options Additional {@link HttpOptions} passed with request
*/
patch: <Body_1 extends T = any>(uri: string, options?: Partial<HttpRequestOptions> | undefined) => Promise<HttpResponse<Body_1>>;
/**
* Performs a DELETE request.
*
* @param uri Address of HTTP endpoint
* @param options Additional {@link HttpOptions} passed with request
*/
delete: <Body_1 extends T = any>(uri: string, options?: Partial<HttpRequestOptions> | undefined) => Promise<HttpResponse<Body_1>>;
/**
* Performs a DELETE request.
* @deprecated Use delete instead of remove, since it matches HTTP request method.
*
* @param uri Address of HTTP endpoint
* @param options Additional {@link HttpOptions} passed with request
*/
remove: <Body_1 extends T = any>(uri: string, options?: Partial<HttpRequestOptions> | undefined) => Promise<HttpResponse<any>>;
request<Body extends T>(url: string, options: HttpRequestOptions): Promise<HttpResponse<Body>>;
}
//# sourceMappingURL=httpClient.d.ts.map