Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* !author: Clavin June
- Fetch request that support method injection for laravel
- */
- 'use strict'
- const req = {
- get: async function (url) {
- try {
- return await(url).then(res => res.json())
- } catch (e) {
- throw Error(e)
- }
- },
- post: async function (url, data={}) {
- try {
- return await fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(data)
- }).then(res => res.json())
- } catch (e) {
- throw Error(e)
- }
- },
- put: async function (url, data={}) {
- return await this.post(url, {...data, _method: 'PUT'})
- },
- delete: async function (url, data={}) {
- return await this.post(url, {...data, _method: 'DELETE'})
- }
- }
Add Comment
Please, Sign In to add comment