Giter Club home page Giter Club logo

resource-axios's Introduction

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

resource-axios's People

Contributors

dependabot[bot] avatar fossabot avatar liuyanzhi08 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

resource-axios's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.