All files / Rindu/utils getAllAlbums.ts

100% Statements 19/19
100% Branches 5/5
100% Functions 1/1
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3444x   8x 6x 6x 6x     5x   5x 5x 5x 4x 2x         2x       4x 2x     2x       2x    
import { getMyAlbums } from "utils/spotifyCalls";
 
export async function getAllAlbums(): Promise<SpotifyApi.UsersSavedAlbumsResponse | null> {
  const limit = 50;
  const albumsData = await getMyAlbums(0);
  if (!albumsData) return null;
 
  let restAlbumsData: SpotifyApi.UsersSavedAlbumsResponse | undefined;
  const max = Math.ceil(albumsData.total / limit);
 
  for (let i = 1; i < max; i++) {
    const resAlbumsData = await getMyAlbums(limit * i);
    if (!resAlbumsData) return null;
    if (restAlbumsData) {
      restAlbumsData = {
        ...restAlbumsData,
        items: [...restAlbumsData.items, ...resAlbumsData.items],
      };
    } else {
      restAlbumsData = resAlbumsData;
    }
  }
 
  if (!restAlbumsData) {
    return albumsData;
  }
 
  const allAlbums = {
    ...albumsData,
    items: [...albumsData.items, ...restAlbumsData.items],
  };
  return allAlbums;
}