Limits on get params

This commit is contained in:
2024-10-18 00:48:22 -03:00
parent 21dccf5854
commit 65d44b7220
3 changed files with 32 additions and 3 deletions

View File

@@ -1,6 +1,5 @@
import SpotifyWebApi from "spotify-web-api-node"; import SpotifyWebApi from "spotify-web-api-node";
import Showcase from "./showcase"; import Showcase from "./showcase";
import { FETCH_ARTISTS_LIMIT, FETCH_TRACKS_LIMIT } from "../utils/contants";
import { getSpotifyData } from "../utils/getSpotifyData"; import { getSpotifyData } from "../utils/getSpotifyData";
import { api } from "@/trpc/server"; import { api } from "@/trpc/server";
@@ -21,10 +20,14 @@ export default async function SpotifyData({
accessToken, accessToken,
refreshToken, refreshToken,
placeholderData, placeholderData,
tracksLimit,
artistsLimit,
}: { }: {
accessToken: string; accessToken: string;
refreshToken: string; refreshToken: string;
placeholderData: boolean; placeholderData: boolean;
tracksLimit?: number;
artistsLimit?: number;
}) { }) {
const fetchData = async () => { const fetchData = async () => {
if (placeholderData) { if (placeholderData) {
@@ -48,6 +51,8 @@ export default async function SpotifyData({
} else { } else {
return getSpotifyData({ return getSpotifyData({
accessToken, accessToken,
tracksLimit,
artistsLimit,
}); });
} }
}; };

View File

@@ -28,6 +28,16 @@ export default async function Home({
redirectUri: process.env.SPOTIFY_REDIRECT_URI, 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) if (userIsLogged)
try { try {
spotifyApi.setAccessToken(access_token as string); spotifyApi.setAccessToken(access_token as string);
@@ -47,6 +57,8 @@ export default async function Home({
accessToken={access_token as string} accessToken={access_token as string}
refreshToken={refresh_token as string} refreshToken={refresh_token as string}
placeholderData={placeholderDataSelected} placeholderData={placeholderDataSelected}
tracksLimit={tracksLimit}
artistsLimit={artistsLimit}
/> />
) : ( ) : (
<LoginPage /> <LoginPage />

View File

@@ -4,8 +4,12 @@ import { TrackByAlbum } from "../_components/spotify-data";
export const getSpotifyData = async ({ export const getSpotifyData = async ({
accessToken, accessToken,
tracksLimit,
artistsLimit,
}: { }: {
accessToken: string; accessToken: string;
tracksLimit?: number;
artistsLimit?: number;
}) => { }) => {
const spotifyApi = new SpotifyWebApi({ const spotifyApi = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID, clientId: process.env.SPOTIFY_CLIENT_ID,
@@ -16,11 +20,19 @@ export const getSpotifyData = async ({
const [artistsData, tracksData, userData] = await Promise.all([ const [artistsData, tracksData, userData] = await Promise.all([
spotifyApi.getMyTopArtists({ spotifyApi.getMyTopArtists({
limit: FETCH_ARTISTS_LIMIT, limit: Math.min(
...[FETCH_ARTISTS_LIMIT, artistsLimit].filter(
(v) => typeof v === "number",
),
),
time_range: "short_term", time_range: "short_term",
}), }),
spotifyApi.getMyTopTracks({ spotifyApi.getMyTopTracks({
limit: FETCH_TRACKS_LIMIT, limit: Math.min(
...[FETCH_TRACKS_LIMIT, tracksLimit].filter(
(v) => typeof v === "number",
),
),
time_range: "short_term", time_range: "short_term",
}), }),
spotifyApi.getMe(), spotifyApi.getMe(),