Skip to content

Commit

Permalink
first pass at token auth, rename baseUrl to host to match CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Jul 21, 2023
1 parent 8ffcc89 commit 59f4a79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
32 changes: 18 additions & 14 deletions client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ export interface FullRequestParams extends Omit<RequestInit, "body"> {
path: string;
query?: QueryParamsType;
body?: unknown;
baseUrl?: string;
host?: string;
}

export type RequestParams = Omit<
FullRequestParams,
"body" | "method" | "query" | "path"
>;

type BaseApiParams = Omit<RequestParams, "host" | "signal">;

export interface ApiConfig {
baseUrl?: string;
baseApiParams?: Omit<RequestParams, "baseUrl" | "signal">;
host?: string;
token?: string;
baseApiParams?: BaseApiParams;
}

/** Success responses from the API */
Expand Down Expand Up @@ -136,17 +139,21 @@ export async function handleResponse<Data>(
}

export class HttpClient {
public baseUrl = "";

private baseApiParams: RequestParams = {
public host?: string;
public token?: string;
private baseApiParams: BaseApiParams = {
credentials: "same-origin",
headers: {},
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
referrerPolicy: "no-referrer",
};

constructor(apiConfig: ApiConfig = {}) {
Object.assign(this, apiConfig);
this.host = apiConfig.host;
this.token = apiConfig.token;
this.baseApiParams = apiConfig.baseApiParams || this.baseApiParams;
}

private mergeRequestParams(params: RequestParams): RequestParams {
Expand All @@ -155,6 +162,7 @@ export class HttpClient {
...params,
headers: {
...this.baseApiParams.headers,
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
...params.headers,
},
};
Expand All @@ -164,19 +172,15 @@ export class HttpClient {
body,
path,
query,
baseUrl,
host,
...params
}: FullRequestParams): Promise<ApiResult<Data>> => {
const requestParams = this.mergeRequestParams(params);

const url = (baseUrl || this.baseUrl || "") + path + toQueryString(query);
const url = (host || this.host || "") + path + toQueryString(query);

const response = await fetch(url, {
...requestParams,
headers: {
"Content-Type": "application/json",
...requestParams.headers,
},
body: JSON.stringify(snakeify(body), replacer),
});

Expand Down
32 changes: 18 additions & 14 deletions static/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ export interface FullRequestParams extends Omit<RequestInit, "body"> {
path: string;
query?: QueryParamsType;
body?: unknown;
baseUrl?: string;
host?: string;
}

export type RequestParams = Omit<
FullRequestParams,
"body" | "method" | "query" | "path"
>;

type BaseApiParams = Omit<RequestParams, "host" | "signal">;

export interface ApiConfig {
baseUrl?: string;
baseApiParams?: Omit<RequestParams, "baseUrl" | "signal">;
host?: string;
token?: string;
baseApiParams?: BaseApiParams;
}

/** Success responses from the API */
Expand Down Expand Up @@ -136,17 +139,21 @@ export async function handleResponse<Data>(
}

export class HttpClient {
public baseUrl = "";

private baseApiParams: RequestParams = {
public host?: string;
public token?: string;
private baseApiParams: BaseApiParams = {
credentials: "same-origin",
headers: {},
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
referrerPolicy: "no-referrer",
};

constructor(apiConfig: ApiConfig = {}) {
Object.assign(this, apiConfig);
this.host = apiConfig.host;
this.token = apiConfig.token;
this.baseApiParams = apiConfig.baseApiParams || this.baseApiParams;
}

private mergeRequestParams(params: RequestParams): RequestParams {
Expand All @@ -155,6 +162,7 @@ export class HttpClient {
...params,
headers: {
...this.baseApiParams.headers,
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
...params.headers,
},
};
Expand All @@ -164,19 +172,15 @@ export class HttpClient {
body,
path,
query,
baseUrl,
host,
...params
}: FullRequestParams): Promise<ApiResult<Data>> => {
const requestParams = this.mergeRequestParams(params);

const url = (baseUrl || this.baseUrl || "") + path + toQueryString(query);
const url = (host || this.host || "") + path + toQueryString(query);

const response = await fetch(url, {
...requestParams,
headers: {
"Content-Type": "application/json",
...requestParams.headers,
},
body: JSON.stringify(snakeify(body), replacer),
});

Expand Down

0 comments on commit 59f4a79

Please sign in to comment.