Skip to content

Instantly share code, notes, and snippets.

@Bizunow
Last active September 14, 2017 15:47
Show Gist options
  • Save Bizunow/7443c77f1533b76fe0ac83aea4a8fe81 to your computer and use it in GitHub Desktop.
Save Bizunow/7443c77f1533b76fe0ac83aea4a8fe81 to your computer and use it in GitHub Desktop.
[Server call] Class for create server-requests, based on fetch #js
// Server.call('GET', 'getVkData', { id: -145636 })
// Server.call('POST', 'postVkData', { id: -145636 })
const MAIN_API_PATH = "http://dune.com/bar/";
class Server {
static call(httpMethod, apiMethod, params = {}) {
let fetchParams = {
headers: { "Content-Type": "application/json" },
mode: "cors",
method: httpMethod
};
let urlParams = "";
if (httpMethod === "POST") {
fetchParams.body = JSON.stringify(params);
} else {
Object.keys(params).forEach(key => {
urlParams += (urlParams ? "&" : "") + (key + "=" + params[key]);
});
urlParams = urlParams ? "?" + urlParams : "";
}
return fetch(
MAIN_API_PATH + apiMethod + urlParams,
fetchParams
).then(function(response) {
return response.json();
});
}
}
export default Server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment