@lx-frontend/multi-ajax 0.2.8 → 0.2.9
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 +80 -33
- package/__mocks__/axios.ts +16 -14
- package/jest.config.ts +3 -12
- package/lib/adapters/index.d.ts +1 -1
- package/lib/adapters/index.js.map +1 -1
- package/lib/adapters/wx-adapter.js.map +1 -1
- package/lib/cancelLastSameRequest.d.ts +1 -1
- package/lib/cancelLastSameRequest.js +1 -0
- package/lib/cancelLastSameRequest.js.map +1 -1
- package/lib/canceller.d.ts +1 -1
- package/lib/canceller.js.map +1 -1
- package/lib/compatibleMethods.d.ts +1 -1
- package/lib/compatibleMethods.js +38 -35
- package/lib/compatibleMethods.js.map +1 -1
- package/lib/duplicateRequestPrevent.d.ts +1 -1
- package/lib/duplicateRequestPrevent.js.map +1 -1
- package/lib/helpers.js +2 -2
- package/lib/helpers.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/methods.js.map +1 -1
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +5 -10
- package/lib/utils.js.map +1 -1
- package/package.json +12 -11
- package/src/adapters/index.ts +2 -4
- package/src/adapters/wx-adapter.ts +43 -65
- package/src/cancelLastSameRequest.ts +3 -3
- package/src/canceller.ts +2 -3
- package/src/compatibleMethods.ts +53 -39
- package/src/duplicateRequestPrevent.ts +1 -1
- package/src/helpers.ts +29 -26
- package/src/index.ts +13 -11
- package/src/methods.ts +96 -96
- package/src/types.ts +2 -1
- package/src/utils.ts +31 -36
- package/src/global.d.ts +0 -1
package/src/methods.ts
CHANGED
|
@@ -2,21 +2,33 @@ import { normalizeConfig } from './helpers'
|
|
|
2
2
|
import type { AjaxInstance, AjaxOption, AxiosMethods } from './types'
|
|
3
3
|
import { addCalledCanceller, addCanceller, deleteCancellerByHash } from './canceller'
|
|
4
4
|
import { generateRequestHash } from './utils'
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
addPendingRequest,
|
|
7
|
+
checkIfHasPendingRequest,
|
|
8
|
+
clearPendingRequest,
|
|
9
|
+
getCachedRequest,
|
|
10
|
+
} from './duplicateRequestPrevent'
|
|
6
11
|
import { addRequestCanceler, clearCancelerByHash, execCanceler } from './cancelLastSameRequest'
|
|
7
12
|
|
|
8
13
|
// GDataShape通过initAjax的第二个泛型参数传入,单独的请求如果不传返回数据的类型,则默认使用GDataShape
|
|
9
14
|
// DataType通过ajax.get, ajax.post等方法的泛型参数传入,如果定义了这个参数,会将GDataShape覆盖
|
|
10
15
|
export interface RequestCreator<COPT extends object, GDataShape extends object = any> {
|
|
11
16
|
// request = ajax.get(url, options_1)
|
|
12
|
-
<DataType = GDataShape>(
|
|
17
|
+
<DataType = GDataShape>(
|
|
18
|
+
url: string,
|
|
19
|
+
options?: Partial<AjaxOption & COPT>
|
|
20
|
+
): {
|
|
13
21
|
// request(params, options_2)
|
|
14
22
|
(params?: object | null, opts?: Partial<AjaxOption & COPT>): Promise<DataType>
|
|
15
23
|
}
|
|
16
24
|
}
|
|
17
25
|
|
|
18
|
-
const postLikeMethods = ['PUT', 'POST', 'DELETE'
|
|
19
|
-
export const generateMethod =
|
|
26
|
+
const postLikeMethods = ['PUT', 'POST', 'DELETE', 'PATCH']
|
|
27
|
+
export const generateMethod =
|
|
28
|
+
<COPT extends NonNullable<unknown>, GDataShape extends object = any>(
|
|
29
|
+
ajax: AjaxInstance<COPT>,
|
|
30
|
+
method: AxiosMethods
|
|
31
|
+
): RequestCreator<COPT, GDataShape> =>
|
|
20
32
|
/**
|
|
21
33
|
*
|
|
22
34
|
* @param url 完整的请求url链接
|
|
@@ -24,107 +36,95 @@ export const generateMethod = <COPT extends NonNullable<unknown>, GDataShape ext
|
|
|
24
36
|
* @returns 返回一个发送请求的函数
|
|
25
37
|
*/
|
|
26
38
|
<DataType = GDataShape>(url: string, options?: Partial<AjaxOption & COPT>) =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const config = {
|
|
38
|
-
...mergedOptions,
|
|
39
|
-
params: isPostLikeMethod ? undefined : params,
|
|
40
|
-
data: isPostLikeMethod ? params : undefined,
|
|
41
|
-
url,
|
|
42
|
-
method
|
|
43
|
-
} as any
|
|
39
|
+
/**
|
|
40
|
+
* 发送请求
|
|
41
|
+
* @param params get类请求的url参数 or post类请求的请求体数据
|
|
42
|
+
* @param opts ajax参数,拥有最高优先级
|
|
43
|
+
* @returns 请求数据
|
|
44
|
+
*/
|
|
45
|
+
async (params?: object | null, opts?: Partial<AjaxOption & COPT>) => {
|
|
46
|
+
const mergedOptions = { ...options, ...opts }
|
|
47
|
+
const isPostLikeMethod = postLikeMethods.includes(method)
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
const config = {
|
|
50
|
+
...mergedOptions,
|
|
51
|
+
params: isPostLikeMethod ? undefined : params,
|
|
52
|
+
data: isPostLikeMethod ? params : undefined,
|
|
53
|
+
url,
|
|
54
|
+
method,
|
|
55
|
+
} as any
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
const mock = finalOpts.mock
|
|
49
|
-
if (mock) {
|
|
50
|
-
return Promise.resolve(mock)
|
|
51
|
-
}
|
|
57
|
+
const finalOpts = normalizeConfig(ajax, config)
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
+
// mock
|
|
60
|
+
const mock = finalOpts.mock
|
|
61
|
+
if (mock) {
|
|
62
|
+
return Promise.resolve(mock)
|
|
63
|
+
}
|
|
59
64
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
65
|
+
// 完全相同的请求,且都处于pending状态,复用上一个
|
|
66
|
+
const hashKey = generateRequestHash(finalOpts)
|
|
67
|
+
if (checkIfHasPendingRequest(hashKey)) {
|
|
68
|
+
// 重复,且处于pending状态的请求,复用
|
|
69
|
+
return getCachedRequest(hashKey)
|
|
70
|
+
}
|
|
67
71
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
// 这个检查应该位于前面复用请求的逻辑后面,不能乱序
|
|
73
|
+
// 如果需要取消之前处于pending状态相同url请求
|
|
74
|
+
if (finalOpts.cancelLastSameUrl) {
|
|
75
|
+
const currUrl = finalOpts.url
|
|
76
|
+
// 执行所有缓存的url相关的取消函数
|
|
77
|
+
execCanceler(currUrl)
|
|
78
|
+
}
|
|
73
79
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
controller.abort.call(controller)
|
|
80
|
-
// 删除挂载在ajax上的请求中断函数
|
|
81
|
-
deleteCancellerByHash(mergedOptions.cancelKey as string, hashKey)
|
|
82
|
-
}
|
|
83
|
-
)
|
|
84
|
-
}
|
|
80
|
+
// 缓存当前请求的取消函数
|
|
81
|
+
if (typeof AbortController !== 'undefined') {
|
|
82
|
+
// 环境支持AbortController
|
|
83
|
+
const controller = new AbortController()
|
|
84
|
+
finalOpts.signal = controller.signal
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
clearCancelerByHash(finalOpts.url, hashKey)
|
|
94
|
-
}
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
// 环境如果不支持AbortController,取消函数挂载到ajax只能交给adapter里面去实现了
|
|
99
|
-
finalOpts.signal = undefined
|
|
100
|
-
// 因为异步的关系(axios中发出请求之前,对拦截函数处理可能是异步的),逻辑可能还没走到adapter中去,取消函数就已经被调用了
|
|
101
|
-
// 此时取消函数还是undefined,所以这里提供默认取消函数
|
|
86
|
+
if (typeof mergedOptions.cancelKey !== 'undefined') {
|
|
87
|
+
addCanceller(mergedOptions.cancelKey, hashKey, () => {
|
|
88
|
+
controller.abort.call(controller)
|
|
89
|
+
// 删除挂载在ajax上的请求中断函数
|
|
90
|
+
deleteCancellerByHash(mergedOptions.cancelKey as string, hashKey)
|
|
91
|
+
})
|
|
92
|
+
}
|
|
102
93
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
addCalledCanceller(mergedOptions.cancelKey as string, hashKey, true)
|
|
110
|
-
deleteCancellerByHash(mergedOptions.cancelKey as string, hashKey)
|
|
111
|
-
}
|
|
112
|
-
)
|
|
113
|
-
}
|
|
94
|
+
// 缓存当前请求的取消函数,假如后续有相同的url请求需要取消该请求,可以通过这里缓存的函数实现(只针对get请求)
|
|
95
|
+
if (finalOpts.cancelLastSameUrl && finalOpts.method.toLocaleLowerCase() === 'get') {
|
|
96
|
+
addRequestCanceler(finalOpts.url, hashKey, () => {
|
|
97
|
+
controller.abort.call(controller)
|
|
98
|
+
clearCancelerByHash(finalOpts.url, hashKey)
|
|
99
|
+
})
|
|
114
100
|
}
|
|
101
|
+
} else {
|
|
102
|
+
// 环境如果不支持AbortController,取消函数挂载到ajax只能交给adapter里面去实现了
|
|
103
|
+
finalOpts.signal = undefined
|
|
104
|
+
// 因为异步的关系(axios中发出请求之前,对拦截函数处理可能是异步的),逻辑可能还没走到adapter中去,取消函数就已经被调用了
|
|
105
|
+
// 此时取消函数还是undefined,所以这里提供默认取消函数
|
|
115
106
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
} finally {
|
|
123
|
-
// 请求成功,删除ajax.canceller上对应的取消函数
|
|
124
|
-
deleteCancellerByHash(finalOpts.cancelKey, hashKey)
|
|
125
|
-
// 删除用于取消相同URL请求功能而缓存的取消函数
|
|
126
|
-
clearCancelerByHash(finalOpts.url, hashKey)
|
|
127
|
-
// 删除缓存的请求(用于复用finalOpts.reUseDuration内完全相同的多个请求)
|
|
128
|
-
clearPendingRequest(hashKey)
|
|
107
|
+
if (typeof mergedOptions.cancelKey !== 'undefined') {
|
|
108
|
+
addCanceller(mergedOptions.cancelKey, hashKey, () => {
|
|
109
|
+
// 默认函数将已经调用的取消函数名称记录下来,在adapter中走到发送请求的逻辑时,直接中断即可
|
|
110
|
+
addCalledCanceller(mergedOptions.cancelKey as string, hashKey, true)
|
|
111
|
+
deleteCancellerByHash(mergedOptions.cancelKey as string, hashKey)
|
|
112
|
+
})
|
|
129
113
|
}
|
|
130
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
|
+
}
|
|
130
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -25,7 +25,8 @@ export type ProxyedAttrs = keyof Omit<AxiosInstance, RequestMethods>
|
|
|
25
25
|
export type CancellerType = {
|
|
26
26
|
[K: string]: () => void | undefined
|
|
27
27
|
}
|
|
28
|
-
export interface AjaxInstance<CusOpt extends NonNullable<unknown> = any, DataShape extends object = any>
|
|
28
|
+
export interface AjaxInstance<CusOpt extends NonNullable<unknown> = any, DataShape extends object = any>
|
|
29
|
+
extends Pick<AxiosInstance, ProxyedAttrs> {
|
|
29
30
|
/** @deprecated 即将废弃,请使用ajax.get方法替换 */
|
|
30
31
|
query: CustomAjaxMethods<CusOpt, DataShape>
|
|
31
32
|
/** @deprecated 即将废弃,请使用ajax.post方法替换 */
|
package/src/utils.ts
CHANGED
|
@@ -1,63 +1,59 @@
|
|
|
1
|
-
import
|
|
1
|
+
import hash from 'object-hash'
|
|
2
2
|
|
|
3
3
|
export function isAbsoluteURL(url: string) {
|
|
4
4
|
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
|
|
5
5
|
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
|
|
6
6
|
// by any combination of letters, digits, plus, period, or hyphen.
|
|
7
|
-
return /^([a-z][a-z\d+-.]*:)?\/\//i.test(url)
|
|
7
|
+
return /^([a-z][a-z\d+-.]*:)?\/\//i.test(url)
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export function combineURLs(baseURL: string, relativeURL?: string) {
|
|
11
|
-
return relativeURL
|
|
12
|
-
? `${baseURL.replace(/\/+$/, '')}/${relativeURL.replace(/^\/+/, '')}`
|
|
13
|
-
: baseURL;
|
|
11
|
+
return relativeURL ? `${baseURL.replace(/\/+$/, '')}/${relativeURL.replace(/^\/+/, '')}` : baseURL
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
export function buildFullPath(baseURL: string, requestedURL: string): string {
|
|
17
15
|
if (baseURL && !isAbsoluteURL(requestedURL)) {
|
|
18
|
-
return combineURLs(baseURL, requestedURL)
|
|
16
|
+
return combineURLs(baseURL, requestedURL)
|
|
19
17
|
}
|
|
20
|
-
return requestedURL
|
|
18
|
+
return requestedURL
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
type DeepCopy<T> = T extends object
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
: T
|
|
22
|
+
? T extends Array<infer Item>
|
|
23
|
+
? DeepCopy<Item>[]
|
|
24
|
+
: {
|
|
25
|
+
[K in keyof T]: T[K] extends object ? DeepCopy<T[K]> : T[K]
|
|
26
|
+
}
|
|
27
|
+
: T
|
|
32
28
|
|
|
33
29
|
export function deepCopy<T>(obj: T): DeepCopy<T> {
|
|
34
|
-
let target: any
|
|
30
|
+
let target: any
|
|
35
31
|
if (typeof obj === 'object' && obj !== null) {
|
|
36
32
|
if (Array.isArray(obj)) {
|
|
37
|
-
target = []
|
|
38
|
-
obj.forEach((elem) => target.push(deepCopy(elem)))
|
|
33
|
+
target = []
|
|
34
|
+
obj.forEach((elem) => target.push(deepCopy(elem)))
|
|
39
35
|
} else {
|
|
40
|
-
target = {}
|
|
36
|
+
target = {}
|
|
41
37
|
Object.keys(obj).forEach((key) => {
|
|
42
|
-
target[key] = deepCopy((obj as Record<string, any>)[key])
|
|
43
|
-
})
|
|
38
|
+
target[key] = deepCopy((obj as Record<string, any>)[key])
|
|
39
|
+
})
|
|
44
40
|
}
|
|
45
41
|
} else {
|
|
46
|
-
target = obj
|
|
42
|
+
target = obj
|
|
47
43
|
}
|
|
48
|
-
return target
|
|
44
|
+
return target
|
|
49
45
|
}
|
|
50
46
|
|
|
51
47
|
export function getType(obj: any): string {
|
|
52
|
-
const type = typeof obj
|
|
53
|
-
if (type !== 'object') return type
|
|
48
|
+
const type = typeof obj
|
|
49
|
+
if (type !== 'object') return type
|
|
54
50
|
|
|
55
|
-
return Object.prototype.toString
|
|
51
|
+
return Object.prototype.toString
|
|
52
|
+
.call(obj)
|
|
56
53
|
.replace(/^\[object\s+(\S+)\]$/, '$1')
|
|
57
|
-
.toLowerCase()
|
|
54
|
+
.toLowerCase()
|
|
58
55
|
}
|
|
59
56
|
|
|
60
|
-
|
|
61
57
|
type NormalizedConfig = {
|
|
62
58
|
params?: Record<string, any>
|
|
63
59
|
data?: Record<string, any>
|
|
@@ -73,12 +69,11 @@ type NormalizedConfig = {
|
|
|
73
69
|
*/
|
|
74
70
|
export function generateRequestHash(config: NormalizedConfig) {
|
|
75
71
|
const { url = '', method = '', params = {}, data = {}, baseURL = '' } = config
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return hash
|
|
72
|
+
return hash({
|
|
73
|
+
...params,
|
|
74
|
+
...data,
|
|
75
|
+
baseURL,
|
|
76
|
+
url,
|
|
77
|
+
method,
|
|
78
|
+
})
|
|
84
79
|
}
|
package/src/global.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
declare module 'hash-sum'
|