Skip to content

Commit

Permalink
rename: getNewAlbumReleases to getNewReleases and `getSeveralAlbu…
Browse files Browse the repository at this point in the history
…ms` to `getAlbums`
  • Loading branch information
MellKam committed Feb 16, 2024
1 parent 545c84e commit f98fdde
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 153 deletions.
2 changes: 1 addition & 1 deletion client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Deno.test("SpotifyClient: error handling", async () => {
mock("PUT@/v1/playlists/:playlistId/followers", () => {
return Response.json(
{ error: { status: 400, message: "Invalid request" } },
{ status: 400, statusText: "Bad Request" }
{ status: 400, statusText: "Bad Request" },
);
});

Expand Down
14 changes: 7 additions & 7 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class SpotifyError extends Error {
message: string,
public readonly response: Response,
public readonly body: RegularErrorObject | string | null,
options?: ErrorOptions
options?: ErrorOptions,
) {
super(message, options);
}
Expand All @@ -45,7 +45,7 @@ const getBodyMessage = (body: RegularErrorObject | string | null) => {

const createSpotifyError = async (
response: Response,
options?: ErrorOptions
options?: ErrorOptions,
) => {
let message = response.statusText
? `${response.status} ${response.statusText}`
Expand Down Expand Up @@ -89,7 +89,7 @@ export interface FetchLikeOptions extends Omit<RequestInit, "body"> {

type FetchLike = (
resource: URL,
options: FetchLikeOptions
options: FetchLikeOptions,
) => Promise<Response>;
export type Middleware = (next: FetchLike) => FetchLike;

Expand Down Expand Up @@ -135,7 +135,7 @@ export class SpotifyClient implements HTTPClient {

constructor(
private accessToken: string,
private readonly options: SpotifyClinetOptions = {}
private readonly options: SpotifyClinetOptions = {},
) {
this.baseUrl = options.baseUrl
? options.baseUrl
Expand All @@ -155,8 +155,8 @@ export class SpotifyClient implements HTTPClient {
const headers = new Headers(opts.headers);
headers.set("Accept", APP_JSON);

const isBodyJSON =
!!opts.body && (isPlainObject(opts.body) || Array.isArray(opts.body));
const isBodyJSON = !!opts.body &&
(isPlainObject(opts.body) || Array.isArray(opts.body));
if (isBodyJSON) {
headers.set(CONTENT_TYPE, APP_JSON);
}
Expand All @@ -169,7 +169,7 @@ export class SpotifyClient implements HTTPClient {

const wrappedFetch = (this.options.middlewares || []).reduceRight(
(next, mw) => mw(next),
(this.options.fetch || globalThis.fetch) as FetchLike
(this.options.fetch || globalThis.fetch) as FetchLike,
);

const recursiveFetch = async (): Promise<Response> => {
Expand Down
15 changes: 5 additions & 10 deletions endpoints/album/album.endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ 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[]) => {
return client.fetch("/v1/me/albums", {
method: "DELETE",
query: { ids: albumIds },
Expand All @@ -138,10 +135,7 @@ export const removeSavedAlbums = (
* @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) => {
return removeSavedAlbums(client, [albumId]);
};

Expand Down Expand Up @@ -191,10 +185,11 @@ export type GetNewReleasesOpts = Prettify<
* @param client Spotify HTTPClient
* @param options Additional option for request
*/
export const getNewAlbumReleases = async (
export const getNewReleases = async (
client: HTTPClient,
options?: GetNewReleasesOpts,
) => {
const res = await client.fetch("/v1/browse/new-releases", { query: options });
return (await res.json() as { albums: PagingObject<SimplifiedAlbum> }).albums;
return ((await res.json()) as { albums: PagingObject<SimplifiedAlbum> })
.albums;
};
4 changes: 2 additions & 2 deletions endpoints/album/album.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { client } from "../../test_client.ts";
import {
getAlbum,
getAlbums,
getNewAlbumReleases,
getNewReleases,
getSavedAlbums,
} from "./album.endpoints.ts";
import type {
Expand Down Expand Up @@ -54,7 +54,7 @@ Deno.test("getSavedAlbums", async () => {
});

Deno.test("getNewAlbumReleases", async () => {
const newAlbumsPage = await getNewAlbumReleases(client, { limit: 10 });
const newAlbumsPage = await getNewReleases(client, { limit: 10 });
pagingObjectSchema(simplifiedAlbumSchema).parse(newAlbumsPage);
});

Expand Down
2 changes: 1 addition & 1 deletion endpoints/artist/artist.endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getArtist = async (client: HTTPClient, artistId: string) => {
* @param client Spotify HTTPClient
* @param artistIds List of the Spotify IDs for the artists. Maximum: 50 IDs.
*/
export const getSeveralArtists = async (
export const getArtists = async (
client: HTTPClient,
artistIds: string[],
) => {
Expand Down
8 changes: 4 additions & 4 deletions endpoints/artist/artist.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
getArtist,
getArtistAlbums,
getArtists,
getArtistTopTracks,
getSeveralArtists,
} from "./artist.endpoints.ts";
import { client } from "../../test_client.ts";
import { artistSchema } from "./artist.schemas.ts";
Expand All @@ -29,8 +29,8 @@ Deno.test("getArtist", async () => {
artistSchema.parse(artist);
});

Deno.test("getSeveralArtists", async () => {
const artists = await getSeveralArtists(client, MOCK_ARTIST_IDS);
Deno.test("getArtists", async () => {
const artists = await getArtists(client, MOCK_ARTIST_IDS);
z.array(artistSchema).parse(artists);
});

Expand All @@ -43,7 +43,7 @@ Deno.test("getArtistTopTracks", async () => {
const artistTopTracks = await getArtistTopTracks(
client,
getRandomArtistId(),
"from_token"
"from_token",
);
z.array(trackSchema).parse(artistTopTracks);
});
51 changes: 26 additions & 25 deletions endpoints/playlist/playlist.endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type GetPlaylistOpts = Prettify<
export const getPlaylist = async (
client: HTTPClient,
playlistId: string,
options?: GetPlaylistOpts
options?: GetPlaylistOpts,
) => {
const res = await client.fetch("/v1/playlists/" + playlistId, {
query: options,
Expand Down Expand Up @@ -80,20 +80,21 @@ export type ChangePlaylistDetailsBody = {
export const changePlaylistDetails = (
client: HTTPClient,
playlistId: string,
body: ChangePlaylistDetailsBody
body: ChangePlaylistDetailsBody,
) => {
return client.fetch("/v1/playlist/" + playlistId, { method: "PUT", body });
};

export type GetPlaylistTracksOpts = Prettify<
PlaylistFieldsOpts &
PagingOptions & {
/**
* An ISO 3166-1 alpha-2 country code.
* If a country code is specified, only content that is available in that market will be returned.
*/
market?: string;
}
& PlaylistFieldsOpts
& PagingOptions
& {
/**
* An ISO 3166-1 alpha-2 country code.
* If a country code is specified, only content that is available in that market will be returned.
*/
market?: string;
}
>;

/**
Expand All @@ -106,7 +107,7 @@ export type GetPlaylistTracksOpts = Prettify<
export const getPlaylistTracks = async (
client: HTTPClient,
playlistId: string,
options?: GetPlaylistTracksOpts
options?: GetPlaylistTracksOpts,
) => {
const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, {
query: options,
Expand All @@ -126,7 +127,7 @@ export const addItemsToPlaylist = async (
client: HTTPClient,
playlistId: string,
uris: string[],
position?: number
position?: number,
) => {
const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, {
method: "POST",
Expand All @@ -147,7 +148,7 @@ export const addItemToPlaylist = (
client: HTTPClient,
playlistId: string,
uri: string,
position?: number
position?: number,
) => {
return addItemsToPlaylist(client, playlistId, [uri], position);
};
Expand Down Expand Up @@ -182,7 +183,7 @@ export type ReorderPlaylistItemsOpts = {
export const reorderPlaylistItems = async (
client: HTTPClient,
playlistId: string,
options?: ReorderPlaylistItemsOpts
options?: ReorderPlaylistItemsOpts,
) => {
const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, {
method: "PUT",
Expand All @@ -202,7 +203,7 @@ export const reorderPlaylistItems = async (
export const replacePlaylistItems = async (
client: HTTPClient,
playlistId: string,
uris: string[]
uris: string[],
) => {
const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, {
method: "PUT",
Expand All @@ -223,7 +224,7 @@ export const removePlaylistItems = async (
client: HTTPClient,
playlistId: string,
uris: string[],
snapshotId?: string
snapshotId?: string,
) => {
const res = await client.fetch(`/v1/playlists/${playlistId}/tracks`, {
method: "DELETE",
Expand All @@ -247,7 +248,7 @@ export const removePlaylistItem = (
client: HTTPClient,
playlistId: string,
uri: string,
snapshotId?: string
snapshotId?: string,
) => {
return removePlaylistItems(client, playlistId, [uri], snapshotId);
};
Expand All @@ -260,7 +261,7 @@ export const removePlaylistItem = (
*/
export const getCurrentUsersPlaylists = async (
client: HTTPClient,
options?: PagingOptions
options?: PagingOptions,
) => {
const res = await client.fetch("/v1/me/playlists", { query: options });
return res.json() as Promise<PagingObject<SimplifiedPlaylist>>;
Expand All @@ -276,7 +277,7 @@ export const getCurrentUsersPlaylists = async (
export const getUsersPlaylists = async (
client: HTTPClient,
userId: string,
options?: PagingOptions
options?: PagingOptions,
) => {
const res = await client.fetch(`/v1/users/${userId}/playlists`, {
query: options,
Expand Down Expand Up @@ -313,7 +314,7 @@ export type CreatePlaylistBody = {
export const createPlaylist = async (
client: HTTPClient,
userId: string,
body: CreatePlaylistBody
body: CreatePlaylistBody,
) => {
const res = await client.fetch(`/v1/users/${userId}/playlists`, {
method: "POST",
Expand Down Expand Up @@ -351,7 +352,7 @@ export type GetFeaturedPlaylistsOpts = Prettify<
*/
export const getFeaturedPlaylists = async (
client: HTTPClient,
options?: GetFeaturedPlaylistsOpts
options?: GetFeaturedPlaylistsOpts,
) => {
const res = await client.fetch("/v1/browse/featured-playlists", {
query: options,
Expand Down Expand Up @@ -379,11 +380,11 @@ export type GetCategorysPlaylistsOpts = Prettify<
export const getCategoryPlaylists = async (
client: HTTPClient,
categoryId: string,
options?: GetCategorysPlaylistsOpts
options?: GetCategorysPlaylistsOpts,
) => {
const res = await client.fetch(
`/v1/browse/categories/${categoryId}/playlists`,
{ query: options }
{ query: options },
);
return res.json() as Promise<FeaturedPlaylists>;
};
Expand All @@ -395,7 +396,7 @@ export const getCategoryPlaylists = async (
*/
export const getPlaylistCoverImage = async (
client: HTTPClient,
playlistId: string
playlistId: string,
) => {
const res = await client.fetch(`/v1/playlists/${playlistId}/images`);
return res.json() as Promise<NonNullableObject<Image>[]>;
Expand All @@ -410,7 +411,7 @@ export const getPlaylistCoverImage = async (
export const uploadPlaylistCoverImage = (
client: HTTPClient,
playlistId: string,
image: string
image: string,
) => {
return client.fetch(`/v1/playlists/${playlistId}/images`, {
method: "PUT",
Expand Down
21 changes: 8 additions & 13 deletions endpoints/search/search.endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ type ItemTypeToResultKey = {
audiobook: "audiobooks";
};

type ItemTypesToSearchResultKeys<T extends ItemType | ItemType[]> =
T extends ItemType[]
? Pick<ItemTypeToResultKey, T[number]>[T[number]]
: T extends ItemType
? ItemTypeToResultKey[T]
: never;
type ItemTypesToSearchResultKeys<T extends ItemType | ItemType[]> = T extends
ItemType[] ? Pick<ItemTypeToResultKey, T[number]>[T[number]]
: T extends ItemType ? ItemTypeToResultKey[T]
: never;

export type SearchResponse = {
tracks: PagingObject<Track>;
Expand Down Expand Up @@ -115,14 +113,11 @@ export const search = async <T extends ItemType[] | ItemType>(
client: HTTPClient,
type: T,
query: string | SearchQueriesFromItemTypes<T>,
options?: SearchOptions
options?: SearchOptions,
): Promise<Pick<SearchResponse, ItemTypesToSearchResultKeys<T>>> => {
const q =
typeof query === "string"
? query
: Object.entries(query)
.map(([key, value]) => (key === "q" ? value : `${key}:${value}`))
.join(" ");
const q = typeof query === "string" ? query : Object.entries(query)
.map(([key, value]) => (key === "q" ? value : `${key}:${value}`))
.join(" ");

const res = await client.fetch("/v1/search", {
query: { q, type, ...options },
Expand Down
Loading

0 comments on commit f98fdde

Please sign in to comment.