2022-05-29 21:39:57 +02:00
|
|
|
import axios from 'axios';
|
2022-07-24 23:18:01 +02:00
|
|
|
import { getCookie } from 'cookies-next';
|
2022-05-29 21:39:57 +02:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
2022-08-07 12:14:17 +02:00
|
|
|
import { getConfig } from '../../../../tools/getConfig';
|
|
|
|
|
import { Config } from '../../../../tools/types';
|
2022-05-29 21:39:57 +02:00
|
|
|
|
2022-07-24 23:18:01 +02:00
|
|
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const configName = getCookie('config-name', { req });
|
|
|
|
|
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
2022-08-07 12:14:17 +02:00
|
|
|
const { query } = req.query;
|
|
|
|
|
const service = config.services.find((service) => service.type === 'Overseerr');
|
2022-05-29 21:39:57 +02:00
|
|
|
// If query is an empty string, return an empty array
|
2022-07-24 23:18:01 +02:00
|
|
|
if (query === '' || query === undefined) {
|
2022-05-29 21:39:57 +02:00
|
|
|
return res.status(200).json([]);
|
|
|
|
|
}
|
2022-07-24 23:18:01 +02:00
|
|
|
if (!service || !query || service === undefined || !service.apiKey) {
|
2022-05-29 21:39:57 +02:00
|
|
|
return res.status(400).json({
|
|
|
|
|
error: 'Wrong request',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const serviceUrl = new URL(service.url);
|
|
|
|
|
const data = await axios
|
|
|
|
|
.get(`${serviceUrl.origin}/api/v1/search?query=${query}`, {
|
|
|
|
|
headers: {
|
|
|
|
|
// Set X-Api-Key to the value of the API key
|
|
|
|
|
'X-Api-Key': service.apiKey,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.then((res) => res.data);
|
|
|
|
|
// Get login, password and url from the body
|
2022-07-24 23:18:01 +02:00
|
|
|
res.status(200).json(data);
|
2022-05-29 21:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
|
// Filter out if the reuqest is a POST or a GET
|
2022-07-24 23:18:01 +02:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
return Get(req, res);
|
2022-05-29 21:39:57 +02:00
|
|
|
}
|
|
|
|
|
return res.status(405).json({
|
|
|
|
|
statusCode: 405,
|
|
|
|
|
message: 'Method not allowed',
|
|
|
|
|
});
|
|
|
|
|
};
|