@lx-frontend/multi-ajax 0.2.9-beta.1 → 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.
Files changed (47) hide show
  1. package/README.md +80 -33
  2. package/__mocks__/axios.ts +16 -14
  3. package/jest.config.ts +3 -12
  4. package/lib/adapters/index.d.ts +2 -2
  5. package/lib/adapters/index.js +6 -6
  6. package/lib/adapters/index.js.map +1 -1
  7. package/lib/adapters/wx-adapter.d.ts +6 -6
  8. package/lib/adapters/wx-adapter.js +81 -81
  9. package/lib/adapters/wx-adapter.js.map +1 -1
  10. package/lib/cancelLastSameRequest.d.ts +8 -8
  11. package/lib/cancelLastSameRequest.js +44 -44
  12. package/lib/cancelLastSameRequest.js.map +1 -1
  13. package/lib/canceller.d.ts +11 -11
  14. package/lib/canceller.js +62 -62
  15. package/lib/canceller.js.map +1 -1
  16. package/lib/compatibleMethods.d.ts +6 -6
  17. package/lib/compatibleMethods.js +62 -61
  18. package/lib/compatibleMethods.js.map +1 -1
  19. package/lib/duplicateRequestPrevent.d.ts +5 -5
  20. package/lib/duplicateRequestPrevent.js +25 -25
  21. package/lib/duplicateRequestPrevent.js.map +1 -1
  22. package/lib/helpers.d.ts +12 -12
  23. package/lib/helpers.js +62 -62
  24. package/lib/helpers.js.map +1 -1
  25. package/lib/index.d.ts +2 -2
  26. package/lib/index.js +33 -33
  27. package/lib/index.js.map +1 -1
  28. package/lib/methods.d.ts +7 -7
  29. package/lib/methods.js +106 -106
  30. package/lib/methods.js.map +1 -1
  31. package/lib/types.d.ts +45 -45
  32. package/lib/types.js +2 -2
  33. package/lib/utils.d.ts +22 -22
  34. package/lib/utils.js +63 -64
  35. package/lib/utils.js.map +1 -1
  36. package/package.json +10 -10
  37. package/src/adapters/index.ts +2 -4
  38. package/src/adapters/wx-adapter.ts +43 -65
  39. package/src/cancelLastSameRequest.ts +2 -3
  40. package/src/canceller.ts +2 -3
  41. package/src/compatibleMethods.ts +47 -33
  42. package/src/duplicateRequestPrevent.ts +1 -1
  43. package/src/helpers.ts +29 -26
  44. package/src/index.ts +13 -11
  45. package/src/methods.ts +96 -96
  46. package/src/types.ts +2 -1
  47. package/src/utils.ts +24 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lx-frontend/multi-ajax",
3
- "version": "0.2.9-beta.1",
3
+ "version": "0.2.9",
4
4
  "description": "一个规范Ajax使用的框架",
5
5
  "author": "lichao.franklee <lichao.franklee@gmail.com>",
6
6
  "homepage": "",
@@ -11,14 +11,6 @@
11
11
  "type": "git",
12
12
  "url": "git@gitlab.lixinio.com:frontend/lx-basic.git"
13
13
  },
14
- "scripts": {
15
- "build": "pnpm clean && tsc",
16
- "build:test": "npm run build -- --sourceMap false --inlineSourceMap true",
17
- "clean": "rimraf lib",
18
- "postinstall": "node build/scripts/patch-axios-post-install.js",
19
- "clean:coverage": "rimraf coverage",
20
- "test": "npm run clean:coverage && jest"
21
- },
22
14
  "dependencies": {
23
15
  "axios": "1.3.4",
24
16
  "object-hash": "^3.0.0",
@@ -32,5 +24,13 @@
32
24
  "jest": "^29.5.0",
33
25
  "rimraf": "^3.0.2",
34
26
  "typescript": "^4.9.5"
27
+ },
28
+ "scripts": {
29
+ "build": "pnpm clean && tsc",
30
+ "build:test": "npm run build -- --sourceMap false --inlineSourceMap true",
31
+ "clean": "rimraf lib",
32
+ "postinstall": "node build/scripts/patch-axios-post-install.js",
33
+ "clean:coverage": "rimraf coverage",
34
+ "test": "npm run clean:coverage && jest"
35
35
  }
36
- }
36
+ }
@@ -1,5 +1,3 @@
1
- import wxAdatper from './wx-adapter';
1
+ import wxAdatper from './wx-adapter'
2
2
 
3
- export {
4
- wxAdatper,
5
- };
3
+ export { wxAdatper }
@@ -1,76 +1,62 @@
1
- import { InternalAxiosRequestConfig, AxiosPromise, AxiosError } from 'axios';
2
- import { buildFullPath, deepCopy, generateRequestHash } from '../utils';
1
+ import { InternalAxiosRequestConfig, AxiosPromise, AxiosError } from 'axios'
2
+ import { buildFullPath, deepCopy, generateRequestHash } from '../utils'
3
3
  import { addCanceller, deleteCalledCancellerByHash, deleteCancellerByHash, hasCalledCancelerRecord } from '../canceller'
4
4
  import { addRequestCanceler, clearCancelerByHash } from '../cancelLastSameRequest'
5
5
 
6
- declare const wx: any;
6
+ declare const wx: any
7
7
 
8
8
  type WxErr = {
9
- errMsg: string;
10
- errno: string;
9
+ errMsg: string
10
+ errno: string
11
11
  }
12
12
 
13
13
  export default function wxAdapter(config: InternalAxiosRequestConfig): AxiosPromise {
14
14
  // adapter接受的config要完全符合axios的配置规范,然后在这里针对不同的引擎做个性化修改
15
15
  return new Promise((resolve, reject) => {
16
- let requestConfig = {} as any;
16
+ let requestConfig = {} as any
17
17
  let isAborted = false
18
18
  let requestTask: any = null
19
- const {
20
- headers,
21
- data,
22
- params,
23
- url,
24
- timeout,
25
- method,
26
- ...rest
27
- } = config;
19
+ const { headers, data, params, url, timeout, method, ...rest } = config
28
20
 
29
21
  const requestHash = generateRequestHash(config)
30
22
 
31
- const requestHeader = deepCopy(headers);
32
- delete requestHeader.Referer; // 小程序文档,header中不能有Referer
33
- requestConfig.header = requestHeader;
23
+ const requestHeader = deepCopy(headers)
24
+ delete requestHeader.Referer // 小程序文档,header中不能有Referer
25
+ requestConfig.header = requestHeader
34
26
 
35
- const requestData = data || params; // wx环境,不论get还是post,参数传递的属性名均为data
36
- requestConfig.data = deepCopy(requestData);
37
- requestConfig.url = buildFullPath(config.baseURL || '', url || '');
38
- requestConfig.timeout = timeout;
39
- requestConfig.method = method?.toUpperCase();
40
- requestConfig = { ...requestConfig, ...rest };
27
+ const requestData = data || params // wx环境,不论get还是post,参数传递的属性名均为data
28
+ requestConfig.data = deepCopy(requestData)
29
+ requestConfig.url = buildFullPath(config.baseURL || '', url || '')
30
+ requestConfig.timeout = timeout
31
+ requestConfig.method = method?.toUpperCase()
32
+ requestConfig = { ...requestConfig, ...rest }
41
33
 
42
34
  requestConfig.success = (res: any) => {
43
- let response = {} as any;
44
- const {
45
- data: resData,
46
- statusCode,
47
- header,
48
- ...restRes
49
- } = res;
50
- response.data = resData;
51
- response.status = statusCode;
52
- response.headers = header;
53
- response.config = config;
54
- response = { ...response, ...restRes };
35
+ let response = {} as any
36
+ const { data: resData, statusCode, header, ...restRes } = res
37
+ response.data = resData
38
+ response.status = statusCode
39
+ response.headers = header
40
+ response.config = config
41
+ response = { ...response, ...restRes }
55
42
 
56
- const { validateStatus } = response.config;
43
+ const { validateStatus } = response.config
57
44
 
58
45
  if (!response.status || !validateStatus || validateStatus(response.status)) {
59
- resolve(response);
46
+ resolve(response)
60
47
  } else {
61
48
  const errMsg = `Request failed with status code ${response.status}`
62
- reject(new AxiosError(errMsg, '', config, requestTask, response));
49
+ reject(new AxiosError(errMsg, '', config, requestTask, response))
63
50
  }
64
- };
51
+ }
65
52
 
66
53
  requestConfig.fail = (err: WxErr) => {
67
-
68
54
  if (isAborted) {
69
55
  reject(new AxiosError(err.errMsg, AxiosError.ERR_CANCELED, config, requestTask))
70
56
  }
71
57
 
72
- reject(new AxiosError(err.errMsg, err.errno, config, requestTask));
73
- };
58
+ reject(new AxiosError(err.errMsg, err.errno, config, requestTask))
59
+ }
74
60
 
75
61
  // 判断是否已经调用了取消函数
76
62
  if (hasCalledCancelerRecord(requestConfig.cancelKey, requestHash)) {
@@ -80,38 +66,30 @@ export default function wxAdapter(config: InternalAxiosRequestConfig): AxiosProm
80
66
  deleteCalledCancellerByHash(requestConfig.cancelKey, requestHash)
81
67
  return
82
68
  }
83
- requestTask = wx.request(requestConfig);
69
+ requestTask = wx.request(requestConfig)
84
70
 
85
71
  // 手动取消请求功能
86
72
  // 设置了cancelKey,但是环境不支持AbortController
87
73
  if ('undefined' === typeof AbortController) {
88
74
  if (requestConfig.cancelKey) {
89
- addCanceller(
90
- requestConfig.cancelKey,
91
- requestHash,
92
- () => {
93
- isAborted = true
94
- requestTask.abort.call(requestTask)
95
- deleteCancellerByHash(requestConfig.cancelKey, requestHash)
96
- reject(new AxiosError('请求被取消!', AxiosError.ERR_CANCELED, config, requestTask))
97
- }
98
- )
75
+ addCanceller(requestConfig.cancelKey, requestHash, () => {
76
+ isAborted = true
77
+ requestTask.abort.call(requestTask)
78
+ deleteCancellerByHash(requestConfig.cancelKey, requestHash)
79
+ reject(new AxiosError('请求被取消!', AxiosError.ERR_CANCELED, config, requestTask))
80
+ })
99
81
  }
100
82
 
101
83
  // 取消上一个相同URL的get请求功能
102
84
  if (requestConfig.cancelLastSameUrl && requestConfig.method.toLocaleLowerCase() === 'get') {
103
- addRequestCanceler(
104
- config.url as string,
105
- requestHash,
106
- () => {
107
- isAborted = true
108
- requestTask.abort.call(requestTask)
109
- clearCancelerByHash(config.url as string, requestHash)
110
- reject(new AxiosError('请求被取消!', AxiosError.ERR_CANCELED, config, requestTask))
111
- }
112
- )
85
+ addRequestCanceler(config.url as string, requestHash, () => {
86
+ isAborted = true
87
+ requestTask.abort.call(requestTask)
88
+ clearCancelerByHash(config.url as string, requestHash)
89
+ reject(new AxiosError('请求被取消!', AxiosError.ERR_CANCELED, config, requestTask))
90
+ })
113
91
  }
114
92
  }
115
- });
93
+ })
116
94
  }
117
95
  wxAdapter.type = 'wx'
@@ -3,7 +3,7 @@
3
3
  * 且只针对get请求
4
4
  */
5
5
  // import hash from 'object-hash'
6
- export { generateRequestHash } from "./utils"
6
+ export { generateRequestHash } from './utils'
7
7
 
8
8
  const cancellerCache: Record<string, Record<string, any>> = {}
9
9
 
@@ -22,7 +22,6 @@ export function clearCancelerByHash(url: string, hash: string) {
22
22
  }
23
23
  }
24
24
 
25
-
26
25
  // export function checkIfHasURLCanceler(url: string): boolean {
27
26
  // if (!cancellerCache[url]) return false
28
27
  // if (Object.keys(cancellerCache[url]).length === 0) return false
@@ -40,4 +39,4 @@ export function execCanceler(url: string) {
40
39
 
41
40
  // export function getCachedCanceler(hashKey: string) {
42
41
  // return cancellerCache[hashKey]
43
- // }
42
+ // }
package/src/canceller.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { generateRequestHash } from "./utils"
1
+ export { generateRequestHash } from './utils'
2
2
 
3
3
  type CancelRecord = Record<string, Record<string, any>>
4
4
 
@@ -22,7 +22,6 @@ function deleteRecordByHash(record: CancelRecord, cancelKey: string, hash: strin
22
22
  record[cancelKey] = cancelObj
23
23
  }
24
24
 
25
-
26
25
  export function addCanceller(cancelKey: string, hash: string, cancelFn: any) {
27
26
  addRecord(canceller, cancelKey, hash, cancelFn)
28
27
  }
@@ -58,4 +57,4 @@ export function deleteCalledCancellerByHash(cancelKey: string, hash: string) {
58
57
 
59
58
  export function hasCalledCancelerRecord(cancelKey: string, hash: string) {
60
59
  return cancellerCalled[cancelKey] && cancellerCalled[cancelKey][hash]
61
- }
60
+ }
@@ -1,15 +1,21 @@
1
1
  // 兼容以前的老项目写法
2
2
 
3
- import { addPendingRequest, checkIfHasPendingRequest, clearPendingRequest, getCachedRequest } from './duplicateRequestPrevent'
3
+ import {
4
+ addPendingRequest,
5
+ checkIfHasPendingRequest,
6
+ clearPendingRequest,
7
+ getCachedRequest,
8
+ } from './duplicateRequestPrevent'
4
9
  import { normalizeConfig } from './helpers'
5
10
  import { generateRequestHash } from './utils'
6
- import type {
7
- AjaxInstance, AjaxOption, AnyObj, AxiosMethods
8
- } from './types'
11
+ import type { AjaxInstance, AjaxOption, AnyObj, AxiosMethods } from './types'
9
12
 
10
13
  export type OldAjaxMethods = 'query' | 'create' | 'deletes' | 'putWay' | 'patchWay'
11
14
 
12
- export type CustomAjaxMethods<T extends object, GDataShape extends AnyObj = any> = <DataType = GDataShape>(path: string, config?: Partial<AjaxOption & T>) => (data?: any, expend?: string, config?: AjaxOption) => Promise<DataType>
15
+ export type CustomAjaxMethods<T extends object, GDataShape extends AnyObj = any> = <DataType = GDataShape>(
16
+ path: string,
17
+ config?: Partial<AjaxOption & T>
18
+ ) => (data?: any, expend?: string, config?: AjaxOption) => Promise<DataType>
13
19
 
14
20
  type MethodMap = {
15
21
  [k in OldAjaxMethods]: AxiosMethods
@@ -20,38 +26,46 @@ const methodMap: MethodMap = {
20
26
  create: 'POST',
21
27
  putWay: 'PUT',
22
28
  deletes: 'DELETE',
23
- patchWay: 'PATCH'
29
+ patchWay: 'PATCH',
24
30
  }
25
31
 
26
- export const generateCustomMethod = <T extends object>(ctx: AjaxInstance<T>, name: OldAjaxMethods): CustomAjaxMethods<T> => <DataType = any>(path: string, config: Partial<AjaxOption | T> = {}) => async (data?: any, expand?: string, _config?: AjaxOption) => {
27
- const url = expand ? `${path}/${expand}` : path;
32
+ export const generateCustomMethod =
33
+ <T extends object>(ctx: AjaxInstance<T>, name: OldAjaxMethods): CustomAjaxMethods<T> =>
34
+ <DataType = any>(path: string, config: Partial<AjaxOption | T> = {}) =>
35
+ async (data?: any, expand?: string, _config?: AjaxOption) => {
36
+ const url = expand ? `${path}/${expand}` : path
28
37
  if (expand) {
29
- // eslint-disable-next-line no-console
30
- console.warn(`业务代码中拼接url发送请求的方式即将废弃,请使用ajax.${methodMap[name].toLocaleLowerCase()}接口替换。相关url:${url}`)
38
+ console.warn(
39
+ `业务代码中拼接url发送请求的方式即将废弃,请使用ajax.${methodMap[name].toLocaleLowerCase()}接口替换。相关url:${url}`
40
+ )
31
41
  }
32
42
  const [newParams, newData] = methodMap[name] === 'GET' ? [data, undefined] : [undefined, data]
33
43
  const config1 = normalizeConfig(ctx, {
34
- ...config, ...(_config ?? {}), url, params: newParams, data: newData, method: methodMap[name],
35
- });
44
+ ...config, ...(_config ?? {}),
45
+ url,
46
+ params: newParams,
47
+ data: newData,
48
+ method: methodMap[name],
49
+ })
36
50
 
37
- const options = {
38
- ...config1,
39
- custom: {
40
- ...ctx.$options,
51
+ const options = {
41
52
  ...config1,
42
- },
43
- };
53
+ custom: {
54
+ ...ctx.$options,
55
+ ...config1,
56
+ },
57
+ }
44
58
 
45
- const hashKey = generateRequestHash(config1)
46
- if (checkIfHasPendingRequest(hashKey)) {
47
- // 重复,且处于pending状态的请求,复用
48
- return getCachedRequest(hashKey)
49
- }
59
+ const hashKey = generateRequestHash(config1)
60
+ if (checkIfHasPendingRequest(hashKey)) {
61
+ // 重复,且处于pending状态的请求,复用
62
+ return getCachedRequest(hashKey)
63
+ }
50
64
 
51
- const request = ctx.request<any, DataType>(options);
65
+ const request = ctx.request<any, DataType>(options)
52
66
 
53
- // 缓存请求
54
- addPendingRequest(hashKey, request, options.reUseDuration)
67
+ // 缓存请求
68
+ addPendingRequest(hashKey, request, options.reUseDuration)
55
69
 
56
70
  try {
57
71
  return await request
@@ -59,14 +73,14 @@ export const generateCustomMethod = <T extends object>(ctx: AjaxInstance<T>, nam
59
73
  // 清理缓存的请求
60
74
  clearPendingRequest(hashKey)
61
75
  }
62
- };
76
+ }
63
77
 
64
78
  function addCompatibleMethods<CusOpts extends NonNullable<unknown>>(ctx: AjaxInstance<CusOpts>) {
65
- ctx.query = generateCustomMethod<CusOpts>(ctx, 'query');
66
- ctx.create = generateCustomMethod<CusOpts>(ctx, 'create');
67
- ctx.deletes = generateCustomMethod<CusOpts>(ctx, 'deletes');
68
- ctx.putWay = generateCustomMethod<CusOpts>(ctx, 'putWay');
69
- ctx.patchWay = generateCustomMethod<CusOpts>(ctx, 'patchWay');
79
+ ctx.query = generateCustomMethod<CusOpts>(ctx, 'query')
80
+ ctx.create = generateCustomMethod<CusOpts>(ctx, 'create')
81
+ ctx.deletes = generateCustomMethod<CusOpts>(ctx, 'deletes')
82
+ ctx.putWay = generateCustomMethod<CusOpts>(ctx, 'putWay')
83
+ ctx.patchWay = generateCustomMethod<CusOpts>(ctx, 'patchWay')
70
84
  }
71
85
 
72
- export default addCompatibleMethods;
86
+ export default addCompatibleMethods
@@ -1,4 +1,4 @@
1
- export { generateRequestHash } from "./utils"
1
+ export { generateRequestHash } from './utils'
2
2
 
3
3
  const pendingRequest: Record<string, any> = {}
4
4
 
package/src/helpers.ts CHANGED
@@ -17,14 +17,14 @@ export function normalizeConfig<CusOpt extends object>(ctx: AjaxInstance<CusOpt>
17
17
  baseURL = '',
18
18
  reUseDuration = 300,
19
19
  ...rest
20
- } = { ...ctx.$options, ...config };
20
+ } = { ...ctx.$options, ...config }
21
21
  // 数据格式为FormData,修改content-type头,以及修改data格式
22
- const contentType = headers['content-type'] || headers['Content-Type'] || 'application/json';
23
- headers['content-type'] = sendFormData ? 'application/x-www-form-urlencoded' : contentType;
24
- const sendData = sendFormData && typeof data === 'object' && data !== null ? qs.stringify(data) : data;
22
+ const contentType = headers['content-type'] || headers['Content-Type'] || 'application/json'
23
+ headers['content-type'] = sendFormData ? 'application/x-www-form-urlencoded' : contentType
24
+ const sendData = sendFormData && typeof data === 'object' && data !== null ? qs.stringify(data) : data
25
25
 
26
26
  // baseUrl,支持两种写法,且用户配置的baseUrl优先级最高
27
- const bUrl = baseUrl || baseURL || ctx.$baseUrl;
27
+ const bUrl = baseUrl || baseURL || ctx.$baseUrl
28
28
 
29
29
  if (typeof reUseDuration !== 'number' || Number.isNaN(reUseDuration)) {
30
30
  throw new Error('ajax配置reUseDuration必须是数字')
@@ -37,43 +37,46 @@ export function normalizeConfig<CusOpt extends object>(ctx: AjaxInstance<CusOpt>
37
37
  baseURL: bUrl,
38
38
  reUseDuration,
39
39
  ...rest,
40
- };
40
+ }
41
41
  }
42
42
 
43
43
  export function addInstanceExtendMethod(ctx: any) {
44
44
  const extend = (source: AnyObj) => {
45
45
  Object.keys(source).forEach((key) => {
46
- ctx[key] = source[key];
47
- });
48
- };
49
- ctx.extend = extend;
46
+ ctx[key] = source[key]
47
+ })
48
+ }
49
+ ctx.extend = extend
50
50
  }
51
51
 
52
- export function proxyAxiosInstanceAttrs<S extends {[K in ProxyedAttrs]: any}>(target: AnyObj, source: S): void {
53
- const attrs: ProxyedAttrs[] = ['request', 'interceptors', 'defaults'];
52
+ export function proxyAxiosInstanceAttrs<S extends { [K in ProxyedAttrs]: any }>(target: AnyObj, source: S): void {
53
+ const attrs: ProxyedAttrs[] = ['request', 'interceptors', 'defaults']
54
54
  // const methods: ProxyedAttrs[] = ['request', 'get', 'post', 'delete', 'head', 'options', 'put', 'patch', 'interceptors'];
55
55
 
56
56
  attrs.forEach((method) => {
57
57
  Object.defineProperty(target, method, {
58
58
  value: source[method],
59
59
  writable: false,
60
- });
61
- });
60
+ })
61
+ })
62
62
  }
63
63
 
64
64
  export function proxyAjaxCanceler(ctx: AjaxInstance) {
65
- ctx.canceller = new Proxy({}, {
66
- get(target, prop: string) {
67
- if (!hasCancelers(prop)) {
65
+ ctx.canceller = new Proxy(
66
+ {},
67
+ {
68
+ get(_target, prop: string) {
69
+ if (!hasCancelers(prop)) {
70
+ return () => {
71
+ const str = `ajax.canceller对象上不存在名为${prop}的函数!`
72
+ console.warn(str)
73
+ return str
74
+ }
75
+ }
68
76
  return () => {
69
- const str = `ajax.canceller对象上不存在名为${prop}的函数!`
70
- console.warn(str)
71
- return str
77
+ callCancellerByCancelKey(prop)
72
78
  }
73
- }
74
- return () => {
75
- callCancellerByCancelKey(prop)
76
- }
79
+ },
77
80
  }
78
- })
79
- }
81
+ )
82
+ }
package/src/index.ts CHANGED
@@ -3,22 +3,24 @@ import { wxAdatper } from './adapters/index'
3
3
  import addCompatibleMethods from './compatibleMethods'
4
4
  import { addInstanceExtendMethod, proxyAjaxCanceler, proxyAxiosInstanceAttrs } from './helpers'
5
5
  import { generateMethod } from './methods'
6
- import type {AjaxInstance, AjaxOption, AxiosMethods, RequestMethods} from './types'
6
+ import type { AjaxInstance, AjaxOption, AxiosMethods, RequestMethods } from './types'
7
7
 
8
- declare const wx: any;
8
+ declare const wx: any
9
9
 
10
10
  function isWxapp(): boolean {
11
- return typeof wx !== 'undefined' && typeof wx.request === 'function';
11
+ return typeof wx !== 'undefined' && typeof wx.request === 'function'
12
12
  }
13
13
 
14
- export default function initAjax<CusOpts extends object, ResDataShape extends object = any>(options: AjaxOption & CusOpts): AjaxInstance<CusOpts, ResDataShape> {
15
- const ajax = {} as AjaxInstance<CusOpts, ResDataShape>;
16
- ajax.$options = options;
14
+ export default function initAjax<CusOpts extends object, ResDataShape extends object = any>(
15
+ options: AjaxOption & CusOpts
16
+ ): AjaxInstance<CusOpts, ResDataShape> {
17
+ const ajax = {} as AjaxInstance<CusOpts, ResDataShape>
18
+ ajax.$options = options
17
19
 
18
20
  // 检测wx如果没有自定义的适配器,则使用默认适配器
19
- const adapter = ajax.$options.adapter || (isWxapp() ? wxAdatper : undefined);
21
+ const adapter = ajax.$options.adapter || (isWxapp() ? wxAdatper : undefined)
20
22
  ajax.$options.adapter = adapter
21
- ajax.$engine = axios.create();
23
+ ajax.$engine = axios.create()
22
24
 
23
25
  const requestMethods: AxiosMethods[] = ['POST', 'PUT', 'PATCH', 'DELETE', 'GET', 'HEAD']
24
26
  requestMethods.forEach((name: AxiosMethods) => {
@@ -30,12 +32,12 @@ export default function initAjax<CusOpts extends object, ResDataShape extends ob
30
32
  proxyAjaxCanceler(ajax)
31
33
 
32
34
  // 把ajax实例上的'request', 'interceptors', defaults属性代理到axios实例上去
33
- proxyAxiosInstanceAttrs(ajax, ajax.$engine);
35
+ proxyAxiosInstanceAttrs(ajax, ajax.$engine)
34
36
 
35
37
  addCompatibleMethods(ajax)
36
38
 
37
39
  // 添加extend方法,用于扩展ajax实例
38
- addInstanceExtendMethod(ajax);
40
+ addInstanceExtendMethod(ajax)
39
41
 
40
- return ajax;
42
+ return ajax
41
43
  }