aboutsummaryrefslogtreecommitdiff
path: root/src/controllers/animeSearchSuggestion.controller.ts
blob: ed8784f3770308e6bbcd2be32bacc0c258092771 (plain)
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
import createHttpError from "http-errors";
import { type RequestHandler } from "express";
import { scrapeAnimeSearchSuggestion } from "../parsers/index.js";
import { type AnimeSearchSuggestQueryParams } from "../types/controllers/index.js";

// /anime/search/suggest?q=${query}
const getAnimeSearchSuggestion: RequestHandler<
  unknown,
  Awaited<ReturnType<typeof scrapeAnimeSearchSuggestion>>,
  unknown,
  AnimeSearchSuggestQueryParams
> = async (req, res, next) => {
  try {
    const query: string | null = req.query.q
      ? decodeURIComponent(req.query.q as string)
      : null;

    if (query === null) {
      throw createHttpError.BadRequest("Search keyword required");
    }

    const data = await scrapeAnimeSearchSuggestion(query);

    res.status(200).json(data);
  } catch (err: any) {
    console.error(err);
    next(err);
  }
};

export default getAnimeSearchSuggestion;