From 72e1d054bdbc29dd41942ab9abfec73248c83f83 Mon Sep 17 00:00:00 2001 From: Hung Tran Date: Fri, 2 Sep 2022 17:05:47 +0700 Subject: [PATCH] Enable to pass custom headers --- src/http-client.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/http-client.ts b/src/http-client.ts index ccedc346d..32714145e 100644 --- a/src/http-client.ts +++ b/src/http-client.ts @@ -139,46 +139,54 @@ export class HttpClient { await this.request({ ...params, uri: this.apiUri(params.uri), - headers: this.headers, + headers: { ...this.headers, ...params.headers }, }), ) } - public async get(resource: string): Promise { + public async get(resource: string, extraHeaders: Headers = {}): Promise { return this.apiRequest({ method: 'GET', uri: resource, + headers: extraHeaders, }) } - public async put(resource: string, body: RequestBody): Promise { + public async put(resource: string, body: RequestBody, extraHeaders: Headers = {}): Promise { return this.apiRequest({ method: 'PUT', uri: resource, body: JSON.stringify(body), + headers: extraHeaders, }) } - public async post(resource: string, body: RequestBody): Promise { + public async post( + resource: string, + body: RequestBody, + extraHeaders: Headers = {}, + ): Promise { return this.apiRequest({ method: 'POST', uri: resource, body: JSON.stringify(body), + headers: extraHeaders, }) } - public async delete(resource: string): Promise { + public async delete(resource: string, extraHeaders: Headers = {}): Promise { return this.apiRequest({ method: 'DELETE', uri: resource, + headers: extraHeaders, }) } - public async download(resource: string): Promise { + public async download(resource: string, headers: Headers = {}): Promise { const res = await this.request({ method: 'GET', uri: this.apiUri(resource), - headers: this.headers, + headers: { ...this.headers, ...headers }, }) // checks for common errors, we don't care about the result, as we expect it to throw