aboutsummaryrefslogtreecommitdiff
path: root/src/controllers
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2023-08-07 14:58:25 +0530
committerRitesh Ghosh <[email protected]>2023-08-07 14:58:25 +0530
commit84a79810c358dece1b3bd2b013a2d788ec100f44 (patch)
treed6c52e37abc4a76a7da2e32a7ae7622ad22f11bd /src/controllers
parent205619be557a7e1ac6152f370f440005feea0050 (diff)
downloadaniwatch-api-84a79810c358dece1b3bd2b013a2d788ec100f44.tar.xz
aniwatch-api-84a79810c358dece1b3bd2b013a2d788ec100f44.zip
feat(controllerTypes): added request handler types
Diffstat (limited to 'src/controllers')
-rw-r--r--src/controllers/animeAboutInfo.controller.ts26
-rw-r--r--src/controllers/animeCategory.controller.ts35
-rw-r--r--src/controllers/animeGenre.controller.ts19
-rw-r--r--src/controllers/animeSearch.controller.ts25
-rw-r--r--src/controllers/homePage.controller.ts13
5 files changed, 68 insertions, 50 deletions
diff --git a/src/controllers/animeAboutInfo.controller.ts b/src/controllers/animeAboutInfo.controller.ts
index 6cde2c7..c22ff31 100644
--- a/src/controllers/animeAboutInfo.controller.ts
+++ b/src/controllers/animeAboutInfo.controller.ts
@@ -1,19 +1,25 @@
-import { scrapeAnimeAboutInfo } from "../parsers";
import createHttpError from "http-errors";
-import { Request, Response, NextFunction, Handler } from "express";
+import { RequestHandler } from "express";
+import { scrapeAnimeAboutInfo } from "../parsers";
+import { AnimeAboutInfoQueryParams } from "../models/controllers";
// /anime/info?id=${anime-id}
-const getAnimeAboutInfo: Handler = async (
- req: Request,
- res: Response,
- next: NextFunction
-) => {
+const getAnimeAboutInfo: RequestHandler<
+ unknown,
+ Awaited<ReturnType<typeof scrapeAnimeAboutInfo>>,
+ unknown,
+ AnimeAboutInfoQueryParams
+> = async (req, res, next) => {
try {
- const id = req.query.id ? decodeURIComponent(req.query.id as string) : null;
- if (id === null)
+ const animeId = req.query.id
+ ? decodeURIComponent(req.query.id as string)
+ : null;
+
+ if (animeId === null) {
throw createHttpError.BadRequest("Anime unique id required");
+ }
- const data = await scrapeAnimeAboutInfo(id);
+ const data = await scrapeAnimeAboutInfo(animeId);
res.status(200).json(data);
} catch (err: any) {
diff --git a/src/controllers/animeCategory.controller.ts b/src/controllers/animeCategory.controller.ts
index ba44127..b6c83cc 100644
--- a/src/controllers/animeCategory.controller.ts
+++ b/src/controllers/animeCategory.controller.ts
@@ -1,30 +1,37 @@
-import { scrapeAnimeCategory } from "../parsers";
import createHttpError from "http-errors";
-import { AnimeCategories } from "../models";
-import { Request, Response, NextFunction, Handler } from "express";
+import { RequestHandler } from "express";
+import { AnimeCategories } from "../models/anime";
+import { scrapeAnimeCategory } from "../parsers";
+import {
+ CategoryAnimePathParams,
+ CategoryAnimeQueryParams,
+} from "../models/controllers";
// /anime/:category?page=${page}
-const getAnimeCategory: Handler = async (
- req: Request,
- res: Response,
- next: NextFunction
-) => {
+const getAnimeCategory: RequestHandler<
+ CategoryAnimePathParams,
+ Awaited<ReturnType<typeof scrapeAnimeCategory>>,
+ unknown,
+ CategoryAnimeQueryParams
+> = async (req, res, next) => {
try {
- const category: AnimeCategories = decodeURIComponent(
- req.params.category
- ) as AnimeCategories;
+ const category = req.params.category
+ ? decodeURIComponent(req.params.category)
+ : null;
const page: number = req.query.page
? Number(decodeURIComponent(req.query?.page as string))
: 1;
- if (!category) throw createHttpError.BadRequest("category required");
+ if (category === null) {
+ throw createHttpError.BadRequest("category required");
+ }
- const data = await scrapeAnimeCategory(category, page);
+ const data = await scrapeAnimeCategory(category as AnimeCategories, page);
res.status(200).json(data);
} catch (err: any) {
- // console.error(err);
+ console.error(err);
next(err);
}
};
diff --git a/src/controllers/animeGenre.controller.ts b/src/controllers/animeGenre.controller.ts
index 4851a18..100c1c5 100644
--- a/src/controllers/animeGenre.controller.ts
+++ b/src/controllers/animeGenre.controller.ts
@@ -1,13 +1,18 @@
-import { scrapeGenreAnime } from "../parsers";
import createHttpError from "http-errors";
-import { Request, Response, NextFunction, Handler } from "express";
+import { RequestHandler } from "express";
+import { scrapeGenreAnime } from "../parsers";
+import {
+ GenreAnimePathParams,
+ GenreAnimeQueryParams,
+} from "../models/controllers";
// /anime/genre/${name}?page=${page}
-const getGenreAnime: Handler = async (
- req: Request,
- res: Response,
- next: NextFunction
-) => {
+const getGenreAnime: RequestHandler<
+ GenreAnimePathParams,
+ Awaited<ReturnType<typeof scrapeGenreAnime>>,
+ unknown,
+ GenreAnimeQueryParams
+> = async (req, res, next) => {
try {
const name: string | null = req.params.name
? decodeURIComponent(req.params.name as string)
diff --git a/src/controllers/animeSearch.controller.ts b/src/controllers/animeSearch.controller.ts
index b0990c3..f03c40b 100644
--- a/src/controllers/animeSearch.controller.ts
+++ b/src/controllers/animeSearch.controller.ts
@@ -1,31 +1,32 @@
-import { scrapeAnimeSearch } from "../parsers";
import createHttpError from "http-errors";
-import { AnimeCategories } from "../models";
-import { Request, Response, NextFunction, Handler } from "express";
+import { RequestHandler } from "express";
+import { scrapeAnimeSearch } from "../parsers";
+import { AnimeSearchQueryParams } from "../models/controllers";
// /anime/search?q=${query}&page=${page}
-const getAnimeSearch: Handler = async (
- req: Request,
- res: Response,
- next: NextFunction
-) => {
+const getAnimeSearch: RequestHandler<
+ unknown,
+ Awaited<ReturnType<typeof scrapeAnimeSearch>>,
+ unknown,
+ AnimeSearchQueryParams
+> = async (req, res, next) => {
try {
- const q: string | null = req.query.q
+ const query: string | null = req.query.q
? decodeURIComponent(req.query.q as string)
: null;
const page: number = req.query.page
? Number(decodeURIComponent(req.query?.page as string))
: 1;
- if (q === null) {
+ if (query === null) {
throw createHttpError.BadGateway("Search keyword required");
}
- const data = await scrapeAnimeSearch(q, page);
+ const data = await scrapeAnimeSearch(query, page);
res.status(200).json(data);
} catch (err: any) {
- // console.error(err);
+ console.error(err);
next(err);
}
};
diff --git a/src/controllers/homePage.controller.ts b/src/controllers/homePage.controller.ts
index c5ee45b..a3f7d53 100644
--- a/src/controllers/homePage.controller.ts
+++ b/src/controllers/homePage.controller.ts
@@ -1,12 +1,11 @@
+import { RequestHandler } from "express";
import { scrapeHomePage } from "../parsers";
-import { Request, Response, NextFunction, Handler } from "express";
// /anime/home
-const getHomePage: Handler = async (
- req: Request,
- res: Response,
- next: NextFunction
-) => {
+const getHomePageInfo: RequestHandler<
+ unknown,
+ Awaited<ReturnType<typeof scrapeHomePage>>
+> = async (req, res, next) => {
try {
const data = await scrapeHomePage();
res.status(200).json(data);
@@ -16,4 +15,4 @@ const getHomePage: Handler = async (
}
};
-export default getHomePage;
+export default getHomePageInfo;