-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(estimatedSchedule): add
/schedule
endpoint controller
- Loading branch information
1 parent
dfed9d9
commit caec8b6
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import createHttpError from "http-errors"; | ||
import { type RequestHandler } from "express"; | ||
import { scrapeEstimatedSchedule } from "../parsers/index.js"; | ||
import { type EstimatedScheduleQueryParams } from "../models/controllers/index.js"; | ||
|
||
// /anime/schedule?date=${date} | ||
const getEstimatedSchedule: RequestHandler< | ||
unknown, | ||
Awaited<ReturnType<typeof scrapeEstimatedSchedule>>, | ||
unknown, | ||
EstimatedScheduleQueryParams | ||
> = async (req, res, next) => { | ||
try { | ||
const dateQuery = req.query.date | ||
? decodeURIComponent(req.query.date as string) | ||
: null; | ||
|
||
if (dateQuery === null) { | ||
throw createHttpError.BadRequest("Date payload required"); | ||
} | ||
if (!/^\d{4}-\d{2}-\d{2}$/.test(dateQuery)) { | ||
throw createHttpError.BadRequest( | ||
"Invalid date payload format. Months and days must have 2 digits" | ||
); | ||
} | ||
|
||
const data = await scrapeEstimatedSchedule(dateQuery); | ||
|
||
res.status(200).json(data); | ||
} catch (err: any) { | ||
console.error(err); | ||
next(err); | ||
} | ||
}; | ||
|
||
export default getEstimatedSchedule; |