@lx-frontend/multi-ajax 0.1.1 → 0.2.0
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/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 +81 -0
- package/lib/adapters/wx-adapter.js.map +1 -0
- package/lib/cancelLastSameRequest.d.ts +9 -0
- package/lib/cancelLastSameRequest.js +48 -0
- package/lib/cancelLastSameRequest.js.map +1 -0
- package/lib/canceller.d.ts +2 -0
- package/lib/canceller.js +8 -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 +28 -0
- package/lib/duplicateRequestPrevent.js.map +1 -0
- package/lib/{types/helpers.d.ts → helpers.d.ts} +2 -2
- package/lib/helpers.js +42 -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} +6 -5
- 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 +66 -0
- package/lib/utils.js.map +1 -0
- package/package.json +14 -22
- package/src/adapters/wx-adapter.ts +35 -10
- package/src/cancelLastSameRequest.ts +43 -0
- package/src/canceller.ts +2 -2
- package/src/compatibleMethods.ts +24 -6
- package/src/duplicateRequestPrevent.ts +23 -0
- package/src/helpers.ts +3 -3
- package/src/index.ts +4 -3
- package/src/methods.ts +80 -37
- package/src/types.ts +7 -6
- package/src/utils.ts +28 -6
- package/tests/duplicateRequestPrevent.test.ts +39 -0
- package/tests/helpers.test.ts +73 -0
- package/tests/index.test.ts +657 -0
- package/tests/jest-setup.ts +4 -0
- package/tests/utils.test.ts +68 -0
- package/tsconfig.json +2 -1
- 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/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/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,18 @@ 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
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
export type RequestMethods = 'get' | 'post' | 'patch' | 'put' | 'delete' | 'head'
|
|
18
|
+
export type RequestMethods = 'get' | 'post' | 'patch' | 'put' | 'delete' | 'head'
|
|
18
19
|
|
|
19
20
|
export type ProxyedAttrs = 'request' | 'interceptors'
|
|
20
21
|
|
|
21
22
|
export type CancellerType = {
|
|
22
23
|
[K: string]: () => void
|
|
23
24
|
}
|
|
24
|
-
export interface AjaxInstance<CusOpt = {}, DataShape = any> extends Pick<AxiosInstance, ProxyedAttrs> {
|
|
25
|
+
export interface AjaxInstance<CusOpt extends Object = {}, DataShape extends Object = any> extends Pick<AxiosInstance, ProxyedAttrs> {
|
|
25
26
|
/** @deprecated 即将废弃,请使用ajax.get方法替换 */
|
|
26
27
|
query: CustomAjaxMethods<CusOpt, DataShape>
|
|
27
28
|
/** @deprecated 即将废弃,请使用ajax.post方法替换 */
|
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,15 +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) => {
|
|
42
|
+
// @ts-ignore
|
|
41
43
|
target[key] = deepCopy(obj[key]);
|
|
42
44
|
});
|
|
43
45
|
}
|
|
@@ -56,7 +58,27 @@ export function getType(obj: any): string {
|
|
|
56
58
|
.toLowerCase();
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
|
|
62
|
+
type NormalizedConfig = {
|
|
63
|
+
params?: Record<string, any>
|
|
64
|
+
data?: Record<string, any>
|
|
65
|
+
url?: string
|
|
66
|
+
baseURL?: string
|
|
67
|
+
method?: string
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 获取请求的标识,相同的请求配置有相同的标识
|
|
72
|
+
* @param config 经过标准化的ajax请求配置
|
|
73
|
+
* @returns 该请求的hash key
|
|
74
|
+
*/
|
|
75
|
+
export function generateRequestHash(config: NormalizedConfig) {
|
|
76
|
+
const { url = '', method = '', params = {}, data = {}, baseURL = '' } = config
|
|
77
|
+
return hash({
|
|
78
|
+
...params,
|
|
79
|
+
...data,
|
|
80
|
+
baseURL,
|
|
81
|
+
url,
|
|
82
|
+
method
|
|
83
|
+
})
|
|
62
84
|
}
|
|
@@ -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}))
|
|
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}))
|
|
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 from '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
|
+
})
|