mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 06:44:07 +00:00
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import {
|
|
getCurrentUsersPlaylistsFirstPage,
|
|
getCurrentUsersPlaylistsNextPage,
|
|
} from "../api/spotify.ts";
|
|
|
|
import type { RequestHandler } from "express";
|
|
import type {
|
|
Pagination,
|
|
SimplifiedPlaylistObject,
|
|
} from "spotify_manager/index.d.ts";
|
|
|
|
import curriedLogger from "../utils/logger.ts";
|
|
const logger = curriedLogger(import.meta.filename);
|
|
|
|
/**
|
|
* Get user's playlists
|
|
*/
|
|
const fetchUserPlaylists: RequestHandler = async (req, res) => {
|
|
try {
|
|
// get first 50
|
|
const respData = await getCurrentUsersPlaylistsFirstPage({ req, res });
|
|
if (!respData) return null;
|
|
|
|
let tmpData = structuredClone(respData);
|
|
const userPlaylists: Pick<
|
|
Pagination<SimplifiedPlaylistObject>,
|
|
"items" | "total"
|
|
> = {
|
|
items: [...tmpData.items],
|
|
total: tmpData.total,
|
|
};
|
|
let nextURL = respData.next;
|
|
// keep getting batches of 50 till exhausted
|
|
while (nextURL) {
|
|
const nextData = await getCurrentUsersPlaylistsNextPage({
|
|
req,
|
|
res,
|
|
nextURL,
|
|
});
|
|
if (!nextData) return null;
|
|
|
|
userPlaylists.items.push(...nextData.items);
|
|
nextURL = nextData.next;
|
|
}
|
|
|
|
res.status(200).send(userPlaylists);
|
|
logger.debug("Fetched user playlists", { num: userPlaylists.total });
|
|
return null;
|
|
} catch (error) {
|
|
res.status(500).send({ message: "Internal Server Error" });
|
|
logger.error("fetchUserPlaylists", { error });
|
|
return null;
|
|
}
|
|
};
|
|
export { fetchUserPlaylists };
|