@seriouslag/httpclient
Version:
Typed wrapper HttpClient for axios
73 lines (72 loc) • 3.2 kB
TypeScript
import { IHttpClientAdaptor, Method, ResponseType, HttpResponse } from './Adaptors';
import { HttpRequestStrategy } from './HttpRequestStrategies';
import { Logger } from './Logger';
/** Config used for setting up http calls */
export interface ApiConfig {
/** If specified, a new axios instance is used instead of the one instantiated in the HttpClient's constructor */
noGlobal?: boolean;
/** The headers that will be used in the HTTP call. Global headers will be added to these.
* // TODO: Test when noGlobal is true if global headers are added to the request
*/
headers?: Record<string, string>;
/** The body of the request that will be sent */
data?: any;
/** The type of response that will be expected */
responseType?: ResponseType;
/** The query parameters that will be sent with the HTTP call */
params?: any;
/** The encoding of the response */
responseEncoding?: string;
/** The strategy to use for this request, if not provided then the request that was provided with the HttpClient will be used */
httpRequestStrategy?: HttpRequestStrategy;
}
/**
* HttpClient configuration options
*/
export interface HttpClientOptions {
/** The strategy that will be used to handle http requests */
httpRequestStrategy?: HttpRequestStrategy;
/** The logger the HttpClient will use */
logger?: Logger;
baseUrl?: string;
}
/** Typed wrapper around axios that standardizes making HTTP calls and handling responses */
export declare class HttpClient {
private httpClientAdaptor;
private logger;
private httpRequestStrategy;
private baseUrl;
constructor(httpClientAdaptor?: IHttpClientAdaptor, options?: HttpClientOptions);
/**
* Sets the logger for the instance
* @param {Logger|undefined} logger
*/
setLogger(logger: Logger | undefined): void;
/** HTTP GET request */
get<T = unknown>(url: string, config?: ApiConfig, cancelToken?: AbortController): Promise<T>;
/** HTTP POST request */
post<T = unknown>(url: string, config?: ApiConfig, cancelToken?: AbortController): Promise<T>;
/** HTTP PUT request */
put<T = unknown>(url: string, config?: ApiConfig, cancelToken?: AbortController): Promise<T>;
/** HTTP DELETE request */
delete<T = unknown>(url: string, config?: ApiConfig, cancelToken?: AbortController): Promise<T>;
/** HTTP PATCH request */
patch<T = unknown>(url: string, config?: ApiConfig, cancelToken?: AbortController): Promise<T>;
/**
* HTTP request that returns the body of the HTTP response
*
* If a cancel token is passed in it will be aborted on request error.
*
* @returns {Promise<T>} body of the HTTP response
*/
dataRequest<T = unknown>(url: string, method: Method, config?: ApiConfig, cancelToken?: AbortController): Promise<T>;
/**
* HTTP request
*
* If a cancel token is passed in it will be aborted on request error.
*
* @returns {Promise<HttpResponse<T>>} HttpResponse
*/
request<T = unknown>(url: string, method: Method, config?: ApiConfig, cancelToken?: AbortController): Promise<HttpResponse<T>>;
private doRequest;
}