mirror of
https://github.com/zyachel/libremdb.git
synced 2024-12-06 19:26:47 +01:00
19f1700a55
Squashed commit of the following: commit9fdd731136Author: zyachel <aricla@protonmail.com> Date: Mon Oct 30 01:25:32 2023 +0530 feat(api): add a catch-all route commit4dffbbc0ecAuthor: zyachel <aricla@protonmail.com> Date: Mon Oct 30 01:24:10 2023 +0530 fix(api): refactor all endpoints a bit disallow methods other that GET properly type return types add type guards where needed make all endpoints return same response format for consistency commit264442448fAuthor: Niklas Poslovski <niklas.poslovski@nikisoft.one> Date: Sun Oct 29 19:00:44 2023 +0100 Add API endpoints for all routes that contain IMDB data
22 lines
918 B
TypeScript
22 lines
918 B
TypeScript
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
import type Title from 'src/interfaces/shared/title';
|
|
import title from 'src/utils/fetchers/title';
|
|
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
|
import { titleKey } from 'src/utils/constants/keys';
|
|
import { AppError } from 'src/utils/helpers';
|
|
|
|
type ResponseData = { status: true; data: Title } | { status: false; message: string };
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
|
try {
|
|
if (req.method !== 'GET') throw new AppError('Invalid method', 400);
|
|
|
|
const titleId = req.query.titleId as string;
|
|
const data = await getOrSetApiCache(titleKey(titleId), title, titleId);
|
|
res.status(200).json({ status: true, data });
|
|
} catch (error: any) {
|
|
const { message = 'Not found', statusCode = 404 } = error;
|
|
res.status(statusCode).json({ status: false, message });
|
|
}
|
|
}
|