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

feat: add js.org and homebrew provider

This commit is contained in:
2019-07-31 00:18:58 +09:00
parent b1514e78d8
commit 84fb52d71a
10 changed files with 183 additions and 64 deletions

View File

@@ -1,9 +1,8 @@
const fetch = require('isomorphic-unfetch')
async function getGitHubAvailability(name) {
const githubURL = 'https://github.com'
const response = await fetch(`${githubURL}/${encodeURIComponent(name)}`)
return response.status === 404
async function getAvailability(name) {
const response = await fetch(`https://github.com/${encodeURIComponent(name)}`)
return response.status !== 200
}
module.exports = async (req, res) => {
@@ -14,7 +13,7 @@ module.exports = async (req, res) => {
}
try {
const availability = await getGitHubAvailability(name)
const availability = await getAvailability(name)
res.json({ availability })
} catch (err) {
res.status(400).json({ error: err.message })

29
src/services/jsorg.js Normal file
View File

@@ -0,0 +1,29 @@
const fetch = require('isomorphic-unfetch')
async function getAvailability(name) {
try {
const response = await fetch(`https://${encodeURIComponent(name)}.js.org`)
return response.status !== 200
} catch (err) {
if (err.code === 'ENOTFOUND') {
return true
} else {
throw new Error(err.message)
}
}
}
module.exports = async (req, res) => {
const name = req.query.name
if (!name) {
return res.status(400).json({ error: 'no query given' })
}
try {
const availability = await getAvailability(name)
res.json({ availability })
} catch (err) {
res.status(400).json({ error: err.message })
}
}

View File

@@ -1,9 +1,10 @@
const fetch = require('isomorphic-unfetch')
async function getTwitterAvailability(name) {
const twitterURL = 'https://twitter.com'
const response = await fetch(`${twitterURL}/${encodeURIComponent(name)}`)
return response.status === 404
async function getAvailability(name) {
const response = await fetch(
`https://twitter.com/${encodeURIComponent(name)}`
)
return response.status !== 200
}
module.exports = async (req, res) => {
@@ -14,7 +15,7 @@ module.exports = async (req, res) => {
}
try {
const availability = await getTwitterAvailability(name)
const availability = await getAvailability(name)
res.json({ availability })
} catch (err) {
res.status(400).json({ error: err.message })