npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uniapp-axios-adapter

v0.3.2

Published

axios adapter for uni.request

Downloads

47

Readme

uniapp-axios-adapter

用于uni-appaxios库以及使用到的UniAdapter适配器

利用axiosadapter适配器来兼容了小程序的请求 api。添加本适配器或者使用本包导出的axios后,axios底层将使用uni.request发起请求

项目源码很简单,感兴趣的可以前往github或者gitee查看

安装

安装 uniapp-axios-adapter

推荐使用pnpm进行包管理。

  pnpm/npm i uniapp-axios-adapter
  # 或者 yarn add uniapp-axios-adapter

安装 axios

我们在包里添加了最新版本的axios作为依赖,如果你不想使用最新版本的axios,可以自行安装指定版本的axios配合我们的UniAdapter来使用,tree-shaking不会将本包依赖的axios打包进生产环境中

axios v1.0+尚不稳定,推荐安装0.27.2版本

  pnpm/npm i [email protected]
  # 或者 yarn add [email protected]

使用

我们按需导出了UniAdapter适配器,并且默认导出了使用了该适配器的axios,你可以自行使用适配器,也可以直接使用我们导出的 axios

自行使用适配器

指定axios的适配器adapter为本适配器即可,其余用法与axios保持一致

import axios from "axios";
import { UniAdapter } from "uniapp-axios-adapter";

// 每次请求都创建一个新的实例
const request = axios.create({
  baseURL: "https://example.com",
  timeout: 10000,
  adapter: UniAdapter, // 指定适配器
});

示例 1 设置请求拦截器与响应拦截器

// src/utils/http.js 中
import axios from "axios";
import { UniAdapter } from "uniapp-axios-adapter";

const request = axios.create({
  baseURL: "https://example.com",
  timeout: 10000,
  adapter: UniAdapter,
});

request.interceptors.request.use((config) => {
  //带上token
  config.headers["Authorization"] = "token";
  return config;
});

request.interceptors.response.use((response) => {
  // 统一处理响应,返回Promise以便链式调用
  if (response.status === 200) {
    const { data } = response;
    if (data && data.code === 200) {
      return Promise.resolve(data);
    } else {
      return Promise.reject(data);
    }
  } else {
    return Promise.reject(response);
  }
});

export default request;
// 具体业务代码文件中
import http from 'src/utils/http.js' // 上一步封装axios的路径中

http({
  url: 'example/api/test'
  method: 'get',
  params: {
    id: 123,
  }
})

http({
  url: 'example/api/test'
  method: 'post',
  data: {
    id: 123,
  }
})

使用开箱即用的 axios

添加拦截器的方式

// http.js中
import axios from "uniapp-axios-adapter";
const request = axios.create({
  baseURL: "https://example.com",
  timeout: 10000,
});

request.interceptors.request.use((config) => {
  //带上token
  config.headers["Authorization"] = "token";
  return config;
});

request.interceptors.response.use((response) => {
  // 统一处理响应,返回Promise以便链式调用
  if (response.status === 200) {
    const { data } = response;
    if (data && data.code === 200) {
      return Promise.resolve(data);
    } else {
      return Promise.reject(data);
    }
  } else {
    return Promise.reject(response);
  }
});

export default request;

直接使用

// 业务代码中
import axios from "uniapp-axios-adapter";
axios.get({
  url: "https://example.com/api/getUserById",
  params: {
    id: 1,
  },
});

更新日志

点击查看

TODO

  • 压缩打包,减小体积
  • 适配uni.downloadFileuni.uploadFile