Skip to content

Commit

Permalink
add token auth (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo authored Jul 21, 2023
1 parent 3967da6 commit 64401fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 10 additions & 5 deletions client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data>({
Expand Down
15 changes: 10 additions & 5 deletions static/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data>({
Expand Down

0 comments on commit 64401fa

Please sign in to comment.