Skip to content

Commit

Permalink
fixup! refactor(manager): separate clients for auth and regular requests
Browse files Browse the repository at this point in the history
  • Loading branch information
seth2810 committed Jul 24, 2024
1 parent aa5b0f4 commit 64b9bfb
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/lib/http/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ export class HttpClient {
*/
private create(): AxiosInstance {
const config: AxiosRequestConfig = {
proxy: this.config.http?.proxy
proxy: this.config.http?.proxy,
headers: {
...this.config.http?.headers,
'User-Agent':
this.config.http?.userAgent ?? '@statsfm/spotify.js https://github.com/statsfm/spotify.js'
}
};

if (this.config.http?.localAddress) {
Expand All @@ -228,15 +233,16 @@ export class HttpClient {
)
};
}

const client = axios.create(config);

axiosBetterStacktrace(client);

// request interceptor
client.interceptors.request.use(async (config) => {
config.headers.Authorization = `Bearer ${await this.handleAuth()}`;
config.headers['User-Agent'] =
this.config.http?.userAgent ?? `@statsfm/spotify.js https://github.com/statsfm/spotify.js`;
config.headers = Object.assign(this.config.http?.headers ?? {}, config.headers);
const accessToken = await this.handleAuth();

config.headers.Authorization = `Bearer ${accessToken}`;

return config;
});
Expand Down Expand Up @@ -372,9 +378,9 @@ export class HttpClient {
default:
if (i === this.config.retry5xxAmount) {
throw new RequestRetriesExceededError(
`Request exceeded ${this.config.retry5xxAmount} number of retry attempts, failed with status code ${statusCode}`,
`Request exceeded ${this.config.retry5xxAmount} retry attempts`,
error.config.url,
error.stack
error.response
);
}
}
Expand All @@ -389,9 +395,9 @@ export class HttpClient {
*/
async get(
slug: string,
options?: { query?: Record<string, string> } & AxiosRequestConfig
config?: { query?: Record<string, string> } & AxiosRequestConfig
): Promise<AxiosResponse> {
return await this.client.get(this.getURL(slug, options?.query), options);
return await this.client.get(this.getURL(slug, config?.query), config);
}

/**
Expand Down Expand Up @@ -431,10 +437,10 @@ export class HttpClient {
async delete(
slug: string,
data: any,
options?: { query?: Record<string, string> } & AxiosRequestConfig
config?: { query?: Record<string, string> } & AxiosRequestConfig
): Promise<AxiosResponse> {
return await this.client.delete(this.getURL(slug, options?.query), {
...options,
return await this.client.delete(this.getURL(slug, config?.query), {
...config,
data
});
}
Expand Down

0 comments on commit 64b9bfb

Please sign in to comment.