Skip to content

Commit

Permalink
feat: make baseURL unversioned
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvdkolk committed Apr 22, 2024
1 parent 2b73bd1 commit 9a89ba6
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 36 deletions.
5 changes: 5 additions & 0 deletions src/interfaces/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface SpotifyConfig {
* Set headers to be sent with every request.
*/
headers?: Record<string, string>;

/**
* Base URL for the API. Default: "https://api.spotify.com".
*/
baseURL?: string;
};
/**
* Enable debug mode.
Expand Down
8 changes: 4 additions & 4 deletions src/lib/album/AlbumManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AlbumManager extends Manager {
* @returns {Promise<Album>} Returns a promise with a single {@link Album}.
*/
async get(id: string): Promise<Album> {
const res = await this.http.get(`/albums/${id}`);
const res = await this.http.get(`/v1/albums/${id}`);

return res.data as Album;
}
Expand All @@ -29,7 +29,7 @@ export class AlbumManager extends Manager {
async list(ids: string[]): Promise<Album[]> {
const albums = await Promise.all(
chunk([...ids], 20).map(async (chunk) => {
const res = await this.http.get('/albums', { query: { ids: chunk.join(',') } });
const res = await this.http.get('/v1/albums', { query: { ids: chunk.join(',') } });
const json = res.data;

return json.albums as Album[];
Expand All @@ -50,7 +50,7 @@ export class AlbumManager extends Manager {

if (options.market) query.market = options.market;

const res = await this.http.get(`/albums/${id}/tracks`, { query });
const res = await this.http.get(`/v1/albums/${id}/tracks`, { query });

return res.data as PagingObject<Track>;
}
Expand All @@ -61,7 +61,7 @@ export class AlbumManager extends Manager {
offset: options?.offset?.toString() || '0'
};

const res = await this.http.get('/browse/new-releases', { query });
const res = await this.http.get('/v1/browse/new-releases', { query });

return res.data as NewReleases;
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/artist/ArtistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ArtistManager extends Manager {
* @returns {Promise<Artist[]>} Returns a promise with a single {@link Artist}.
*/
async get(id: string): Promise<Artist> {
const res = await this.http.get(`/artists/${id}`);
const res = await this.http.get(`/v1/artists/${id}`);

return res.data as Artist;
}
Expand All @@ -29,7 +29,7 @@ export class ArtistManager extends Manager {
async list(ids: string[]): Promise<Artist[]> {
const artists = await Promise.all(
chunk([...ids], 50).map(async (chunk) => {
const res = await this.http.get('/artists', { query: { ids: chunk.join(',') } });
const res = await this.http.get('/v1/artists', { query: { ids: chunk.join(',') } });
const json = res.data;

return json.artists as Artist[];
Expand Down Expand Up @@ -72,7 +72,7 @@ export class ArtistManager extends Manager {

if (options?.market) query.market = options.market;

const res = await this.http.get(`/artists/${id}/albums`, {
const res = await this.http.get(`/v1/artists/${id}/albums`, {
query
});

Expand All @@ -85,7 +85,7 @@ export class ArtistManager extends Manager {
* @returns {Promise<CursorPagingObject<Album[]>>} Returns a promise with an array of {@link Artist}s.
*/
async related(id: string): Promise<Artist[]> {
const res = await this.http.get(`/artists/${id}/related-artists`);
const res = await this.http.get(`/v1/artists/${id}/related-artists`);

return res.data.artists as Artist[];
}
Expand All @@ -104,7 +104,7 @@ export class ArtistManager extends Manager {

if (market) query.market = market;

const res = await this.http.get(`/artists/${id}/top-tracks`, {
const res = await this.http.get(`/v1/artists/${id}/top-tracks`, {
query
});

Expand Down
6 changes: 3 additions & 3 deletions src/lib/audio/AudioManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AudioManager extends Manager {
* @returns {Promise<AudioFeatures>} Returns a promise with a {@link AudioFeatures}.
*/
async feature(id: string): Promise<AudioFeatures> {
const res = await this.http.get(`/audio-features/${id}`);
const res = await this.http.get(`/v1/audio-features/${id}`);

return res.data as AudioFeatures;
}
Expand All @@ -20,7 +20,7 @@ export class AudioManager extends Manager {
* @returns {Promise<AudioFeatures[]>} Returns a promise with a {@link AudioFeatures}.
*/
async features(trackIds: string[]): Promise<AudioFeatures[]> {
const res = await this.http.get('/audio-features', {
const res = await this.http.get('/v1/audio-features', {
query: {
ids: trackIds.join(',')
}
Expand All @@ -35,7 +35,7 @@ export class AudioManager extends Manager {
* @returns {Promise<AudioAnalysis>} Returns a promise with a {@link AudioAnalysis}.
*/
async analysis(id: string): Promise<AudioAnalysis> {
const res = await this.http.get(`/audio-analysis/${id}`);
const res = await this.http.get(`/v1/audio-analysis/${id}`);

return res.data as AudioAnalysis;
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/http/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import {
import { PrivateConfig, SpotifyConfig } from '../../interfaces/Config';

export class HttpClient {
protected baseURL = 'https://api.spotify.com/v1';
protected baseURL = 'https://api.spotify.com';

protected tokenURL = 'https://accounts.spotify.com/api/token';

protected client = this.create({ resInterceptor: true });

constructor(protected config: SpotifyConfig, protected privateConfig: PrivateConfig) {}
constructor(protected config: SpotifyConfig, protected privateConfig: PrivateConfig) {
if (config.http?.baseURL) {
this.baseURL = config.http.baseURL;
}
}

/**
* @param {string} slug
Expand Down
20 changes: 10 additions & 10 deletions src/lib/me/MeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class MeManager extends Manager {
* @param {TopOptions} options
*/
async top(type: 'artists' | 'tracks', options?: TopOptions): Promise<unknown> {
const res = await this.http.get(`/me/top/${type}`, {
const res = await this.http.get(`/v1/me/top/${type}`, {
query: {
time_range: options?.timeRange,
limit: options?.limit as unknown as string,
Expand All @@ -53,7 +53,7 @@ export class MeManager extends Manager {
* @returns {Promise<UserPrivate>}
*/
async get(): Promise<UserPrivate> {
const res = await this.http.get('/me');
const res = await this.http.get('/v1/me');

return res.data as UserPrivate;
}
Expand All @@ -75,7 +75,7 @@ export class MeManager extends Manager {
if (options?.after) query.after = options.after as unknown as string;
if (options?.before) query.before = options.before as unknown as string;

const res = await this.http.get(`/me/player/recently-played`, {
const res = await this.http.get(`/v1/me/player/recently-played`, {
query
});

Expand All @@ -94,7 +94,7 @@ export class MeManager extends Manager {
* @returns {Promise} Returns a promise with the current playback state.
*/
async getPlaybackState(): Promise<PlayerState> {
const res = await this.http.get(`/me/player`);
const res = await this.http.get(`/v1/me/player`);

const json = res.data;

Expand All @@ -116,7 +116,7 @@ export class MeManager extends Manager {
if (options?.limit) query.limit = options.limit.toString();
if (options?.offset) query.offset = options.offset.toString();

const res = await this.http.get(`/me/tracks`, {
const res = await this.http.get(`/v1/me/tracks`, {
query
});

Expand All @@ -128,7 +128,7 @@ export class MeManager extends Manager {
* @returns {Promise<boolean[]>} Returns a promise with the an array of booleans.
*/
async containTracks(ids: string[]): Promise<boolean[]> {
const res = await this.http.get(`/me/tracks/contains`, {
const res = await this.http.get(`/v1/me/tracks/contains`, {
query: { ids: ids.join(',') }
});

Expand All @@ -140,7 +140,7 @@ export class MeManager extends Manager {
* @param {string} ids Array of IDs.
*/
async saveTracks(ids: string[]): Promise<void> {
await this.http.put(`/me/tracks`, {
await this.http.put(`/v1/me/tracks`, {
ids
});
}
Expand All @@ -150,7 +150,7 @@ export class MeManager extends Manager {
* @param {string} ids Array of IDs.
*/
async unsaveTracks(ids: string[]): Promise<void> {
await this.http.delete(`/me/tracks`, {
await this.http.delete(`/v1/me/tracks`, {
query: { ids: ids.join(',') }
});
}
Expand All @@ -164,7 +164,7 @@ export class MeManager extends Manager {
if (options?.limit) query.limit = options.limit.toString();
if (options?.offset) query.offset = options.offset.toString();

const res = await this.http.get(`/me/playlists`, {
const res = await this.http.get(`/v1/me/playlists`, {
query
});

Expand All @@ -176,6 +176,6 @@ export class MeManager extends Manager {
* @param {string} id The Spotify ID of the playlist to unfollow.
*/
async unfollowPlaylist(id: string): Promise<void> {
await this.http.delete(`/playlists/${id}/followers`, {});
await this.http.delete(`/v1/playlists/${id}/followers`, {});
}
}
14 changes: 7 additions & 7 deletions src/lib/playlist/PlaylistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class PlaylistManager extends Manager {
if (options?.market) query.market = options.market;
if (options?.fields) query.fields = options.fields;

const res = await this.http.get(`/playlists/${id}`, {
const res = await this.http.get(`/v1/playlists/${id}`, {
query
});

Expand All @@ -55,7 +55,7 @@ export class PlaylistManager extends Manager {
description: string;
}
): Promise<Playlist> {
const res = await this.http.post(`/users/${userId}/playlists`, data);
const res = await this.http.post(`/v1/users/${userId}/playlists`, data);

return res.data as Playlist;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ export class PlaylistManager extends Manager {
if (options?.market) query.market = options.market;
if (options?.fields) query.fields = options.fields;

const res = await this.http.get(`/playlists/${id}/tracks`, { query });
const res = await this.http.get(`/v1/playlists/${id}/tracks`, { query });

return res.data as PagingObject<PlaylistTrack>;
}
Expand All @@ -116,7 +116,7 @@ export class PlaylistManager extends Manager {
if (position > 0) body.position = position.toString();

// eslint-disable-next-line no-await-in-loop
await this.http.post(`/playlists/${id}/tracks`, body);
await this.http.post(`/v1/playlists/${id}/tracks`, body);
}

// Adding items to a playlist must be done in the right order,
Expand Down Expand Up @@ -162,7 +162,7 @@ export class PlaylistManager extends Manager {

if (snapshot_id) body.snapshot_id = snapshot_id.toString();

await this.http.delete(`/playlists/${id}/tracks`, body);
await this.http.delete(`/v1/playlists/${id}/tracks`, body);
})
);

Expand All @@ -179,7 +179,7 @@ export class PlaylistManager extends Manager {
throw new Error('Length of id must be greater than 0');
}

await this.http.put(`/playlists/${id}/images`, imageData, {
await this.http.put(`/v1/playlists/${id}/images`, imageData, {
headers: {
'Content-Type': 'image/jpeg'
}
Expand All @@ -204,7 +204,7 @@ export class PlaylistManager extends Manager {
timestamp?: string;
}
): Promise<FeaturedPlaylist> {
const res = await this.http.get('/browse/featured-playlists', {
const res = await this.http.get('/v1/browse/featured-playlists', {
query: {
...(options?.offset && { offset: String(options.offset) }),
...(options?.limit && { limit: String(options.limit) }),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/recommendations/RecommendationsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RecommendationsManager extends Manager {
query.seed_genres = options.seed_genres?.join(',');
}

const res = await this.http.get('/recommendations', {
const res = await this.http.get('/v1/recommendations', {
query
});

Expand Down
2 changes: 1 addition & 1 deletion src/lib/search/SearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class SearchManager extends Manager {

if (options?.includeExternal) query.include_external = options.includeExternal;

const res = await this.http.get('/search', {
const res = await this.http.get('/v1/search', {
query
});

Expand Down
4 changes: 2 additions & 2 deletions src/lib/track/TrackManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class TrackManager extends Manager {
* @returns {Promise<Track>} Returns a promise with a single {@link Track}.
*/
async get(id: string): Promise<Track> {
const res = await this.http.get(`/tracks/${id}`);
const res = await this.http.get(`/v1/tracks/${id}`);

return res.data as Track;
}
Expand All @@ -22,7 +22,7 @@ export class TrackManager extends Manager {
async list(ids: string[]): Promise<Track[]> {
const tracks = await Promise.all(
chunk([...ids], 50).map(async (chunk) => {
const res = await this.http.get('/tracks', { query: { ids: chunk.join(',') } });
const res = await this.http.get('/v1/tracks', { query: { ids: chunk.join(',') } });
const json = res.data;

return json.tracks as Track[];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/user/UserManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class UserManager extends Manager {
* @returns {Promise<UserPublic>} Returns a promise with the {@link UserPublic}.
*/
async get(id: string): Promise<UserPublic> {
const res = await this.http.get(`/users/${id}`);
const res = await this.http.get(`/v1/users/${id}`);

return res.data as UserPublic;
}
Expand Down

0 comments on commit 9a89ba6

Please sign in to comment.