Files
shields/services/opm/opm-version.service.js
dependabot[bot] 0f288a42fc chore(deps): bump got from 11.8.3 to 12.0.1 (#7370)
* chore(deps): bump got from 11.8.3 to 12.0.1
* update limit syntax
* update CancelError import
* update timeout syntax
* set missing user/pass to empty string for basic auth
* fix opm badge

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: chris48s <chris.shaw480@gmail.com>
Co-authored-by: chris48s <chris48s@users.noreply.github.com>
2022-01-29 20:04:11 +00:00

60 lines
1.4 KiB
JavaScript

import { renderVersionBadge } from '../version.js'
import { BaseService, NotFound, InvalidResponse } from '../index.js'
export default class OpmVersion extends BaseService {
static category = 'version'
static route = {
base: 'opm/v',
pattern: ':user/:moduleName',
}
static examples = [
{
title: 'OPM',
namedParams: {
user: 'openresty',
moduleName: 'lua-resty-lrucache',
},
staticPreview: renderVersionBadge({ version: 'v0.08' }),
},
]
static defaultBadgeData = {
label: 'opm',
}
async fetch({ user, moduleName }) {
const { res } = await this._request({
url: `https://opm.openresty.org/api/pkg/fetch`,
options: {
method: 'HEAD',
searchParams: {
account: user,
name: moduleName,
},
},
errorMessages: {
404: 'module not found',
},
})
// TODO: set followRedirect to false and intercept 302 redirects
const location = res.redirectUrls[0].toString()
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 })
}
}