init
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
|
||||
|
||||
## [0.3.2](https://github.com/zxfd/uniapp-axios-adapter/compare/0.3.1...0.3.2) (2022-11-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **adapters:** 🐞 移除不需要的Promise处理 ([2a637aa](https://github.com/zxfd/uniapp-axios-adapter/commit/2a637aa86d4cdce8704227a70d4830e00c1ea631))
|
||||
|
||||
## [0.3.1](https://github.com/zxfd/uniapp-axios-adapter/compare/0.3.0...0.3.1) (2022-11-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **adapters:** 🐞 修复参数为对象时,中断请求的问题 ([8cc480a](https://github.com/zxfd/uniapp-axios-adapter/commit/8cc480a33fac61d4df0997de880af632d8de0a72))
|
||||
|
||||
# [0.3.0](https://github.com/zxfd/uniapp-axios-adapter/compare/0.2.2...0.3.0) (2022-11-08)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **index.d.ts:** ✨ 新增类型声明文件,支持ts ([7f7076b](https://github.com/zxfd/uniapp-axios-adapter/commit/7f7076b8c5ade0227e8f568cc62b3ab5f9e70f63))
|
||||
* **package.json:** ✨ 同时支持commonJS esm和ts ([2cde861](https://github.com/zxfd/uniapp-axios-adapter/commit/2cde8611b4fcd6efe5b31d4cb100ccf91c5bde01))
|
||||
|
||||
## [0.2.2](https://github.com/zxfd/uniapp-axios-adapter/compare/0.2.1...0.2.2) (2022-10-28)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **adapters适配器:** 🐞 修复参数未正确携带的问题 ([cc45064](https://github.com/zxfd/uniapp-axios-adapter/commit/cc45064ff4c21172694bb679cd0afe7adcbcb335))
|
||||
|
||||
## [0.2.1](https://github.com/zxfd/uniapp-axios-adapter/compare/0.2.0...0.2.1) (2022-10-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **adapters:** 🐞 在非uni-app环境下报错 ([783198f](https://github.com/zxfd/uniapp-axios-adapter/commit/783198f86db6d48f2d528fd50b59ab0d3cb16062))
|
||||
|
||||
# [0.2.0](https://github.com/zxfd/uniapp-axios-adapter/compare/0.1.0...0.2.0) (2022-10-22)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **index入口文件:** ✨ 默认导出设置了适配器的axios ([1b37542](https://github.com/zxfd/uniapp-axios-adapter/commit/1b3754231892a8793ff11f6fc636c098c87b277a))
|
||||
|
||||
## [0.1.1](https://github.com/zxfd/uniapp-axios-adapter/compare/0.1.0...0.1.1) (2022-10-21)
|
||||
|
||||
# 0.1.0 (2022-10-21)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **init:** ✨ 添加uni-app下的axios适配器 ([1b98b03](https://github.com/zxfd/uniapp-axios-adapter/commit/1b98b03b5c0d334213af1f38347aaa04745fa07b))
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
## uniapp-axios-adapter
|
||||
|
||||
用于`uni-app`的`axios`库以及使用到的`UniAdapter`适配器
|
||||
|
||||
利用`axios`的`adapter`适配器来兼容了小程序的请求 api。添加本适配器或者使用本包导出的`axios`后,`axios`底层将使用`uni.request`发起请求
|
||||
|
||||
项目源码很简单,感兴趣的可以前往`github`或者`gitee`查看
|
||||
|
||||
## 安装
|
||||
|
||||
### 安装 uniapp-axios-adapter
|
||||
|
||||
推荐使用`pnpm`进行包管理。
|
||||
|
||||
```shell
|
||||
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`版本
|
||||
|
||||
```shell
|
||||
pnpm/npm i axios@0.27.2
|
||||
# 或者 yarn add axios@0.27.2
|
||||
```
|
||||
|
||||
### 使用
|
||||
|
||||
我们按需导出了`UniAdapter`适配器,并且默认导出了使用了该适配器的`axios`,你可以自行使用适配器,也可以直接使用我们导出的 axios
|
||||
|
||||
### 自行使用适配器
|
||||
|
||||
指定`axios`的适配器`adapter`为本适配器即可,其余用法与`axios`保持一致
|
||||
|
||||
```js
|
||||
import axios from "axios";
|
||||
import { UniAdapter } from "uniapp-axios-adapter";
|
||||
|
||||
// 每次请求都创建一个新的实例
|
||||
const request = axios.create({
|
||||
baseURL: "https://example.com",
|
||||
timeout: 10000,
|
||||
adapter: UniAdapter, // 指定适配器
|
||||
});
|
||||
```
|
||||
|
||||
#### 示例 1 设置请求拦截器与响应拦截器
|
||||
|
||||
```js
|
||||
// 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;
|
||||
```
|
||||
|
||||
```js
|
||||
// 具体业务代码文件中
|
||||
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
|
||||
|
||||
#### 添加拦截器的方式
|
||||
|
||||
```js
|
||||
// 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;
|
||||
```
|
||||
|
||||
#### 直接使用
|
||||
|
||||
```js
|
||||
// 业务代码中
|
||||
import axios from "uniapp-axios-adapter";
|
||||
axios.get({
|
||||
url: "https://example.com/api/getUserById",
|
||||
params: {
|
||||
id: 1,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
## 更新日志
|
||||
|
||||
[点击查看](https://gitee.com/black-key/uniapp-axios-adapter/blob/main/CHANGELOG.md)
|
||||
|
||||
## TODO
|
||||
|
||||
- 压缩打包,减小体积
|
||||
- 适配`uni.downloadFile`和`uni.uploadFile`
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { AxiosStatic, AxiosPromise, AxiosRequestConfig } from "axios";
|
||||
|
||||
export declare function UniAdapter(config: AxiosRequestConfig): AxiosPromise
|
||||
|
||||
|
||||
declare const axios: AxiosStatic;
|
||||
|
||||
export default axios
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var axios = require('axios');
|
||||
|
||||
const getResponse = (res, config) => {
|
||||
const { statusCode, errMsg } = res;
|
||||
const response = {
|
||||
...res,
|
||||
status: statusCode,
|
||||
statusText: errMsg,
|
||||
config,
|
||||
request: null,
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const uniAdapter = (config) => {
|
||||
if (!uni) {
|
||||
throw new Error("please use this in uni-app project!");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const { baseURL, url, headers, data, params } = config;
|
||||
const uniConfig = {
|
||||
...config,
|
||||
url: baseURL + url,
|
||||
header: headers,
|
||||
};
|
||||
|
||||
if (data || params) {
|
||||
try {
|
||||
uniConfig.data = JSON.parse(data || params);
|
||||
} catch (e) {
|
||||
uniConfig.data = data || params;
|
||||
}
|
||||
}
|
||||
uni.request({
|
||||
...uniConfig,
|
||||
success(res) {
|
||||
const response = getResponse(res, config);
|
||||
resolve(response);
|
||||
},
|
||||
fail(res) {
|
||||
const response = getResponse(res, config);
|
||||
reject(response);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const UniAdapter = uniAdapter;
|
||||
|
||||
axios.defaults.adapter = uniAdapter;
|
||||
|
||||
exports.default = axios;
|
||||
exports.UniAdapter = UniAdapter;
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
import axios from 'axios';
|
||||
export { default } from 'axios';
|
||||
|
||||
const getResponse = (res, config) => {
|
||||
const { statusCode, errMsg } = res;
|
||||
const response = {
|
||||
...res,
|
||||
status: statusCode,
|
||||
statusText: errMsg,
|
||||
config,
|
||||
request: null,
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const uniAdapter = (config) => {
|
||||
if (!uni) {
|
||||
throw new Error("please use this in uni-app project!");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const { baseURL, url, headers, data, params } = config;
|
||||
const uniConfig = {
|
||||
...config,
|
||||
url: baseURL + url,
|
||||
header: headers,
|
||||
};
|
||||
|
||||
if (data || params) {
|
||||
try {
|
||||
uniConfig.data = JSON.parse(data || params);
|
||||
} catch (e) {
|
||||
uniConfig.data = data || params;
|
||||
}
|
||||
}
|
||||
uni.request({
|
||||
...uniConfig,
|
||||
success(res) {
|
||||
const response = getResponse(res, config);
|
||||
resolve(response);
|
||||
},
|
||||
fail(res) {
|
||||
const response = getResponse(res, config);
|
||||
reject(response);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const UniAdapter = uniAdapter;
|
||||
|
||||
axios.defaults.adapter = uniAdapter;
|
||||
|
||||
export { UniAdapter };
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "uniapp-axios-adapter",
|
||||
"version": "0.3.2",
|
||||
"description": "axios adapter for uni.request",
|
||||
"main": "lib/index.esm.js",
|
||||
"scripts": {
|
||||
"release": "release-it",
|
||||
"prebuild": "yarn release",
|
||||
"build": "rollup -c && yarn zip",
|
||||
"zip": "zip -r uniapp-axios-adapter.zip lib index.d.ts README.md CHANGELOG.md package.json"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./lib/index.cjs.js",
|
||||
"import": "./lib/index.esm.js"
|
||||
}
|
||||
},
|
||||
"typings": "index.d.ts",
|
||||
"files": [
|
||||
"lib",
|
||||
"README.md",
|
||||
"CHANGELOG.md",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"axios",
|
||||
"uni-app",
|
||||
"adapter",
|
||||
"小程序",
|
||||
"微信小程序",
|
||||
"请求"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/zxfd/uniapp-axios-adapter"
|
||||
},
|
||||
"author": "zxiaofoo@qq.com",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@release-it/conventional-changelog": "^5.1.1",
|
||||
"release-it": "^15.5.0",
|
||||
"rollup": "^3.2.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"axios": "*"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user