-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
bit-components.service.js
61 lines (52 loc) · 1.45 KB
/
bit-components.service.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict'
const Joi = require('@hapi/joi')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { downloadCount } = require('../color-formatters')
const { BaseJsonService } = require('..')
const collectionSchema = Joi.object({
payload: Joi.object({
totalComponents: nonNegativeInteger,
}).required(),
}).required()
module.exports = class BitComponents extends BaseJsonService {
static get category() {
return 'other'
}
static get route() {
return {
base: 'bit/collection/total-components',
pattern: ':owner/:collection',
}
}
static get examples() {
return [
{
title: 'bit',
namedParams: { owner: 'ramda', collection: 'ramda' },
staticPreview: this.render({ count: 330 }),
keywords: ['components'],
},
]
}
static get defaultBadgeData() {
return { label: 'components' }
}
static render({ count }) {
return { message: metric(count), color: downloadCount(count) }
}
async fetch({ owner, collection }) {
const url = `https://api.bit.dev/scope/${owner}/${collection}/`
return this._requestJson({
url,
schema: collectionSchema,
errorMessages: {
404: 'collection not found',
},
})
}
async handle({ owner, collection }) {
const json = await this.fetch({ owner, collection })
return this.constructor.render({ count: json.payload.totalComponents })
}
}