This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
/
index.js
159 lines (146 loc) · 4.68 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import express from "express";
import pkg from "../../package.json";
const debug = require("debug")("phenomic:core:api");
export const encode = (text: string) => new Buffer(text).toString("base64");
export const decode = (text: string) => new Buffer(text, "base64").toString();
const connect = (list, limit, previousList = []) => {
const hasNextPage = limit === undefined ? false : list.length > limit;
const hasPreviousPage = previousList.length > 0;
const previousPageIsFirst = limit ? previousList.length <= limit : undefined;
// we are retrieving limit + 1 to know if there is more page or not
// so when getting the previous item, we need to check if we want the last
// item or the one before (since we added one to the limit)
const previousIndex = previousList.length - 1 - (previousPageIsFirst ? 0 : 1);
const nextIndex = list.length - 1;
return {
hasPreviousPage,
previousPageIsFirst,
previous:
hasPreviousPage && previousList[previousIndex]
? encode(previousList[previousIndex].key)
: undefined,
hasNextPage,
next:
hasNextPage && list[nextIndex] ? encode(list[nextIndex].key) : undefined,
list: list.slice(0, limit)
};
};
function createServer(db: PhenomicDB, plugins: PhenomicPlugins) {
debug("creating server");
const server = express();
server.get("/", async function(req: express$Request, res: express$Response) {
debug("get api version");
res.json({
engine: "phenomic",
version: pkg.version
});
});
server.get("/:path/by-:filter/:value/:order.json", async function(
req: express$Request,
res: express$Response
) {
debug(req.url, JSON.stringify(req.params));
try {
const reverse = req.params.order === "desc";
const list = await db.getList(
req.params.path,
{ reverse },
req.params.filter,
req.params.value
);
res.json(connect(list));
} catch (error) {
console.error(error);
res.status(404).end();
}
});
server.get(
"/:path/by-:filter/:value/:order/limit-:limit.json",
async function(req: express$Request, res: express$Response) {
debug(req.url, JSON.stringify(req.params));
try {
const limit = parseInt(req.params.limit);
const reverse = req.params.order === "desc";
const list = await db.getList(
req.params.path,
{
limit: limit + 1,
reverse
},
req.params.filter,
req.params.value
);
res.json(connect(list, limit));
} catch (error) {
console.error(error);
res.status(404).end();
}
}
);
server.get(
"/:path/by-:filter/:value/:order/limit-:limit/after-:after.json",
async function(req: express$Request, res: express$Response) {
debug(req.url, JSON.stringify(req.params));
try {
const limit = parseInt(req.params.limit);
const after = decode(req.params.after);
// @todo check lt validity (exist?); otherwise, trigger an error (404?)
// cause during dev all "lt" are responding 200, even random values
// but in production, it's not the case as only known values are
// generated as endpoints
const reverse = req.params.order === "desc";
const [list, previousList] = await Promise.all([
db.getList(
req.params.path,
{
limit: limit + 1,
[reverse ? "lte" : "gte"]: after,
reverse
},
req.params.filter,
req.params.value
),
db.getList(
req.params.path,
{
limit: limit + 1,
[reverse ? "gt" : "lt"]: after,
reverse: !reverse
},
req.params.filter,
req.params.value
)
]);
res.json(connect(list, limit, previousList));
} catch (error) {
console.error(error);
res.status(404).end();
}
}
);
server.get("/:path/item/*.json", async function(
req: express$Request,
res: express$Response
) {
debug(req.url, JSON.stringify(req.params));
try {
const resource = await db.get(req.params.path, req.params["0"]);
res.json(resource.value);
} catch (error) {
console.error(error);
res.status(404).end();
}
});
// Install the plugins
plugins.forEach(plugin => {
if (typeof plugin.define === "function") {
debug(`installing plugin '${plugin.name}'`);
// $FlowFixMe typeof above is not enough?
plugin.define(server, db);
} else {
debug(`plugin '${plugin.name}' have no API definition`);
}
});
return server;
}
export default createServer;