-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathclient-routes.js
More file actions
executable file
·218 lines (194 loc) · 5.44 KB
/
Copy pathclient-routes.js
File metadata and controls
executable file
·218 lines (194 loc) · 5.44 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const app = require("express");
const router = app.Router();
const esUtils = require("../utility/ES-utils");
const esDataSpaceClient = esUtils.getDataSpaceESClient();
/**
* Helper: Safely parses req.query.body as JSON.
* Returns parsed object on success, or sends a 400 error response and returns null.
*/
function parseQueryBody(req, res) {
try {
const parsed = JSON.parse(req.query.body);
if (!parsed || typeof parsed !== "object") {
res.status(400).send({ error: "Query body must be a non-empty JSON object" });
return null;
}
return parsed;
} catch (e) {
res.status(400).send({ error: "Invalid query body: must be valid JSON" });
return null;
}
}
// autosuggest
router.get("/auto-suggest", function (req, res) {
const body = parseQueryBody(req, res);
if (!body) return;
esDataSpaceClient
.search({
index: "scigraph",
body,
})
.then((response) => {
res.send(response);
})
.catch((err) => {
console.error("Error occured in auto suggest");
console.error(req.query.body);
console.error(err);
res.send([]);
});
});
// entity-search
router.get("/find-slug-by-curie", async function (req, res) {
const slug = await esUtils.findSlugByCurie(req.query.curie);
res.send(slug);
});
router.get("/find-by-slug", async function (req, res) {
const slugDetails = await esUtils.findBySlug(req.query.id);
res.send(slugDetails);
});
router.get("/details", function (req, res) {
const body = parseQueryBody(req, res);
if (!body) return;
esDataSpaceClient
.search({
index: "scigraph",
body,
})
.then((response) => {
res.send(response);
})
.catch((err) => {
console.error("Error occurred in details");
console.error(err);
res.status(500).send({ error: "Failed to fetch details" });
});
});
// datasource client
router.get("/all-data-by-entity", async function (req, res) {
const response = await esUtils.queryAllDataSpaceByEntity(req.query.labels);
res.send(response);
});
/* this API is similar to all-data-by-entity
* Key difference: above API fetches no data but aggregation
*/
router.get("/all-data-by-free-text", async function (req, res) {
const esclientToUse = esDataSpaceClient;
const index = req.query.index || "scr*";
// FIX 2: Validate and parse req.query.body before use
const query = parseQueryBody(req, res);
if (!query) return;
const handleCountResponse = (total_count) => {
esclientToUse
.search({
index,
body: req.query.body,
})
.then((response) => {
response.total_count = total_count;
res.send(response);
})
.catch((exp) => {
console.error("error occured in all-data-by-free-text search");
console.error(exp);
res.send([]);
});
};
esclientToUse
.count({
index,
body: {
query: query.query,
},
})
.then((response) => {
console.debug("check free data response");
console.debug(response);
handleCountResponse(response.count);
})
.catch((exp) => {
// FIX 1: was logging undefined `response`, now correctly logs `exp`
console.debug("check free data exp");
console.debug(exp);
handleCountResponse();
});
});
router.get("/source-data-by-entity", function (req, res) {
// FIX 2: Validate and parse req.query.body before use
const query = parseQueryBody(req, res);
if (!query) return;
const esclientToUse = esDataSpaceClient;
// FIX: moved esclientToUse declaration above handleCountResponse to avoid temporal dead zone
const handleCountResponse = (total_count) => {
esclientToUse
.search({
index: req.query.source,
body: req.query.body,
})
.then((response) => {
response.total_count = total_count;
res.send(response);
})
.catch((exp) => {
console.error("error occured in source-data-by-entity");
console.error(exp);
res.send([]);
});
};
esclientToUse
.count({
index: req.query.source,
body: {
query: query.query,
},
})
.then((response) => {
handleCountResponse(response.count);
})
.catch((exp) => {
console.error("error occurred in source-data-by-entity count");
console.error(exp);
handleCountResponse();
});
});
// Literature
router.get("/literature-by-curie-paths", function (req, res) {
const body = parseQueryBody(req, res);
if (!body) return;
esDataSpaceClient
.search({
index: "pubmed-19",
body,
})
.then((response) => {
res.send(response);
})
.catch((err) => {
console.error("Error occurred in literature-by-curie-paths");
console.error(err);
res.status(500).send({ error: "Failed to fetch literature data" });
});
});
// Training
router.get("/training-space-by-curie-paths", function (req, res) {
console.debug("check query");
console.debug(req.query);
const body = parseQueryBody(req, res);
if (!body) return;
esDataSpaceClient
.search({
index: "train_scr_022036_trainingspace",
body,
})
.then((response) => {
console.debug("check training space response");
console.debug(response);
res.send(response);
})
.catch((exp) => {
console.error("Error occurred in training space search");
console.error(exp);
res.status(500).send({ error: "Failed to fetch training space data" });
});
});
module.exports = router;