@lx-frontend/multi-ajax 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/__mocks__/axios.ts +49 -0
- package/coverage/coverage-summary.json +12 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/src/adapters/index.html +131 -0
- package/coverage/lcov-report/src/adapters/index.ts.html +100 -0
- package/coverage/lcov-report/src/adapters/wx-adapter.ts.html +436 -0
- package/coverage/lcov-report/src/cancelLastSameRequest.ts.html +211 -0
- package/coverage/lcov-report/src/canceller.ts.html +265 -0
- package/coverage/lcov-report/src/compatibleMethods.ts.html +301 -0
- package/coverage/lcov-report/src/duplicateRequestPrevent.ts.html +151 -0
- package/coverage/lcov-report/src/helpers.ts.html +319 -0
- package/coverage/lcov-report/src/index.html +221 -0
- package/coverage/lcov-report/src/index.ts.html +211 -0
- package/coverage/lcov-report/src/methods.ts.html +475 -0
- package/coverage/lcov-report/src/utils.ts.html +334 -0
- package/coverage/lcov.info +610 -0
- package/jest.config.ts +24 -0
- package/lib/adapters/index.js +7 -0
- package/lib/adapters/index.js.map +1 -0
- package/lib/adapters/wx-adapter.d.ts +6 -0
- package/lib/adapters/wx-adapter.js +82 -0
- package/lib/adapters/wx-adapter.js.map +1 -0
- package/lib/cancelLastSameRequest.d.ts +8 -0
- package/lib/cancelLastSameRequest.js +45 -0
- package/lib/cancelLastSameRequest.js.map +1 -0
- package/lib/canceller.d.ts +11 -0
- package/lib/canceller.js +63 -0
- package/lib/canceller.js.map +1 -0
- package/lib/compatibleMethods.d.ts +6 -0
- package/lib/compatibleMethods.js +60 -0
- package/lib/compatibleMethods.js.map +1 -0
- package/lib/duplicateRequestPrevent.d.ts +5 -0
- package/lib/duplicateRequestPrevent.js +26 -0
- package/lib/duplicateRequestPrevent.js.map +1 -0
- package/lib/helpers.d.ts +12 -0
- package/lib/helpers.js +63 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +34 -0
- package/lib/index.js.map +1 -0
- package/lib/methods.d.ts +7 -0
- package/lib/methods.js +107 -0
- package/lib/methods.js.map +1 -0
- package/lib/{types/types.d.ts → types.d.ts} +12 -7
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/{types/utils.d.ts → utils.d.ts} +13 -1
- package/lib/utils.js +65 -0
- package/lib/utils.js.map +1 -0
- package/package.json +18 -26
- package/src/adapters/wx-adapter.ts +47 -16
- package/src/cancelLastSameRequest.ts +43 -0
- package/src/canceller.ts +58 -2
- package/src/compatibleMethods.ts +24 -6
- package/src/duplicateRequestPrevent.ts +22 -0
- package/src/helpers.ts +42 -8
- package/src/index.ts +12 -10
- package/src/methods.ts +87 -37
- package/src/types.ts +13 -8
- package/src/utils.ts +28 -7
- package/tests/duplicateRequestPrevent.test.ts +39 -0
- package/tests/helpers.test.ts +73 -0
- package/tests/index.test.ts +877 -0
- package/tests/jest-setup.ts +4 -0
- package/tests/utils.test.ts +68 -0
- package/tsconfig.json +4 -2
- package/types/global.d.ts +4 -0
- package/.babelrc.json +0 -5
- package/__tests__/lx-frontend-ajax.test.js +0 -7
- package/build/rollup.config.cjs.js +0 -43
- package/build/rollup.config.esm.js +0 -40
- package/lib/ajax.cjs.js +0 -1
- package/lib/ajax.esm.js +0 -1
- package/lib/types/adapters/wx-adapter.d.ts +0 -2
- package/lib/types/canceller.d.ts +0 -2
- package/lib/types/compatibleMethods.d.ts +0 -6
- package/lib/types/helpers.d.ts +0 -11
- package/lib/types/index.d.ts +0 -2
- package/lib/types/methods.d.ts +0 -9
- /package/lib/{types/adapters → adapters}/index.d.ts +0 -0
package/src/methods.ts
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
1
|
import { normalizeConfig } from './helpers'
|
|
2
|
-
import { AjaxInstance, AjaxOption, AxiosMethods } from './types'
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import type { AjaxInstance, AjaxOption, AxiosMethods } from './types'
|
|
3
|
+
import { addCalledCanceller, addCanceller, deleteCancellerByHash } from './canceller'
|
|
4
|
+
import { generateRequestHash } from './utils'
|
|
5
|
+
import { addPendingRequest, checkIfHasPendingRequest, clearPendingRequest, getCachedRequest } from './duplicateRequestPrevent'
|
|
6
|
+
import { addRequestCanceler, clearCancelerByHash, execCanceler } from './cancelLastSameRequest'
|
|
5
7
|
|
|
6
8
|
// GDataShape通过initAjax的第二个泛型参数传入,单独的请求如果不传返回数据的类型,则默认使用GDataShape
|
|
7
9
|
// DataType通过ajax.get, ajax.post等方法的泛型参数传入,如果定义了这个参数,会将GDataShape覆盖
|
|
8
|
-
export interface RequestCreator<COPT extends
|
|
10
|
+
export interface RequestCreator<COPT extends object, GDataShape extends object = any> {
|
|
9
11
|
// request = ajax.get(url, options_1)
|
|
10
|
-
<DataType = GDataShape>(url: string, options?: Partial<AjaxOption
|
|
12
|
+
<DataType = GDataShape>(url: string, options?: Partial<AjaxOption & COPT>): {
|
|
11
13
|
// request(params, options_2)
|
|
12
|
-
(params?:
|
|
14
|
+
(params?: object | null, opts?: Partial<AjaxOption & COPT>): Promise<DataType>
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
const postLikeMethods = ['PUT', 'POST', 'DELETE' , 'PATCH']
|
|
17
|
-
export const generateMethod = <COPT extends
|
|
19
|
+
export const generateMethod = <COPT extends NonNullable<unknown>, GDataShape extends object = any>(ajax: AjaxInstance<COPT>, method: AxiosMethods): RequestCreator<COPT, GDataShape> =>
|
|
18
20
|
/**
|
|
19
21
|
*
|
|
20
22
|
* @param url 完整的请求url链接
|
|
21
23
|
* @param options ajax配置
|
|
22
24
|
* @returns 返回一个发送请求的函数
|
|
23
25
|
*/
|
|
24
|
-
<DataType = GDataShape>(url: string, options?: Partial<AjaxOption
|
|
26
|
+
<DataType = GDataShape>(url: string, options?: Partial<AjaxOption & COPT>) =>
|
|
25
27
|
/**
|
|
26
28
|
* 发送请求
|
|
27
29
|
* @param params get类请求的url参数 or post类请求的请求体数据
|
|
28
30
|
* @param opts ajax参数,拥有最高优先级
|
|
29
31
|
* @returns 请求数据
|
|
30
32
|
*/
|
|
31
|
-
async (params?:
|
|
33
|
+
async (params?: object | null, opts?: Partial<AjaxOption & COPT>) => {
|
|
32
34
|
const mergedOptions = { ...options, ...opts }
|
|
33
|
-
|
|
34
35
|
const isPostLikeMethod = postLikeMethods.includes(method)
|
|
35
36
|
|
|
36
37
|
const config = {
|
|
@@ -41,31 +42,6 @@ export const generateMethod = <COPT extends AnyObject, GDataShape extends AnyObj
|
|
|
41
42
|
method
|
|
42
43
|
} as any
|
|
43
44
|
|
|
44
|
-
// 处理取消请求逻辑
|
|
45
|
-
const needCancel = typeof mergedOptions.cancelKey !== 'undefined'
|
|
46
|
-
|
|
47
|
-
if (needCancel) {
|
|
48
|
-
if (typeof AbortController !== 'undefined') {
|
|
49
|
-
// 环境支持AbortController
|
|
50
|
-
const controller = new AbortController()
|
|
51
|
-
config.signal = controller.signal
|
|
52
|
-
ajax.canceller[mergedOptions.cancelKey] = () => {
|
|
53
|
-
controller.abort.call(controller)
|
|
54
|
-
// 删除挂载在ajax上的请求中断函数
|
|
55
|
-
Reflect.deleteProperty(ajax.canceller, mergedOptions.cancelKey)
|
|
56
|
-
}
|
|
57
|
-
} else {
|
|
58
|
-
// 环境如果不支持AbortController,取消函数挂载到ajax只能交给adapter里面去实现了
|
|
59
|
-
config.signal = undefined
|
|
60
|
-
// 默认的取消函数,因为异步的关系,逻辑可能还没走到adapter中去,取消函数就已经被调用了,此时取消函数还是undefined
|
|
61
|
-
// 默认函数将已经调用的取消函数名称记录下来,在adapter中走到发送请求的逻辑时,直接中断即可
|
|
62
|
-
ajax.canceller[mergedOptions.cancelKey] = () => {
|
|
63
|
-
cancellerCalled.push(mergedOptions.cancelKey)
|
|
64
|
-
Reflect.deleteProperty(ajax.canceller, mergedOptions.cancelKey)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
45
|
const finalOpts = normalizeConfig(ajax, config)
|
|
70
46
|
|
|
71
47
|
// mock
|
|
@@ -74,7 +50,81 @@ export const generateMethod = <COPT extends AnyObject, GDataShape extends AnyObj
|
|
|
74
50
|
return Promise.resolve(mock)
|
|
75
51
|
}
|
|
76
52
|
|
|
77
|
-
|
|
53
|
+
// 完全相同的请求,且都处于pending状态,复用上一个
|
|
54
|
+
const hashKey = generateRequestHash(finalOpts)
|
|
55
|
+
if (checkIfHasPendingRequest(hashKey)) {
|
|
56
|
+
// 重复,且处于pending状态的请求,复用
|
|
57
|
+
return getCachedRequest(hashKey)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 这个检查应该位于前面复用请求的逻辑后面,不能乱序
|
|
61
|
+
// 如果需要取消之前处于pending状态相同url请求
|
|
62
|
+
if (finalOpts.cancelLastSameUrl) {
|
|
63
|
+
const currUrl = finalOpts.url
|
|
64
|
+
// 执行所有缓存的url相关的取消函数
|
|
65
|
+
execCanceler(currUrl)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 缓存当前请求的取消函数
|
|
69
|
+
if (typeof AbortController !== 'undefined') {
|
|
70
|
+
// 环境支持AbortController
|
|
71
|
+
const controller = new AbortController()
|
|
72
|
+
finalOpts.signal = controller.signal
|
|
78
73
|
|
|
79
|
-
|
|
74
|
+
if (typeof mergedOptions.cancelKey !== 'undefined') {
|
|
75
|
+
addCanceller(
|
|
76
|
+
mergedOptions.cancelKey,
|
|
77
|
+
hashKey,
|
|
78
|
+
() => {
|
|
79
|
+
controller.abort.call(controller)
|
|
80
|
+
// 删除挂载在ajax上的请求中断函数
|
|
81
|
+
deleteCancellerByHash(mergedOptions.cancelKey as string, hashKey)
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 缓存当前请求的取消函数,假如后续有相同的url请求需要取消该请求,可以通过这里缓存的函数实现(只针对get请求)
|
|
87
|
+
if (finalOpts.cancelLastSameUrl && finalOpts.method.toLocaleLowerCase() === 'get') {
|
|
88
|
+
addRequestCanceler(
|
|
89
|
+
finalOpts.url,
|
|
90
|
+
hashKey,
|
|
91
|
+
() => {
|
|
92
|
+
controller.abort.call(controller)
|
|
93
|
+
clearCancelerByHash(finalOpts.url, hashKey)
|
|
94
|
+
}
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
// 环境如果不支持AbortController,取消函数挂载到ajax只能交给adapter里面去实现了
|
|
99
|
+
finalOpts.signal = undefined
|
|
100
|
+
// 因为异步的关系(axios中发出请求之前,对拦截函数处理可能是异步的),逻辑可能还没走到adapter中去,取消函数就已经被调用了
|
|
101
|
+
// 此时取消函数还是undefined,所以这里提供默认取消函数
|
|
102
|
+
|
|
103
|
+
if (typeof mergedOptions.cancelKey !== 'undefined') {
|
|
104
|
+
addCanceller(
|
|
105
|
+
mergedOptions.cancelKey,
|
|
106
|
+
hashKey,
|
|
107
|
+
() => {
|
|
108
|
+
// 默认函数将已经调用的取消函数名称记录下来,在adapter中走到发送请求的逻辑时,直接中断即可
|
|
109
|
+
addCalledCanceller(mergedOptions.cancelKey as string, hashKey, true)
|
|
110
|
+
deleteCancellerByHash(mergedOptions.cancelKey as string, hashKey)
|
|
111
|
+
}
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const request = ajax.request<any, DataType>(finalOpts)
|
|
118
|
+
addPendingRequest(hashKey, request, finalOpts.reUseDuration)
|
|
119
|
+
return await request
|
|
120
|
+
} catch (e) {
|
|
121
|
+
return Promise.reject(e)
|
|
122
|
+
} finally {
|
|
123
|
+
// 请求成功,删除ajax.canceller上对应的取消函数
|
|
124
|
+
deleteCancellerByHash(finalOpts.cancelKey, hashKey)
|
|
125
|
+
// 删除用于取消相同URL请求功能而缓存的取消函数
|
|
126
|
+
clearCancelerByHash(finalOpts.url, hashKey)
|
|
127
|
+
// 删除缓存的请求(用于复用finalOpts.reUseDuration内完全相同的多个请求)
|
|
128
|
+
clearPendingRequest(hashKey)
|
|
129
|
+
}
|
|
80
130
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AxiosInstance, AxiosRequestConfig } from 'axios'
|
|
2
|
-
import { CustomAjaxMethods } from './compatibleMethods'
|
|
3
|
-
import { RequestCreator } from './methods'
|
|
1
|
+
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
|
|
2
|
+
import type { CustomAjaxMethods } from './compatibleMethods'
|
|
3
|
+
import type { RequestCreator } from './methods'
|
|
4
4
|
|
|
5
5
|
export type AnyObj = Record<string, any>
|
|
6
6
|
|
|
@@ -11,17 +11,21 @@ export interface AjaxOption extends AxiosRequestConfig {
|
|
|
11
11
|
cancelKey?: string
|
|
12
12
|
/** 如果有值,则请求直接resolve这个值,阻断请求发送。用于开发mock数据 */
|
|
13
13
|
mock?: any
|
|
14
|
-
|
|
14
|
+
/** 如果请求的URL和method与一个pending状态的请求相同,是否要终止前面的请求 */
|
|
15
|
+
cancelLastSameUrl?: boolean
|
|
16
|
+
/** 防重设置,在此时间内触发的相同请求会复用上一个处于pending状态的请求,单位为ms。请求返回后,该参数自动失效。 */
|
|
17
|
+
reUseDuration?: number
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
export type RequestMethods = 'get' | 'post' | 'patch' | 'put' | 'delete' | 'head'
|
|
20
|
+
export type RequestMethods = 'get' | 'post' | 'patch' | 'put' | 'delete' | 'head'
|
|
18
21
|
|
|
19
|
-
export type ProxyedAttrs = 'request' | 'interceptors'
|
|
22
|
+
// export type ProxyedAttrs = 'request' | 'interceptors'
|
|
23
|
+
export type ProxyedAttrs = keyof Omit<AxiosInstance, RequestMethods>
|
|
20
24
|
|
|
21
25
|
export type CancellerType = {
|
|
22
|
-
[K: string]: () => void
|
|
26
|
+
[K: string]: () => void | undefined
|
|
23
27
|
}
|
|
24
|
-
export interface AjaxInstance<CusOpt =
|
|
28
|
+
export interface AjaxInstance<CusOpt extends NonNullable<unknown> = any, DataShape extends object = any> extends Pick<AxiosInstance, ProxyedAttrs> {
|
|
25
29
|
/** @deprecated 即将废弃,请使用ajax.get方法替换 */
|
|
26
30
|
query: CustomAjaxMethods<CusOpt, DataShape>
|
|
27
31
|
/** @deprecated 即将废弃,请使用ajax.post方法替换 */
|
|
@@ -38,6 +42,7 @@ export interface AjaxInstance<CusOpt = {}, DataShape = any> extends Pick<AxiosIn
|
|
|
38
42
|
put: RequestCreator<CusOpt, DataShape>
|
|
39
43
|
delete: RequestCreator<CusOpt, DataShape>
|
|
40
44
|
patch: RequestCreator<CusOpt, DataShape>
|
|
45
|
+
extend: (obj: object) => void
|
|
41
46
|
canceller: CancellerType
|
|
42
47
|
$baseUrl: string
|
|
43
48
|
$options: AjaxOption
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import hash from 'object-hash'
|
|
2
|
+
|
|
1
3
|
export function isAbsoluteURL(url: string) {
|
|
2
4
|
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
|
|
3
5
|
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
|
|
@@ -29,16 +31,15 @@ type DeepCopy<T> = T extends object
|
|
|
29
31
|
: T
|
|
30
32
|
|
|
31
33
|
export function deepCopy<T>(obj: T): DeepCopy<T> {
|
|
32
|
-
let target;
|
|
34
|
+
let target: any;
|
|
33
35
|
if (typeof obj === 'object' && obj !== null) {
|
|
34
|
-
|
|
35
|
-
if (isArray) {
|
|
36
|
+
if (Array.isArray(obj)) {
|
|
36
37
|
target = [];
|
|
37
38
|
obj.forEach((elem) => target.push(deepCopy(elem)));
|
|
38
39
|
} else {
|
|
39
40
|
target = {};
|
|
40
41
|
Object.keys(obj).forEach((key) => {
|
|
41
|
-
target[key] = deepCopy(obj[key]);
|
|
42
|
+
target[key] = deepCopy((obj as Record<string, any>)[key]);
|
|
42
43
|
});
|
|
43
44
|
}
|
|
44
45
|
} else {
|
|
@@ -56,7 +57,27 @@ export function getType(obj: any): string {
|
|
|
56
57
|
.toLowerCase();
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
|
|
61
|
+
type NormalizedConfig = {
|
|
62
|
+
params?: Record<string, any>
|
|
63
|
+
data?: Record<string, any>
|
|
64
|
+
url?: string
|
|
65
|
+
baseURL?: string
|
|
66
|
+
method?: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 获取请求的标识,相同的请求配置有相同的标识
|
|
71
|
+
* @param config 经过标准化的ajax请求配置
|
|
72
|
+
* @returns 该请求的hash key
|
|
73
|
+
*/
|
|
74
|
+
export function generateRequestHash(config: NormalizedConfig) {
|
|
75
|
+
const { url = '', method = '', params = {}, data = {}, baseURL = '' } = config
|
|
76
|
+
return hash({
|
|
77
|
+
...params,
|
|
78
|
+
...data,
|
|
79
|
+
baseURL,
|
|
80
|
+
url,
|
|
81
|
+
method
|
|
82
|
+
})
|
|
62
83
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { addPendingRequest, checkIfHasPendingRequest, clearPendingRequest, getCachedRequest } from '../lib/duplicateRequestPrevent'
|
|
2
|
+
import { generateRequestHash } from '../lib/utils';
|
|
3
|
+
|
|
4
|
+
describe('generateRequestHash测试', () => {
|
|
5
|
+
const config = {
|
|
6
|
+
params: {
|
|
7
|
+
page: 1,
|
|
8
|
+
count: 10
|
|
9
|
+
},
|
|
10
|
+
url: 'https://test/url',
|
|
11
|
+
method: 'GET'
|
|
12
|
+
}
|
|
13
|
+
test('相同的config生成相同的hash', () => {
|
|
14
|
+
expect(generateRequestHash(config)).toBe(generateRequestHash(config))
|
|
15
|
+
})
|
|
16
|
+
test('不同的config生成不同的hash', () => {
|
|
17
|
+
const config2 = { ...config, method: 'POST' }
|
|
18
|
+
expect(generateRequestHash(config)).not.toBe(generateRequestHash(config2))
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
describe('addPendingRequest测试', () => {
|
|
23
|
+
test('添加删除测试', () => {
|
|
24
|
+
addPendingRequest('123', Promise.resolve({a: 1}), 300)
|
|
25
|
+
expect(getCachedRequest('123')).toEqual(Promise.resolve({a: 1}))
|
|
26
|
+
expect(checkIfHasPendingRequest('123')).toBe(true)
|
|
27
|
+
clearPendingRequest('123')
|
|
28
|
+
expect(getCachedRequest('123')).toBeUndefined()
|
|
29
|
+
expect(checkIfHasPendingRequest('123')).toBe(false)
|
|
30
|
+
})
|
|
31
|
+
test('超时删除测试', (done) => {
|
|
32
|
+
addPendingRequest('123', Promise.resolve({a: 1}), 500)
|
|
33
|
+
expect(getCachedRequest('123')).toEqual(Promise.resolve({a: 1}))
|
|
34
|
+
setTimeout(() => {
|
|
35
|
+
expect(getCachedRequest('123')).toBeUndefined()
|
|
36
|
+
done()
|
|
37
|
+
}, 600)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { addInstanceExtendMethod, normalizeConfig, proxyAxiosMethods } from '../lib/helpers'
|
|
2
|
+
import qs = require('qs')
|
|
3
|
+
import type { AjaxInstance } from '../lib/types.d'
|
|
4
|
+
|
|
5
|
+
describe('标准化Ajax配置函数normalizeConfig测试', () => {
|
|
6
|
+
const baseConfig = {
|
|
7
|
+
headers: {},
|
|
8
|
+
data: {
|
|
9
|
+
data1: 1,
|
|
10
|
+
data2: false,
|
|
11
|
+
data3: 'str'
|
|
12
|
+
},
|
|
13
|
+
baseUrl: 'https://base/url',
|
|
14
|
+
sendFormData: false
|
|
15
|
+
}
|
|
16
|
+
const context = {
|
|
17
|
+
$options: {}
|
|
18
|
+
}
|
|
19
|
+
test('发送JSON格式数据', () => {
|
|
20
|
+
const newConfig = normalizeConfig(context as AjaxInstance, baseConfig)
|
|
21
|
+
expect(newConfig.headers['content-type']).toBe('application/json')
|
|
22
|
+
})
|
|
23
|
+
test('发送FormData格式数据', () => {
|
|
24
|
+
const copyConfig = {
|
|
25
|
+
...baseConfig,
|
|
26
|
+
sendFormData: true
|
|
27
|
+
}
|
|
28
|
+
const newConfig = normalizeConfig(context as AjaxInstance, copyConfig)
|
|
29
|
+
expect(newConfig.headers['content-type']).toBe('application/x-www-form-urlencoded')
|
|
30
|
+
expect(newConfig.data).toEqual(qs.stringify(copyConfig.data))
|
|
31
|
+
})
|
|
32
|
+
test('baseUrl和baseURL兼容', () => {
|
|
33
|
+
const newConfig = normalizeConfig(context as AjaxInstance, baseConfig)
|
|
34
|
+
expect(newConfig.baseURL).not.toBeUndefined()
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('设置代理函数proxyAxiosMethods测试', () => {
|
|
39
|
+
let target: Record<string, any>, source: { request: any, interceptors: any}
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
target = {} as any
|
|
42
|
+
source = {
|
|
43
|
+
request: {a: 1},
|
|
44
|
+
interceptors: {b: 1}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
test('代理测试', () => {
|
|
48
|
+
proxyAxiosMethods(target, source)
|
|
49
|
+
expect(target.request).not.toBeUndefined()
|
|
50
|
+
expect(target.interceptors).not.toBeUndefined()
|
|
51
|
+
})
|
|
52
|
+
test('不可写测试', () => {
|
|
53
|
+
proxyAxiosMethods(target, source)
|
|
54
|
+
expect(() => {target.request = {}}).toThrow()
|
|
55
|
+
expect(() => {target.interceptors = {}}).toThrow()
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('addInstanceExtendMethod测试', () => {
|
|
60
|
+
test('添加extend函数测试', () => {
|
|
61
|
+
const ctx: any = {}
|
|
62
|
+
const source = {
|
|
63
|
+
testFn: jest.fn(),
|
|
64
|
+
testAttr: 'tst'
|
|
65
|
+
}
|
|
66
|
+
addInstanceExtendMethod(ctx)
|
|
67
|
+
expect(typeof ctx.extend).toBe('function')
|
|
68
|
+
ctx.extend(source)
|
|
69
|
+
ctx.testFn()
|
|
70
|
+
expect(source.testFn).toHaveBeenCalled()
|
|
71
|
+
expect(ctx.testAttr).toBe('tst')
|
|
72
|
+
})
|
|
73
|
+
})
|