All files / Rindu/utils/spotifyCalls search.ts

20.68% Statements 6/29
0% Branches 0/11
0% Functions 0/4
21.42% Lines 6/28

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 68 69 70 71 72 73 74 75 76 77 78 79  42x 42x   4x                           4x                                     4x                                       4x                                          
import type { ServerApiContext } from "types/serverContext";
import { handleJsonResponse } from "utils";
import { callSpotifyApi } from "utils/spotifyCalls";
 
export async function search(
  query: string,
  context?: ServerApiContext
): Promise<SpotifyApi.SearchResponse | null> {
  const q = query.replaceAll(" ", "+");
  const res = await callSpotifyApi({
    endpoint: `/search?q=${q}&type=album,track,artist,playlist,show,episode&market=from_token&limit=10`,
    method: "GET",
    context,
  });
 
  return handleJsonResponse<SpotifyApi.SearchResponse>(res);
}
 
export async function searchArtist(
  query: string,
  context?: ServerApiContext
): Promise<SpotifyApi.ArtistObjectFull[] | null> {
  const q = query.replaceAll(" ", "+");
  const res = await callSpotifyApi({
    endpoint: `/search?q=${q}&type=artist&market=from_token&limit=1`,
    method: "GET",
    context,
  });
 
  const data = await handleJsonResponse<SpotifyApi.SearchResponse>(res);
  Iif (data) {
    return data?.artists?.items ?? null;
  }
 
  return null;
}
 
export async function searchTrack(
  query: string,
  limit: number = 10,
  context?: ServerApiContext
): Promise<SpotifyApi.TrackObjectFull[] | null> {
  const q = query.replaceAll(" ", "+");
  const res = await callSpotifyApi({
    endpoint: `/search?q=${q}&type=track&market=from_token&limit=${limit}`,
    method: "GET",
    context,
  });
  const data = await handleJsonResponse<SpotifyApi.SearchResponse>(res);
 
  Iif (data) {
    return data?.tracks?.items ?? null;
  }
 
  return null;
}
 
export async function searchPlaylist(
  query?: string,
  context?: ServerApiContext
): Promise<SpotifyApi.PlaylistObjectFull[] | null> {
  Iif (!query) return null;
  const q = query.replaceAll(" ", "+");
  const res = await callSpotifyApi({
    endpoint: `/search?q=${q}&type=playlist&market=from_token&limit=50`,
    method: "GET",
    context,
  });
  const data = await handleJsonResponse<SpotifyApi.PlaylistSearchResponse>(res);
 
  Iif (data) {
    return (data?.playlists?.items ?? null) as
      | SpotifyApi.PlaylistObjectFull[]
      | null;
  }
 
  return null;
}