https://github.com/xiyusullos/axios-rest
Axios-REST: A RESTful client based on axios
https://github.com/xiyusullos/axios-rest
axios client rest restful-client
Last synced: 7 months ago
JSON representation
Axios-REST: A RESTful client based on axios
- Host: GitHub
- URL: https://github.com/xiyusullos/axios-rest
- Owner: xiyusullos
- License: mit
- Created: 2022-03-08T15:12:52.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-19T15:19:05.000Z (over 2 years ago)
- Last Synced: 2024-04-20T11:47:05.868Z (over 1 year ago)
- Topics: axios, client, rest, restful-client
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# Axios-REST: A RESTful client based on axios
This library intends to provide an elegant RESTful client.
## Installation
```shell
npm i @aponder.top/axios-rest
```
## Usage example
```javascript
import axiosRest from '@aponder.top/axios-rest'
const handleOk = (r) => console.info(r)
const handleError = (e) => console.debug(e.toString())
let baseUrl = 'https://httpbin.org'
const axiosRequestConfig = {}
let Rest
// create Rest with axios interceptors and debug
Rest = axiosRest(baseUrl, axiosRequestConfig, false, true)
Rest.axiosInstance.interceptors.response.use(handleOk, handleError)
// Here, create Rest without interceptors and debug normally
// Rest = axiosRest(baseUrl)
// use a common name to be the alias of the resource variable name
let _
// define the resource with a '/'
let Users = Rest('/users')
const newUser = { username: 'root', password: 'root' }
_ = Users
console.info(`\nTest: ${_.resourcePath}`)
_.create(newUser).response//.then(handleOk).catch(handleError)
_.list({ page: 1, page_size: 10 }).response
_.detail(1).response
_.update(10, newUser).response
_.partial_update(11, newUser).response
_.delete(12).response
_.login = _.make_plural_action('login')
_.login(newUser).response
_.disable = _.make_single_action('disable')
_.disable(13).response
_.log = _.make_single_action('log', 'GET')
_.log(14).response
// define the response without a '/'
let Blogs = Rest('blogs')
_ = Blogs
console.info(`\nTest: ${_.resourcePath}`)
_.create(newUser).response//.then(handleOk).catch(handleError)
_.list({ page: 1, page_size: 10 }).response
_.detail(1).response
_.update(10, newUser).response
_.partial_update(11, newUser).response
_.delete(12).response
// combine two resources, the parameter of `of` is an instance of Resource
let User10Blogs = Users.detail(10).of(Blogs)
_ = User10Blogs
console.info(`\nTest: ${_.resourcePath}`)
_.create(newUser).response//.then(handleOk).catch(handleError)
_.list({ page: 1, page_size: 10 }).response
_.detail(1).response
_.update(10, newUser).response
_.partial_update(11, newUser).response
_.delete(12).response
// combine two resources, the parameter of `of` is an instance of Resource
let User10Articles = Users.detail(10).of('articles')
_ = User10Articles
console.info(`\nTest: ${_.resourcePath}`)
_.create(newUser).response//.then(handleOk).catch(handleError)
_.list({ page: 1, page_size: 10 }).response
_.detail(1).response
_.update(10, newUser).response
_.partial_update(11, newUser).response
_.delete(12).response
```