ClavinJune

req.js

Apr 18th, 2019
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. /* !author: Clavin June
  2. Fetch request that support method injection for laravel
  3. */
  4. 'use strict'
  5. const req = {
  6. get: async function (url) {
  7. try {
  8. return await(url).then(res => res.json())
  9. } catch (e) {
  10. throw Error(e)
  11. }
  12. },
  13. post: async function (url, data={}) {
  14. try {
  15. return await fetch(url, {
  16. method: 'POST',
  17. headers: {
  18. 'Content-Type': 'application/json'
  19. },
  20. body: JSON.stringify(data)
  21. }).then(res => res.json())
  22. } catch (e) {
  23. throw Error(e)
  24. }
  25. },
  26. put: async function (url, data={}) {
  27. return await this.post(url, {...data, _method: 'PUT'})
  28. },
  29. delete: async function (url, data={}) {
  30. return await this.post(url, {...data, _method: 'DELETE'})
  31. }
  32. }
Add Comment
Please, Sign In to add comment