-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathWidgets.ts
67 lines (58 loc) · 2.02 KB
/
Widgets.ts
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
import type { RequestHandler, Response } from 'express';
import express from 'express';
import * as WidgetsHandler from '../handler/widgets/WidgetsHandler';
import { getRBAC, isAuthorized } from '../handler/AuthHandler';
import { getTokenFromCookies } from '../utils';
import type {
GetWidgetsResponse,
GetWidgetsUsedResponse,
PostWidgetsQueryParams,
PostWidgetsResponse,
PutWidgetsQueryParams,
PutWidgetsResponse
} from './Widgets.types';
const router = express.Router();
router.use(express.json());
router.get('/', (_req, res: Response<GetWidgetsResponse>, next) => {
WidgetsHandler.listWidgets()
.then(widgets => res.send(widgets))
.catch(next);
});
const validateInstallWidgetsPermission: RequestHandler = async (req, res, next) => {
const permissionId = 'stage_install_widgets';
const rbac = await getRBAC(getTokenFromCookies(req));
const permission = rbac.permissions[permissionId];
if (!isAuthorized(req.user!, permission)) {
res.sendStatus(403);
}
next();
};
router.post<never, PostWidgetsResponse, any, PostWidgetsQueryParams>(
'/',
validateInstallWidgetsPermission,
(req, res, next) => {
WidgetsHandler.installWidget(req.query.url, req.user!.username, req)
.then(data => res.send(data))
.catch(next);
}
);
router.put<never, PutWidgetsResponse, any, PutWidgetsQueryParams>(
'/',
validateInstallWidgetsPermission,
(req, res, next) => {
WidgetsHandler.updateWidget(req.query.id, req.query.url, req)
.then(data => res.send(data))
.catch(next);
}
);
router.get('/:widgetId/used', (req, res: Response<GetWidgetsUsedResponse>, next) => {
WidgetsHandler.isWidgetUsed(req.params.widgetId)
.then(result => res.send(result))
.catch(next);
});
router.delete('/:widgetId', validateInstallWidgetsPermission, (req, res, next) => {
WidgetsHandler.deleteWidget(req.params.widgetId)
.then(() => res.status(200).end())
.catch(next);
});
export default router;