forked from rajtatata/cloudflare-worker-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
45 lines (41 loc) · 949 Bytes
/
request.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
module.exports = class AppReq {
constructor(request) {
this.request = request;
this.params = {};
}
async body() {
try {
return await this.request.json();
} catch (error) {
return {}; // cases when body is null, but still json in content header
}
}
query() {
try {
let query = {};
let queryString = this.request.url.split("?")[1];
queryString.split("&").forEach((el) => {
const temp = el.split("=");
if (temp.length === 2) {
query[temp[0]] = temp[1];
}
});
return query;
} catch (error) {
return {};
}
}
/**
* Takes name of the header and returns value of the header
* @param {string} name Header Name
* @returns {string} Value of Header
*/
async header(name) {
try {
const header = await this.request.headers.get(name);
return header;
} catch (error) {
return {};
}
}
};