.ts
TypeScript
(application/typescript)
import { IncomingMessage } from "http";

export class HttpResponse {
  readonly statusCode: number;
  readonly statusText: string;
  readonly ok: boolean;
  readonly headers: IncomingMessage["headers"];

  constructor(incoming: IncomingMessage);

  text(): Promise<string>;
  isJson(): Promise<boolean>;
  json<T extends any = any>(): Promise<T>;
}

export interface RequestConfig {
  headers?: Record<string, string>;
  body?: string | Buffer | any;
}

export class HttpClient {
  constructor();

  get(url: string, config?: RequestConfig): Promise<HttpResponse>;
  post(url: string, config?: RequestConfig): Promise<HttpResponse>;
  put(url: string, config?: RequestConfig): Promise<HttpResponse>;
  patch(url: string, config?: RequestConfig): Promise<HttpResponse>;
  delete(url: string, config?: RequestConfig): Promise<HttpResponse>;
  head(url: string, config?: RequestConfig): Promise<HttpResponse>;
  options(url: string, config?: RequestConfig): Promise<HttpResponse>;
  custom(
    method: string,
    url: string,
    config?: RequestConfig,
  ): Promise<HttpResponse>;
}