import assert from 'assert' import axios, {AxiosInstance} from 'axios' import {IHTTPClient} from './IHTTPClient' import {IHeader} from './IHeader' import {IMethod, IRoutes} from '@rondo/common' import {ITypedRequestParams} from './ITypedRequestParams' export class HTTPClient implements IHTTPClient { protected readonly axios: AxiosInstance constructor(baseURL = '', headers?: IHeader) { this.axios = axios.create({ baseURL, headers, }) } async request< P extends keyof T & string, M extends IMethod, >(params: ITypedRequestParams): Promise { const url = params.path.replace(/:[a-zA-Z0-9-]+/g, (match) => { const key = match.substring(1) assert(params.params, 'Params is required, but not defined') assert(params.params!.hasOwnProperty(key)) return params.params![key] }) const response = await this.axios.request({ method: params.method, url, params: params.query, data: params.body, }) return response.data } get

( path: P, query?: T[P]['get']['query'], params?: T[P]['get']['params'], ) { return this.request({ method: 'get', path, query, params, }) } post

( path: P, body: T[P]['post']['body'], params?: T[P]['post']['params'], ) { return this.request({ method: 'post', path, body, params, }) } put

( path: P, body: T[P]['put']['body'], params?: T[P]['put']['params'], ) { return this.request({ method: 'put', path, body, params, }) } delete

( path: P, body: T[P]['delete']['body'], params?: T[P]['delete']['params'], ) { return this.request({ method: 'delete', path, body, params, }) } head

( path: P, query?: T[P]['head']['query'], params?: T[P]['head']['params'], ) { return this.request({ method: 'head', path, params, query, }) } options

( path: P, query?: T[P]['options']['query'], params?: T[P]['options']['params'], ) { return this.request({ method: 'options', path, params, query, }) } patch

( path: P, body: T[P]['patch']['body'], params?: T[P]['patch']['params'], ) { return this.request({ method: 'patch', path, body, params, }) } }