1.2.5 • Published 5 years ago

resource-axios v1.2.5

Weekly downloads
13
License
MIT
Repository
github
Last release
5 years ago

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

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

License

MIT

Copyright (c) 2018-present, liuyanzhi08

FOSSA Status

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago