Files
shields/services/bit/bit-components.service.js
T
chris48sandGitHub b2f47a3303 migrate examples to openApi part 2; affects [archlinux bitcomponents bountysource cdnjs chrome clearlydefined clojars cocoapods coincap] (#9428)
* convert an example that doesn't matter

* migrate some services from examples to openApi

* improve and de-dupe service titles

* revert changes to codefactor
2023-08-08 23:57:47 +00:00

60 lines
1.5 KiB
JavaScript

import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { downloadCount } from '../color-formatters.js'
import { BaseJsonService, pathParams } from '../index.js'
const collectionSchema = Joi.object({
payload: Joi.object({
totalComponents: nonNegativeInteger,
}).required(),
}).required()
export default class BitComponents extends BaseJsonService {
static category = 'other'
static route = {
base: 'bit/collection/total-components',
pattern: ':owner/:collection',
}
static openApi = {
'/bit/collection/total-components/{owner}/{collection}': {
get: {
summary: 'Bit',
parameters: pathParams(
{
name: 'owner',
example: 'ramda',
},
{
name: 'collection',
example: 'ramda',
},
),
},
},
}
static defaultBadgeData = { 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,
httpErrors: {
404: 'collection not found',
},
})
}
async handle({ owner, collection }) {
const json = await this.fetch({ owner, collection })
return this.constructor.render({ count: json.payload.totalComponents })
}
}