From 19f1700a55867c1fb8d6c11431bd4557e7520de1 Mon Sep 17 00:00:00 2001 From: zyachel Date: Mon, 30 Oct 2023 01:34:28 +0530 Subject: [PATCH] feat(api): add api endpoints for dynamic routes Squashed commit of the following: commit 9fdd7311368411d59784977f77d1af103ae16543 Author: zyachel Date: Mon Oct 30 01:25:32 2023 +0530 feat(api): add a catch-all route commit 4dffbbc0ec870a8f9a56e4ee62e6a6c472552f6a Author: zyachel 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 commit 264442448f6863addcceaf669e16d3fc4c586e26 Author: Niklas Poslovski Date: Sun Oct 29 19:00:44 2023 +0100 Add API endpoints for all routes that contain IMDB data --- src/pages/api/[...error].ts | 7 +++++++ src/pages/api/find.ts | 32 ++++++++++++++++++++++++++++++++ src/pages/api/list/[listId].ts | 23 +++++++++++++++++++++++ src/pages/api/name/[nameId].ts | 22 ++++++++++++++++++++++ src/pages/api/title/[titleId].ts | 21 +++++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 src/pages/api/[...error].ts create mode 100644 src/pages/api/find.ts create mode 100644 src/pages/api/list/[listId].ts create mode 100644 src/pages/api/name/[nameId].ts create mode 100644 src/pages/api/title/[titleId].ts diff --git a/src/pages/api/[...error].ts b/src/pages/api/[...error].ts new file mode 100644 index 0000000..cbc00f6 --- /dev/null +++ b/src/pages/api/[...error].ts @@ -0,0 +1,7 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; + +type ResponseData = { status: false; message: string }; + +export default async function handler(_: NextApiRequest, res: NextApiResponse) { + res.status(400).json({ status: false, message: 'Not found' }); +} diff --git a/src/pages/api/find.ts b/src/pages/api/find.ts new file mode 100644 index 0000000..2fcee7f --- /dev/null +++ b/src/pages/api/find.ts @@ -0,0 +1,32 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import Find, { type FindQueryParams } from 'src/interfaces/shared/search'; +import basicSearch from 'src/utils/fetchers/basicSearch'; +import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +import { findKey } from 'src/utils/constants/keys'; +import { AppError, cleanQueryStr } from 'src/utils/helpers'; + +type ResponseData = + | { status: true; data: { title: null | string; results: null | Find } } + | { status: false; message: string }; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + if (req.method !== 'GET') throw new AppError('Invalid method', 400); + + const queryObj = req.query as FindQueryParams | Record; + const query = queryObj.q?.trim(); + + if (!query) { + return res.status(200).json({ status: true, data: { title: null, results: null } }); + } + + const entries = Object.entries(queryObj); + const queryStr = cleanQueryStr(entries); + const results = await getOrSetApiCache(findKey(queryStr), basicSearch, queryStr); + + res.status(200).json({ status: true, data: { title: query, results } }); + } catch (error: any) { + const { message = 'Not found', statusCode = 404 } = error; + res.status(statusCode).json({ status: false, message }); + } +} diff --git a/src/pages/api/list/[listId].ts b/src/pages/api/list/[listId].ts new file mode 100644 index 0000000..c58e401 --- /dev/null +++ b/src/pages/api/list/[listId].ts @@ -0,0 +1,23 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import type List from 'src/interfaces/shared/list'; +import list from 'src/utils/fetchers/list'; +import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +import { listKey } from 'src/utils/constants/keys'; +import { AppError } from 'src/utils/helpers'; + +type ResponseData = { status: true; data: List } | { status: false; message: string }; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + if (req.method !== 'GET') throw new AppError('Invalid method', 400); + + const listId = req.query.listId as string; + const pageNum = req.query.page as string | undefined; + + const data = await getOrSetApiCache(listKey(listId, pageNum), list, listId, pageNum); + res.status(200).json({ status: true, data }); + } catch (error: any) { + const { message = 'Not found', statusCode = 404 } = error; + res.status(statusCode).json({ status: false, message }); + } +} diff --git a/src/pages/api/name/[nameId].ts b/src/pages/api/name/[nameId].ts new file mode 100644 index 0000000..3351a8e --- /dev/null +++ b/src/pages/api/name/[nameId].ts @@ -0,0 +1,22 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import type Name from 'src/interfaces/shared/name'; +import name from 'src/utils/fetchers/name'; +import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +import { nameKey } from 'src/utils/constants/keys'; +import { AppError } from 'src/utils/helpers'; + +type ResponseData = { status: true; data: Name } | { status: false; message: string }; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + if (req.method !== 'GET') throw new AppError('Invalid method', 400); + + const nameId = req.query.nameId as string; + + const data = await getOrSetApiCache(nameKey(nameId), name, nameId); + res.status(200).json({ status: true, data }); + } catch (error: any) { + const { message = 'Not found', statusCode = 404 } = error; + res.status(statusCode).json({ status: false, message }); + } +} diff --git a/src/pages/api/title/[titleId].ts b/src/pages/api/title/[titleId].ts new file mode 100644 index 0000000..9965a27 --- /dev/null +++ b/src/pages/api/title/[titleId].ts @@ -0,0 +1,21 @@ +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) { + 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 }); + } +}