forked from parse-community/parse-server-example
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
httpsRequest.js
43 lines (39 loc) · 1.17 KB
/
httpsRequest.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
// This helper file is needed by bloomFirebaseAuthAdapter.
// It was copied (and slightly modified) from a file required by apple authAdapter in parser-server core code.
const https = require("https");
function makeCallback(resolve, reject, noJSON) {
return function (res) {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
if (noJSON) {
return resolve(data);
}
try {
data = JSON.parse(data);
} catch (e) {
return reject(e);
}
resolve(data);
});
res.on("error", reject);
};
}
function get(options, noJSON = false) {
return new Promise((resolve, reject) => {
https
.get(options, makeCallback(resolve, reject, noJSON))
.on("error", reject);
});
}
function request(options, postData) {
return new Promise((resolve, reject) => {
const req = https.request(options, makeCallback(resolve, reject));
req.on("error", reject);
req.write(postData);
req.end();
});
}
module.exports = { get, request };