1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-17 12:30:32 +09:00
namae/api/services/debian/[query].ts

30 lines
764 B
TypeScript
Raw Normal View History

2020-06-19 16:15:53 +09:00
import {send, sendError, fetch} from '../../../util/http';
import {NowRequest, NowResponse} from '@vercel/node';
2019-08-05 22:59:47 +09:00
2019-12-24 01:57:07 +09:00
export default async function handler(
req: NowRequest,
res: NowResponse,
): Promise<void> {
2019-09-17 14:30:26 +09:00
const {query} = req.query;
2019-08-05 22:59:47 +09:00
2020-06-19 16:15:53 +09:00
if (!query || typeof query !== 'string') {
2019-12-10 18:33:39 +09:00
return sendError(res, new Error('No query given'));
}
if (/[^a-zA-Z0-9_-]/.test(query)) {
return sendError(res, new Error('Invalid characters'));
2019-08-05 22:59:47 +09:00
}
try {
const response = await fetch(
2019-08-06 01:17:45 +09:00
`https://packages.debian.org/buster/${encodeURIComponent(query)}`,
2019-09-17 14:30:26 +09:00
'GET',
);
const body = await response.text();
const availability = body.includes('No such package');
send(res, {availability});
2019-08-05 22:59:47 +09:00
} catch (err) {
2019-09-17 14:30:26 +09:00
sendError(res, err);
2019-08-05 22:59:47 +09:00
}
}