refactor: 新的目录结构
parent
2d691b69cb
commit
cd65ce4a91
10
src/axios.ts
10
src/axios.ts
|
@ -1,12 +1,16 @@
|
||||||
|
import {
|
||||||
|
CancelToken,
|
||||||
|
CancelTokenConstructor,
|
||||||
|
isCancel,
|
||||||
|
} from './request/cancel';
|
||||||
|
import { isAxiosError } from './request/createError';
|
||||||
|
import { mergeConfig } from './core/mergeConfig';
|
||||||
import { AxiosDomainRequest } from './core/AxiosDomain';
|
import { AxiosDomainRequest } from './core/AxiosDomain';
|
||||||
import Axios, {
|
import Axios, {
|
||||||
AxiosConstructor,
|
AxiosConstructor,
|
||||||
AxiosRequestConfig,
|
AxiosRequestConfig,
|
||||||
AxiosRequestHeaders,
|
AxiosRequestHeaders,
|
||||||
} from './core/Axios';
|
} from './core/Axios';
|
||||||
import { CancelToken, CancelTokenConstructor, isCancel } from './core/cancel';
|
|
||||||
import { isAxiosError } from './core/createError';
|
|
||||||
import { mergeConfig } from './core/mergeConfig';
|
|
||||||
import { createAdapter } from './adapter';
|
import { createAdapter } from './adapter';
|
||||||
import defaults from './defaults';
|
import defaults from './defaults';
|
||||||
import { version } from './version';
|
import { version } from './version';
|
||||||
|
|
|
@ -2,6 +2,9 @@ import { buildURL } from '../helpers/buildURL';
|
||||||
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
||||||
import { combineURL } from '../helpers/combineURL';
|
import { combineURL } from '../helpers/combineURL';
|
||||||
import { isString } from '../helpers/isTypes';
|
import { isString } from '../helpers/isTypes';
|
||||||
|
import { CancelToken } from '../request/cancel';
|
||||||
|
import { dispatchRequest } from '../request/dispatchRequest';
|
||||||
|
import { AxiosTransformer } from '../request/transformData';
|
||||||
import {
|
import {
|
||||||
AxiosAdapter,
|
AxiosAdapter,
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
|
@ -11,9 +14,6 @@ import {
|
||||||
} from '../adapter';
|
} from '../adapter';
|
||||||
import InterceptorManager, { Interceptor } from './InterceptorManager';
|
import InterceptorManager, { Interceptor } from './InterceptorManager';
|
||||||
import { mergeConfig } from './mergeConfig';
|
import { mergeConfig } from './mergeConfig';
|
||||||
import { CancelToken } from './cancel';
|
|
||||||
import { dispatchRequest } from './dispatchRequest';
|
|
||||||
import { AxiosTransformer } from './transformData';
|
|
||||||
import AxiosDomain from './AxiosDomain';
|
import AxiosDomain from './AxiosDomain';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -36,9 +36,9 @@ export type {
|
||||||
AxiosStatic,
|
AxiosStatic,
|
||||||
} from './axios';
|
} from './axios';
|
||||||
|
|
||||||
export { CancelToken, isCancel } from './core/cancel';
|
export { CancelToken, isCancel } from './request/cancel';
|
||||||
|
export { isAxiosError } from './request/createError';
|
||||||
export { default as Axios } from './core/Axios';
|
export { default as Axios } from './core/Axios';
|
||||||
export { isAxiosError } from './core/createError';
|
|
||||||
export { createAdapter } from './adapter';
|
export { createAdapter } from './adapter';
|
||||||
export { version } from './version';
|
export { version } from './version';
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { cleanStack } from '../helpers/error';
|
import { cleanStack } from '../helpers/error';
|
||||||
|
import {
|
||||||
|
AxiosRequestConfig,
|
||||||
|
AxiosResponse,
|
||||||
|
AxiosResponseError,
|
||||||
|
} from '../core/Axios';
|
||||||
import { AxiosAdapterPlatformTask } from '../adapter';
|
import { AxiosAdapterPlatformTask } from '../adapter';
|
||||||
import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
|
||||||
|
|
||||||
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
import { isFunction, isString } from '../helpers/isTypes';
|
import { isFunction, isString } from '../helpers/isTypes';
|
||||||
import { assert } from '../helpers/error';
|
import { assert } from '../helpers/error';
|
||||||
|
import { requestMethodWithDataNames } from '../core/AxiosDomain';
|
||||||
|
import {
|
||||||
|
AxiosRequestConfig,
|
||||||
|
AxiosRequestMethod,
|
||||||
|
AxiosResponse,
|
||||||
|
} from '../core/Axios';
|
||||||
import { Cancel, isCancel, isCancelToken } from './cancel';
|
import { Cancel, isCancel, isCancelToken } from './cancel';
|
||||||
import { flattenHeaders } from './flattenHeaders';
|
import { flattenHeaders } from './flattenHeaders';
|
||||||
import { AxiosTransformer, transformData } from './transformData';
|
import { AxiosTransformer, transformData } from './transformData';
|
||||||
import { request } from './request';
|
import { request } from './request';
|
||||||
import { AxiosRequestConfig, AxiosRequestMethod, AxiosResponse } from './Axios';
|
|
||||||
import { transformURL } from './transformURL';
|
import { transformURL } from './transformURL';
|
||||||
import { AxiosErrorResponse } from './createError';
|
import { AxiosErrorResponse } from './createError';
|
||||||
import { requestMethodWithDataNames } from './AxiosDomain';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 可以携带 data 的请求方法
|
* 可以携带 data 的请求方法
|
|
@ -1,6 +1,6 @@
|
||||||
import { isPlainObject } from '../helpers/isTypes';
|
import { isPlainObject } from '../helpers/isTypes';
|
||||||
import { ignore } from '../helpers/ignore';
|
import { ignore } from '../helpers/ignore';
|
||||||
import { AxiosRequestConfig, AxiosRequestHeaders } from './Axios';
|
import { AxiosRequestConfig, AxiosRequestHeaders } from '../core/Axios';
|
||||||
|
|
||||||
export function flattenHeaders(
|
export function flattenHeaders(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
|
@ -1,5 +1,5 @@
|
||||||
|
import { AxiosRequestConfig } from '../core/Axios';
|
||||||
import { AxiosAdapterRequestType } from '../adapter';
|
import { AxiosAdapterRequestType } from '../adapter';
|
||||||
import { AxiosRequestConfig } from './Axios';
|
|
||||||
|
|
||||||
export function generateType(config: AxiosRequestConfig) {
|
export function generateType(config: AxiosRequestConfig) {
|
||||||
let requestType: AxiosAdapterRequestType = 'request';
|
let requestType: AxiosAdapterRequestType = 'request';
|
|
@ -1,4 +1,9 @@
|
||||||
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||||
|
import {
|
||||||
|
AxiosRequestConfig,
|
||||||
|
AxiosResponse,
|
||||||
|
AxiosResponseError,
|
||||||
|
} from '../core/Axios';
|
||||||
import {
|
import {
|
||||||
AxiosAdapterRequestConfig,
|
AxiosAdapterRequestConfig,
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
|
@ -6,7 +11,7 @@ import {
|
||||||
AxiosAdapterResponseError,
|
AxiosAdapterResponseError,
|
||||||
AxiosAdapterPlatformTask,
|
AxiosAdapterPlatformTask,
|
||||||
} from '../adapter';
|
} from '../adapter';
|
||||||
import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
|
||||||
import { isCancelToken } from './cancel';
|
import { isCancelToken } from './cancel';
|
||||||
import { AxiosErrorResponse, createError } from './createError';
|
import { AxiosErrorResponse, createError } from './createError';
|
||||||
import { generateType } from './generateType';
|
import { generateType } from './generateType';
|
|
@ -3,17 +3,15 @@ import { buildURL } from '../helpers/buildURL';
|
||||||
import { combineURL } from '../helpers/combineURL';
|
import { combineURL } from '../helpers/combineURL';
|
||||||
import { dynamicURL } from '../helpers/dynamicURL';
|
import { dynamicURL } from '../helpers/dynamicURL';
|
||||||
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
||||||
import { AxiosRequestConfig } from './Axios';
|
import { AxiosRequestConfig } from '../core/Axios';
|
||||||
|
|
||||||
export function transformURL(config: AxiosRequestConfig) {
|
export function transformURL(config: AxiosRequestConfig) {
|
||||||
let url = config.url ?? '';
|
let url = config.url ?? '';
|
||||||
|
|
||||||
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL ?? '', url);
|
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL ?? '', url);
|
||||||
url = dynamicURL(
|
|
||||||
url,
|
const data = isPlainObject(config.data) ? config.data : {};
|
||||||
config.params,
|
url = dynamicURL(url, config.params, data);
|
||||||
isPlainObject(config.data) ? config.data : {},
|
|
||||||
);
|
|
||||||
url = buildURL(url, config.params, config.paramsSerializer);
|
url = buildURL(url, config.params, config.paramsSerializer);
|
||||||
|
|
||||||
return url;
|
return url;
|
|
@ -1,11 +1,11 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import { eachMethods } from 'scripts/test.utils';
|
||||||
|
import { CancelToken, isCancel } from '@/request/cancel';
|
||||||
|
import { isAxiosError } from '@/request/createError';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
import { CancelToken, isCancel } from '@/core/cancel';
|
import defaults from '@/defaults';
|
||||||
import { isAxiosError } from '@/core/createError';
|
|
||||||
import { createAdapter } from '@/adapter';
|
import { createAdapter } from '@/adapter';
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
import defaults from '@/defaults';
|
|
||||||
import { eachMethods } from 'scripts/test.utils';
|
|
||||||
|
|
||||||
describe('src/axios.ts', () => {
|
describe('src/axios.ts', () => {
|
||||||
test('应该有这些静态属性', () => {
|
test('应该有这些静态属性', () => {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { ignore } from '@/helpers/ignore';
|
import { ignore } from '@/helpers/ignore';
|
||||||
|
import { CancelToken } from '@/request/cancel';
|
||||||
import { mergeConfig } from '@/core/mergeConfig';
|
import { mergeConfig } from '@/core/mergeConfig';
|
||||||
import { CancelToken } from '@/core/cancel';
|
|
||||||
|
|
||||||
describe('src/core/mergeConfig.ts', () => {
|
describe('src/core/mergeConfig.ts', () => {
|
||||||
test('应该支持空参数', () => {
|
test('应该支持空参数', () => {
|
||||||
|
|
|
@ -6,16 +6,16 @@ import {
|
||||||
noop,
|
noop,
|
||||||
asyncTimeout,
|
asyncTimeout,
|
||||||
} from 'scripts/test.utils';
|
} from 'scripts/test.utils';
|
||||||
import axios from 'src/axios';
|
|
||||||
import {
|
import {
|
||||||
Cancel,
|
Cancel,
|
||||||
isCancel,
|
isCancel,
|
||||||
CancelToken,
|
CancelToken,
|
||||||
isCancelToken,
|
isCancelToken,
|
||||||
CancelAction,
|
CancelAction,
|
||||||
} from '@/core/cancel';
|
} from '@/request/cancel';
|
||||||
|
import axios from '@/axios';
|
||||||
|
|
||||||
describe('src/helpers/cancel.ts', () => {
|
describe('src/request/cancel.ts', () => {
|
||||||
test('应该支持空参数', () => {
|
test('应该支持空参数', () => {
|
||||||
const c = new Cancel();
|
const c = new Cancel();
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { checkStack } from 'scripts/test.utils';
|
import { checkStack } from 'scripts/test.utils';
|
||||||
import { createError, isAxiosError } from '@/core/createError';
|
import { createError, isAxiosError } from '@/request/createError';
|
||||||
|
|
||||||
describe('src/core/createError.ts', () => {
|
describe('src/request/createError.ts', () => {
|
||||||
test('应该支持空参数', () => {
|
test('应该支持空参数', () => {
|
||||||
const c = {};
|
const c = {};
|
||||||
const r = {} as any;
|
const r = {} as any;
|
|
@ -1,6 +1,6 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
|
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
|
||||||
import { dispatchRequest } from '@/core/dispatchRequest';
|
import { dispatchRequest } from '@/request/dispatchRequest';
|
||||||
import {
|
import {
|
||||||
requestMethodNames,
|
requestMethodNames,
|
||||||
requestMethodWithDataNames,
|
requestMethodWithDataNames,
|
||||||
|
@ -10,7 +10,7 @@ import {
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
import _defaults from '@/defaults';
|
import _defaults from '@/defaults';
|
||||||
|
|
||||||
describe('src/core/dispatchRequest.ts', () => {
|
describe('src/request/dispatchRequest.ts', () => {
|
||||||
const defaults = {
|
const defaults = {
|
||||||
..._defaults,
|
..._defaults,
|
||||||
adapter: mockAdapter(),
|
adapter: mockAdapter(),
|
|
@ -1,8 +1,8 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { flattenHeaders } from '@/core/flattenHeaders';
|
|
||||||
import { eachMethods, methods } from 'scripts/test.utils';
|
import { eachMethods, methods } from 'scripts/test.utils';
|
||||||
|
import { flattenHeaders } from '@/request/flattenHeaders';
|
||||||
|
|
||||||
describe('src/core/flattenHeaders.ts', () => {
|
describe('src/request/flattenHeaders.ts', () => {
|
||||||
const baseHeaders = Object.fromEntries(
|
const baseHeaders = Object.fromEntries(
|
||||||
methods.map((k) => [k, { v1: `${k}1`, v2: `${k}2` }]),
|
methods.map((k) => [k, { v1: `${k}1`, v2: `${k}2` }]),
|
||||||
) as unknown as Record<(typeof methods)[number], AnyObject>;
|
) as unknown as Record<(typeof methods)[number], AnyObject>;
|
|
@ -1,8 +1,8 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { generateType } from '@/core/generateType';
|
|
||||||
import { testEachMethods } from 'scripts/test.utils';
|
import { testEachMethods } from 'scripts/test.utils';
|
||||||
|
import { generateType } from '@/request/generateType';
|
||||||
|
|
||||||
describe('src/core/generateType.ts', () => {
|
describe('src/request/generateType.ts', () => {
|
||||||
testEachMethods('%s 应该是一个 reuqest', (k) => {
|
testEachMethods('%s 应该是一个 reuqest', (k) => {
|
||||||
expect(generateType({ method: k })).toBe('request');
|
expect(generateType({ method: k })).toBe('request');
|
||||||
});
|
});
|
|
@ -7,10 +7,10 @@ import {
|
||||||
mockAdapterFail,
|
mockAdapterFail,
|
||||||
testEachMethods,
|
testEachMethods,
|
||||||
} from 'scripts/test.utils';
|
} from 'scripts/test.utils';
|
||||||
import { request } from '@/core/request';
|
import { request } from '@/request/request';
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
|
|
||||||
describe('src/core/request.ts', () => {
|
describe('src/request/request.ts', () => {
|
||||||
testEachMethods('%s 方法应该返回正确的响应体结构', (k) => {
|
testEachMethods('%s 方法应该返回正确的响应体结构', (k) => {
|
||||||
const c = {
|
const c = {
|
||||||
adapter: mockAdapter(),
|
adapter: mockAdapter(),
|
|
@ -1,7 +1,7 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { transformData } from '@/core/transformData';
|
import { transformData } from '@/request/transformData';
|
||||||
|
|
||||||
describe('src/core/transformData.ts', () => {
|
describe('src/request/transformData.ts', () => {
|
||||||
test('应该支持空配置', () => {
|
test('应该支持空配置', () => {
|
||||||
expect(transformData()).toBeUndefined();
|
expect(transformData()).toBeUndefined();
|
||||||
expect(transformData({})).toEqual({});
|
expect(transformData({})).toEqual({});
|
|
@ -1,7 +1,7 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { transformURL } from '@/core/transformURL';
|
import { transformURL } from '@/request/transformURL';
|
||||||
|
|
||||||
describe('src/core/transformURL.ts', () => {
|
describe('src/request/transformURL.ts', () => {
|
||||||
test('应该支持空配置', () => {
|
test('应该支持空配置', () => {
|
||||||
expect(transformURL({})).toBe('');
|
expect(transformURL({})).toBe('');
|
||||||
expect(transformURL({ baseURL: 'http://api.com' })).toBe('http://api.com');
|
expect(transformURL({ baseURL: 'http://api.com' })).toBe('http://api.com');
|
Loading…
Reference in New Issue