3.0.0 • Published 9 months ago

vue-fetch v3.0.0

Weekly downloads
38
License
ISC
Repository
github
Last release
9 months ago

travis-ci npm dependencies

vue-fetch

Use fetch in vue just like $http in Angular

polyfill

polyfill you may need

  • url-search-params-polyfill
  • whatwg-fetch
  • es6-promise

Why

Fetch API is awesome... but it has some feature I don't like :()

  • if you need cookies, you must pass credentials: 'include' as second params
  • pass json while manually JSON.parse(body) and headers: new Headers({'content-type': 'application/json' })

So I wrote this small plugin to provide a "better way" ;)

Usage

// optionally use a fetch polyfill
import 'whatwg-fetch'

import VueFetch from 'vue-fetch'
import Vue from 'vue'

Vue.use(VueFetch, {
  polyfill: true   //should vue-fetch load promise polyfill, set to false to use customer polyfill
});

const vm = new Vue({
});

(async function(){
    let login = await vm.$fetch.post('/user/login', {
        username: 'somebody',
        password: 'wow',
    });
    if (login.status != 200) {
        alert('login error');
    }
    let user = await vm.$fetch.get('/user/profile');
    if (user.status != 200){
        alert('can not get profile')
    }
    vm.user = await user.json();
})()

Inside component

Access via this.$fetch

Outside component

Makes sense to have a separate data layer to keep with Single Responsibility guidelines.

import 'isomorphic-fetch'
import { Fetch } from 'vue-fetch'

// you can pass in mocked fetch or Headers if required
const $ = Fetch({
  // fetch,
  // Headers,
  logging: true
})

export default {
  create: async (data) => {
    return await $.post('/user', data)
  },
  updateById: async (id, data) => {
    return await $.put(`/user/${id}`, )
  },
  deleteById: async (id) => {
    return await $.delete(`/user/${id}`)
  },
  findById: async (id) => {
    return await $.get(`/user/${id}`)
  }
}

Support

GET/DELETE

vm.$fetch.get(url, {
  page: 1
});
vm.$fetch.del(url, {
  limit: 1
});

POST/PUT/PATCH

vm.$fetch.post(url, {
  foo: 'bar'
});

vm.$fetch.put(url, {
  foo: 'bar'
});

vm.$fetch.patch(url, {
  foo: 'bar'
});

FETCH

vm.$fetch.fetch('POST', url, {
  page: 1
}, {
  body: 'some info'
});
3.0.0

9 months ago

4.0.0-0

1 year ago

3.0.0-1

1 year ago

3.0.0-0

1 year ago

3.0.0-2

1 year ago

2.0.0

2 years ago

1.1.3

5 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago