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

24 lines
537 B
TypeScript
Raw Normal View History

2019-09-17 14:30:26 +09:00
import npmName from 'npm-name';
2019-12-24 01:57:07 +09:00
import {send, sendError, NowRequest, NowResponse} from '../util/http';
2019-07-31 01:12:51 +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-07-31 01:12:51 +09:00
2019-08-06 01:17:45 +09:00
if (!query) {
2019-12-10 18:33:39 +09:00
return sendError(res, new Error('No query given'));
2019-07-31 01:12:51 +09:00
}
try {
2019-09-17 14:30:26 +09:00
const availability = await npmName(`@${query}`);
send(res, {availability});
2019-07-31 01:12:51 +09:00
} catch (err) {
2019-12-10 18:33:39 +09:00
if (err.code === 'ENOTFOUND') {
return send(res, {availability: true});
}
2019-09-17 14:30:26 +09:00
sendError(res, err);
2019-07-31 01:12:51 +09:00
}
}