Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 18, 2018
1 parent 83372e5 commit 42d8674
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
44 changes: 14 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
'use strict'

const { forEach, reduce } = require('lodash')
const { forEach } = require('lodash')
const isEmail = require('is-email-like')
const aigle = require('aigle')
const got = require('got')

const services = require('./services')
const { services, servicesBy } = require('./services')

const servicesBy = reduce(
services,
(acc, service, serviceName) => {
const { supported } = service
if (supported.email) acc.email.push(serviceName)
if (supported.username) acc.username.push(serviceName)
return acc
},
{ email: [], username: [] }
)

const createAvatarFromService = (fn, { json }) => async (req, res) => {
const { key } = req.params
const url = await fn(key)
if (json) return res.json({ url })
const sendAvatar = ({ url, res }) => {
const stream = got.stream(url)
stream.on('response', resAvatar =>
res.set('Content-Type', resAvatar.headers['content-type'])
)
return stream.pipe(res)
}

const createAvatarFromService = (fn, { json }) => async (req, res) => {
const { key } = req.params
const url = await fn(key)
return json ? res.json({ url }) : sendAvatar({ res, url })
}

const createAvatarBy = ({ json }) => async (req, res) => {
const { key } = req.params
const collection = isEmail(key) ? servicesBy.email : servicesBy.username
Expand All @@ -38,12 +30,7 @@ const createAvatarBy = ({ json }) => async (req, res) => {
.map(service => services[service](key))
.find(url => got.head(url))

if (json) return res.json({ url })
const stream = got.stream(url)
stream.on('response', resAvatar =>
res.set('Content-Type', resAvatar.headers['content-type'])
)
return stream.pipe(res)
return json ? res.json({ url }) : sendAvatar({ res, url })
}

module.exports = (app, express) => {
Expand All @@ -62,14 +49,11 @@ module.exports = (app, express) => {
app.get(`/:key`, createAvatarBy({ json: false }))
app.get(`/:key/json`, createAvatarBy({ json: true }))

forEach(services, (service, serviceName) => {
app.get(
`/${serviceName}/:key`,
createAvatarFromService(service, { json: false })
)
forEach(services, (fn, service) => {
app.get(`/${service}/:key`, createAvatarFromService(fn, { json: false }))
app.get(
`/${serviceName}/:key/json`,
createAvatarFromService(service, { json: true })
`/${service}/:key/json`,
createAvatarFromService(fn, { json: true })
)
})

Expand Down
18 changes: 17 additions & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
module.exports = {
'use strict'

const { reduce } = require('lodash')

const services = {
twitter: require('./twitter'),
gravatar: require('./gravatar'),
github: require('./github')
}

const servicesBy = reduce(
services,
(acc, { supported }, service) => {
if (supported.email) acc.email.push(service)
if (supported.username) acc.username.push(service)
return acc
},
{ email: [], username: [] }
)

module.exports = { services, servicesBy }

0 comments on commit 42d8674

Please sign in to comment.