🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

fant-axios-adapter

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fant-axios-adapter

#### 介绍 `fant-axios-adapter`用于帮助开发者在`uni-app`项目使用`axios`请求库,提供了使用`uni.request`发起网络请求的`axios`适配器。

latest
npmnpm
Version
0.0.6
Version published
Weekly downloads
19
90%
Maintainers
1
Weekly downloads
 
Created
Source

fant-axios-adapter

介绍

fant-axios-adapter用于帮助开发者在uni-app项目使用axios请求库,提供了使用uni.request发起网络请求的axios适配器。

安装

yarn add fant-axios-adapter -Dnpm i fant-axios-adapter --save

安装axios

yarn add axios -D

使用

配置axios拦截器
// http.ts
import axios from 'axios'
import { uniAdapter } from 'fant-axios-adapter'

axios.defaults.timeout = 60000
export default class ApiClient {
  public static server() {
    // 可以在这里拦截
    const baseURL = import.meta.env.VITE_BASEURL
    return ApiClient.create(baseURL)
  }

  public static create(baseURL: string) {
    const instance = axios.create({
      withCredentials: true,
      baseURL: baseURL,
      adapter: uniAdapter // 配置适配器
    })

    instance.interceptors.request.use(
      (request) => {
        return request
      },
      (error) => {
        return Promise.reject(error)
      }
    )

    instance.interceptors.response.use(
      (response) => {
        return response
      },
      (error) => {
        return Promise.reject(error)
      }
    )
    return instance
  }
}
使用拦截器
// api.ts
    http
      .server()
      .post(
        '/login',
        {},
        {
          params: { a: 22 }
        }
      )
      .then((res) => {
        return res.data
      })

Keywords

uni-app

FAQs

Package last updated on 20 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts