docs: 修正下载文件示例
parent
359b09172e
commit
1152853ce2
|
@ -17,7 +17,7 @@ body:
|
|||
id: suggested-solution
|
||||
attributes:
|
||||
label: 建议的解决方案
|
||||
description: '在模块 [xy] 中,我们可以提供以下实现......'
|
||||
description: '我们可以提供以下实现......'
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
|
|
|
@ -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) => {
|
||||
// 失败之后做些什么
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||
import { isFunction, isPlainObject } from '../helpers/types';
|
||||
import { AxiosAdapterPlatform, createAdapter } from './createAdapter';
|
||||
|
||||
/**
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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 强化器
|
||||
*/
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isArray, isDate, isNull, isPlainObject, isUndefined } from './isTypes';
|
||||
import { isArray, isDate, isNull, isPlainObject, isUndefined } from './types';
|
||||
|
||||
export function buildURL(
|
||||
url = '',
|
||||
|
|
|
@ -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 = {};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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,
|
||||
) {
|
||||
if (isFunction(toggle)) {
|
||||
const { type, onUploadProgress, onDownloadProgress } = config;
|
||||
if (isFunction(progress)) {
|
||||
switch (type) {
|
||||
case 'upload':
|
||||
if (isFunction(onUploadProgress)) {
|
||||
progress(onUploadProgress);
|
||||
toggle(onUploadProgress);
|
||||
}
|
||||
break;
|
||||
case 'download':
|
||||
if (isFunction(onDownloadProgress)) {
|
||||
progress(onDownloadProgress);
|
||||
toggle(onDownloadProgress);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isArray, isFunction } from '../helpers/isTypes';
|
||||
import { isArray, isFunction } from '../helpers/types';
|
||||
|
||||
export interface AxiosTransformCallback<TData = unknown> {
|
||||
(
|
||||
|
|
|
@ -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();
|
Loading…
Reference in New Issue