msidolphin / vue-api-creator

An api manager bases on axios
MIT License
10 stars 1 forks source link
ajax api-manager axios http vuejs

vue-api-creator

Build Status Coverage Status codebeat badge

An api manager bases on axios

中文文档

Usage

Install

npm install vue-api-creator -S
import api from './api'
import ApiCreator from 'vue-api-creator'

Vue.use(ApiCreator, {
  baseURL: '/baseURL'
  modules: api,
  // Response fails if response status is in this list
  permanentErrors: [404, 415, 500, 501, 429],
  beforeRequest (options) {
    console.log(options)
  },
  afterRequest (res) {
    console.log(res)
  },
  onError (err) {
    if (err.isInternalError) {
      console.log('An exception has occurred on your network')
    } else {
      console.log('An error response from server')
    }
  },
  mock: 'https://www.xxxx.com'
})

Use

export default {
    created () {
        this.$api('app/list').then(res => {
            // ....
        }).catch(err => {
            // ....
        })
    }
}

Api directory

├──  api
│    │── app
│    │    ├── index.js
│    └── index.js   

app/index.js

export default {
    BASE_URL: '/sys/app',
    api: [
      {
        name: 'list',
        desc: 'get apps',
        method: 'GET',
        path: 'list',
        mock: true // enable mock
      }
    ]
}

api/index.js

import app from './app'

export default {
    app
}

Server Response Structure

Your server response body shoule be like this:

{
  code: Number, // status code
  success: Boolean, // whether the response was successful 
  msg: String, // response message
  data: <T> // response data
}

Configuration

ApiCreator config

Api config

License

MIT

Copyright © 2019, msidolphin