vue-api-service
A convinient endpoints and mocks configs based on axios lib.
Table of contents
Installation
npm install --save vue-api-service
Default import
Install all the components:
const endpoints = userData: method: 'GET' url: '/api/user' updateUser: method: 'PUT' url: '/api/user/:id/' Vue
Browser
Usage
Let's say you've got an endpoints config like this:
const endpoints = userData: method: 'GET' url: '/api/user' addComment: method: 'PUT' url: '/api/posts/:postId/comment'
Call an API endpoint in the app like this:
this$api
or like this:
Vue$api
Mocks
Sometimes you need to simulate the response without calling a real API. Simple enough: just provide the mocks object to the service's options.
const mocks = {} // Vue
Each key of this mocks object covers the corresponding endpoint key. A mock item should be a function that returns a Promise (as axios methods do):
const mocks = // simple sample { return Promise } // advanced usage { if requestdatauser === 'admin' return Promise return Promise }
Plugin Development
Installation
The first time you create or clone your plugin, you need to install the default dependencies:
npm install
Watch and compile
This will run webpack in watching mode and output the compiled files in the dist
folder.
npm run dev