2024-05-06 22:51:32 +02:00
|
|
|
import https from 'https';
|
|
|
|
import middleware from './_common/middleware.js';
|
2023-07-07 21:56:58 +02:00
|
|
|
|
2024-05-06 22:51:32 +02:00
|
|
|
const featuresHandler = async (url) => {
|
2023-07-07 21:56:58 +02:00
|
|
|
const apiKey = process.env.BUILT_WITH_API_KEY;
|
|
|
|
|
|
|
|
if (!url) {
|
2023-08-09 23:33:36 +02:00
|
|
|
throw new Error('URL query parameter is required');
|
2023-07-10 00:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiKey) {
|
2023-08-09 23:33:36 +02:00
|
|
|
throw new Error('Missing BuiltWith API key in environment variables');
|
2023-07-07 21:56:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const apiUrl = `https://api.builtwith.com/free1/api.json?KEY=${apiKey}&LOOKUP=${encodeURIComponent(url)}`;
|
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
try {
|
|
|
|
const response = await new Promise((resolve, reject) => {
|
|
|
|
const req = https.get(apiUrl, res => {
|
|
|
|
let data = '';
|
2023-07-07 21:56:58 +02:00
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
res.on('data', chunk => {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on('end', () => {
|
|
|
|
if (res.statusCode >= 200 && res.statusCode <= 299) {
|
|
|
|
resolve(data);
|
|
|
|
} else {
|
|
|
|
reject(new Error(`Request failed with status code: ${res.statusCode}`));
|
|
|
|
}
|
|
|
|
});
|
2023-07-07 21:56:58 +02:00
|
|
|
});
|
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
req.on('error', error => {
|
|
|
|
reject(error);
|
2023-07-07 21:56:58 +02:00
|
|
|
});
|
2023-07-10 00:23:50 +02:00
|
|
|
|
|
|
|
req.end();
|
2023-07-07 21:56:58 +02:00
|
|
|
});
|
2023-07-10 00:23:50 +02:00
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
return response;
|
2023-07-10 00:23:50 +02:00
|
|
|
} catch (error) {
|
2023-08-09 23:33:36 +02:00
|
|
|
throw new Error(`Error making request: ${error.message}`);
|
2023-07-10 00:23:50 +02:00
|
|
|
}
|
2023-07-07 21:56:58 +02:00
|
|
|
};
|
2023-08-09 23:33:36 +02:00
|
|
|
|
2024-05-06 22:51:32 +02:00
|
|
|
export const handler = middleware(featuresHandler);
|
|
|
|
export default handler;
|