1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-17 04:30:31 +09:00

32 lines
920 B
TypeScript
Raw Normal View History

2020-08-31 08:41:53 +09:00
import isURL from 'validator/lib/isURL';
import { send, sendError, fetch } from '../../../util/http';
import { VercelRequest, VercelResponse } from '@vercel/node';
2019-08-27 03:15:35 +09:00
2019-12-24 01:57:07 +09:00
export default async function handler(
req: VercelRequest,
res: VercelResponse
2019-12-24 01:57:07 +09:00
): Promise<void> {
2022-07-10 09:59:16 +09:00
const { query, existIf = '404' } = req.query;
const availableStatus = (existIf as string).split(',').map((s) => s.trim());
2019-08-27 03:15:35 +09:00
2020-06-19 16:15:53 +09:00
if (!query || typeof query !== 'string') {
2020-08-31 08:41:53 +09:00
return sendError(res, new Error('no query given'));
2019-08-27 03:15:35 +09:00
}
2020-06-11 22:00:16 +09:00
if (!isURL(query)) {
2020-08-31 08:41:53 +09:00
return sendError(res, new Error('Invalid URL: ' + query));
2019-08-27 03:15:35 +09:00
}
try {
2020-08-31 08:41:53 +09:00
const response = await fetch(`https://${query}`);
2022-07-10 09:59:16 +09:00
const availability = availableStatus.includes(response.status.toString());
2020-08-31 08:41:53 +09:00
send(res, { availability });
2022-03-09 15:16:54 +09:00
} catch (err: any) {
if ((err as any).code === 'ENOTFOUND') {
2020-08-31 08:41:53 +09:00
return send(res, { availability: true });
2019-12-10 18:33:39 +09:00
}
2022-03-09 15:16:54 +09:00
sendError(res, err);
2019-08-27 03:15:35 +09:00
}
}