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';
|
2023-07-21 18:08:40 +09:00
|
|
|
|
2022-12-19 18:17:56 +01:00
|
|
|
import { getConfig } from '../../../../tools/config/getConfig';
|
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 });
|
2022-12-19 18:17:56 +01:00
|
|
|
const config = getConfig(configName?.toString() ?? 'default');
|
2022-08-07 12:14:17 +02:00
|
|
|
const { query } = req.query;
|
2022-12-18 22:27:01 +01:00
|
|
|
const app = config.apps.find(
|
2022-12-19 18:17:56 +01:00
|
|
|
(app) => app.integration?.type === 'overseerr' || app.integration?.type === 'jellyseerr'
|
2022-08-11 17:04:43 +02:00
|
|
|
);
|
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-12-19 18:17:56 +01:00
|
|
|
|
|
|
|
|
const apiKey = app?.integration?.properties.find((x) => x.field === 'apiKey')?.value;
|
|
|
|
|
if (!app || !query || !apiKey) {
|
2022-05-29 21:39:57 +02:00
|
|
|
return res.status(400).json({
|
|
|
|
|
error: 'Wrong request',
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-12-18 22:27:01 +01:00
|
|
|
const appUrl = new URL(app.url);
|
2022-05-29 21:39:57 +02:00
|
|
|
const data = await axios
|
2022-12-18 22:27:01 +01:00
|
|
|
.get(`${appUrl.origin}/api/v1/search?query=${query}`, {
|
2022-05-29 21:39:57 +02:00
|
|
|
headers: {
|
|
|
|
|
// Set X-Api-Key to the value of the API key
|
2022-12-19 18:17:56 +01:00
|
|
|
'X-Api-Key': apiKey,
|
2022-05-29 21:39:57 +02:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.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',
|
|
|
|
|
});
|
|
|
|
|
};
|