-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
opm-version.service.js
66 lines (58 loc) · 1.55 KB
/
opm-version.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { renderVersionBadge } from '../version.js'
import { BaseService, NotFound, InvalidResponse, pathParams } from '../index.js'
export default class OpmVersion extends BaseService {
static category = 'version'
static route = {
base: 'opm/v',
pattern: ':user/:moduleName',
}
static openApi = {
'/opm/v/{user}/{moduleName}': {
get: {
summary: 'OPM Version',
parameters: pathParams(
{
name: 'user',
example: 'openresty',
},
{
name: 'moduleName',
example: 'lua-resty-lrucache',
},
),
},
},
}
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,
},
},
httpErrors: {
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 })
}
}