* allow serviceData to override cacheSeconds with a longer value * prevent [endpoint] json cacheSeconds property exceeding service default * allow ShieldsRuntimeError to specify a cacheSeconds property By default error responses use the cacheLength of the service class throwing the error. This allows error to tell the handling layer the maxAge that should be set on the error badge response. * add customExceptions param This 1. allows us to specify custom properties to pass to the exception constructor if we throw any of the standard got errors e.g: `ETIMEDOUT`, `ECONNRESET`, etc 2. uses a custom `cacheSeconds` property (if set on the exception) to set the response maxAge * customExceptions --> systemErrors * errorMessages --> httpErrors
51 lines
1.3 KiB
JavaScript
51 lines
1.3 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 } 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 examples = [
|
|
{
|
|
title: 'bit',
|
|
namedParams: { owner: 'ramda', collection: 'ramda' },
|
|
staticPreview: this.render({ count: 330 }),
|
|
keywords: ['components'],
|
|
},
|
|
]
|
|
|
|
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 })
|
|
}
|
|
}
|