@vepler/http-client
Version:
A flexible and extensible API service library for making HTTP requests with built-in authentication support for bearer tokens and API keys.
70 lines (61 loc) • 1.87 kB
TypeScript
import { AxiosInstance, AxiosResponse } from 'axios';
interface Options {
host: string;
timeout?: number;
logLevel: string;
headers?: Record<string, string>;
}
interface QueryParams {
token?: string;
apiKey?: string;
params?: Record<string, any>;
headers?: Record<string, string>;
}
type ResourceParams = Record<string, any>;
// Error Classes
export class HttpError extends Error {
public status: number;
public statusText: string;
public endpoint: string;
public method: string;
public url: string;
public data: any;
public isHttpError: boolean;
constructor(
message: string,
status?: number,
statusText?: string,
endpoint?: string,
method?: string,
url?: string,
data?: any
);
toJSON(): Record<string, any>;
static fromAxiosError(error: any): HttpError;
}
export class ClientError extends HttpError {}
export class ServerError extends HttpError {}
export class NetworkError extends HttpError {
public request: any;
}
export class AuthError extends ClientError {
public credentials: Record<string, string>;
}
export class TimeoutError extends NetworkError {}
export class RateLimitError extends ClientError {
public retryAfter?: number;
}
export class ValidationError extends ClientError {
public validationErrors: Record<string, string[]>;
}
declare const ApiService: {
create(options: Options): {
client: AxiosInstance;
query(resource: string, params: ResourceParams, queryParams?: QueryParams): Promise<any>;
get(resource: string, slug?: string, queryParams?: QueryParams): Promise<any>;
post(resource: string, data: any, queryParams?: QueryParams): Promise<any>;
put(resource: string, data: any, queryParams?: QueryParams): Promise<any>;
delete(resource: string, slug?: string, queryParams?: QueryParams): Promise<any>;
};
};
export default ApiService;