From 4dffbbc0ec870a8f9a56e4ee62e6a6c472552f6a Mon Sep 17 00:00:00 2001 From: zyachel Date: Mon, 30 Oct 2023 01:24:10 +0530 Subject: [PATCH] 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 --- src/pages/api/find.ts | 51 ++++++++++++++------------------ src/pages/api/list/[listId].ts | 27 +++++++++-------- src/pages/api/name/[nameId].ts | 25 ++++++++-------- src/pages/api/title/[titleId].ts | 24 +++++++-------- 4 files changed, 62 insertions(+), 65 deletions(-) diff --git a/src/pages/api/find.ts b/src/pages/api/find.ts index 67e77ee..2fcee7f 100644 --- a/src/pages/api/find.ts +++ b/src/pages/api/find.ts @@ -1,37 +1,32 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import Find, { FindQueryParams } from 'src/interfaces/shared/search'; -import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import Find, { type FindQueryParams } from 'src/interfaces/shared/search'; import basicSearch from 'src/utils/fetchers/basicSearch'; -import { cleanQueryStr } from 'src/utils/helpers'; +import getOrSetApiCache from 'src/utils/getOrSetApiCache'; import { findKey } from 'src/utils/constants/keys'; +import { AppError, cleanQueryStr } from 'src/utils/helpers'; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const queryObj = req.query as FindQueryParams; - const query = queryObj.q?.trim(); - - if(!query) { - res.status(200); - res.json({ - title: null, - results: null - }); - } +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 { - const entries = Object.entries(queryObj); + 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 result = await getOrSetApiCache(findKey(queryStr), basicSearch, queryStr); - res.status(200); - res.json({ - title: query, - results: result - }); + const results = await getOrSetApiCache(findKey(queryStr), basicSearch, queryStr); + + res.status(200).json({ status: true, data: { title: query, results } }); } catch (error: any) { - const { message, statusCode } = error; - res.status(statusCode); - res.json({ - success: false, - message: message - }); + 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 index 515ba10..c58e401 100644 --- a/src/pages/api/list/[listId].ts +++ b/src/pages/api/list/[listId].ts @@ -1,22 +1,23 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +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'; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const listId = req.query.listId; - const pageNum = (req.query.page as string | undefined) ?? '1'; +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); - res.json(data); + res.status(200).json({ status: true, data }); } catch (error: any) { - const { message, statusCode } = error; - res.status(statusCode); - res.json({ - success: false, - message: message - }); + 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 index 4fac6c7..3351a8e 100644 --- a/src/pages/api/name/[nameId].ts +++ b/src/pages/api/name/[nameId].ts @@ -1,21 +1,22 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +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'; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const nameId = req.query.nameId; +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); - res.json(data); + res.status(200).json({ status: true, data }); } catch (error: any) { - const { message, statusCode } = error; - res.status(statusCode); - res.json({ - success: false, - message: message - }); + 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 index 19c41b6..9965a27 100644 --- a/src/pages/api/title/[titleId].ts +++ b/src/pages/api/title/[titleId].ts @@ -1,21 +1,21 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import getOrSetApiCache from 'src/utils/getOrSetApiCache'; +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'; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const titleId = req.query.titleId; +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); - res.json(data); + res.status(200).json({ status: true, data }); } catch (error: any) { - const { message, statusCode } = error; - res.status(statusCode); - res.json({ - success: false, - message: message - }); + const { message = 'Not found', statusCode = 404 } = error; + res.status(statusCode).json({ status: false, message }); } }