@lx-frontend/multi-ajax 0.2.7 → 0.2.9-beta.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/lib/utils.d.ts CHANGED
@@ -1,22 +1,22 @@
1
- export declare function isAbsoluteURL(url: string): boolean;
2
- export declare function combineURLs(baseURL: string, relativeURL?: string): string;
3
- export declare function buildFullPath(baseURL: string, requestedURL: string): string;
4
- type DeepCopy<T> = T extends object ? T extends Array<infer Item> ? DeepCopy<Item>[] : {
5
- [K in keyof T]: T[K] extends object ? DeepCopy<T[K]> : T[K];
6
- } : T;
7
- export declare function deepCopy<T>(obj: T): DeepCopy<T>;
8
- export declare function getType(obj: any): string;
9
- type NormalizedConfig = {
10
- params?: Record<string, any>;
11
- data?: Record<string, any>;
12
- url?: string;
13
- baseURL?: string;
14
- method?: string;
15
- };
16
- /**
17
- * 获取请求的标识,相同的请求配置有相同的标识
18
- * @param config 经过标准化的ajax请求配置
19
- * @returns 该请求的hash key
20
- */
21
- export declare function generateRequestHash(config: NormalizedConfig): any;
22
- export {};
1
+ export declare function isAbsoluteURL(url: string): boolean;
2
+ export declare function combineURLs(baseURL: string, relativeURL?: string): string;
3
+ export declare function buildFullPath(baseURL: string, requestedURL: string): string;
4
+ type DeepCopy<T> = T extends object ? T extends Array<infer Item> ? DeepCopy<Item>[] : {
5
+ [K in keyof T]: T[K] extends object ? DeepCopy<T[K]> : T[K];
6
+ } : T;
7
+ export declare function deepCopy<T>(obj: T): DeepCopy<T>;
8
+ export declare function getType(obj: any): string;
9
+ type NormalizedConfig = {
10
+ params?: Record<string, any>;
11
+ data?: Record<string, any>;
12
+ url?: string;
13
+ baseURL?: string;
14
+ method?: string;
15
+ };
16
+ /**
17
+ * 获取请求的标识,相同的请求配置有相同的标识
18
+ * @param config 经过标准化的ajax请求配置
19
+ * @returns 该请求的hash key
20
+ */
21
+ export declare function generateRequestHash(config: NormalizedConfig): string;
22
+ export {};
package/lib/utils.js CHANGED
@@ -1,71 +1,65 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateRequestHash = exports.getType = exports.deepCopy = exports.buildFullPath = exports.combineURLs = exports.isAbsoluteURL = void 0;
4
- var tslib_1 = require("tslib");
5
- var crypto = require('crypto');
6
- function isAbsoluteURL(url) {
7
- // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
8
- // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
9
- // by any combination of letters, digits, plus, period, or hyphen.
10
- return /^([a-z][a-z\d+-.]*:)?\/\//i.test(url);
11
- }
12
- exports.isAbsoluteURL = isAbsoluteURL;
13
- function combineURLs(baseURL, relativeURL) {
14
- return relativeURL
15
- ? "".concat(baseURL.replace(/\/+$/, ''), "/").concat(relativeURL.replace(/^\/+/, ''))
16
- : baseURL;
17
- }
18
- exports.combineURLs = combineURLs;
19
- function buildFullPath(baseURL, requestedURL) {
20
- if (baseURL && !isAbsoluteURL(requestedURL)) {
21
- return combineURLs(baseURL, requestedURL);
22
- }
23
- return requestedURL;
24
- }
25
- exports.buildFullPath = buildFullPath;
26
- function deepCopy(obj) {
27
- var target;
28
- if (typeof obj === 'object' && obj !== null) {
29
- if (Array.isArray(obj)) {
30
- target = [];
31
- obj.forEach(function (elem) { return target.push(deepCopy(elem)); });
32
- }
33
- else {
34
- target = {};
35
- Object.keys(obj).forEach(function (key) {
36
- target[key] = deepCopy(obj[key]);
37
- });
38
- }
39
- }
40
- else {
41
- target = obj;
42
- }
43
- return target;
44
- }
45
- exports.deepCopy = deepCopy;
46
- function getType(obj) {
47
- var type = typeof obj;
48
- if (type !== 'object')
49
- return type;
50
- return Object.prototype.toString.call(obj)
51
- .replace(/^\[object\s+(\S+)\]$/, '$1')
52
- .toLowerCase();
53
- }
54
- exports.getType = getType;
55
- /**
56
- * 获取请求的标识,相同的请求配置有相同的标识
57
- * @param config 经过标准化的ajax请求配置
58
- * @returns 该请求的hash key
59
- */
60
- function generateRequestHash(config) {
61
- var _a = config.url, url = _a === void 0 ? '' : _a, _b = config.method, method = _b === void 0 ? '' : _b, _c = config.params, params = _c === void 0 ? {} : _c, _d = config.data, data = _d === void 0 ? {} : _d, _e = config.baseURL, baseURL = _e === void 0 ? '' : _e;
62
- // 合并请求参数
63
- var combinedData = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, params), data), { baseURL: baseURL, url: url, method: method });
64
- // 将对象序列化为字符串
65
- var serializedData = JSON.stringify(combinedData);
66
- // 创建一个 md5 哈希
67
- var hash = crypto.createHash('md5').update(serializedData).digest('hex');
68
- return hash;
69
- }
70
- exports.generateRequestHash = generateRequestHash;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateRequestHash = exports.getType = exports.deepCopy = exports.buildFullPath = exports.combineURLs = exports.isAbsoluteURL = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var object_hash_1 = tslib_1.__importDefault(require("object-hash"));
6
+ function isAbsoluteURL(url) {
7
+ // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
8
+ // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
9
+ // by any combination of letters, digits, plus, period, or hyphen.
10
+ return /^([a-z][a-z\d+-.]*:)?\/\//i.test(url);
11
+ }
12
+ exports.isAbsoluteURL = isAbsoluteURL;
13
+ function combineURLs(baseURL, relativeURL) {
14
+ return relativeURL
15
+ ? "".concat(baseURL.replace(/\/+$/, ''), "/").concat(relativeURL.replace(/^\/+/, ''))
16
+ : baseURL;
17
+ }
18
+ exports.combineURLs = combineURLs;
19
+ function buildFullPath(baseURL, requestedURL) {
20
+ if (baseURL && !isAbsoluteURL(requestedURL)) {
21
+ return combineURLs(baseURL, requestedURL);
22
+ }
23
+ return requestedURL;
24
+ }
25
+ exports.buildFullPath = buildFullPath;
26
+ function deepCopy(obj) {
27
+ var target;
28
+ if (typeof obj === 'object' && obj !== null) {
29
+ if (Array.isArray(obj)) {
30
+ target = [];
31
+ obj.forEach(function (elem) { return target.push(deepCopy(elem)); });
32
+ }
33
+ else {
34
+ target = {};
35
+ Object.keys(obj).forEach(function (key) {
36
+ target[key] = deepCopy(obj[key]);
37
+ });
38
+ }
39
+ }
40
+ else {
41
+ target = obj;
42
+ }
43
+ return target;
44
+ }
45
+ exports.deepCopy = deepCopy;
46
+ function getType(obj) {
47
+ var type = typeof obj;
48
+ if (type !== 'object')
49
+ return type;
50
+ return Object.prototype.toString.call(obj)
51
+ .replace(/^\[object\s+(\S+)\]$/, '$1')
52
+ .toLowerCase();
53
+ }
54
+ exports.getType = getType;
55
+ /**
56
+ * 获取请求的标识,相同的请求配置有相同的标识
57
+ * @param config 经过标准化的ajax请求配置
58
+ * @returns 该请求的hash key
59
+ */
60
+ function generateRequestHash(config) {
61
+ var _a = config.url, url = _a === void 0 ? '' : _a, _b = config.method, method = _b === void 0 ? '' : _b, _c = config.params, params = _c === void 0 ? {} : _c, _d = config.data, data = _d === void 0 ? {} : _d, _e = config.baseURL, baseURL = _e === void 0 ? '' : _e;
62
+ return (0, object_hash_1.default)(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, params), data), { baseURL: baseURL, url: url, method: method }));
63
+ }
64
+ exports.generateRequestHash = generateRequestHash;
71
65
  //# sourceMappingURL=utils.js.map
package/lib/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;AAAA,IAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEhC,SAAgB,aAAa,CAAC,GAAW;IACvC,gGAAgG;IAChG,gGAAgG;IAChG,kEAAkE;IAClE,OAAO,4BAA4B,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChD,CAAC;AALD,sCAKC;AAED,SAAgB,WAAW,CAAC,OAAe,EAAE,WAAoB;IAC/D,OAAO,WAAW;QAChB,CAAC,CAAC,UAAG,OAAO,CAAC,OAAO,CAAC,MAAM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AE,CAAC;AACnB,CAAC;AAPD,0BAOC;AAWD;;;;GAIG;AACH,SAAgB,mBAAmB,CAAC,MAAwB;IAClD,IAAA,KAAgE,MAAM,IAA9D,EAAR,GAAG,mBAAG,EAAE,KAAA,EAAE,KAAsD,MAAM,OAAjD,EAAX,MAAM,mBAAG,EAAE,KAAA,EAAE,KAAyC,MAAM,OAApC,EAAX,MAAM,mBAAG,EAAE,KAAA,EAAE,KAA4B,MAAM,KAAzB,EAAT,IAAI,mBAAG,EAAE,KAAA,EAAE,KAAiB,MAAM,QAAX,EAAZ,OAAO,mBAAG,EAAE,KAAA,CAAW;IAC9E,OAAO,IAAA,qBAAI,yDACN,MAAM,GACN,IAAI,KACP,OAAO,SAAA,EACP,GAAG,KAAA,EACH,MAAM,QAAA,IACN,CAAA;AACJ,CAAC;AATD,kDASC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lx-frontend/multi-ajax",
3
- "version": "0.2.7",
3
+ "version": "0.2.9-beta.1",
4
4
  "description": "一个规范Ajax使用的框架",
5
5
  "author": "lichao.franklee <lichao.franklee@gmail.com>",
6
6
  "homepage": "",
@@ -11,24 +11,26 @@
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
+ },
14
22
  "dependencies": {
15
23
  "axios": "1.3.4",
24
+ "object-hash": "^3.0.0",
16
25
  "qs": "^6.10.1",
17
26
  "resolve": "^1.22.2"
18
27
  },
19
28
  "devDependencies": {
20
29
  "@jest/globals": "^29.5.0",
30
+ "@types/object-hash": "^3.0.2",
21
31
  "@types/qs": "^6.9.7",
22
32
  "jest": "^29.5.0",
23
33
  "rimraf": "^3.0.2",
24
34
  "typescript": "^4.9.5"
25
- },
26
- "scripts": {
27
- "build": "pnpm clean && tsc",
28
- "build:test": "npm run build -- --sourceMap false --inlineSourceMap true",
29
- "clean": "rimraf lib",
30
- "postinstall": "node build/scripts/patch-axios-post-install.js",
31
- "clean:coverage": "rimraf coverage",
32
- "test": "npm run clean:coverage && jest"
33
35
  }
34
- }
36
+ }
@@ -2,6 +2,7 @@
2
2
  * 取消相同URL和method的上一个未结束请求,主要目的是防止页面切换时串数据,比如tab切换
3
3
  * 且只针对get请求
4
4
  */
5
+ // import hash from 'object-hash'
5
6
  export { generateRequestHash } from "./utils"
6
7
 
7
8
  const cancellerCache: Record<string, Record<string, any>> = {}
@@ -9,7 +9,7 @@ import type {
9
9
 
10
10
  export type OldAjaxMethods = 'query' | 'create' | 'deletes' | 'putWay' | 'patchWay'
11
11
 
12
- export type CustomAjaxMethods<T extends object, GDataShape extends AnyObj = any> = <DataType = GDataShape>(path: string, config?: Partial<AjaxOption & T>) => (data?: any, expend?: string) => Promise<DataType>
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>
13
13
 
14
14
  type MethodMap = {
15
15
  [k in OldAjaxMethods]: AxiosMethods
@@ -23,7 +23,7 @@ const methodMap: MethodMap = {
23
23
  patchWay: 'PATCH'
24
24
  }
25
25
 
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) => {
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
27
  const url = expand ? `${path}/${expand}` : path;
28
28
  if (expand) {
29
29
  // eslint-disable-next-line no-console
@@ -31,7 +31,7 @@ export const generateCustomMethod = <T extends object>(ctx: AjaxInstance<T>, nam
31
31
  }
32
32
  const [newParams, newData] = methodMap[name] === 'GET' ? [data, undefined] : [undefined, data]
33
33
  const config1 = normalizeConfig(ctx, {
34
- ...config, url, params: newParams, data: newData, method: methodMap[name],
34
+ ...config, ...(_config ?? {}), url, params: newParams, data: newData, method: methodMap[name],
35
35
  });
36
36
 
37
37
  const options = {
@@ -53,12 +53,12 @@ export const generateCustomMethod = <T extends object>(ctx: AjaxInstance<T>, nam
53
53
  // 缓存请求
54
54
  addPendingRequest(hashKey, request, options.reUseDuration)
55
55
 
56
- const res = await request
57
-
58
- // 清理缓存的请求
59
- clearPendingRequest(hashKey)
60
-
61
- return res
56
+ try {
57
+ return await request
58
+ } finally {
59
+ // 清理缓存的请求
60
+ clearPendingRequest(hashKey)
61
+ }
62
62
  };
63
63
 
64
64
  function addCompatibleMethods<CusOpts extends NonNullable<unknown>>(ctx: AjaxInstance<CusOpts>) {
package/src/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- const crypto = require('crypto')
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).
@@ -73,15 +73,11 @@ type NormalizedConfig = {
73
73
  */
74
74
  export function generateRequestHash(config: NormalizedConfig) {
75
75
  const { url = '', method = '', params = {}, data = {}, baseURL = '' } = config
76
-
77
- // 合并请求参数
78
- const combinedData = { ...params, ...data, baseURL, url, method }
79
-
80
- // 将对象序列化为字符串
81
- const serializedData = JSON.stringify(combinedData)
82
-
83
- // 创建一个 md5 哈希
84
- const hash = crypto.createHash('md5').update(serializedData).digest('hex')
85
-
86
- return hash
76
+ return hash({
77
+ ...params,
78
+ ...data,
79
+ baseURL,
80
+ url,
81
+ method
82
+ })
87
83
  }