Last active
September 14, 2017 15:47
-
-
Save Bizunow/7443c77f1533b76fe0ac83aea4a8fe81 to your computer and use it in GitHub Desktop.
[Server call] Class for create server-requests, based on fetch #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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