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

View File

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

View File

@ -48,7 +48,7 @@
"docs:preview": "pnpm -C docs preview", "docs:preview": "pnpm -C docs preview",
"docs:deploy": "esno scripts/docs.deploy.ts", "docs:deploy": "esno scripts/docs.deploy.ts",
"start": "esno scripts/start.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": { "devDependencies": {
"@commitlint/cli": "^17.4.4", "@commitlint/cli": "^17.4.4",
@ -94,7 +94,7 @@
"*.{js,json}": [ "*.{js,json}": [
"prettier --write" "prettier --write"
], ],
"*.ts?(x)": [ "*.ts": [
"eslint", "eslint",
"prettier --parser=typescript --write" "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 { assert } from '../helpers/error';
import { origIgnore } from '../helpers/ignore'; import { origIgnore } from '../helpers/ignore';
import { import {

View File

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

View File

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

View File

@ -1,5 +1,5 @@
import { assert } from '../helpers/error'; import { assert } from '../helpers/error';
import { isFunction } from '../helpers/isTypes'; import { isFunction } from '../helpers/types';
import { AxiosRequestConfig, AxiosResponse } from './Axios'; import { AxiosRequestConfig, AxiosResponse } from './Axios';
export interface MiddlewareNext { 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) { use(middleware: MiddlewareCallback) {
assert(isFunction(middleware), 'middleware 不是一个 function'); assert(isFunction(middleware), 'middleware 不是一个 function');
@ -59,7 +81,7 @@ export default class MiddlewareManager {
} }
/** /**
* *
* *
* @param ctx * @param ctx
* @param respond * @param respond
@ -73,7 +95,7 @@ export default class MiddlewareManager {
} }
/** /**
* *
* *
* @param enhancer * @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 { deepMerge } from '../helpers/deepMerge';
import { AxiosRequestConfig } from './Axios'; 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( export function buildURL(
url = '', 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 { export function deepMerge<T extends AnyObject>(...objs: (T | undefined)[]): T {
const result: AnyObject = {}; const result: AnyObject = {};

View File

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

View File

@ -1,5 +1,5 @@
import { WITH_DATA_RE } from '../constants/methods'; 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 { assert } from '../helpers/error';
import { AxiosRequestConfig, AxiosResponse } from '../core/Axios'; import { AxiosRequestConfig, AxiosResponse } from '../core/Axios';
import { Cancel, isCancel, isCancelToken } from './cancel'; 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 { transformURL } from '../helpers/transformURL';
import { import {
AxiosRequestConfig, AxiosRequestConfig,
@ -91,7 +91,9 @@ export function request(config: AxiosRequestConfig) {
if (isPlainObject(adapterTask)) { if (isPlainObject(adapterTask)) {
tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate); tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate);
adapterTask?.abort?.(); if (isFunction(adapterTask.abort)) {
adapterTask.abort();
}
} }
reject(reason); reject(reason);
@ -102,19 +104,19 @@ export function request(config: AxiosRequestConfig) {
function tryToggleProgressUpdate( function tryToggleProgressUpdate(
config: AxiosAdapterRequestConfig, config: AxiosAdapterRequestConfig,
progress?: (cb: (event: AnyObject) => void) => void, toggle?: (cb: (event: AnyObject) => void) => void,
) { ) {
const { type, onUploadProgress, onDownloadProgress } = config; if (isFunction(toggle)) {
if (isFunction(progress)) { const { type, onUploadProgress, onDownloadProgress } = config;
switch (type) { switch (type) {
case 'upload': case 'upload':
if (isFunction(onUploadProgress)) { if (isFunction(onUploadProgress)) {
progress(onUploadProgress); toggle(onUploadProgress);
} }
break; break;
case 'download': case 'download':
if (isFunction(onDownloadProgress)) { if (isFunction(onDownloadProgress)) {
progress(onDownloadProgress); toggle(onDownloadProgress);
} }
break; break;
} }

View File

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

View File

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