Skip to content

Commit

Permalink
Package cleanup (#63)
Browse files Browse the repository at this point in the history
* Revert "feat(http): better handle ECONNRESET errors"

This reverts commit 3109494.

* chore(package): updated yarn version

* refactor(util): removed unused index file

* chore(errors): moved errors into separate package

* style(http): return types

* chore(http): renamed http manager filename
  • Loading branch information
seth2810 authored Aug 19, 2024
1 parent 3109494 commit 3464e3b
Show file tree
Hide file tree
Showing 14 changed files with 948 additions and 33 deletions.
925 changes: 925 additions & 0 deletions .yarn/releases/yarn-4.4.0.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.4.0.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
"axios": "^1.7.2",
"axios-better-stacktrace": "^2.1.6"
},
"packageManager": "yarn@4.3.1"
"packageManager": "yarn@4.4.0"
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export * from './lib/SpotifyAPI';
export * from './lib/Manager';
export * from './lib/http/HttpClient';
export * from './lib/album/AlbumManager';
export * from './lib/artist/ArtistManager';
export * from './lib/audio/AudioManager';
export * from './lib/http/HttpManager';
export * from './lib/me/MeManager';
export * from './lib/search/SearchManager';
export * from './lib/track/TrackManager';
export * from './lib/user/UserManager';
export * from './lib/errors';

// Interfaces
export * from './interfaces/Config';
export * from './interfaces/Errors';
export * from './interfaces/Spotify';
2 changes: 1 addition & 1 deletion src/lib/Manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient } from './http/HttpManager';
import { HttpClient } from './http/HttpClient';

export abstract class Manager {
// eslint-disable-next-line no-unused-vars
Expand Down
2 changes: 1 addition & 1 deletion src/lib/SpotifyAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrivateConfig, SpotifyConfig } from '../interfaces/Config';
import { AlbumManager } from './album/AlbumManager';
import { ArtistManager } from './artist/ArtistManager';
import { AudioManager } from './audio/AudioManager';
import { HttpClient } from './http/HttpManager';
import { HttpClient } from './http/HttpClient';
import { MeManager } from './me/MeManager';
import { PlaylistManager } from './playlist/PlaylistManager';
import { RecommendationsManager } from './recommendations/RecommendationsManager';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/album/AlbumManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chunk } from '../../util';
import { chunk } from '../../util/chunk';
import {
Album,
Markets,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/artist/ArtistManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chunk } from '../../util';
import { chunk } from '../../util/chunk';
import {
AlbumSimplified,
Artist,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/http/AuthManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import axios, { AxiosInstance } from 'axios';
import { AuthError } from '../../interfaces/Errors';
import { AuthError } from '../errors';
import { PrivateConfig, SpotifyConfig } from '../../interfaces/Config';

const accountsApiUrl = 'https://accounts.spotify.com/api';
Expand Down
33 changes: 11 additions & 22 deletions src/lib/http/HttpManager.ts → src/lib/http/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import axios, {
AxiosError,
AxiosInstance,
AxiosPromise,
AxiosRequestConfig,
AxiosResponse,
InternalAxiosRequestConfig
} from 'axios';
import { URL, URLSearchParams } from 'url';
Expand All @@ -20,7 +20,7 @@ import {
RatelimitError,
RequestRetriesExceededError,
UnauthorizedError
} from '../../interfaces/Errors';
} from '../errors';
import { PrivateConfig, SpotifyConfig } from '../../interfaces/Config';
import { sleep } from '../../util/sleep';
import { AuthManager } from './AuthManager';
Expand Down Expand Up @@ -109,7 +109,7 @@ export class HttpClient {
return client;
}

private async handleError(client: AxiosInstance, err: unknown): Promise<AxiosResponse> {
private async handleError(client: AxiosInstance, err: unknown): AxiosPromise {
if (axios.isCancel(err) || axios.isAxiosError(err) === false || !this.shouldRetryRequest(err)) {
return await Promise.reject(this.extractResponseError(err));
}
Expand All @@ -119,7 +119,6 @@ export class HttpClient {
requestConfig.retryAttempt ||= 0;

const isRateLimited = err.response && err.response.status === 429;
const isConnectionReset = (err as AxiosError).code === 'ECONNRESET';

if (isRateLimited) {
if (this.config.logRetry) {
Expand All @@ -137,16 +136,6 @@ export class HttpClient {
await sleep(retryAfter * 1_000);

requestConfig.retryAttempt = 0;
} else if (isConnectionReset) {
await sleep(1_000 * (requestConfig.retryAttempt + 1));

requestConfig.retryAttempt += 1;

if (this.config.debug) {
console.log(
`(${requestConfig.retryAttempt}/${this.maxRetryAttempts}) retry ${requestConfig.url} - ${err}`
);
}
} else {
await sleep(1_000);

Expand Down Expand Up @@ -273,54 +262,54 @@ export class HttpClient {
/**
* @param {string} slug The slug to get.
* @param {{query?: Record<string, string> & AxiosRequestConfig}} config Config.
* @returns {Promise<AxiosResponse>} Returns a promise with the response.
* @returns {AxiosPromise} Returns a promise with the response.
*/
async get(
slug: string,
config?: { query?: Record<string, string> } & AxiosRequestConfig
): Promise<AxiosResponse> {
): AxiosPromise {
return await this.client.get(this.getURL(slug, config?.query), config);
}

/**
* @param {string} slug The slug to post.
* @param {any} data Body data.
* @param {{Record<string, string> & RequestInit}} config Config.
* @returns {Promise<Response>} Returns a promise with the response.
* @returns {AxiosPromise} Returns a promise with the response.
*/
async post(
slug: string,
data: unknown,
config?: { query?: Record<string, string> } & AxiosRequestConfig
): Promise<AxiosResponse> {
): AxiosPromise {
return await this.client.post(this.getURL(slug, config?.query), data, config);
}

/**
* @param {string} slug The slug to put.
* @param {any} data Body data.
* @param {{Record<string, string> & RequestInit}} config Config.
* @returns {Promise<Response>} Returns a promise with the response.
* @returns {AxiosPromise} Returns a promise with the response.
*/
async put(
slug: string,
data: unknown,
config?: { query?: Record<string, string> } & AxiosRequestConfig
): Promise<AxiosResponse> {
): AxiosPromise {
return await this.client.put(this.getURL(slug, config?.query), data, config);
}

/**
* @param {string} slug The slug to delete.
* @param {unknown} data Body data.
* @param {{Record<string, string> & RequestInit}} config Config.
* @returns {Promise<Response>} Returns a promise with the response.
* @returns {AxiosPromise} Returns a promise with the response.
*/
async delete(
slug: string,
data: unknown,
config?: { query?: Record<string, string> } & AxiosRequestConfig
): Promise<AxiosResponse> {
): AxiosPromise {
return await this.client.delete(this.getURL(slug, config?.query), {
...config,
data
Expand Down
2 changes: 1 addition & 1 deletion src/lib/playlist/PlaylistManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chunk } from '../../util';
import { chunk } from '../../util/chunk';
import {
FeaturedPlaylist,
Markets,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/track/TrackManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Track } from '../../interfaces/Spotify';
import { Manager } from '../Manager';
import { chunk } from '../../util';
import { chunk } from '../../util/chunk';

export class TrackManager extends Manager {
/**
Expand Down
1 change: 0 additions & 1 deletion src/util/index.ts

This file was deleted.

0 comments on commit 3464e3b

Please sign in to comment.