forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-fetch-adapter.js
52 lines (46 loc) · 1.26 KB
/
request-fetch-adapter.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
const packageJson = require("./package.json");
module.exports = {
async request(verb, url, requestBody, callback) {
if (typeof requestBody === "function") {
callback = requestBody;
requestBody = null;
}
const headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"X-CodePush-Plugin-Name": packageJson.name,
"X-CodePush-Plugin-Version": packageJson.version,
"X-CodePush-SDK-Version": packageJson.dependencies["code-push"]
};
if (requestBody && typeof requestBody === "object") {
requestBody = JSON.stringify(requestBody);
}
try {
const response = await fetch(url, {
method: getHttpMethodName(verb),
headers: headers,
body: requestBody
});
const statusCode = response.status;
const body = await response.text();
callback(null, { statusCode, body });
} catch (err) {
callback(err);
}
}
};
function getHttpMethodName(verb) {
// Note: This should stay in sync with the enum definition in
// https://github.com/Microsoft/code-push/blob/master/sdk/script/acquisition-sdk.ts#L6
return [
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE",
"OPTIONS",
"CONNECT",
"PATCH"
][verb];
}