1
0
mirror of https://github.com/uetchy/namae.git synced 2025-07-01 22:10:04 +09:00

fix: reconstruct api

This commit is contained in:
uetchy 2019-08-06 01:17:45 +09:00
parent 65453f95f7
commit a38cab3002
16 changed files with 87 additions and 80 deletions

View File

@ -17,32 +17,40 @@ yarn start
## Add new provider ## Add new provider
Create `web/src/components/cards/<NewCard>.js` and paste following template into it: Create `web/src/components/cards/<NewCard>.js`. Here is the example card that checks if spcified repository on GitHub is available.
```jsx ```jsx
import React from 'react' import React from 'react'
import { useTranslation } from 'react-i18next'
import { FaGithub } from 'react-icons/fa' import { FaGithub } from 'react-icons/fa'
import { Card } from '../Card' import { Card, Repeater, DedicatedAvailability } from '../Cards'
import { ExistentialAvailability } from '../Cards'
import { capitalize } from '../../util/text' export default function GithubCard({ name }) {
const { t } = useTranslation()
const lowerCase = name.toLowerCase()
const names = [name]
const moreNames = [
`${lowerCase}hq`,
`${lowerCase}-team`,
`${lowerCase}-org`,
`${lowerCase}-js`,
]
export default function NewCard({ name }) {
return ( return (
<Card <Card title={t('providers.github')}>
title="NewCard" <Repeater items={names} moreItems={moreNames}>
key={name} {(name) => (
nameList={[name, `${name}-team`]} <DedicatedAvailability
alternativeList={[`${capitalize(name)}HQ`]}> name={name}
{(name) => ( service="github"
<ExistentialAvailability link={`https://github.com/${name}`}
name={name} prefix="github.com/"
target={`https://api.newservice.com/items/${name}`} icon={<FaGithub />}
link={`https://newservice.com/${name}`} />
prefix="newservice.com/" )}
icon={<FaGithub />} </Repeater>
/>
)}
</Card> </Card>
) )
} }
@ -55,9 +63,8 @@ import NewCard from './components/cards/NewCard'
``` ```
```patch ```patch
<Cards> <SearchResult>
<CardHeader>Result for {query}</CardHeader> <Cards>
<CardContainer>
<GithubCard name={query} /> <GithubCard name={query} />
<DomainCard name={query} /> <DomainCard name={query} />
<TwitterCard name={query} /> <TwitterCard name={query} />
@ -69,8 +76,8 @@ import NewCard from './components/cards/NewCard'
<SlackCard name={query} /> <SlackCard name={query} />
<S3Card name={query} /> <S3Card name={query} />
+ <NewCard name={query} /> + <NewCard name={query} />
</CardContainer> </Cards>
</Cards> </SearchResult>
``` ```
### ExistentialAvailability ### ExistentialAvailability

View File

@ -1,18 +1,18 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return res.status(400).json({ error: 'no query given' }) return res.status(400).json({ error: 'no query given' })
} }
try { try {
const response = await fetch( const response = await fetch(
`https://crates.io/api/v1/crates/${encodeURIComponent(name)}` `https://crates.io/api/v1/crates/${encodeURIComponent(query)}`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,20 +1,20 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://packages.debian.org/buster/${encodeURIComponent(name)}`, `https://packages.debian.org/buster/${encodeURIComponent(query)}`,
'GET' 'GET'
) )
const body = await response.text() const body = await response.text()
const availability = body.includes('No such package') const availability = body.includes('No such package')
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -11,16 +11,16 @@ function resolvePromise(hostname) {
} }
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await resolvePromise(name) const response = await resolvePromise(query)
const availability = response && response.length > 0 ? false : true const availability = response && response.length > 0 ? false : true
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
if (err.code === 'ENODATA' || err.code === 'ENOTFOUND') { if (err.code === 'ENODATA' || err.code === 'ENOTFOUND') {
return res.status(200).json({ availability: true }) return res.status(200).json({ availability: true })

View File

@ -2,16 +2,16 @@ import whois from 'whois-json'
const { send, sendError } = require('../util/http') const { send, sendError } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await whois(name, { follow: 3, verbose: true }) const response = await whois(query, { follow: 3, verbose: true })
const availability = response[0].data.domainName ? false : true const availability = response[0].data.domainName ? false : true
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,18 +1,18 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://github.com/${encodeURIComponent(name)}` `https://github.com/${encodeURIComponent(query)}`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,21 +1,21 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://api.launchpad.net/devel/ubuntu/+source/${encodeURIComponent( `https://api.launchpad.net/devel/ubuntu/+source/${encodeURIComponent(
name query
)}`, )}`,
'GET' 'GET'
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -2,15 +2,15 @@ const npmName = require('npm-name')
const { send, sendError } = require('../util/http') const { send, sendError } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const availability = await npmName(`@${name}`) const availability = await npmName(`@${query}`)
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -2,15 +2,15 @@ const npmName = require('npm-name')
const { send, sendError } = require('../util/http') const { send, sendError } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const availability = await npmName(name) const availability = await npmName(query)
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,18 +1,18 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://pypi.org/pypi/${encodeURIComponent(name)}/json` `https://pypi.org/pypi/${encodeURIComponent(query)}/json`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,18 +1,18 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://rubygems.org/gems/${encodeURIComponent(name)}` `https://rubygems.org/gems/${encodeURIComponent(query)}`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,18 +1,18 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://${encodeURIComponent(name)}.s3.amazonaws.com` `https://${encodeURIComponent(query)}.s3.amazonaws.com`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -1,18 +1,18 @@
const { send, sendError, fetch } = require('../util/http') const { send, sendError, fetch } = require('../util/http')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return sendError(res, new Error('no query given')) return sendError(res, new Error('no query given'))
} }
try { try {
const response = await fetch( const response = await fetch(
`https://${encodeURIComponent(name)}.slack.com` `https://${encodeURIComponent(query)}.slack.com`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
if (err.code === 'ENOTFOUND') { if (err.code === 'ENOTFOUND') {
send(res, true) send(res, true)

View File

@ -3,18 +3,18 @@ const { send, sendError, fetch } = require('../util/http')
async function getAvailability(name) {} async function getAvailability(name) {}
module.exports = async (req, res) => { module.exports = async (req, res) => {
const name = req.query.name const { query } = req.query
if (!name) { if (!query) {
return res.status(400).json({ error: 'no query given' }) return res.status(400).json({ error: 'no query given' })
} }
try { try {
const response = await fetch( const response = await fetch(
`https://twitter.com/${encodeURIComponent(name)}` `https://twitter.com/${encodeURIComponent(query)}`
) )
const availability = response.status !== 200 const availability = response.status !== 200
send(res, availability) send(res, { availability })
} catch (err) { } catch (err) {
sendError(res, err) sendError(res, err)
} }

View File

@ -4,9 +4,9 @@ exports.fetch = (url, method = 'HEAD') => {
return fetch(url, { method }) return fetch(url, { method })
} }
exports.send = (res, availability) => { exports.send = (res, obj) => {
res.setHeader('Cache-Control', 'maxage=0, s-maxage=3600') res.setHeader('Cache-Control', 'maxage=0, s-maxage=3600')
res.json({ availability }) res.json(obj)
} }
exports.sendError = (res, error) => { exports.sendError = (res, error) => {

View File

@ -15,8 +15,8 @@
], ],
"routes": [ "routes": [
{ {
"src": "/availability/(?<provider>[^/]+)/(?<id>[^/]+)", "src": "/availability/(?<provider>[^/]+)/(?<query>[^/]+)",
"dest": "/api/services/$provider.js?name=$id" "dest": "/api/services/$provider.js?query=$query"
}, },
{ {
"src": "/(.*)", "src": "/(.*)",