1
0
mirror of https://github.com/uetchy/namae.git synced 2025-08-20 01:48:12 +09:00

fix: use dedicated api for twittter

This commit is contained in:
2020-06-11 21:59:58 +09:00
parent ec858f3473
commit b8ce81b276
2 changed files with 38 additions and 8 deletions

27
api/services/twitter.ts Normal file
View File

@@ -0,0 +1,27 @@
import {send, sendError, fetch, NowResponse, NowRequest} from '../util/http';
export default async function handler(
req: NowRequest,
res: NowResponse,
): Promise<void> {
const {query} = req.query;
if (!query) {
return sendError(res, new Error('No query given'));
}
if (/[^a-zA-Z0-9_]/.test(query)) {
return sendError(res, new Error('Invalid characters'));
}
try {
const response = await fetch(
`https://api.twitter.com/i/users/username_available.json?username=${query}`,
'GET',
).then((res) => res.json());
const availability = response.valid;
send(res, {availability});
} catch (err) {
sendError(res, err);
}
}