
Security News
Frontier AI Is Now Critical Infrastructure
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.
uniapp-axios-adapter
Advanced tools
用于uni-app的axios库以及使用到的UniAdapter适配器
利用axios的adapter适配器来兼容了小程序的请求 api。添加本适配器或者使用本包导出的axios后,axios底层将使用uni.request发起请求
项目源码很简单,感兴趣的可以前往github或者gitee查看
推荐使用pnpm进行包管理。
pnpm/npm i uniapp-axios-adapter
# 或者 yarn add uniapp-axios-adapter
我们在包里添加了最新版本的axios作为依赖,如果你不想使用最新版本的axios,可以自行安装指定版本的axios配合我们的UniAdapter来使用,tree-shaking不会将本包依赖的axios打包进生产环境中
axios v1.0+尚不稳定,推荐安装0.27.2版本
pnpm/npm i axios@0.27.2
# 或者 yarn add axios@0.27.2
我们按需导出了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, // 指定适配器
});
// 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,
}
})
// 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,
},
});
uni.downloadFile和uni.uploadFileFAQs
axios adapter for uni.request
The npm package uniapp-axios-adapter receives a total of 68 weekly downloads. As such, uniapp-axios-adapter popularity was classified as not popular.
We found that uniapp-axios-adapter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.

Security News
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.