forked from robburger/citizen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
115 lines (97 loc) · 3.05 KB
/
list.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { Router } = require('express');
const _ = require('lodash');
const { findAll, getVersions } = require('../lib/store');
const { makeUrl } = require('../lib/util');
const router = Router();
// https://www.terraform.io/docs/registry/api.html#search-modules
router.get('/search', async (req, res) => {
if (!req.query.q) {
return res.status(400).render('error', {
message: 'q parameter required.',
});
}
// `q` search in `name` field.
// It could be extended to other fields. Specification said it depends on registry implementation.
const options = {
...req.query,
selector: {
name: {
$regex: new RegExp(req.query.q),
},
},
q: null,
};
if (req.params.namespace) {
options.namespace = req.params.namespace;
}
const data = await findAll(options);
if (data.meta.nextOffset) {
data.meta.nextUrl = makeUrl(req, {
limit: data.meta.limit,
offset: data.meta.nextOffset,
});
}
return res.render('modules/list', data);
});
// https://www.terraform.io/docs/registry/api.html#list-modules
router.get(['/', '/:namespace'], async (req, res) => {
const options = { ...req.query };
if (req.params.namespace) {
options.namespace = req.params.namespace;
}
const data = await findAll(options);
if (data.meta.nextOffset) {
data.meta.nextUrl = makeUrl(req, {
limit: data.meta.limit,
offset: data.meta.nextOffset,
});
}
res.render('modules/list', data);
});
// https://www.terraform.io/docs/registry/api.html#list-available-versions-for-a-specific-module
router.get('/:namespace/:name/:provider/versions', async (req, res, next) => {
const options = { ...req.params };
try {
const versions = await getVersions(options);
res.render('modules/versions', {
source: `${req.params.namespace}/${req.params.name}/${req.params.provider}`,
versions,
});
} catch (e) {
next(e);
}
});
// https://www.terraform.io/docs/registry/api.html#list-latest-version-of-module-for-all-providers
router.get('/:namespace/:name', async (req, res) => {
const options = {
offset: 0,
// FIXME: to support for too many modules
limit: 100,
selector: {
namespace: req.params.namespace,
name: req.params.name,
},
};
const result = await findAll(options);
const grouped = _.groupBy(result.modules, m => `${m.namespace}/${m.name}/${m.provider}`);
const modules = Object.keys(grouped).map((key) => {
const sorted = _.orderBy(grouped[key], ['versions']);
return sorted[sorted.length - 1];
});
const totalCount = modules.length;
const offset = +req.query.offset || 0;
const limit = +req.query.limit || 15;
const nextOffset = (offset + 1) * limit;
const hasNext = totalCount > nextOffset;
const pagedModules = _.slice(modules, offset, nextOffset);
res.render('modules/list', {
meta: {
limit,
currentOffset: offset,
nextOffset: hasNext ? nextOffset : null,
nextUrl: hasNext ? makeUrl(req, { limit, offset: nextOffset }) : null,
},
modules: pagedModules,
});
});
module.exports = router;