Add API endpoints for all routes that contain IMDB data

This commit is contained in:
Niklas Poslovski 2023-10-29 19:00:44 +01:00
parent 2b00d5406a
commit 264442448f
4 changed files with 101 additions and 0 deletions

37
src/pages/api/find.ts Normal file
View file

@ -0,0 +1,37 @@
import { NextApiRequest, NextApiResponse } from 'next';
import Find, { FindQueryParams } from 'src/interfaces/shared/search';
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
import basicSearch from 'src/utils/fetchers/basicSearch';
import { cleanQueryStr } from 'src/utils/helpers';
import { findKey } from 'src/utils/constants/keys';
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
});
}
try {
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
});
} catch (error: any) {
const { message, statusCode } = error;
res.status(statusCode);
res.json({
success: false,
message: message
});
}
}

View file

@ -0,0 +1,22 @@
import { NextApiRequest, NextApiResponse } from 'next';
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
import list from 'src/utils/fetchers/list';
import { listKey } from 'src/utils/constants/keys';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const listId = req.query.listId;
const pageNum = (req.query.page as string | undefined) ?? '1';
try {
const data = await getOrSetApiCache(listKey(listId, pageNum), list, listId, pageNum);
res.status(200);
res.json(data);
} catch (error: any) {
const { message, statusCode } = error;
res.status(statusCode);
res.json({
success: false,
message: message
});
}
}

View file

@ -0,0 +1,21 @@
import { NextApiRequest, NextApiResponse } from 'next';
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
import name from 'src/utils/fetchers/name';
import { nameKey } from 'src/utils/constants/keys';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const nameId = req.query.nameId;
try {
const data = await getOrSetApiCache(nameKey(nameId), name, nameId);
res.status(200);
res.json(data);
} catch (error: any) {
const { message, statusCode } = error;
res.status(statusCode);
res.json({
success: false,
message: message
});
}
}

View file

@ -0,0 +1,21 @@
import { NextApiRequest, NextApiResponse } from 'next';
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
import title from 'src/utils/fetchers/title';
import { titleKey } from 'src/utils/constants/keys';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const titleId = req.query.titleId;
try {
const data = await getOrSetApiCache(titleKey(titleId), title, titleId);
res.status(200);
res.json(data);
} catch (error: any) {
const { message, statusCode } = error;
res.status(statusCode);
res.json({
success: false,
message: message
});
}
}