All files / Rindu/utils getLyrics.ts

73.68% Statements 14/19
71.42% Branches 5/7
100% Functions 1/1
73.68% Lines 14/19

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67          44x 44x   5x           3x 3x                     2x 1x 1x 1x       1x                 1x                   1x               1x       1x    
import {
  GetLyrics,
  IlrclibResponse,
  ILyrics,
  LyricsAction,
} from "types/lyrics";
import { getSiteUrl, normalizeLrcLibLyrics } from "utils";
 
export async function getLyrics(
  artistName: string,
  title: string,
  trackId?: string | null,
  action?: LyricsAction
): Promise<GetLyrics> {
  if (action === LyricsAction.Fullscreen) {
    const resSyncedLyrics = await fetch(
      process.env.NEXT_PUBLIC_LYRICS_API_URL ??
        `${getSiteUrl()}/api/synced-lyrics`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ trackId }),
      }
    );
    if (resSyncedLyrics.ok) {
      const data = (await resSyncedLyrics.json()) as ILyrics;
      if (data) {
        return { ...data, isFullscreen: true };
      }
    }
 
    const lrclibRes = await fetch(
      `https://lrclib.net/api/get?artist_name=${artistName}&track_name=${title}`,
      {
        headers: {
          "User-Agent": "Rindu v0.0.0 (https://github.com/marcomadera/rindu)",
        },
      }
    );
 
    Iif (lrclibRes.ok) {
      const data = (await lrclibRes.json()) as IlrclibResponse;
      const nomarlizedData = normalizeLrcLibLyrics(data);
      return {
        ...nomarlizedData,
        isFullscreen: true,
      };
    }
  }
 
  const res = await fetch(`${getSiteUrl()}/api/lyrics`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ artistName, title }),
  });
 
  Iif (res.ok) {
    const lyrics = (await res.json()) as string | null;
    return { lyrics, isFullscreen: false };
  }
  return null;
}