boardgame-lib / lib / bgg / search.ts
search.ts
Raw
"use server";

import { bggInstance } from "@/lib/bgg";
import { serverResponse } from "@/lib/response";

export interface Boardgames {
  items: Boardgame[];
}

export interface Boardgame {
  objectid: string;
  subtype: string;
  primaryname: string;
  nameid: string;
  yearpublished: number;
  ordtitle: string;
  rep_imageid: number;
  objecttype: string;
  name: string;
  sortindex: string;
  type: string;
  id: string;
  href: string;
}

export async function searchBoardgames(searchTerm: string) {
  if (!searchTerm)
    return serverResponse(false, "No search term provided.", null);
  const response = await bggInstance.get("/search/boardgame", {
    params: {
      q: searchTerm,
      nosession: 1,
      showcount: 50,
    },
  });

  if (!response)
    return serverResponse(false, "No response returned from API", null);

  return serverResponse(true, "Boardgames found", response.data as Boardgames);
}