import MockAdapter from "axios-mock-adapter"; import { Auth } from "../api/types"; import { CancelableResponse } from "./CancelableResponse"; export interface HttpClientRepository { request(options: HttpRequest): CancelableResponse; getMockAdapter(): MockAdapter; } // Using patch is highly likely to result in a 405 Method Not Allowed. PATCH is much more likely to succeed (https://fetch.spec.whatwg.org/#example-normalization) export type Method = "get" | "post" | "put" | "delete" | "PATCH"; export type ParamValue = string | number | boolean | undefined; export interface HttpRequest { method: Method; url: string; params?: Record; data?: unknown; requestBodyType?: "json" | "raw"; responseDataType?: "json" | "raw"; validateStatus?(status: number): boolean; timeout?: number; headers?: Record; } export interface HttpClientResponse { status: number; data: Data; headers: Record; } export interface Credentials { username: string; password: string; } export interface ConstructorOptions { baseUrl?: string; auth?: Auth; timeout?: number; agent?: Agent; } export type Agent = unknown; interface HttpErrorOptions { request: HttpRequest; response?: HttpClientResponse; } export class HttpError extends Error implements HttpErrorOptions { request: HttpRequest; response?: HttpClientResponse; constructor(message: string, obj: HttpErrorOptions) { super(message); this.request = obj.request; this.response = obj.response; } } export function getBody(dataType: HttpRequest["responseDataType"], data: any) { switch (dataType) { case "json": { return JSON.stringify(data); } default: { return data; } } }