import { IRequestHeaders, RequestFunction, IRequestOptions, IResponse, IJsonWebClient } from './contracts/index'; import { AbstractWebClient } from './abstract'; export function JsonWebClient(request: RequestFunction): { new(userAgent?: string): IJsonWebClient } { async function jsonRequest(options: IRequestOptions): Promise> { if (!options.headers) options.headers = {}; options.headers['Content-Type'] = 'application/json'; let response = await request(options); response.data = JSON.parse(response.data); return response; } return class JsonWebClient extends AbstractWebClient(jsonRequest) implements IJsonWebClient { public async get(url: string, headers?: IRequestHeaders): Promise> { return await this.request({ method: 'GET', url, headers }); } public async post(url: string, body: any, headers?: IRequestHeaders): Promise> { return await this.request({ method: 'POST', url, body, headers }); } public async put(url: string, body: any, headers?: IRequestHeaders): Promise> { return await this.request({ method: 'PUT', url, body, headers }); } public async delete(url: string, headers?: IRequestHeaders): Promise> { return await this.request({ method: 'DELETE', url, headers }); } } }