diff --git a/client.ts b/client.ts index b4f9a20..bec91dc 100644 --- a/client.ts +++ b/client.ts @@ -23,11 +23,11 @@ export class SpotifyError extends Error { super(message, options); } - get url() { + get url(): string { return this.response.url; } - get status() { + get status(): number { return this.response.status; } } @@ -142,7 +142,7 @@ export class SpotifyClient implements HTTPClient { : "https://api.spotify.com/"; } - fetch(path: string, opts: FetchLikeOptions = {}) { + fetch(path: string, opts: FetchLikeOptions = {}): Promise { const url = new URL(path, this.baseUrl); if (opts.query) { for (const key in opts.query) { diff --git a/endpoints/album/album.endpoints.ts b/endpoints/album/album.endpoints.ts index a2ba76f..860bcc0 100644 --- a/endpoints/album/album.endpoints.ts +++ b/endpoints/album/album.endpoints.ts @@ -15,7 +15,7 @@ export const getAlbum = async ( client: HTTPClient, albumId: string, market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/albums/" + albumId, { query: { market }, }); @@ -33,7 +33,7 @@ export const getAlbums = async ( client: HTTPClient, albumIds: string[], market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/albums", { query: { market, ids: albumIds }, }); @@ -62,7 +62,7 @@ export const getAlbumTracks = async ( client: HTTPClient, albumId: string, options?: GetAlbumTrackOpts, -) => { +): Promise> => { const res = await client.fetch(`/v1/albums/${albumId}/tracks`, { query: options, }); @@ -88,7 +88,7 @@ export type GetSavedAlbumsOpts = Prettify< export const getSavedAlbums = async ( client: HTTPClient, options?: GetSavedAlbumsOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/albums", { query: options }); return res.json() as Promise>; }; @@ -99,7 +99,10 @@ export const getSavedAlbums = async ( * @param client Spotify HTTPClient * @param albumIds List of the Spotify IDs for the albums. Maximum: 20 IDs */ -export const saveAlbums = (client: HTTPClient, albumIds: string[]) => { +export const saveAlbums = ( + client: HTTPClient, + albumIds: string[], +): Promise => { return client.fetch("/v1/me/albums", { method: "PUT", query: { ids: albumIds }, @@ -112,7 +115,10 @@ export const saveAlbums = (client: HTTPClient, albumIds: string[]) => { * @param client Spotify HTTPClient * @param albums_id The Spotify ID of the album */ -export const saveAlbum = (client: HTTPClient, albumId: string) => { +export const saveAlbum = ( + client: HTTPClient, + albumId: string, +): Promise => { return saveAlbums(client, [albumId]); }; @@ -122,7 +128,10 @@ export const saveAlbum = (client: HTTPClient, albumId: string) => { * @param client Spotify HTTPClient * @param albumIds List of the Spotify IDs for the albums. Maximum: 20 IDs */ -export const removeSavedAlbums = (client: HTTPClient, albumIds: string[]) => { +export const removeSavedAlbums = ( + client: HTTPClient, + albumIds: string[], +): Promise => { return client.fetch("/v1/me/albums", { method: "DELETE", query: { ids: albumIds }, @@ -135,7 +144,10 @@ export const removeSavedAlbums = (client: HTTPClient, albumIds: string[]) => { * @param client Spotify HTTPClient * @param albumId The Spotify ID of the album */ -export const removeSavedAlbum = (client: HTTPClient, albumId: string) => { +export const removeSavedAlbum = ( + client: HTTPClient, + albumId: string, +): Promise => { return removeSavedAlbums(client, [albumId]); }; @@ -148,7 +160,7 @@ export const removeSavedAlbum = (client: HTTPClient, albumId: string) => { export const checkIfAlbumsSaved = async ( client: HTTPClient, albumIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/albums/contains", { query: { ids: albumIds }, }); @@ -164,7 +176,7 @@ export const checkIfAlbumsSaved = async ( export const checkIfAlbumSaved = async ( client: HTTPClient, albumId: string, -) => { +): Promise => { return (await checkIfAlbumsSaved(client, [albumId]))[0]!; }; @@ -188,7 +200,7 @@ export type GetNewReleasesOpts = Prettify< export const getNewReleases = async ( client: HTTPClient, options?: GetNewReleasesOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/browse/new-releases", { query: options }); return ((await res.json()) as { albums: PagingObject }) .albums; diff --git a/endpoints/artist/artist.endpoints.ts b/endpoints/artist/artist.endpoints.ts index 2725db9..88285b5 100644 --- a/endpoints/artist/artist.endpoints.ts +++ b/endpoints/artist/artist.endpoints.ts @@ -11,7 +11,10 @@ import type { Artist } from "./artist.types.ts"; * @param client Spotify HTTPClient * @param artistId Spotify artist ID */ -export const getArtist = async (client: HTTPClient, artistId: string) => { +export const getArtist = async ( + client: HTTPClient, + artistId: string, +): Promise => { const res = await client.fetch("/v1/artists/" + artistId); return res.json() as Promise; }; @@ -25,7 +28,7 @@ export const getArtist = async (client: HTTPClient, artistId: string) => { export const getArtists = async ( client: HTTPClient, artistIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/artists", { query: { ids: artistIds } }); return ((await res.json()) as { artists: Artist[] }).artists; }; @@ -55,7 +58,7 @@ export const getArtistAlbums = async ( client: HTTPClient, artistId: string, options?: GetArtistAlbumsOpts, -) => { +): Promise> => { const res = await client.fetch(`/v1/artists/${artistId}/albums`, { query: options, }); @@ -73,7 +76,7 @@ export const getArtistTopTracks = async ( client: HTTPClient, artistId: string, market?: string, -) => { +): Promise => { const res = await client.fetch(`/v1/artists/${artistId}/top-tracks`, { query: { market }, }); @@ -90,7 +93,7 @@ export const getArtistTopTracks = async ( export const getArtistRelatedArtists = async ( client: HTTPClient, artistId: string, -) => { +): Promise => { const res = await client.fetch(`/v1/artists/${artistId}/related-artists`); return ((await res.json()) as { artists: Artist[] }).artists; }; diff --git a/endpoints/audiobook/audiobook.endpoints.ts b/endpoints/audiobook/audiobook.endpoints.ts index 26a7600..221510f 100644 --- a/endpoints/audiobook/audiobook.endpoints.ts +++ b/endpoints/audiobook/audiobook.endpoints.ts @@ -2,7 +2,7 @@ import type { SimplifiedChapter } from "../chapter/chapter.types.ts"; import type { HTTPClient } from "../../client.ts"; import type { Prettify } from "../../shared.ts"; import type { PagingObject, PagingOptions } from "../general.types.ts"; -import type { Audiobook, SimplifiedAudiobook } from "./audiobook.types.ts"; +import type { Audiobook, SavedAudiobook } from "./audiobook.types.ts"; /** * Get Spotify catalog information for a single Audiobook. @@ -15,7 +15,7 @@ export const getAudiobook = async ( client: HTTPClient, audiobookId: string, market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/audiobooks/" + audiobookId, { query: { market }, }); @@ -33,7 +33,7 @@ export const getAudiobooks = async ( client: HTTPClient, audiobookIds: string[], market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/audiobooks", { query: { market, ids: audiobookIds }, }); @@ -62,7 +62,7 @@ export const getAudiobookChapters = async ( client: HTTPClient, audiobookId: string, options?: GetAudiobookChapterOpts, -) => { +): Promise> => { const res = await client.fetch(`/v1/audiobooks/${audiobookId}/chapters`, { query: options, }); @@ -88,20 +88,9 @@ export type GetSavedAudiobooksOpts = Prettify< export const getSavedAudiobooks = async ( client: HTTPClient, options?: GetSavedAudiobooksOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/audiobooks", { query: options }); - return res.json() as Promise< - PagingObject<{ - /** - * The date and time the audiobook was saved Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - */ - added_at: string; - /** - * Information about the audiobook. - */ - audiobook: SimplifiedAudiobook; - }> - >; + return res.json() as Promise>; }; /** @@ -113,7 +102,7 @@ export const getSavedAudiobooks = async ( export const saveAudiobooks = ( client: HTTPClient, audiobookIds: string[], -) => { +): Promise => { return client.fetch("/v1/me/audiobooks", { method: "PUT", query: { ids: audiobookIds }, @@ -129,7 +118,7 @@ export const saveAudiobooks = ( export const saveAudiobook = ( client: HTTPClient, audiobookId: string, -) => { +): Promise => { return saveAudiobooks(client, [audiobookId]); }; @@ -142,7 +131,7 @@ export const saveAudiobook = ( export const removeSavedAudiobooks = ( client: HTTPClient, audiobookIds: string[], -) => { +): Promise => { return client.fetch("/v1/me/audiobooks", { method: "DELETE", query: { ids: audiobookIds }, @@ -158,7 +147,7 @@ export const removeSavedAudiobooks = ( export const removeSavedAudiobook = ( client: HTTPClient, audiobookId: string, -) => { +): Promise => { return removeSavedAudiobooks(client, [audiobookId]); }; @@ -171,7 +160,7 @@ export const removeSavedAudiobook = ( export const checkIfAudiobooksSaved = async ( client: HTTPClient, audiobookIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/audiobooks/contains", { query: { ids: audiobookIds }, }); @@ -187,6 +176,6 @@ export const checkIfAudiobooksSaved = async ( export const checkIfAudiobookSaved = async ( client: HTTPClient, audiobookId: string, -) => { +): Promise => { return (await checkIfAudiobooksSaved(client, [audiobookId]))[0]!; }; diff --git a/endpoints/audiobook/audiobook.types.ts b/endpoints/audiobook/audiobook.types.ts index bad5ea6..8cf465c 100644 --- a/endpoints/audiobook/audiobook.types.ts +++ b/endpoints/audiobook/audiobook.types.ts @@ -90,3 +90,14 @@ export interface Audiobook extends SimplifiedAudiobook { */ chapters: PagingObject; } + +export type SavedAudiobook = { + /** + * The date and time the audiobook was saved Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. + */ + added_at: string; + /** + * Information about the audiobook. + */ + audiobook: SimplifiedAudiobook; +}; \ No newline at end of file diff --git a/endpoints/category/category.endpoints.ts b/endpoints/category/category.endpoints.ts index d2d6edb..d804f7d 100644 --- a/endpoints/category/category.endpoints.ts +++ b/endpoints/category/category.endpoints.ts @@ -40,7 +40,7 @@ export type GetBrowseCategoriesOpts = { export const getBrowseCategories = async ( client: HTTPClient, options?: GetBrowseCategoriesOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/browse/categories", { query: options, }); @@ -73,7 +73,7 @@ export const getBrowseCategory = async ( client: HTTPClient, categoryId: string, options?: GetBrowseCategoryOpts, -) => { +): Promise => { const res = await client.fetch("/v1/browse/categories/" + categoryId, { query: options, }); diff --git a/endpoints/chapter/chapter.endpoints.ts b/endpoints/chapter/chapter.endpoints.ts index 3ad3956..0350757 100644 --- a/endpoints/chapter/chapter.endpoints.ts +++ b/endpoints/chapter/chapter.endpoints.ts @@ -10,7 +10,7 @@ export const getChapter = async ( client: HTTPClient, chapterId: string, market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/chapters/" + chapterId, { query: { market }, }); @@ -27,7 +27,7 @@ export const getChapters = async ( client: HTTPClient, chapterIds: string[], market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/chapters", { query: { market, ids: chapterIds }, }); diff --git a/endpoints/episode/episode.endpoints.ts b/endpoints/episode/episode.endpoints.ts index 25de606..1e4909b 100644 --- a/endpoints/episode/episode.endpoints.ts +++ b/endpoints/episode/episode.endpoints.ts @@ -2,6 +2,7 @@ import type { HTTPClient } from "../../client.ts"; import type { Episode } from "./episode.types.ts"; import type { PagingObject, PagingOptions } from "../general.types.ts"; import type { Prettify } from "../../shared.ts"; +import { SavedEpisode } from "./episode.types.ts"; /** * Get Spotify catalog informnation for a single episode. @@ -14,7 +15,7 @@ export const getEpisode = async ( client: HTTPClient, episodeId: string, market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/episodes/" + episodeId, { query: { market }, }); @@ -32,7 +33,7 @@ export const getEpisodes = async ( client: HTTPClient, episodeIds: string[], market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/episodes", { query: { market, ids: episodeIds }, }); @@ -58,20 +59,9 @@ export type GetSavedEpisodesOpts = Prettify< export const getSavedEpisodes = async ( client: HTTPClient, options?: GetSavedEpisodesOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/episodes", { query: options }); - return res.json() as Promise< - PagingObject<{ - /** - * The date and time the episode was saved Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - */ - added_at: string; - /** - * Information about the episode. - */ - episode: Episode; - }> - >; + return res.json(); }; /** @@ -83,7 +73,7 @@ export const getSavedEpisodes = async ( export const saveEpisodes = ( client: HTTPClient, episodeIds: string[], -) => { +): Promise => { return client.fetch(`/v1/me/episodes`, { method: "PUT", query: { ids: episodeIds }, @@ -96,7 +86,7 @@ export const saveEpisodes = ( * @param client Spotify HTTPClient * @param episodeId The Spotify ID of the episode */ -export const saveEpisode = (client: HTTPClient, episodeId: string) => { +export const saveEpisode = (client: HTTPClient, episodeId: string): Promise => { return saveEpisodes(client, [episodeId]); }; @@ -109,7 +99,7 @@ export const saveEpisode = (client: HTTPClient, episodeId: string) => { export const removeSavedEpisodes = ( client: HTTPClient, episodeIds: string[], -) => { +): Promise => { return client.fetch("/v1/me/episodes", { query: { ids: episodeIds }, }); @@ -124,7 +114,7 @@ export const removeSavedEpisodes = ( export const removeSavedEpisode = ( client: HTTPClient, episodeId: string, -) => { +): Promise => { return removeSavedEpisodes(client, [episodeId]); }; @@ -137,7 +127,7 @@ export const removeSavedEpisode = ( export const checkIfEpisodesSaved = async ( client: HTTPClient, episodeIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/episodes/contains", { query: { ids: episodeIds }, }); @@ -153,6 +143,6 @@ export const checkIfEpisodesSaved = async ( export const checkIfEpisodeSaved = async ( client: HTTPClient, episodeId: string, -) => { +): Promise => { return (await checkIfEpisodesSaved(client, [episodeId]))[0]!; }; diff --git a/endpoints/episode/episode.types.ts b/endpoints/episode/episode.types.ts index 09ad869..1418547 100644 --- a/endpoints/episode/episode.types.ts +++ b/endpoints/episode/episode.types.ts @@ -95,3 +95,14 @@ export interface Episode extends SimplifiedEpisode { */ show: SimplifiedShow; } + +export type SavedEpisode = { + /** + * The date and time the episode was saved Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. + */ + added_at: string; + /** + * Information about the episode. + */ + episode: Episode; +} diff --git a/endpoints/genre/genre.endpoints.ts b/endpoints/genre/genre.endpoints.ts index 91b01f4..4f5a0d4 100644 --- a/endpoints/genre/genre.endpoints.ts +++ b/endpoints/genre/genre.endpoints.ts @@ -5,7 +5,7 @@ import type { HTTPClient } from "../../client.ts"; * * @param client Spotify HTTPClient */ -export const getAvailableGenreSeeds = async (client: HTTPClient) => { +export const getAvailableGenreSeeds = async (client: HTTPClient): Promise => { const res = await client.fetch("/v1/recommendations/available-genre-seeds"); return ((await res.json()) as { genres: string[] }).genres; }; diff --git a/endpoints/market/market.endpoints.ts b/endpoints/market/market.endpoints.ts index 8d4d3d8..34822ef 100644 --- a/endpoints/market/market.endpoints.ts +++ b/endpoints/market/market.endpoints.ts @@ -5,7 +5,7 @@ import type { HTTPClient } from "../../client.ts"; * * @param client Spotify HTTPClient */ -export const getAvailableMarkets = async (client: HTTPClient) => { +export const getAvailableMarkets = async (client: HTTPClient): Promise => { const res = await client.fetch("/v1/markets"); return (await res.json() as { markets: string[] }).markets; }; diff --git a/endpoints/player/player.endpoints.ts b/endpoints/player/player.endpoints.ts index a1010d5..d305d96 100644 --- a/endpoints/player/player.endpoints.ts +++ b/endpoints/player/player.endpoints.ts @@ -30,7 +30,7 @@ export type GetPlaybackStateOpts = { export const getPlaybackState = async ( client: HTTPClient, options: GetPlaybackStateOpts = {}, -) => { +): Promise => { const res = await client.fetch("/v1/me/player", { query: { market: options.market, @@ -38,7 +38,7 @@ export const getPlaybackState = async ( }, }); if (res.status === 204) return null; - return res.json() as Promise; + return res.json(); }; export type TransferPlaybackBody = { @@ -63,7 +63,7 @@ export type TransferPlaybackBody = { export const transferPlayback = ( client: HTTPClient, body: TransferPlaybackBody, -) => { +): Promise => { return client.fetch("/v1/me/player", { method: "PUT", body }); }; @@ -72,7 +72,9 @@ export const transferPlayback = ( * * @requires `user-read-playback-state` */ -export const getAvailableDevices = async (client: HTTPClient) => { +export const getAvailableDevices = async ( + client: HTTPClient, +): Promise => { const res = await client.fetch("/v1/me/player/devices"); return (await res.json() as { devices: Device[] }).devices; }; @@ -96,7 +98,7 @@ export type GetCurrentPlayingTrackOpts = { export const getCurrentPlayingTrack = async ( client: HTTPClient, options?: GetCurrentPlayingTrackOpts, -) => { +): Promise => { const res = await client.fetch("/v1/me/player/currently-playing", { query: options, }); @@ -152,7 +154,7 @@ export type StartResumePlaybackBody = { export const startPlayback = ( client: HTTPClient, options: StartResumePlaybackBody = {}, -) => { +): Promise => { const { device_id, ...body } = options; return client.fetch("/v1/me/player/play", { method: "PUT", @@ -179,7 +181,7 @@ export const resumePlayback = startPlayback; export const pausePlayback = ( client: HTTPClient, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/pause", { method: "PUT", query: { device_id: deviceId }, @@ -197,7 +199,7 @@ export const pausePlayback = ( export const skipToNext = ( client: HTTPClient, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/next", { method: "POST", query: { device_id: deviceId }, @@ -215,7 +217,7 @@ export const skipToNext = ( export const skipToPrevious = ( client: HTTPClient, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/previous", { method: "POST", query: { device_id: deviceId }, @@ -235,7 +237,7 @@ export const seekToPosition = ( client: HTTPClient, positionMs: number, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/seek", { method: "PUT", query: { position_ms: positionMs, device_id: deviceId }, @@ -258,7 +260,7 @@ export const setRepeatMode = ( client: HTTPClient, state: RepeatMode, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/repeat", { method: "PUT", query: { state, device_id: deviceId }, @@ -278,7 +280,7 @@ export const togglePlaybackShuffle = ( client: HTTPClient, state: boolean, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/shuffle", { method: "PUT", query: { state, device_id: deviceId }, @@ -311,11 +313,11 @@ export type GetRecentlyPlayedTracksOpts = { export const getRecentPlayedTracks = async ( client: HTTPClient, options?: GetRecentlyPlayedTracksOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/player/recently-played", { query: options, }); - return res.json() as Promise>; + return res.json(); }; /** @@ -324,7 +326,7 @@ export const getRecentPlayedTracks = async ( * @requires `user-read-currently-playing`, `user-read-playback-state` */ -export const getUserQueue = async (client: HTTPClient) => { +export const getUserQueue = async (client: HTTPClient): Promise => { const res = await client.fetch("/v1/me/player/queue"); return res.json() as Promise; }; @@ -342,7 +344,7 @@ export const addItemToPlaybackQueue = ( client: HTTPClient, uri: string, deviceId?: string, -) => { +): Promise => { return client.fetch("/v1/me/player/queue", { method: "POST", query: { uri, device_id: deviceId }, diff --git a/endpoints/playlist/playlist.endpoints.ts b/endpoints/playlist/playlist.endpoints.ts index 355c40f..ff1ebce 100644 --- a/endpoints/playlist/playlist.endpoints.ts +++ b/endpoints/playlist/playlist.endpoints.ts @@ -42,7 +42,7 @@ export const getPlaylist = async ( client: HTTPClient, playlistId: string, options?: GetPlaylistOpts, -) => { +): Promise => { const res = await client.fetch("/v1/playlists/" + playlistId, { query: options, }); @@ -81,7 +81,7 @@ export const changePlaylistDetails = ( client: HTTPClient, playlistId: string, body: ChangePlaylistDetailsBody, -) => { +): Promise => { return client.fetch("/v1/playlist/" + playlistId, { method: "PUT", body }); }; @@ -108,7 +108,7 @@ export const getPlaylistTracks = async ( client: HTTPClient, playlistId: string, options?: GetPlaylistTracksOpts, -) => { +): Promise> => { const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, { query: options, }); @@ -128,7 +128,7 @@ export const addItemsToPlaylist = async ( playlistId: string, uris: string[], position?: number, -) => { +): Promise => { const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, { method: "POST", query: { uris, position }, @@ -149,7 +149,7 @@ export const addItemToPlaylist = ( playlistId: string, uri: string, position?: number, -) => { +): Promise => { return addItemsToPlaylist(client, playlistId, [uri], position); }; @@ -184,7 +184,7 @@ export const reorderPlaylistItems = async ( client: HTTPClient, playlistId: string, options?: ReorderPlaylistItemsOpts, -) => { +): Promise => { const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, { method: "PUT", body: options, @@ -204,7 +204,7 @@ export const replacePlaylistItems = async ( client: HTTPClient, playlistId: string, uris: string[], -) => { +): Promise => { const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, { method: "PUT", body: { uris }, @@ -225,7 +225,7 @@ export const removePlaylistItems = async ( playlistId: string, uris: string[], snapshotId?: string, -) => { +): Promise => { const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, { method: "DELETE", body: { @@ -249,7 +249,7 @@ export const removePlaylistItem = ( playlistId: string, uri: string, snapshotId?: string, -) => { +): Promise => { return removePlaylistItems(client, playlistId, [uri], snapshotId); }; @@ -262,7 +262,7 @@ export const removePlaylistItem = ( export const getCurrentUsersPlaylists = async ( client: HTTPClient, options?: PagingOptions, -) => { +): Promise> => { const res = await client.fetch("/v1/me/playlists", { query: options }); return res.json() as Promise>; }; @@ -278,7 +278,7 @@ export const getUsersPlaylists = async ( client: HTTPClient, userId: string, options?: PagingOptions, -) => { +): Promise> => { const res = await client.fetch(`/v1/users/${userId}/playlists`, { query: options, }); @@ -315,7 +315,7 @@ export const createPlaylist = async ( client: HTTPClient, userId: string, body: CreatePlaylistBody, -) => { +): Promise => { const res = await client.fetch(`/v1/users/${userId}/playlists`, { method: "POST", body, @@ -353,7 +353,7 @@ export type GetFeaturedPlaylistsOpts = Prettify< export const getFeaturedPlaylists = async ( client: HTTPClient, options?: GetFeaturedPlaylistsOpts, -) => { +): Promise => { const res = await client.fetch("/v1/browse/featured-playlists", { query: options, }); @@ -381,7 +381,7 @@ export const getCategoryPlaylists = async ( client: HTTPClient, categoryId: string, options?: GetCategorysPlaylistsOpts, -) => { +): Promise => { const res = await client.fetch( `/v1/browse/categories/${categoryId}/playlists`, { query: options }, @@ -397,7 +397,7 @@ export const getCategoryPlaylists = async ( export const getPlaylistCoverImage = async ( client: HTTPClient, playlistId: string, -) => { +): Promise[]> => { const res = await client.fetch(`/v1/playlists/${playlistId}/images`); return res.json() as Promise[]>; }; @@ -412,7 +412,7 @@ export const uploadPlaylistCoverImage = ( client: HTTPClient, playlistId: string, image: string, -) => { +): Promise => { return client.fetch(`/v1/playlists/${playlistId}/images`, { method: "PUT", headers: { diff --git a/endpoints/show/show.endpoints.ts b/endpoints/show/show.endpoints.ts index 9b284d8..2d6a69e 100644 --- a/endpoints/show/show.endpoints.ts +++ b/endpoints/show/show.endpoints.ts @@ -2,7 +2,7 @@ import type { HTTPClient } from "../../client.ts"; import type { Prettify } from "../../shared.ts"; import type { SimplifiedEpisode } from "../episode/episode.types.ts"; import type { PagingObject, PagingOptions } from "../general.types.ts"; -import type { Show, SimplifiedShow } from "./show.types.ts"; +import type { SavedShow, Show, SimplifiedShow } from "./show.types.ts"; /** * Get spotify catalog information for a single show by its unique Spotify ID. @@ -15,7 +15,7 @@ export const getShow = async ( client: HTTPClient, showId: string, market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/shows/" + showId, { query: { market } }); return res.json() as Promise; }; @@ -31,7 +31,7 @@ export const getShows = async ( client: HTTPClient, showIds: string[], market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/shows", { query: { market, ids: showIds }, }); @@ -60,7 +60,7 @@ export const getShowEpisodes = async ( client: HTTPClient, showId: string, options?: GetShowEpisodesOpts, -) => { +): Promise> => { const res = await client.fetch(`/v1/shows/${showId}/episodes`, { query: options, }); @@ -86,20 +86,9 @@ export type GetSavedShowsOpts = Prettify< export const getSavedShows = async ( client: HTTPClient, options?: GetSavedShowsOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/shows", { query: options }); - return res.json() as Promise< - PagingObject<{ - /** - * The date and time the album was saved Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - */ - added_at: string; - /** - * Information about the show. - */ - show: Show; - }> - >; + return res.json() as Promise>; }; /** @@ -108,7 +97,10 @@ export const getSavedShows = async ( * @param client Spotify HTTPClient * @param showIds List of the Spotify IDs for the shows. Maximum: 20 */ -export const saveShows = (client: HTTPClient, showIds: string[]) => { +export const saveShows = ( + client: HTTPClient, + showIds: string[], +): Promise => { return client.fetch("/v1/me/shows", { method: "PUT", query: { ids: showIds }, @@ -121,7 +113,10 @@ export const saveShows = (client: HTTPClient, showIds: string[]) => { * @param client Spotify HTTPClient * @param showId The Spotify ID of the show */ -export const saveShow = (client: HTTPClient, showId: string) => { +export const saveShow = ( + client: HTTPClient, + showId: string, +): Promise => { return saveShows(client, [showId]); }; @@ -131,7 +126,10 @@ export const saveShow = (client: HTTPClient, showId: string) => { * @param client Spotify HTTPClient * @param showIds List of the Spotify IDs for the shows. Maximum: 20 */ -export const removeSavedShows = (client: HTTPClient, showIds: string[]) => { +export const removeSavedShows = ( + client: HTTPClient, + showIds: string[], +): Promise => { return client.fetch("/v1/me/shows", { method: "DELETE", query: { @@ -146,7 +144,10 @@ export const removeSavedShows = (client: HTTPClient, showIds: string[]) => { * @param client Spotify HTTPClient * @param showId The Spotify ID of the show */ -export const removeSavedShow = (client: HTTPClient, showId: string) => { +export const removeSavedShow = ( + client: HTTPClient, + showId: string, +): Promise => { return removeSavedShows(client, [showId]); }; @@ -159,7 +160,7 @@ export const removeSavedShow = (client: HTTPClient, showId: string) => { export const checkIfShowsSaved = async ( client: HTTPClient, showIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/shows/contains", { query: { ids: showIds, @@ -174,6 +175,9 @@ export const checkIfShowsSaved = async ( * @param client Spotify HTTPClient * @param showId The Spotify ID of the show */ -export const checkIfShowSaved = async (client: HTTPClient, showId: string) => { +export const checkIfShowSaved = async ( + client: HTTPClient, + showId: string, +): Promise => { return (await checkIfShowsSaved(client, [showId]))[0]!; }; diff --git a/endpoints/show/show.types.ts b/endpoints/show/show.types.ts index fbe615b..032e773 100644 --- a/endpoints/show/show.types.ts +++ b/endpoints/show/show.types.ts @@ -80,3 +80,14 @@ export interface Show extends SimplifiedShow { */ episodes: PagingObject; } + +export type SavedShow = { + /** + * The date and time the album was saved Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. + */ + added_at: string; + /** + * Information about the show. + */ + show: Show; +}; diff --git a/endpoints/track/track.endpoints.ts b/endpoints/track/track.endpoints.ts index 020b8df..b8290db 100644 --- a/endpoints/track/track.endpoints.ts +++ b/endpoints/track/track.endpoints.ts @@ -21,7 +21,7 @@ export const getTrack = async ( client: HTTPClient, trackId: string, market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/tracks/" + trackId, { query: { market }, }); @@ -39,7 +39,7 @@ export const getTracks = async ( client: HTTPClient, trackIds: string[], market?: string, -) => { +): Promise => { const res = await client.fetch("/v1/tracks", { query: { ids: trackIds, @@ -68,7 +68,7 @@ export type GetSavedTracksOpts = Prettify< export const getSavedTracks = async ( client: HTTPClient, options: GetSavedTracksOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/tracks", { query: options, }); @@ -81,7 +81,10 @@ export const getSavedTracks = async ( * @param client Spotify HTTPClient * @param trackIds List of the Spotify track IDs. Maximum 50 IDs */ -export const saveTracks = (client: HTTPClient, trackIds: string[]) => { +export const saveTracks = ( + client: HTTPClient, + trackIds: string[], +): Promise => { return client.fetch("/v1/me/tracks", { method: "PUT", query: { @@ -96,7 +99,10 @@ export const saveTracks = (client: HTTPClient, trackIds: string[]) => { * @param client Spotify HTTPClient * @param trackId Spotify track ID */ -export const saveTrack = (client: HTTPClient, trackId: string) => { +export const saveTrack = ( + client: HTTPClient, + trackId: string, +): Promise => { return saveTracks(client, [trackId]); }; @@ -106,7 +112,10 @@ export const saveTrack = (client: HTTPClient, trackId: string) => { * @param client Spotify HTTPClient * @param trackIds List of the Spotify track IDs. Maximum 50 IDs */ -export const removeSavedTracks = (client: HTTPClient, trackIds: string[]) => { +export const removeSavedTracks = ( + client: HTTPClient, + trackIds: string[], +): Promise => { return client.fetch("/v1/me/tracks", { method: "DELETE", query: { @@ -121,7 +130,10 @@ export const removeSavedTracks = (client: HTTPClient, trackIds: string[]) => { * @param client Spotify HTTPClient * @param trackId Spotify track ID */ -export const removeSavedTrack = (client: HTTPClient, trackId: string) => { +export const removeSavedTrack = ( + client: HTTPClient, + trackId: string, +): Promise => { return removeSavedTracks(client, [trackId]); }; @@ -134,7 +146,7 @@ export const removeSavedTrack = (client: HTTPClient, trackId: string) => { export const checkIfTracksSaved = async ( client: HTTPClient, track_ids: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/tracks/contains", { query: { ids: track_ids, @@ -152,7 +164,7 @@ export const checkIfTracksSaved = async ( export const checkIfTrackSaved = async ( client: HTTPClient, trackId: string, -) => { +): Promise => { return (await checkIfTracksSaved(client, [trackId]))[0]; }; @@ -165,7 +177,7 @@ export const checkIfTrackSaved = async ( export const getTracksAudioFeatures = async ( client: HTTPClient, track_ids: string[], -) => { +): Promise => { const res = await client.fetch("/v1/audio-features", { query: { ids: track_ids, @@ -184,7 +196,7 @@ export const getTracksAudioFeatures = async ( export const getTrackAudioFeatures = async ( client: HTTPClient, trackId: string, -) => { +): Promise => { const res = await client.fetch("/v1/audio-features/" + trackId); return res.json() as Promise; }; @@ -199,7 +211,7 @@ export const getTrackAudioFeatures = async ( export const getTracksAudioAnalysis = async ( client: HTTPClient, trackId: string, -) => { +): Promise => { const res = await client.fetch("/v1/audio-analysis/" + trackId); return res.json() as Promise; }; @@ -213,7 +225,7 @@ export const getTracksAudioAnalysis = async ( export const getRecommendations = async ( client: HTTPClient, options: RecommendationsOptions, -) => { +): Promise => { const res = await client.fetch("/v1/recommendations", { query: options, }); diff --git a/endpoints/user/user.endpoints.ts b/endpoints/user/user.endpoints.ts index c3c78b9..50cb264 100644 --- a/endpoints/user/user.endpoints.ts +++ b/endpoints/user/user.endpoints.ts @@ -14,9 +14,11 @@ import type { Prettify } from "../../shared.ts"; * * @param client Spotify HTTPClient */ -export const getCurrentUser = async (client: HTTPClient) => { +export const getCurrentUser = async ( + client: HTTPClient, +): Promise => { const res = await client.fetch("/v1/me"); - return res.json() as Promise; + return res.json(); }; export type GetUserTopItemsOpts = Prettify< @@ -51,13 +53,13 @@ interface UserTopItemMap extends Record { * @param type The type of entity to return. ("artists" or "tracks") * @param opts Additional option for request */ -export const getUserTopItems = async ( +export const getUserTopItems = async ( client: HTTPClient, - type: T, + type: TItem, options?: GetUserTopItemsOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/top/" + type, { query: options }); - return res.json() as Promise>; + return res.json() as Promise>; }; /** @@ -71,7 +73,7 @@ export const getUserTopItems = async ( export const getUserTopArtists = async ( client: HTTPClient, opts?: GetUserTopItemsOpts, -) => { +): Promise> => { return await getUserTopItems(client, "artists", opts); }; @@ -86,7 +88,7 @@ export const getUserTopArtists = async ( export const getUserTopTracks = async ( client: HTTPClient, opts?: GetUserTopItemsOpts, -) => { +): Promise> => { return await getUserTopItems(client, "tracks", opts); }; @@ -96,7 +98,10 @@ export const getUserTopTracks = async ( * @param client Spotify HTTPClient * @param userId Spotify user ID */ -export const getUser = async (client: HTTPClient, userId: string) => { +export const getUser = async ( + client: HTTPClient, + userId: string, +): Promise => { const res = await client.fetch("/v1/users/" + userId); return res.json() as Promise; }; @@ -111,12 +116,12 @@ export const getUser = async (client: HTTPClient, userId: string) => { * @param isPublic If true the playlist will be included in user's public * playlists, if false it will remain private. By default - true */ -export const followPlaylist = async ( +export const followPlaylist = ( client: HTTPClient, playlistId: string, isPublic?: boolean, -) => { - await client.fetch(`/v1/playlists/${playlistId}/followers`, { +): Promise => { + return client.fetch(`/v1/playlists/${playlistId}/followers`, { method: "PUT", body: { public: isPublic }, }); @@ -130,11 +135,11 @@ export const followPlaylist = async ( * @param client Spotify HTTPClient * @param playlistId Spotify playlist ID */ -export const unfollowPlaylist = async ( +export const unfollowPlaylist = ( client: HTTPClient, playlistId: string, -) => { - await client.fetch(`/v1/playlists/${playlistId}/followers`, { +): Promise => { + return client.fetch(`/v1/playlists/${playlistId}/followers`, { method: "DELETE", }); }; @@ -162,7 +167,7 @@ export type GetFollowedArtistsOpts = { export const getFollowedArtists = async ( client: HTTPClient, options?: GetFollowedArtistsOpts, -) => { +): Promise> => { const res = await client.fetch("/v1/me/following", { query: { ...options, @@ -181,7 +186,10 @@ export const getFollowedArtists = async ( * @param client Spotify HTTPClient * @param artistIds List of Spotify artist IDs. Maximum 50 */ -export const followArtists = (client: HTTPClient, artistIds: string[]) => { +export const followArtists = ( + client: HTTPClient, + artistIds: string[], +): Promise => { return client.fetch("/v1/me/following", { method: "PUT", query: { @@ -199,7 +207,10 @@ export const followArtists = (client: HTTPClient, artistIds: string[]) => { * @param client Spotify HTTPClient * @param artistId Spotify artist ID */ -export const followArtist = (client: HTTPClient, artistId: string) => { +export const followArtist = ( + client: HTTPClient, + artistId: string, +): Promise => { return followArtists(client, [artistId]); }; @@ -211,7 +222,10 @@ export const followArtist = (client: HTTPClient, artistId: string) => { * @param client Spotify HTTPClient * @param userIds List of Spotify user IDs. Maximum 50 */ -export const followUsers = (client: HTTPClient, userIds: string[]) => { +export const followUsers = ( + client: HTTPClient, + userIds: string[], +): Promise => { return client.fetch("/v1/me/following", { method: "PUT", query: { @@ -229,7 +243,10 @@ export const followUsers = (client: HTTPClient, userIds: string[]) => { * @param client Spotify HTTPClient * @param artist_id Spotify user ID */ -export const followUser = (client: HTTPClient, userId: string) => { +export const followUser = ( + client: HTTPClient, + userId: string, +): Promise => { return followUsers(client, [userId]); }; @@ -241,7 +258,10 @@ export const followUser = (client: HTTPClient, userId: string) => { * @param client Spotify HTTPClient * @param artistIds List of Spotify artist IDs. Maximum 50 */ -export const unfollowArtists = (client: HTTPClient, artistIds: string[]) => { +export const unfollowArtists = ( + client: HTTPClient, + artistIds: string[], +): Promise => { return client.fetch("/v1/me/following", { method: "DELETE", query: { @@ -259,7 +279,10 @@ export const unfollowArtists = (client: HTTPClient, artistIds: string[]) => { * @param client Spotify HTTPClient * @param artistId Spotify artist ID */ -export const unfollowArtist = (client: HTTPClient, artistId: string) => { +export const unfollowArtist = ( + client: HTTPClient, + artistId: string, +): Promise => { return unfollowArtists(client, [artistId]); }; @@ -271,7 +294,10 @@ export const unfollowArtist = (client: HTTPClient, artistId: string) => { * @param client Spotify HTTPClient * @param userIds List of Spotify user IDs. Maximum 50 */ -export const unfollowUsers = (client: HTTPClient, userIds: string[]) => { +export const unfollowUsers = ( + client: HTTPClient, + userIds: string[], +): Promise => { return client.fetch("/v1/me/following", { method: "DELETE", query: { @@ -289,7 +315,10 @@ export const unfollowUsers = (client: HTTPClient, userIds: string[]) => { * @param client Spotify HTTPClient * @param artist_id Spotify user ID */ -export const unfollowUser = (client: HTTPClient, userId: string) => { +export const unfollowUser = ( + client: HTTPClient, + userId: string, +): Promise => { return unfollowUsers(client, [userId]); }; @@ -304,7 +333,7 @@ export const unfollowUser = (client: HTTPClient, userId: string) => { export const checkIfUserFollowsArtists = async ( client: HTTPClient, artistIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/following/contains", { query: { type: "artist", @@ -325,7 +354,7 @@ export const checkIfUserFollowsArtists = async ( export const checkIfUserFollowsArtist = async ( client: HTTPClient, artistId: string, -) => { +): Promise => { return (await checkIfUserFollowsArtists(client, [artistId]))[0]!; }; @@ -340,7 +369,7 @@ export const checkIfUserFollowsArtist = async ( export const checkIfUserFollowsUsers = async ( client: HTTPClient, userIds: string[], -) => { +): Promise => { const res = await client.fetch("/v1/me/following/contains", { query: { type: "user", @@ -361,7 +390,7 @@ export const checkIfUserFollowsUsers = async ( export const checkIfUserFollowsUser = async ( client: HTTPClient, userId: string, -) => { +): Promise => { return (await checkIfUserFollowsUsers(client, [userId]))[0]!; }; @@ -376,7 +405,7 @@ export const checkIfUsersFollowPlaylist = async ( client: HTTPClient, userIds: string[], playlistId: string, -) => { +): Promise => { const res = await client.fetch( `/v1/playlists/${playlistId}/followers/contains`, { @@ -399,6 +428,6 @@ export const checkIfUserFollowsPlaylist = async ( client: HTTPClient, userId: string, playlistId: string, -) => { +): Promise => { return (await checkIfUsersFollowPlaylist(client, [userId], playlistId))[0]!; }; diff --git a/pagination.ts b/pagination.ts index 0eef0d3..0c45c1b 100644 --- a/pagination.ts +++ b/pagination.ts @@ -33,7 +33,11 @@ export class ChunkIterator { this.defaults = { ...DEFAULTS, ...defaults }; } - asyncIterator() { + asyncIterator(): AsyncIterator< + TItem[], + TItem[], + NextPageOptions | undefined + > { return this[Symbol.asyncIterator](); } @@ -78,7 +82,7 @@ export class PageIterator { this.defaults = { ...DEFAULTS, ...defaults }; } - asyncIterator() { + asyncIterator(): AsyncGenerator { return this[Symbol.asyncIterator](); } @@ -104,7 +108,7 @@ export class PageIterator { } } - async collect() { + async collect(): Promise { const items: TItem[] = []; for await (const item of this) { items.push(item); diff --git a/tsconfig.json b/tsconfig.json index 0633539..2d7cdc8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ "module": "ESNext", "target": "ESNext", "outDir": "dist", + "declaration": true, "emitDeclarationOnly": true, "allowImportingTsExtensions": true, "forceConsistentCasingInFileNames": true, @@ -26,4 +27,4 @@ "allowUnusedLabels": false, "skipLibCheck": true } -} +} \ No newline at end of file