Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
katydecorah committed Jun 6, 2024
1 parent 92ab9bc commit c4871ce
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ describe("action", () => {
jest
.spyOn(core, "getInput")
.mockImplementation((name) => defaultInputs[name] || undefined);

process.env.SpotifyClientID = "test-client-id";
process.env.SpotifyClientSecret = "test-client-secret";
});

test("works", async () => {
Expand Down
14 changes: 14 additions & 0 deletions src/__test__/list-playlists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ jest.mock("spotify-web-api-node", () => {
jest.mock("@actions/core");

describe("listPlaylists", () => {
beforeEach(() => {
process.env.SpotifyClientID = "test-client-id";
process.env.SpotifyClientSecret = "test-client-secret";
jest.clearAllMocks();
});
test("returns", async () => {
expect(await listPlaylists("2021 Fall")).toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -177,6 +182,15 @@ describe("listPlaylists", () => {
'Could not find playlist "2022 Fall". Is it private?'
);
});

test("throw error when SpotifyClientID or SpotifyClientSecret is missing", async () => {
delete process.env.SpotifyClientID;
delete process.env.SpotifyClientSecret;

await expect(listPlaylists("2021 Fall")).rejects.toThrow(
"Missing SpotifyClientID or SpotifyClientSecret in environment variables"
);
});
});

describe("formatTracks", () => {
Expand Down
75 changes: 56 additions & 19 deletions src/list-playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,11 @@ export default async function listPlaylists(
listName: string
): Promise<Playlist> {
try {
const spotifyApi = new SpotifyWebApi({
clientId: process.env.SpotifyClientID,
clientSecret: process.env.SpotifyClientSecret,
});
const spotifyApi = await initializeSpotifyApi();
const username = getInput("spotify-username");
const {
body: { access_token },
} = await spotifyApi.clientCredentialsGrant();

spotifyApi.setAccessToken(access_token);

const { body } = await spotifyApi.getUserPlaylists(username);
const findPlaylist = body.items.find(({ name }) => name === listName);
if (!findPlaylist) {
throw new Error(`Could not find playlist "${listName}". Is it private?`);
}
const {
body: { items },
} = await spotifyApi.getPlaylistTracks(findPlaylist.id);
if (!items.length) throw new Error("Playlist has no tracks.");

const findPlaylist = await getPlaylist(spotifyApi, username, listName);
const items = await getPlaylistTracks(spotifyApi, findPlaylist.id);

return formatTracks({
name: findPlaylist.name,
Expand All @@ -38,6 +23,58 @@ export default async function listPlaylists(
}
}

async function getPlaylist(
spotifyApi: SpotifyWebApi,
username: string,
listName: string
): Promise<SpotifyApi.PlaylistObjectSimplified> {
const { body } = await spotifyApi.getUserPlaylists(username);
const findPlaylist = body.items.find(({ name }) => name === listName);

if (!findPlaylist) {
throw new Error(`Could not find playlist "${listName}". Is it private?`);
}

return findPlaylist;
}

async function getPlaylistTracks(
spotifyApi: SpotifyWebApi,
playlistId: string
): Promise<SpotifyApi.PlaylistTrackObject[]> {
const {
body: { items },
} = await spotifyApi.getPlaylistTracks(playlistId);

if (!items.length) throw new Error("Playlist has no tracks.");

return items;
}

export async function initializeSpotifyApi(): Promise<SpotifyWebApi> {
const clientId = process.env.SpotifyClientID;
const clientSecret = process.env.SpotifyClientSecret;

if (!clientId || !clientSecret) {
throw new Error(
"Missing SpotifyClientID or SpotifyClientSecret in environment variables"
);
}

const spotifyApi = new SpotifyWebApi({
clientId,
clientSecret,
});

const {
body: { access_token },
} = await spotifyApi.clientCredentialsGrant();

spotifyApi.setAccessToken(access_token);

return spotifyApi;
}

export function formatTracks({
name,
external_urls,
Expand Down

0 comments on commit c4871ce

Please sign in to comment.