parent
3848fdd81b
commit
172f35327b
|
@ -34,7 +34,7 @@ pnpm cz
|
|||
脚本列表
|
||||
|
||||
- `pnpm build` 打包源代码
|
||||
- `pnpm watch` 监听文件变更并运行 `build`
|
||||
- `pnpm dev` 监听文件变更并运行 `build`
|
||||
- `pnpm test` 单元测试
|
||||
- `pnpm test:watch` 监听文件变更并运行 `test`
|
||||
- `pnpm test:cov` 运行 `test` 并输出测试覆盖率
|
||||
|
|
17
README.md
17
README.md
|
@ -2,19 +2,19 @@
|
|||
|
||||
<p align="center">
|
||||
<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">
|
||||
</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">
|
||||
<img src="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml/badge.svg" alt="ci"/>
|
||||
</a>
|
||||
<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"/>
|
||||
</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">
|
||||
<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>
|
||||
</p>
|
||||
|
||||
|
@ -63,7 +63,6 @@ axios-miniprogram 是一款为小程序平台量身定制的轻量级请求库
|
|||
|
||||
[提问点这里](https://github.com/zjxxxxxxxxx/axios-miniprogram/issues)
|
||||
|
||||
|
||||
## 跨端框架示例
|
||||
|
||||
### Taro
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
"format": "prettier -w {src,test,scripts}/**/*.{ts,tsx,js,jsx}",
|
||||
"build": "esno scripts/build.ts",
|
||||
"build:assets": "esno scripts/build.assets.ts",
|
||||
"watch": "pnpm build -a -w",
|
||||
"dev": "pnpm build -a -w",
|
||||
"release": "esno scripts/release.ts",
|
||||
"publish:ci": "esno scripts/publish.ts",
|
||||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
|
||||
|
|
|
@ -261,47 +261,48 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
|||
function transformOptions(
|
||||
config: AxiosAdapterRequestConfig,
|
||||
): AxiosAdapterBaseOptions {
|
||||
const { success, fail } = config;
|
||||
|
||||
return {
|
||||
...config,
|
||||
header: config.headers,
|
||||
success(_response: AxiosAdapterResponse) {
|
||||
const response = transformResponse(_response) as AxiosAdapterResponse;
|
||||
|
||||
config.success(response);
|
||||
success(rawRes: AxiosAdapterResponse) {
|
||||
const response = transformResponse(rawRes) as AxiosAdapterResponse;
|
||||
success(response);
|
||||
},
|
||||
fail(_responseError: AxiosAdapterResponseError) {
|
||||
const responseError = Object.assign(transformResponse(_responseError), {
|
||||
fail(rawErr: AxiosAdapterResponseError) {
|
||||
const responseError = {
|
||||
...transformResponse(rawErr),
|
||||
data: {
|
||||
errno:
|
||||
// 微信 | 飞书新规范
|
||||
_responseError.errno ??
|
||||
rawErr.errno ??
|
||||
// 支付宝 | 钉钉
|
||||
_responseError.error ??
|
||||
rawErr.error ??
|
||||
// 百度 | 360 | 飞书
|
||||
_responseError.errCode ??
|
||||
rawErr.errCode ??
|
||||
// 抖音
|
||||
_responseError.errNo,
|
||||
rawErr.errNo,
|
||||
errMsg:
|
||||
// 飞书新规范
|
||||
_responseError.errString ??
|
||||
rawErr.errString ??
|
||||
// 微信 | 支付宝 | 百度 | 抖音 | QQ | 360 | 飞书
|
||||
_responseError.errMsg ??
|
||||
rawErr.errMsg ??
|
||||
// 钉钉
|
||||
_responseError.errorMessage,
|
||||
rawErr.errorMessage,
|
||||
},
|
||||
});
|
||||
|
||||
config.fail(responseError);
|
||||
};
|
||||
fail(responseError);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function transformResponse(
|
||||
_response: AxiosAdapterResponse | AxiosAdapterResponseError,
|
||||
rawRes: AxiosAdapterResponse | AxiosAdapterResponseError,
|
||||
) {
|
||||
return Object.assign(
|
||||
ignore(
|
||||
_response,
|
||||
return {
|
||||
...ignore(
|
||||
rawRes,
|
||||
'statusCode',
|
||||
'header',
|
||||
|
||||
|
@ -316,45 +317,40 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
|||
'errorMessage',
|
||||
'errString',
|
||||
),
|
||||
{
|
||||
status: _response.status ?? _response.statusCode,
|
||||
headers: _response.headers ?? _response.header,
|
||||
},
|
||||
);
|
||||
status: rawRes.status ?? rawRes.statusCode,
|
||||
headers: rawRes.headers ?? rawRes.header,
|
||||
};
|
||||
}
|
||||
|
||||
function processRequest(
|
||||
request: AxiosAdapterRequest,
|
||||
baseOptions: AxiosAdapterBaseOptions,
|
||||
rawOpts: AxiosAdapterBaseOptions,
|
||||
): AxiosAdapterPlatformTask {
|
||||
return request(baseOptions);
|
||||
return request(rawOpts);
|
||||
}
|
||||
|
||||
function processDownload(
|
||||
download: AxiosAdapterDownload,
|
||||
baseOptions: AxiosAdapterBaseOptions,
|
||||
rawOpts: AxiosAdapterBaseOptions,
|
||||
): AxiosAdapterPlatformTask {
|
||||
const options = baseOptions as AxiosAdapterDownloadOptions;
|
||||
const options = rawOpts as AxiosAdapterDownloadOptions;
|
||||
const { params, success } = options;
|
||||
|
||||
options.filePath = params?.filePath;
|
||||
options.success = (_response) => {
|
||||
const response = Object.assign(
|
||||
ignore(_response, 'tempFilePath', 'apFilePath', 'filePath', 'fileSize'),
|
||||
{
|
||||
data: {
|
||||
filePath: _response.filePath,
|
||||
tempFilePath:
|
||||
_response.tempFilePath ??
|
||||
// 支付宝
|
||||
_response.apFilePath,
|
||||
fileSize:
|
||||
// 飞书
|
||||
_response.fileSize,
|
||||
},
|
||||
options.success = (rawRes) => {
|
||||
const response = {
|
||||
...ignore(rawRes, 'tempFilePath', 'apFilePath', 'filePath', 'fileSize'),
|
||||
data: {
|
||||
filePath: rawRes.filePath,
|
||||
tempFilePath:
|
||||
rawRes.tempFilePath ??
|
||||
// 支付宝
|
||||
rawRes.apFilePath,
|
||||
fileSize:
|
||||
// 飞书
|
||||
rawRes.fileSize,
|
||||
},
|
||||
);
|
||||
|
||||
};
|
||||
success(response);
|
||||
};
|
||||
|
||||
|
@ -364,10 +360,11 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
|||
|
||||
function processUpload(
|
||||
upload: AxiosAdapterUpload,
|
||||
baseOptions: AxiosAdapterBaseOptions,
|
||||
rawOpts: AxiosAdapterBaseOptions,
|
||||
): AxiosAdapterPlatformTask {
|
||||
const options = baseOptions as AxiosAdapterUploadOptions;
|
||||
const { name, filePath, fileType, ...formData } = options.data as AnyObject;
|
||||
const options = rawOpts as AxiosAdapterUploadOptions;
|
||||
const { data, success } = options;
|
||||
const { name, filePath, fileType, ...formData } = data as AnyObject;
|
||||
|
||||
options.name = name;
|
||||
options.filePath = filePath;
|
||||
|
@ -377,6 +374,17 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
|||
options.fileName = name;
|
||||
// 支付宝 | 钉钉
|
||||
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']);
|
||||
return upload(options);
|
||||
|
|
|
@ -327,4 +327,33 @@ describe('src/adapter/createAdapter.ts', () => {
|
|||
createAdapter(p2)(c2);
|
||||
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