Files
shields/services/bit/bit-components.service.js
T
chris48sandGitHub 8621fe42d7 Upgrade Joi (#3505)
* upgrade joi
* find & replace ALL THE THINGS
* update related deps
2019-06-02 21:59:55 +01:00

62 lines
1.4 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { BaseJsonService } = require('..')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { downloadCount } = require('../color-formatters')
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 })
}
}