forked from ngryman/badge-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (35 loc) · 990 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const color = require('./lib/color')
const compression = require('./lib/compression')
const constraints = require('./lib/constraints')
const fetch = require('./lib/fetch')
const init = require('./lib/init')
const pretty = require('./lib/pretty')
const send = require('./lib/send')
const bind = (fn, ...args) => fn.bind(fn, ...args)
const cond = (fn, param) => async (baton) => {
if (baton[param] != null) {
await fn(baton)
}
}
const tap = (fn) => async (baton) => {
await fn(baton)
return baton
}
/**
* Handle a badge request.
* It redirects to a shields.io badge of type: `size-{size}-brightgreen`.
*
* @param {ServerRequest} req
* @param {ServerResponse} res
* @return {Promise}
*/
module.exports = function badgeSize(req, res) {
return init(req)
.then(tap(fetch))
.then(tap(cond(compression, 'compression')))
.then(tap(pretty))
.then(tap(cond(constraints, 'max')))
.then(tap(color))
.then(bind(send, res))
.catch(bind(send, res))
}