From 64401fa2a550a4964d32312b5081e8bb87b194b6 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 21 Jul 2023 16:48:06 -0500 Subject: [PATCH] add token auth (#214) --- client/http-client.ts | 15 ++++++++++----- static/http-client.ts | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/client/http-client.ts b/client/http-client.ts index 4a87f1d..36c0754 100644 --- a/client/http-client.ts +++ b/client/http-client.ts @@ -128,19 +128,24 @@ export interface ApiConfig { * the web console. */ host?: string; + token?: string; baseParams?: FetchParams; } export class HttpClient { host: string; + token?: string; baseParams: FetchParams; - constructor({ host = "", baseParams = {} }: ApiConfig = {}) { + constructor({ host = "", baseParams = {}, token }: ApiConfig = {}) { this.host = host; - this.baseParams = mergeParams( - { headers: { "Content-Type": "application/json" } }, - baseParams - ); + this.token = token; + + const headers = new Headers({ "Content-Type": "application/json" }); + if (token) { + headers.append("Authorization", `Bearer ${token}`); + } + this.baseParams = mergeParams({ headers }, baseParams); } public async request({ diff --git a/static/http-client.ts b/static/http-client.ts index 4a87f1d..36c0754 100644 --- a/static/http-client.ts +++ b/static/http-client.ts @@ -128,19 +128,24 @@ export interface ApiConfig { * the web console. */ host?: string; + token?: string; baseParams?: FetchParams; } export class HttpClient { host: string; + token?: string; baseParams: FetchParams; - constructor({ host = "", baseParams = {} }: ApiConfig = {}) { + constructor({ host = "", baseParams = {}, token }: ApiConfig = {}) { this.host = host; - this.baseParams = mergeParams( - { headers: { "Content-Type": "application/json" } }, - baseParams - ); + this.token = token; + + const headers = new Headers({ "Content-Type": "application/json" }); + if (token) { + headers.append("Authorization", `Bearer ${token}`); + } + this.baseParams = mergeParams({ headers }, baseParams); } public async request({