Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const baseUrl = 'https://baas.kinvey.com';
- const appKey = 'kid_SkL8nMb6B';
- const appSecret = 'd9666c510fc045faae5184ca8f412a72';
- function createAuthorization(type) {
- return type === 'Basic' ?
- `Basic ${btoa(`${appKey}:${appSecret}`)}` :
- `Kinvey ${sessionStorage.getItem('authtoken')}`;
- }
- function createHeader(type,httpMethod, data,) {
- const headers = {
- method: httpMethod,
- headers: {
- 'Authorization': createAuthorization(type),
- 'Content-Type': 'application/json'
- }
- };
- if (httpMethod === "POST" || httpMethod === "PUT") {
- headers.body = JSON.stringify(data);
- }
- return headers;
- }
- function handleError(e) {
- if (!e.ok) {
- throw new Error(e.statusText);
- }
- return e;
- }
- function deserializeData(x) {
- if(x.status === 204){
- return x;
- }
- return x.json();
- }
- function baseFetch(module, endpoint, headers) {
- const url = `${baseUrl}/${module}/${appKey}/${endpoint}`;
- return fetch(url, headers)
- .then(handleError)
- .then(deserializeData);
- }
- function get(module, endpoint,type) {
- return baseFetch(module, endpoint, createHeader(type,'get'));
- }
- function post(module, endpoint, data,type) {
- return baseFetch(module, endpoint, createHeader(type,'post', data));
- }
- function put(module, endpoint, data,type) {
- return baseFetch(module, endpoint, createHeader(type,'put', data));
- }
- function remove(module, endpoint,type) {
- return baseFetch(module, endpoint, createHeader(type,'delete'));
- }
- export {get, post, put, remove};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement