-
Notifications
You must be signed in to change notification settings - Fork 4
/
req2resp.js
53 lines (51 loc) · 1.26 KB
/
req2resp.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
"use strict";
var route = require("pouchdb-route");
var Promise = Promise || require("lie");
var uuid = require("random-uuid-v4");
module.exports = function (PouchDB) {
return function req2resp(req) {
if (!PouchDB) {
return Promise.reject("no_pouchdb_found");
}
return Promise.resolve()
.then(function () {
if (!req.path.length) {
return {
version: "0.1.0",
};
} else if (req.path[0] === "_session") {
return {
ok: true,
userCtx: {
name: null,
roles: ["_admin"]
}
};
} else if (req.path[0] === "_uuids") {
var uuids = [];
for (var i = 0; i < (req.query.count || 1); i += 1) {
uuids.push(uuid());
}
return {
uuids: uuids
};
} else if (req.path[0] === "_active_tasks") {
return [];
} else {
return route(PouchDB, req, {});
}
})
.then(function (resp) {
return {
status: 200,
body: JSON.stringify(resp)
};
})
.catch(function (err) {
return {
status: err.status || 500,
body: JSON.stringify(err)
};
});
};
};