Files
react-native-code-push/request-fetch-adapter.js
Richard Hua d2e150a0c2 Send the CodePush plugin version with server requests (#560)
This allows the server to maintain backward compatibility by distinguishing between different plugins (e.g. React Native/Cordova/Windows), as well as individual versions of these plugins, without needing to use the old SDK version (which refers to the acquisition SDK, which is shared between all plugins).

This will also enable us to resolve #514, by rolling out a CDN only for newer plugin versions. (On the Cordova end at the very least, some users have plugins installed that have a whitelist of acceptable endpoints).
2016-10-10 14:56:06 -07:00

52 lines
1.3 KiB
JavaScript

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];
}