forked from pycontw/pycontw-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-server-middlewares.js
32 lines (30 loc) · 1.02 KB
/
json-server-middlewares.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
'use strict'
const url = require('url')
module.exports = (req, res, next) => {
// enable inserting a comma-separated string as
// multi-value query parameters for event type
const eventType = req.query.event_type
if (eventType && !Array.isArray(eventType)) {
req.query.event_type = eventType.split(',')
}
// return Object instead of Array if the singular flag is given
const _send = res.send
res.send = function (body) {
// eslint-disable-next-line
const parsedUrl = url.parse(req.url, true)
if (parsedUrl.query.singular) {
try {
const json = JSON.parse(body)
if (Array.isArray(json)) {
if (json.length === 1) {
return _send.call(this, json[0])
} else if (json.length === 0) {
return _send.call(this, {}, 404)
}
}
} catch (e) {}
}
return _send.call(this, body)
}
next()
}