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 c96a4df
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
43 changes: 25 additions & 18 deletions client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@ 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;
/**
* API token. Easiest way to get one is to auth with the CLI with `oxide auth
* login` and then print the token with `oxide auth status --show-token`. Web
* console uses a session cookie, so it does not need this.
*/
token?: string;
baseApiParams?: BaseApiParams;
}

/** Success responses from the API */
Expand Down Expand Up @@ -136,17 +144,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);
constructor({ host, token, baseApiParams }: ApiConfig = {}) {
this.host = host;
this.token = token;
this.baseApiParams = baseApiParams || this.baseApiParams;
}

private mergeRequestParams(params: RequestParams): RequestParams {
Expand All @@ -155,6 +167,7 @@ export class HttpClient {
...params,
headers: {
...this.baseApiParams.headers,
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
...params.headers,
},
};
Expand All @@ -164,19 +177,13 @@ 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,
},
...this.mergeRequestParams(params),
body: JSON.stringify(snakeify(body), replacer),
});

Expand Down
43 changes: 25 additions & 18 deletions static/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@ 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;
/**
* API token. Easiest way to get one is to auth with the CLI with `oxide auth
* login` and then print the token with `oxide auth status --show-token`. Web
* console uses a session cookie, so it does not need this.
*/
token?: string;
baseApiParams?: BaseApiParams;
}

/** Success responses from the API */
Expand Down Expand Up @@ -136,17 +144,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);
constructor({ host, token, baseApiParams }: ApiConfig = {}) {
this.host = host;
this.token = token;
this.baseApiParams = baseApiParams || this.baseApiParams;
}

private mergeRequestParams(params: RequestParams): RequestParams {
Expand All @@ -155,6 +167,7 @@ export class HttpClient {
...params,
headers: {
...this.baseApiParams.headers,
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
...params.headers,
},
};
Expand All @@ -164,19 +177,13 @@ 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,
},
...this.mergeRequestParams(params),
body: JSON.stringify(snakeify(body), replacer),
});

Expand Down

0 comments on commit c96a4df

Please sign in to comment.