Files
shields/services/crates/crates-version.service.js
Caleb Cartwright 9f814fa3e2 convert some service classes to static props, run [cran crates ctan] (#5517)
* refactor(cran): convert to static props

* refactor(crates): convert to static props

* refactor(ctan): convert to static props

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2020-09-12 17:46:33 +00:00

33 lines
899 B
JavaScript

'use strict'
const { renderVersionBadge } = require('../version')
const { InvalidResponse } = require('..')
const { BaseCratesService, keywords } = require('./crates-base')
module.exports = class CratesVersion extends BaseCratesService {
static category = 'version'
static route = { base: 'crates/v', pattern: ':crate' }
static examples = [
{
title: 'Crates.io',
namedParams: { crate: 'rustc-serialize' },
staticPreview: renderVersionBadge({ version: '0.3.24' }),
keywords,
},
]
transform(json) {
if (json.errors) {
throw new InvalidResponse({ prettyMessage: json.errors[0].detail })
}
return { version: json.version ? json.version.num : json.crate.max_version }
}
async handle({ crate }) {
const json = await this.fetch({ crate })
const { version } = this.transform(json)
return renderVersionBadge({ version })
}
}