docs: 修正下载文件示例

pull/49/head
zjx0905 2023-05-07 23:16:28 +08:00
parent 359b09172e
commit 1152853ce2
16 changed files with 52 additions and 29 deletions

View File

@ -17,7 +17,7 @@ body:
id: suggested-solution
attributes:
label: 建议的解决方案
description: '在模块 [xy] 中,我们可以提供以下实现......'
description: '我们可以提供以下实现......'
validations:
required: true
- type: textarea

View File

@ -26,7 +26,7 @@ axios('https://api.com/test', {
const {
// 临时文件路径 (本地路径)。没传入 filePath 指定文件存储路径时会返回,下载后的文件会存储到一个临时文件
tempFilePath,
} = response;
} = response.data;
})
.catch((error) => {
// 失败之后做些什么
@ -50,7 +50,7 @@ axios('https://api.com/test', {
const {
// 指定文件下载后存储的路径 (本地路径)
filePath,
} = response;
} = response.data;
})
.catch((error) => {
// 失败之后做些什么
@ -86,7 +86,7 @@ axios('https://api.com/test', {
const {
// 指定文件下载后存储的路径 (本地路径)
filePath,
} = response;
} = response.data;
})
.catch((error) => {
// 失败之后做些什么

View File

@ -48,7 +48,7 @@
"docs:preview": "pnpm -C docs preview",
"docs:deploy": "esno scripts/docs.deploy.ts",
"start": "esno scripts/start.ts",
"tidy": "prettier **/*.{js,json} --write && prettier **/*.ts?(x) --parser=typescript --write"
"tidy-up": "prettier **/*.{js,json} --write & prettier **/*.ts --parser=typescript --write"
},
"devDependencies": {
"@commitlint/cli": "^17.4.4",
@ -94,7 +94,7 @@
"*.{js,json}": [
"prettier --write"
],
"*.ts?(x)": [
"*.ts": [
"eslint",
"prettier --parser=typescript --write"
]

View File

@ -1,4 +1,4 @@
import { isFunction, isPlainObject } from '../helpers/isTypes';
import { isFunction, isPlainObject } from '../helpers/types';
import { assert } from '../helpers/error';
import { origIgnore } from '../helpers/ignore';
import {

View File

@ -1,4 +1,4 @@
import { isFunction, isPlainObject } from '../helpers/isTypes';
import { isFunction, isPlainObject } from '../helpers/types';
import { AxiosAdapterPlatform, createAdapter } from './createAdapter';
/**

View File

@ -3,7 +3,7 @@ import {
WITH_DATA_METHODS,
WITH_PARAMS_METHODS,
} from '../constants/methods';
import { isString } from '../helpers/isTypes';
import { isString } from '../helpers/types';
import { dispatchRequest } from '../request/dispatchRequest';
import { CancelToken } from '../request/cancel';
import { AxiosTransformer } from '../request/transformData';

View File

@ -1,5 +1,5 @@
import { assert } from '../helpers/error';
import { isFunction } from '../helpers/isTypes';
import { isFunction } from '../helpers/types';
import { AxiosRequestConfig, AxiosResponse } from './Axios';
export interface MiddlewareNext {
@ -41,7 +41,29 @@ export default class MiddlewareManager {
/**
*
*
* @param middleware
* 1
* ```ts
* axios.use(async function middleware(ctx, next) {
* console.log(ctx.req);
* await next();
* console.log(ctx.res);
* });
* ```
*
* 2
* ```ts
* axios
* .use(async function middleware1(ctx, next) {
* console.log(ctx.req);
* await next();
* console.log(ctx.res);
* })
* .use(async function middleware2(ctx, next) {
* console.log(ctx.req);
* await next();
* console.log(ctx.res);
* });
* ```
*/
use(middleware: MiddlewareCallback) {
assert(isFunction(middleware), 'middleware 不是一个 function');
@ -59,7 +81,7 @@ export default class MiddlewareManager {
}
/**
*
*
*
* @param ctx
* @param respond
@ -73,7 +95,7 @@ export default class MiddlewareManager {
}
/**
*
*
*
* @param enhancer
*/

View File

@ -1,4 +1,4 @@
import { isUndefined, isPlainObject } from '../helpers/isTypes';
import { isUndefined, isPlainObject } from '../helpers/types';
import { deepMerge } from '../helpers/deepMerge';
import { AxiosRequestConfig } from './Axios';

View File

@ -1,4 +1,4 @@
import { isArray, isDate, isNull, isPlainObject, isUndefined } from './isTypes';
import { isArray, isDate, isNull, isPlainObject, isUndefined } from './types';
export function buildURL(
url = '',

View File

@ -1,4 +1,4 @@
import { isPlainObject } from './isTypes';
import { isPlainObject } from './types';
export function deepMerge<T extends AnyObject>(...objs: (T | undefined)[]): T {
const result: AnyObject = {};

View File

@ -1,5 +1,5 @@
import { AxiosRequestConfig } from '../core/Axios';
import { isPlainObject } from './isTypes';
import { isPlainObject } from './types';
import { buildURL } from './buildURL';
import { combineURL } from './combineURL';
import { dynamicURL } from './dynamicURL';
@ -10,6 +10,5 @@ export function transformURL(config: AxiosRequestConfig) {
config.params,
isPlainObject(config.data) ? config.data : {},
);
return buildURL(fullPath, config.params, config.paramsSerializer);
}

View File

@ -1,5 +1,5 @@
import { WITH_DATA_RE } from '../constants/methods';
import { isFunction, isString } from '../helpers/isTypes';
import { isFunction, isString } from '../helpers/types';
import { assert } from '../helpers/error';
import { AxiosRequestConfig, AxiosResponse } from '../core/Axios';
import { Cancel, isCancel, isCancelToken } from './cancel';

View File

@ -1,4 +1,4 @@
import { isFunction, isPlainObject } from '../helpers/isTypes';
import { isFunction, isPlainObject } from '../helpers/types';
import { transformURL } from '../helpers/transformURL';
import {
AxiosRequestConfig,
@ -91,7 +91,9 @@ export function request(config: AxiosRequestConfig) {
if (isPlainObject(adapterTask)) {
tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate);
adapterTask?.abort?.();
if (isFunction(adapterTask.abort)) {
adapterTask.abort();
}
}
reject(reason);
@ -102,19 +104,19 @@ export function request(config: AxiosRequestConfig) {
function tryToggleProgressUpdate(
config: AxiosAdapterRequestConfig,
progress?: (cb: (event: AnyObject) => void) => void,
toggle?: (cb: (event: AnyObject) => void) => void,
) {
const { type, onUploadProgress, onDownloadProgress } = config;
if (isFunction(progress)) {
if (isFunction(toggle)) {
const { type, onUploadProgress, onDownloadProgress } = config;
switch (type) {
case 'upload':
if (isFunction(onUploadProgress)) {
progress(onUploadProgress);
toggle(onUploadProgress);
}
break;
case 'download':
if (isFunction(onDownloadProgress)) {
progress(onDownloadProgress);
toggle(onDownloadProgress);
}
break;
}

View File

@ -1,4 +1,4 @@
import { isArray, isFunction } from '../helpers/isTypes';
import { isArray, isFunction } from '../helpers/types';
export interface AxiosTransformCallback<TData = unknown> {
(

View File

@ -7,9 +7,9 @@ import {
isNull,
isUndefined,
isString,
} from '@/helpers/isTypes';
} from '@/helpers/types';
describe('src/helpers/isTypes.ts', () => {
describe('src/helpers/types.ts', () => {
test('应该能判断是数组', () => {
expect(isArray(new Array(1))).toBeTruthy();
expect(isArray([])).toBeTruthy();