1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-18 04:50:32 +09:00
namae/api/services/reddit/[query].ts

29 lines
794 B
TypeScript
Raw Permalink Normal View History

2022-03-06 20:30:43 +01:00
import { send, sendError, fetch } from '../../../util/http';
2022-03-09 15:16:54 +09:00
import { VercelRequest, VercelResponse } from '@vercel/node';
2022-03-06 20:30:43 +01:00
export default async function handler(
2022-03-09 15:16:54 +09:00
req: VercelRequest,
res: VercelResponse
2022-03-06 20:30:43 +01:00
): Promise<void> {
const { query } = req.query;
if (!query || typeof query !== 'string') {
return sendError(res, new Error('No query given'));
}
if (/[^a-zA-Z0-9_]/.test(query)) {
return sendError(res, new Error('Invalid characters'));
}
try {
2022-03-09 15:16:54 +09:00
const response = await fetch(`https://reddit.com/r/${query}`, 'GET');
2022-03-06 20:30:43 +01:00
const body = await response.text();
const availability = body.includes(
'Sorry, there aren’t any communities on Reddit with that name.'
);
send(res, { availability });
2022-03-09 15:16:54 +09:00
} catch (err: any) {
2022-03-06 20:30:43 +01:00
sendError(res, err);
}
}