parent
3848fdd81b
commit
df42674895
|
@ -34,7 +34,7 @@ pnpm cz
|
||||||
脚本列表
|
脚本列表
|
||||||
|
|
||||||
- `pnpm build` 打包源代码
|
- `pnpm build` 打包源代码
|
||||||
- `pnpm watch` 监听文件变更并运行 `build`
|
- `pnpm dev` 监听文件变更并运行 `build`
|
||||||
- `pnpm test` 单元测试
|
- `pnpm test` 单元测试
|
||||||
- `pnpm test:watch` 监听文件变更并运行 `test`
|
- `pnpm test:watch` 监听文件变更并运行 `test`
|
||||||
- `pnpm test:cov` 运行 `test` 并输出测试覆盖率
|
- `pnpm test:cov` 运行 `test` 并输出测试覆盖率
|
||||||
|
|
17
README.md
17
README.md
|
@ -2,19 +2,19 @@
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml">
|
<a href="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml">
|
||||||
<img src="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml/badge.svg" alt="ci">
|
<img src="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml/badge.svg" alt="ci"/>
|
||||||
</a>
|
|
||||||
<a style="margin-left:5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
|
||||||
<img src="https://img.shields.io/npm/v/axios-miniprogram" alt="npm">
|
|
||||||
</a>
|
</a>
|
||||||
<a style="margin-left:5px;" href="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram" >
|
<a style="margin-left:5px;" href="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram" >
|
||||||
<img src="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram/branch/main/graph/badge.svg?token=WIQVYX2WIK" alt="codecov"/>
|
<img src="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram/branch/main/graph/badge.svg?token=WIQVYX2WIK" alt="codecov"/>
|
||||||
</a>
|
</a>
|
||||||
<a style="margin-left:5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
|
||||||
<img src="https://img.shields.io/bundlephobia/min/axios-miniprogram" alt="npm bundle size">
|
|
||||||
</a>
|
|
||||||
<a style="margin-left:5px;" href="https://opensource.org/licenses/MIT">
|
<a style="margin-left:5px;" href="https://opensource.org/licenses/MIT">
|
||||||
<img src="https://img.shields.io/github/license/zjxxxxxxxxx/axios-miniprogram" alt="license">
|
<img src="https://img.shields.io/github/license/zjxxxxxxxxx/axios-miniprogram" alt="license"/>
|
||||||
|
</a>
|
||||||
|
<a style="margin-left:5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
||||||
|
<img src="https://img.shields.io/bundlephobia/min/axios-miniprogram" alt="npm bundle size"/>
|
||||||
|
</a>
|
||||||
|
<a style="margin-left:5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
||||||
|
<img alt="npm" src="https://img.shields.io/npm/dt/axios-miniprogram"/>
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -63,7 +63,6 @@ axios-miniprogram 是一款为小程序平台量身定制的轻量级请求库
|
||||||
|
|
||||||
[提问点这里](https://github.com/zjxxxxxxxxx/axios-miniprogram/issues)
|
[提问点这里](https://github.com/zjxxxxxxxxx/axios-miniprogram/issues)
|
||||||
|
|
||||||
|
|
||||||
## 跨端框架示例
|
## 跨端框架示例
|
||||||
|
|
||||||
### Taro
|
### Taro
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
"format": "prettier -w {src,test,scripts}/**/*.{ts,tsx,js,jsx}",
|
"format": "prettier -w {src,test,scripts}/**/*.{ts,tsx,js,jsx}",
|
||||||
"build": "esno scripts/build.ts",
|
"build": "esno scripts/build.ts",
|
||||||
"build:assets": "esno scripts/build.assets.ts",
|
"build:assets": "esno scripts/build.assets.ts",
|
||||||
"watch": "pnpm build -a -w",
|
"dev": "pnpm build -a -w",
|
||||||
"release": "esno scripts/release.ts",
|
"release": "esno scripts/release.ts",
|
||||||
"publish:ci": "esno scripts/publish.ts",
|
"publish:ci": "esno scripts/publish.ts",
|
||||||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
|
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
|
||||||
|
|
|
@ -261,47 +261,48 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
function transformOptions(
|
function transformOptions(
|
||||||
config: AxiosAdapterRequestConfig,
|
config: AxiosAdapterRequestConfig,
|
||||||
): AxiosAdapterBaseOptions {
|
): AxiosAdapterBaseOptions {
|
||||||
|
const { success, fail } = config;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...config,
|
...config,
|
||||||
header: config.headers,
|
header: config.headers,
|
||||||
success(_response: AxiosAdapterResponse) {
|
success(rawRes: AxiosAdapterResponse) {
|
||||||
const response = transformResponse(_response) as AxiosAdapterResponse;
|
const response = transformResponse(rawRes) as AxiosAdapterResponse;
|
||||||
|
success(response);
|
||||||
config.success(response);
|
|
||||||
},
|
},
|
||||||
fail(_responseError: AxiosAdapterResponseError) {
|
fail(rawErr: AxiosAdapterResponseError) {
|
||||||
const responseError = Object.assign(transformResponse(_responseError), {
|
const responseError = {
|
||||||
|
...transformResponse(rawErr),
|
||||||
data: {
|
data: {
|
||||||
errno:
|
errno:
|
||||||
// 微信 | 飞书新规范
|
// 微信 | 飞书新规范
|
||||||
_responseError.errno ??
|
rawErr.errno ??
|
||||||
// 支付宝 | 钉钉
|
// 支付宝 | 钉钉
|
||||||
_responseError.error ??
|
rawErr.error ??
|
||||||
// 百度 | 360 | 飞书
|
// 百度 | 360 | 飞书
|
||||||
_responseError.errCode ??
|
rawErr.errCode ??
|
||||||
// 抖音
|
// 抖音
|
||||||
_responseError.errNo,
|
rawErr.errNo,
|
||||||
errMsg:
|
errMsg:
|
||||||
// 飞书新规范
|
// 飞书新规范
|
||||||
_responseError.errString ??
|
rawErr.errString ??
|
||||||
// 微信 | 支付宝 | 百度 | 抖音 | QQ | 360 | 飞书
|
// 微信 | 支付宝 | 百度 | 抖音 | QQ | 360 | 飞书
|
||||||
_responseError.errMsg ??
|
rawErr.errMsg ??
|
||||||
// 钉钉
|
// 钉钉
|
||||||
_responseError.errorMessage,
|
rawErr.errorMessage,
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
fail(responseError);
|
||||||
config.fail(responseError);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformResponse(
|
function transformResponse(
|
||||||
_response: AxiosAdapterResponse | AxiosAdapterResponseError,
|
rawRes: AxiosAdapterResponse | AxiosAdapterResponseError,
|
||||||
) {
|
) {
|
||||||
return Object.assign(
|
return {
|
||||||
ignore(
|
...ignore(
|
||||||
_response,
|
rawRes,
|
||||||
'statusCode',
|
'statusCode',
|
||||||
'header',
|
'header',
|
||||||
|
|
||||||
|
@ -316,45 +317,40 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
'errorMessage',
|
'errorMessage',
|
||||||
'errString',
|
'errString',
|
||||||
),
|
),
|
||||||
{
|
status: rawRes.status ?? rawRes.statusCode,
|
||||||
status: _response.status ?? _response.statusCode,
|
headers: rawRes.headers ?? rawRes.header,
|
||||||
headers: _response.headers ?? _response.header,
|
};
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function processRequest(
|
function processRequest(
|
||||||
request: AxiosAdapterRequest,
|
request: AxiosAdapterRequest,
|
||||||
baseOptions: AxiosAdapterBaseOptions,
|
rawOpts: AxiosAdapterBaseOptions,
|
||||||
): AxiosAdapterPlatformTask {
|
): AxiosAdapterPlatformTask {
|
||||||
return request(baseOptions);
|
return request(rawOpts);
|
||||||
}
|
}
|
||||||
|
|
||||||
function processDownload(
|
function processDownload(
|
||||||
download: AxiosAdapterDownload,
|
download: AxiosAdapterDownload,
|
||||||
baseOptions: AxiosAdapterBaseOptions,
|
rawOpts: AxiosAdapterBaseOptions,
|
||||||
): AxiosAdapterPlatformTask {
|
): AxiosAdapterPlatformTask {
|
||||||
const options = baseOptions as AxiosAdapterDownloadOptions;
|
const options = rawOpts as AxiosAdapterDownloadOptions;
|
||||||
const { params, success } = options;
|
const { params, success } = options;
|
||||||
|
|
||||||
options.filePath = params?.filePath;
|
options.filePath = params?.filePath;
|
||||||
options.success = (_response) => {
|
options.success = (rawRes) => {
|
||||||
const response = Object.assign(
|
const response = {
|
||||||
ignore(_response, 'tempFilePath', 'apFilePath', 'filePath', 'fileSize'),
|
...ignore(rawRes, 'tempFilePath', 'apFilePath', 'filePath', 'fileSize'),
|
||||||
{
|
|
||||||
data: {
|
data: {
|
||||||
filePath: _response.filePath,
|
filePath: rawRes.filePath,
|
||||||
tempFilePath:
|
tempFilePath:
|
||||||
_response.tempFilePath ??
|
rawRes.tempFilePath ??
|
||||||
// 支付宝
|
// 支付宝
|
||||||
_response.apFilePath,
|
rawRes.apFilePath,
|
||||||
fileSize:
|
fileSize:
|
||||||
// 飞书
|
// 飞书
|
||||||
_response.fileSize,
|
rawRes.fileSize,
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
);
|
|
||||||
|
|
||||||
success(response);
|
success(response);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -364,10 +360,11 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
|
|
||||||
function processUpload(
|
function processUpload(
|
||||||
upload: AxiosAdapterUpload,
|
upload: AxiosAdapterUpload,
|
||||||
baseOptions: AxiosAdapterBaseOptions,
|
rawOpts: AxiosAdapterBaseOptions,
|
||||||
): AxiosAdapterPlatformTask {
|
): AxiosAdapterPlatformTask {
|
||||||
const options = baseOptions as AxiosAdapterUploadOptions;
|
const options = rawOpts as AxiosAdapterUploadOptions;
|
||||||
const { name, filePath, fileType, ...formData } = options.data as AnyObject;
|
const { data, success } = options;
|
||||||
|
const { name, filePath, fileType, ...formData } = data as AnyObject;
|
||||||
|
|
||||||
options.name = name;
|
options.name = name;
|
||||||
options.filePath = filePath;
|
options.filePath = filePath;
|
||||||
|
@ -377,6 +374,17 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
options.fileName = name;
|
options.fileName = name;
|
||||||
// 支付宝 | 钉钉
|
// 支付宝 | 钉钉
|
||||||
options.fileType = fileType;
|
options.fileType = fileType;
|
||||||
|
options.success = (rawRes) => {
|
||||||
|
const response = { ...rawRes };
|
||||||
|
if (options.responseType === 'text' && options.dataType === 'json') {
|
||||||
|
try {
|
||||||
|
response.data = JSON.parse(rawRes.data);
|
||||||
|
} catch {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
success(response);
|
||||||
|
};
|
||||||
|
|
||||||
orgIgnore(options, ['params', 'data']);
|
orgIgnore(options, ['params', 'data']);
|
||||||
return upload(options);
|
return upload(options);
|
||||||
|
|
|
@ -327,4 +327,33 @@ describe('src/adapter/createAdapter.ts', () => {
|
||||||
createAdapter(p2)(c2);
|
createAdapter(p2)(c2);
|
||||||
createAdapter(p3)(c3);
|
createAdapter(p3)(c3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('响应数据应该进行JSON.parse', async () => {
|
||||||
|
const a = createAdapter({
|
||||||
|
request: vi.fn(),
|
||||||
|
upload: (options) => {
|
||||||
|
options.success({
|
||||||
|
data: '{"id":1}',
|
||||||
|
});
|
||||||
|
},
|
||||||
|
download: vi.fn(),
|
||||||
|
});
|
||||||
|
|
||||||
|
a({
|
||||||
|
type: 'upload',
|
||||||
|
url: 'test',
|
||||||
|
method: 'POST',
|
||||||
|
dataType: 'json',
|
||||||
|
responseType: 'text',
|
||||||
|
data: {},
|
||||||
|
success(res) {
|
||||||
|
expect(res.data).toBeTypeOf('object');
|
||||||
|
// @ts-ignore
|
||||||
|
expect(res.data.id).toBe(1);
|
||||||
|
},
|
||||||
|
fail() {
|
||||||
|
//
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue