Files
shields/services/crates/crates-license.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

51 lines
1.3 KiB
JavaScript

'use strict'
const { BaseCratesService, keywords } = require('./crates-base')
module.exports = class CratesLicense extends BaseCratesService {
static category = 'license'
static route = { base: 'crates/l', pattern: ':crate/:version?' }
static examples = [
{
title: 'Crates.io',
pattern: ':crate',
namedParams: { crate: 'rustc-serialize' },
staticPreview: this.render({ license: 'MIT/Apache-2.0' }),
keywords,
},
{
title: 'Crates.io',
pattern: ':crate/:version',
namedParams: { crate: 'rustc-serialize', version: '0.3.24' },
staticPreview: this.render({ license: 'MIT/Apache-2.0' }),
keywords,
},
]
static render({ license }) {
return {
label: 'license',
message: license,
color: 'blue',
}
}
async handle({ crate, version }) {
const json = await this.fetch({ crate, version })
if (json.errors) {
/* a call like
https://crates.io/api/v1/crates/libc/0.1
or
https://crates.io/api/v1/crates/libc/0.1.76
returns a 200 OK with an errors object */
return { message: json.errors[0].detail }
}
return this.constructor.render({
license: json.version ? json.version.license : json.versions[0].license,
})
}
}