Simon He @ simonhe.me

v-axios

Jul 29 · 2day

Author: Simon

中文 Chinese Version

This article is documentation that describes the encapsulated utility functions @simon_he/v-axios

peculiarity: High scalability, flexible and convenient calling

More

Instructions for use

npm i @simon_he/v-axios # install

import { 
  vAxios,
  ...
 } from '@simon_he/v-axios' # Ingestion on demand

Directory structure

vAxios

  • Axios-based packaging
const axios = vAxios({
  baseURL: 'http://localhost:3000',
  timeout: 1000,
  headers: {
    'Content-Type': 'application/json',
  },
  interceptor: {
    request(config) {
      console.log(config)
      return config
    },
    response(response) {
      console.log(response)
      return response
    },
    requestError(error) {
      console.log(error)
      return Promise.reject(error)
    },
    responseError(error) {
      console.log(error)
      return Promise.reject(error)
    },
  },
})

axios.get('/api/get', { // The get request can be passed across like any other request
  id: 1,
  name: '22',
}, {
  headers: {
    'Content-Type': 'application/json',
  },
}).then((res) => {
  console.log(res)
}).catch((err) => {
  console.log(err)
}).finally(() => {
  console.log('finally')
})

axios.post('/api/get', {
  id: 1,
  name: '22',
}, {
  headers: {
    'Content-Type': 'application/json',
  },
}).then((res) => {
  console.log(res)
}).catch((err) => {
  console.log(err)
}).finally(() => {
  console.log('finally')
})

GitHub

Welcome to PR