mirror of
https://github.com/FranP-code/spooky-spotify-showcase.git
synced 2025-10-13 00:02:36 +00:00
Limits on get params
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import SpotifyWebApi from "spotify-web-api-node";
|
||||
import Showcase from "./showcase";
|
||||
import { FETCH_ARTISTS_LIMIT, FETCH_TRACKS_LIMIT } from "../utils/contants";
|
||||
import { getSpotifyData } from "../utils/getSpotifyData";
|
||||
import { api } from "@/trpc/server";
|
||||
|
||||
@@ -21,10 +20,14 @@ export default async function SpotifyData({
|
||||
accessToken,
|
||||
refreshToken,
|
||||
placeholderData,
|
||||
tracksLimit,
|
||||
artistsLimit,
|
||||
}: {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
placeholderData: boolean;
|
||||
tracksLimit?: number;
|
||||
artistsLimit?: number;
|
||||
}) {
|
||||
const fetchData = async () => {
|
||||
if (placeholderData) {
|
||||
@@ -48,6 +51,8 @@ export default async function SpotifyData({
|
||||
} else {
|
||||
return getSpotifyData({
|
||||
accessToken,
|
||||
tracksLimit,
|
||||
artistsLimit,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -28,6 +28,16 @@ export default async function Home({
|
||||
redirectUri: process.env.SPOTIFY_REDIRECT_URI,
|
||||
});
|
||||
|
||||
const [tracksLimit, artistsLimit] = [
|
||||
searchParams?.["tracks-limit"],
|
||||
searchParams?.["artists-limit"],
|
||||
].map((value) => {
|
||||
if (value && typeof value === "string") {
|
||||
return parseInt(value, 10);
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
if (userIsLogged)
|
||||
try {
|
||||
spotifyApi.setAccessToken(access_token as string);
|
||||
@@ -47,6 +57,8 @@ export default async function Home({
|
||||
accessToken={access_token as string}
|
||||
refreshToken={refresh_token as string}
|
||||
placeholderData={placeholderDataSelected}
|
||||
tracksLimit={tracksLimit}
|
||||
artistsLimit={artistsLimit}
|
||||
/>
|
||||
) : (
|
||||
<LoginPage />
|
||||
|
||||
@@ -4,8 +4,12 @@ import { TrackByAlbum } from "../_components/spotify-data";
|
||||
|
||||
export const getSpotifyData = async ({
|
||||
accessToken,
|
||||
tracksLimit,
|
||||
artistsLimit,
|
||||
}: {
|
||||
accessToken: string;
|
||||
tracksLimit?: number;
|
||||
artistsLimit?: number;
|
||||
}) => {
|
||||
const spotifyApi = new SpotifyWebApi({
|
||||
clientId: process.env.SPOTIFY_CLIENT_ID,
|
||||
@@ -16,11 +20,19 @@ export const getSpotifyData = async ({
|
||||
|
||||
const [artistsData, tracksData, userData] = await Promise.all([
|
||||
spotifyApi.getMyTopArtists({
|
||||
limit: FETCH_ARTISTS_LIMIT,
|
||||
limit: Math.min(
|
||||
...[FETCH_ARTISTS_LIMIT, artistsLimit].filter(
|
||||
(v) => typeof v === "number",
|
||||
),
|
||||
),
|
||||
time_range: "short_term",
|
||||
}),
|
||||
spotifyApi.getMyTopTracks({
|
||||
limit: FETCH_TRACKS_LIMIT,
|
||||
limit: Math.min(
|
||||
...[FETCH_TRACKS_LIMIT, tracksLimit].filter(
|
||||
(v) => typeof v === "number",
|
||||
),
|
||||
),
|
||||
time_range: "short_term",
|
||||
}),
|
||||
spotifyApi.getMe(),
|
||||
|
||||
Reference in New Issue
Block a user