Giter Club home page Giter Club logo

axios-rest's Introduction

Axios-REST: A RESTful client based on axios

This library intends to provide an elegant RESTful client.

Installation

npm i @aponder.top/axios-rest

Usage example

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

axios-rest's People

Contributors

xiyusullos avatar

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.