MeGaDeTH_91

Requester

Aug 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. let requester = (() => {
  2. const kinveyBaseUrl = "https://baas.kinvey.com/";
  3. const kinveyAppKey = "";
  4. const kinveyAppSecret = "";
  5.  
  6. // Creates the authentication header
  7. function makeAuth(type) {
  8. return type === 'basic'
  9. ? 'Basic ' + btoa(kinveyAppKey + ':' + kinveyAppSecret)
  10. : 'Kinvey ' + sessionStorage.getItem('authtoken');
  11. }
  12.  
  13. // Creates request object to kinvey
  14. function makeRequest(method, module, endpoint, auth) {
  15. return req = {
  16. method,
  17. url: kinveyBaseUrl + module + '/' + kinveyAppKey + '/' + endpoint,
  18. headers: {
  19. 'Authorization': makeAuth(auth)
  20. }
  21. };
  22. }
  23.  
  24. // Function to return GET promise
  25. function get (module, endpoint, auth) {
  26. return $.ajax(makeRequest('GET', module, endpoint, auth));
  27. }
  28.  
  29. // Function to return POST promise
  30. function post (module, endpoint, auth, data) {
  31. let req = makeRequest('POST', module, endpoint, auth);
  32. req.data = data;
  33. return $.ajax(req);
  34. }
  35.  
  36. // Function to return PUT promise
  37. function update (module, endpoint, auth, data) {
  38. let req = makeRequest('PUT', module, endpoint, auth);
  39. req.data = data;
  40. return $.ajax(req);
  41. }
  42.  
  43. // Function to return DELETE promise
  44. function remove (module, endpoint, auth) {
  45. return $.ajax(makeRequest('DELETE', module, endpoint, auth));
  46. }
  47.  
  48. return {
  49. get,
  50. post,
  51. update,
  52. remove
  53. }
  54. })();
Advertisement
Add Comment
Please, Sign In to add comment