Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 18, 2018
0 parents commit 83372e5
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .bumpedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
files:
- package.json
plugins:
prerelease:
Linting config files:
plugin: bumped-finepack
postrelease:
Generating CHANGELOG file:
plugin: bumped-changelog
Committing new version:
plugin: bumped-terminal
command: 'git add CHANGELOG.md package.json && git commit -m "Release $newVersion"'
Detecting problems before publish:
plugin: bumped-terminal
command: 'git-dirty && npm test'
Publishing tag to GitHub:
plugin: bumped-terminal
command: 'git tag $newVersion && git push && git push --tags'
Publishing to NPM:
plugin: bumped-terminal
command: npm publish
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
indent_brace_style = 1TBS
spaces_around_operators = true
quote_type = auto

[package.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
############################
# npm
############################
node_modules
npm-debug.log
.node_history
yarn.lock
package-lock.json

############################
# tmp, editor & OS files
############################
.tmp
*.swo
*.swp
*.swn
*.swm
.DS_Store
*#
*~
.idea
*sublime*
nbproject

############################
# Tests
############################
testApp
coverage
.nyc_output

############################
# Other
############################
4 changes: 4 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unsafe-perm=true
save-prefix=~
shrinkwrap=false
save=false
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "node"
- "lts/*"
after_success: npm run coveralls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2018 Kiko Beats <josefrancisco.verdu@gmail.com> (kikobeats.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# avatar-microservice

<p align="center">
<br>
<img src="https://i.imgur.com/Mh13XWB.gif" alt="avatar-microservice">
<br>
</p>

![Last version](https://img.shields.io/github/tag/Kikobeats/avatar-microservice.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/Kikobeats/avatar-microservice/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/avatar-microservice)
[![Dependency status](https://img.shields.io/david/Kikobeats/avatar-microservice.svg?style=flat-square)](https://david-dm.org/Kikobeats/avatar-microservice)
[![Dev Dependencies Status](https://img.shields.io/david/dev/Kikobeats/avatar-microservice.svg?style=flat-square)](https://david-dm.org/Kikobeats/avatar-microservice#info=devDependencies)
[![NPM Status](https://img.shields.io/npm/dm/avatar-microservice.svg?style=flat-square)](https://www.npmjs.org/package/avatar-microservice)
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/Kikobeats)

> Get users avatar across multiple services
## Install

```bash
$ npm install avatar-microservice --save
```

## Usage

```js
const avatarMicroservice = require('avatar-microservice')

avatarMicroservice('do something')
// => return something
```

## API

### avatarMicroservice(input, [options])

#### input

*Required*<br>
Type: `string`

Lorem ipsum.

#### options

##### foo

Type: `boolean`<br>
Default: `false`

Lorem ipsum.

## License

**avatar-microservice** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/avatar-microservice/blob/master/LICENSE.md) License.<br>
Authored and maintained by Kiko Beats with help from [contributors](https://github.com/Kikobeats/avatar-microservice/contributors).

> [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)
16 changes: 16 additions & 0 deletions bin/server
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

'use strict'

const express = require('express')

const app = express()

require('./index')(app, express)

const port = process.env.PORT || process.env.port || 3000
const { name } = require('../package.json')

app.listen(port, function () {
console.log(`${name} is running at http://localhost:${port}`)
})
76 changes: 76 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "avatar-microservice",
"description": "Get users avatar across multiple services",
"homepage": "https://documentup.com/Kikobeats/avatar-microservice",
"version": "0.0.0",
"main": "src/index.js",
"author": {
"email": "josefrancisco.verdu@gmail.com",
"name": "Kiko Beats",
"url": "https://kikobeats.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kikobeats/avatar-microservice.git"
},
"bugs": {
"url": "https://github.com/Kikobeats/avatar-microservice/issues"
},
"keywords": [],
"dependencies": {
"aigle": "~1.12.0-alpha.6",
"compression": "~1.7.2",
"cors": "~2.8.4",
"express": "~4.16.3",
"helmet": "~3.12.0",
"is-email-like": "~1.0.0",
"lodash": "~4.17.5",
"morgan": "~1.9.0"
},
"devDependencies": {
"ava": "latest",
"coveralls": "latest",
"finepack": "latest",
"git-dirty": "latest",
"husky": "latest",
"lint-staged": "latest",
"nyc": "latest",
"prettier-standard": "latest",
"standard": "latest",
"standard-markdown": "latest",
"svr": "latest"
},
"engines": {
"node": ">= 6"
},
"files": [
"bin",
"src"
],
"scripts": {
"clean": "rm -rf node_modules",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"dev": "svr",
"lint": "standard-markdown && standard",
"precommit": "lint-staged",
"pretest": "npm run lint",
"pretty": "prettier-standard index.js {core,test,bin}/**/*.js --single-quote",
"start": "bin/server",
"test": "nyc ava"
},
"license": "MIT",
"lint-staged": {
"package.json": [
"finepack",
"git add"
],
"*.js": [
"prettier-standard",
"git add"
],
"*.md": [
"standard-markdown",
"git add"
]
}
}
77 changes: 77 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'

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

const services = 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 stream = got.stream(url)
stream.on('response', resAvatar =>
res.set('Content-Type', resAvatar.headers['content-type'])
)
return stream.pipe(res)
}

const createAvatarBy = ({ json }) => async (req, res) => {
const { key } = req.params
const collection = isEmail(key) ? servicesBy.email : servicesBy.username

const url = await aigle
.resolve(collection)
.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)
}

module.exports = (app, express) => {
app
.use(require('helmet')())
.use(require('compression')())
.use(require('cors')())
.use(require('morgan')('combined'))
.use(express.static('static'))
.disable('x-powered-by')

app.get('/', (req, res) => res.status(204).send())
app.get('/robots.txt', (req, res) => res.status(204).send())
app.get('/favicon.txt', (req, res) => res.status(204).send())

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 })
)
app.get(
`/${serviceName}/:key/json`,
createAvatarFromService(service, { json: true })
)
})

return app
}
9 changes: 9 additions & 0 deletions src/services/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

module.exports = async username =>
`https://avatars.githubusercontent.com/${username}?size=500`

module.exports.supported = {
email: false,
username: true
}
17 changes: 17 additions & 0 deletions src/services/gravatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const crypto = require('crypto')

const md5 = str =>
crypto
.createHash('md5')
.update(str)
.digest('hex')

module.exports = async username =>
`https://gravatar.com/avatar/${md5(username)}?size=500`

module.exports.supported = {
email: true,
username: false
}
5 changes: 5 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
twitter: require('./twitter'),
gravatar: require('./gravatar'),
github: require('./github')
}
10 changes: 10 additions & 0 deletions src/services/twitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

module.exports = async username => {
return `https://twitter.com/${username}/profile_image?size=original`
}

module.exports.supported = {
email: false,
username: true
}

0 comments on commit 83372e5

Please sign in to comment.