-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapi.js
58 lines (46 loc) · 1.27 KB
/
api.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
const express = require('express')
exports.handler = (dsp) => {
const api = express.Router()
api.post('/incidents', handler(createIncident))
api.put('/incidents', handler(updateIncident))
api.get('/incidents', handler(getIncidents))
api.get('/settings', handler(getSettings))
api.put('/settings', handler(updateSettings))
api.get('/status', handler(getStatus))
api.get('/publish', handler(publish))
return api
function getIncidents (req, res, next) {
return dsp.getIncidents()
}
function createIncident (req, res, next) {
return dsp.createIncident(req.body)
}
function updateIncident (req, res, next) {
return dsp.updateIncident(req.body)
}
function getSettings (req, res, next) {
return dsp.getSettings()
}
function updateSettings (req, res, next) {
return dsp.updateSettings(req.body, true)
}
function getStatus (req, res, next) {
return dsp.getStatus()
}
function publish (req, res, next) {
return dsp.publish()
}
}
function handler (fn) {
return async function (req, res, next) {
try {
res.json(await fn(req, res, next))
} catch (err) {
next(err)
}
}
}
function errorHandler (err, req, res, next) {
res.status(500).json({ message: err.message })
}
exports.errorHandler = errorHandler