Files
shields/services/requires/requires.service.js
T
Paul MelnikowandGitHub 1cdcaabd38 Unify order of properties and methods in services (#3353)
I find having these in a consistent order makes the services much faster to read.

This is the order I’ve generally been using:

1. Category
2. Route
3. Examples
4. Rendering
5. Other helpers (`fetch()`, `transform()`)
6. `handle()`
2019-04-23 21:36:04 -04:00

76 lines
1.8 KiB
JavaScript

'use strict'
const { BaseJsonService } = require('..')
const Joi = require('joi')
const statusSchema = Joi.object({
status: Joi.string().required(),
}).required()
module.exports = class RequiresIo extends BaseJsonService {
static get category() {
return 'dependencies'
}
static get route() {
return {
base: 'requires',
pattern: ':service/:user/:repo/:branch*',
}
}
static get examples() {
return [
{
title: 'Requires.io',
pattern: ':service/:user/:repo',
namedParams: { service: 'github', user: 'celery', repo: 'celery' },
staticPreview: this.render({ status: 'up-to-date' }),
},
{
title: 'Requires.io (branch)',
pattern: ':service/:user/:repo/:branch',
namedParams: {
service: 'github',
user: 'celery',
repo: 'celery',
branch: 'master',
},
staticPreview: this.render({ status: 'up-to-date' }),
},
]
}
static get defaultBadgeData() {
return { label: 'requirements' }
}
static render({ status }) {
let message = status
let color = 'lightgrey'
if (status === 'up-to-date') {
message = 'up to date'
color = 'brightgreen'
} else if (status === 'outdated') {
color = 'yellow'
} else if (status === 'insecure') {
color = 'red'
}
return { message, color }
}
async fetch({ service, user, repo, branch }) {
const url = `https://requires.io/api/v1/status/${service}/${user}/${repo}`
return this._requestJson({
url,
schema: statusSchema,
options: { qs: { branch } },
})
}
async handle({ service, user, repo, branch }) {
const { status } = await this.fetch({ service, user, repo, branch })
return this.constructor.render({ status })
}
}