forked from andr11111/relayer-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandardRelayerApi.js
More file actions
80 lines (66 loc) · 2.26 KB
/
standardRelayerApi.js
File metadata and controls
80 lines (66 loc) · 2.26 KB
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
const url = require("url");
const addStandardRelayerApi = (server, router) => {
server.post("/v0/debt_order", (req, res) => {
res.redirect(307, "/loanRequests");
});
server.get("/v0/debt_order/:agreementId", (req, res) => {
const agreementId = req.params.agreementId;
const redirectUrl = url.format({
pathname: `/loanRequests/${agreementId}`,
query: {
standardize: true,
},
});
res.redirect(redirectUrl);
});
server.get("/v0/debt_orders", (req, res) => {
const originalQuery = req.query;
const redirectUrl = url.format({
pathname: "/loanRequests",
query: {
...originalQuery,
standardize: true,
},
});
res.redirect(redirectUrl);
});
// correctly format POST requests that are redirected from the Standard Relayer API
server.use((req, res, next) => {
if (req.method === "POST" && req.path === "/loanRequests" && req.body.debtOrder) {
req.body = req.body.debtOrder;
}
next();
});
// reformat responses returned by the Standard Relayer API
router.render = (req, res) => {
const parsedUrl = url.parse(req.url, true);
const query = parsedUrl.query;
if (req.method === "GET" && req.path === "/loanRequests" && query.standardize) {
const debtOrders = res.locals.data.map((loanRequest) => {
return {
debtOrder: loanRequest,
metaData: { agreementId: loanRequest.id },
};
});
res.jsonp({
total: debtOrders.length,
page: 1,
perPage: debtOrders.length,
debtOrders,
});
} else if (
req.method === "GET" &&
req.path.match(/loanRequests\/(\d+)/) &&
query.standardize
) {
const loanRequest = res.locals.data;
res.jsonp({
debtOrder: loanRequest,
metaData: { agreementId: loanRequest.id },
});
} else {
res.jsonp(res.locals.data);
}
};
};
module.exports = { addStandardRelayerApi };