demi-axios

Based on vue-demi, useAxios written by compositionApi supports vue2/vue3.
381 5
Install
npm install demi-axios
Latest Version:0.1.1
Vue:2 · 3
License:MIT
Last Updated:Nov 30, 2022
Links: GitHub  ·  npm
Maintainer: chenxch


logo of demi-axios repository

English | 简体中文

demi-axios

NPM version

  • 🦾 Based on vue-demi, useAxios written by compositionApi supports vue2/vue3.

Installation

# npm
npm i demi-axios

# yarn
yarn add demi-axios

# pnpm
pnpm i demi-axios

Usage Example

init

// init 
import { create } from 'demi-axios'
import type { AxiosInstance } from 'axios'

const axios: AxiosInstance = create({/* axiosOptions */})

// axios.interceptors.response.use((response) => {
//   ...
//   return response
// }, (error) => {
//   return Promise.reject(error)
// })

Usage

// Usage
// use***(initialData, url, Formatter(responseData, currentData))

import { useGet } from 'demi-axios'

const { data, loading, task, error, response } = useGet(
  {}, 
  'https://jsonplaceholder.typicode.com/posts/1',
  (responseData, currentData)=>{
    // process
    return responseData
  }
)
// data : Ref<T> Return value
// loading : Ref<boolean>
// task(playload, config)
// error : Ref<T>
// response : Ref<T>

// example 1
task({params: 'demi'}).then((res) => {
  console.log(res)
})

// example 2
await task()
console.log(data.value)
consoe.log(response.value)

API

Fetch json

useGet
useHead
useDelete
useOptions

Fetch Blob

useGetBlob
useHeadBlob
useDeleteBlob
useOptionsBlob

Modify application/json

usePost
usePut
usePatch

Modify application/x-www-form-urlencoded

usePostEncoded
usePutEncoded
usePatchEncoded

Modify multipart/form-data

usePostMultipart usePutMultipart usePatchMultipart

FAQ

1. data.value is undefined

The default data format returned by the server is {data:any}. If you are {}, you can do a layer of data processing in the interceptor.

axios.interceptors.response.use((response) => {
  // ...
  return { data:response }
}, (error) => {
  return Promise.reject(error)
})


Related Packages

vue-compositions

Collection of Vue Composition Apis

479 1
iot-axios2

kaifangpingtai-vue2

1,255
vue-request

This is a library that can easily help you manage request states, supporting com...

233,950 1,393
vue-inertia-composable

Inertia for Vue2 Composition API.

4,147 1
vue-axios-optimize

vue项目对axios请求的优化,实现精准控制全局加载动画,轻松实现重复请求时取消前面的...

1,605

Version History

Version Released Vue License
0.1.1 ^2.6.0 || ^3.2.0 MIT
0.1.0 ^3.2.31 || ^2.6.14 MIT
0.0.9 ^3.2.31 || ^2.6.14 MIT
0.0.7 ^3.2.31 || ^2.6.14 MIT
0.0.6 ^3.2.31 || ^2.6.14 MIT