2023-04-25 16:06:15 +09:00
|
|
|
import axios, { AxiosError } from 'axios';
|
2022-12-11 14:45:33 +09:00
|
|
|
import https from 'https';
|
2023-04-21 10:21:34 +02:00
|
|
|
import Consola from 'consola';
|
2022-05-17 04:02:14 +02:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
|
|
|
|
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
2022-12-18 22:27:01 +01:00
|
|
|
// Parse req.body as a AppItem
|
2022-05-17 04:02:14 +02:00
|
|
|
const { url } = req.query;
|
2022-12-11 14:25:27 +09:00
|
|
|
const agent = new https.Agent({ rejectUnauthorized: false });
|
|
|
|
|
await axios
|
|
|
|
|
.get(url as string, { httpsAgent: agent, timeout: 2000 })
|
|
|
|
|
.then((response) => {
|
|
|
|
|
res.status(response.status).json(response.statusText);
|
|
|
|
|
})
|
2023-04-25 16:06:15 +09:00
|
|
|
.catch((error: AxiosError) => {
|
2022-12-11 14:25:27 +09:00
|
|
|
if (error.response) {
|
2023-04-25 16:06:15 +09:00
|
|
|
Consola.warn(`Unexpected response: ${error.message}`);
|
2022-12-11 14:25:27 +09:00
|
|
|
res.status(error.response.status).json(error.response.statusText);
|
|
|
|
|
} else if (error.code === 'ECONNABORTED') {
|
|
|
|
|
res.status(408).json('Request Timeout');
|
|
|
|
|
} else {
|
2023-04-25 16:06:15 +09:00
|
|
|
Consola.error(`Unexpected error: ${error.message}`);
|
|
|
|
|
res.status(500).json('Internal Server Error');
|
2022-12-11 14:25:27 +09:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// // Make a request to the URL
|
|
|
|
|
// const response = await axios.get(url);
|
|
|
|
|
// // Return the response
|
2022-05-17 04:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
|
// Filter out if the reuqest is a POST or a GET
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
return Get(req, res);
|
|
|
|
|
}
|
|
|
|
|
return res.status(405).json({
|
|
|
|
|
statusCode: 405,
|
|
|
|
|
message: 'Method not allowed',
|
|
|
|
|
});
|
|
|
|
|
};
|