Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. class Drupal {
  2.  
  3. static async get(uri) {
  4. return await fetch(uri, {
  5. credentials: 'same-origin',
  6. method: 'GET',
  7. headers: {
  8. 'Content-Type': 'application/json',
  9. },
  10. });
  11. }
  12.  
  13. static async post(uri, data) {
  14. return await fetch(uri + '?_format=json', {
  15. credentials: 'same-origin',
  16. method: 'POST',
  17. headers: {
  18. 'Content-Type': 'application/json',
  19. 'X-CSRF-Token': await this.getToken(),
  20. },
  21. body: JSON.stringify(data),
  22. });
  23. }
  24.  
  25. static async patch(uri, data) {
  26. return await fetch(uri + '?_format=json', {
  27. credentials: 'same-origin',
  28. method: 'PATCH',
  29. headers: {
  30. 'Content-Type': 'application/json',
  31. 'X-CSRF-Token': await this.getToken(),
  32. },
  33. body: JSON.stringify(data),
  34. });
  35. }
  36.  
  37. static async delete(uri) {
  38. return await fetch(uri + '?_format=json', {
  39. credentials: 'same-origin',
  40. method: 'DELETE',
  41. headers: {
  42. 'Content-Type': 'application/json',
  43. 'X-CSRF-Token': await this.getToken(),
  44. },
  45. });
  46. }
  47.  
  48. static async getToken() {
  49. let tokenResponse = await fetch('/rest/session/token', {credentials: 'same-origin'});
  50. return await tokenResponse.text();
  51. }
  52.  
  53. }
  54.  
  55. export default Drupal;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement