Skip to content

liuyanzhi08/resource-axios

Repository files navigation

resource-axios

npm version build status code coverage npm downloads code helpers FOSSA Status

Create vue-resource's resource like object. Restful methods, interceptors support.

Installation

npm i -S resource-axios npm i -S axios

Usage

import resource from 'resource-axios'; import axios from 'axios'; const Book = resource('/api/books', axios); // get book of id:1 => curl '/api/books/1' Book.get(1).then(res => console.log(res)); Book.get({ id: 1 }).then(res => console.log(res)); Book.get({ _id: 1 }).then(res => console.log(res)); // query books name:foo => curl '/api/books?name=foo' Book.query({ name: 'foo' }).then(res => console.log(res)); // add book of id:1 => curl -H "Content-Type:application/json" -X POST --data '{"name":"foo"}' /api/books Book.save({ name: 'foo' }).then(res => console.log(res)); Book.post({ name: 'foo' }).then(res => console.log(res)); // update book of id:1 => curl -H "Content-Type:application/json" -X PUT --data '{"name":"foo"}' /api/books/1 Book.update(1, { name: 'foo' }).then(res => console.log(res)); Book.update({ id: 1, name: 'foo' }).then(res => console.log(res)); Book.put(1, { name: 'foo' }).then(res => console.log(res)); Book.put({ id: 1, name: 'foo' }).then(res => console.log(res)); // delete book of id:1 => curl -X DELETE /api/books/1 Book.delete(1).then(res => console.log(res)); Book.delete({ id: 1 }).then(res => console.log(res)); Book.del(1).then(res => console.log(res)); Book.del({ id: 1 }).then(res => console.log(res));

Customize actions

Axios doc: axios-api

import resource from 'resource-axios'; import axios from 'axios'; const Book = resource('/api/books', { sell: (id) => axios.get(`/api/books/${id}/sell`), }, axios); // sell book of id:1 => curl /api/books/1/sell Book.sell(1).then(res => console.log(res));

Interceptors

Axios doc: interceptors

import resource from 'resource-axios'; import axios from 'axios'; const Book = resource('/api/books', axios); // const Book = resource('/api/books', { /* customized actions */ }, axios); // Add a request interceptor axios.interceptors.request.use((config) => { // Do something before request is sent console.log(config); return config; }); // Add a response interceptor axios.interceptors.response.use((response) => { // Do something with response data console.log(response); return response; }); // get book of id:1 => curl '/api/books/1' Book.get(1);

Changelog

version log
v1.2.1 support more params format; params error will be throw out; add post and put methods changelog
v1.1.2 add notice when axios is not imported
v1.1.1 refactoring codes
v1.1.0 remove axios self-injecting. changelog
v1.0.16 fix issue#1

License

MIT

Copyright (c) 2018-present, liuyanzhi08

FOSSA Status

About

Create vue-resource's resource like object with axios. Restful methods, interceptors support.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •