Files
shields/services/opm/opm-version.service.js
Wangchong Zhou a7b7cc00b5 Added [OPM] version badge (#4615)
Co-Authored-By: Caleb Cartwright <calebcartwright@users.noreply.github.com>

Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
2020-02-17 12:08:04 -06:00

70 lines
1.5 KiB
JavaScript

'use strict'
const { renderVersionBadge } = require('../version')
const { BaseService, NotFound, InvalidResponse } = require('..')
module.exports = class OpmVersion extends BaseService {
static get category() {
return 'version'
}
static get route() {
return {
base: 'opm/v',
pattern: ':user/:moduleName',
}
}
static get examples() {
return [
{
title: 'OPM',
namedParams: {
user: 'openresty',
moduleName: 'lua-resty-lrucache',
},
staticPreview: renderVersionBadge({ version: 'v0.08' }),
},
]
}
static get defaultBadgeData() {
return {
label: 'opm',
}
}
async fetch({ user, moduleName }) {
const { res } = await this._request({
url: `https://opm.openresty.org/api/pkg/fetch`,
options: {
method: 'HEAD',
qs: {
account: user,
name: moduleName,
},
},
errorMessages: {
404: 'module not found',
},
})
// XXX: intercept 302 redirects and set followRedirect to false
const location = res.request.path
if (!location) {
throw new NotFound({ prettyMessage: 'module not found' })
}
const version = location.match(`${moduleName}-(.+).opm`)[1]
if (!version) {
throw new InvalidResponse({ prettyMessage: 'version invalid' })
}
return version
}
async handle({ user, moduleName }) {
const version = await this.fetch({ user, moduleName })
return renderVersionBadge({ version })
}
}