@varlet/axle 0.2.18-alpha.1702565170429 → 0.2.18

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/es/index.d.ts CHANGED
@@ -63,4 +63,16 @@ interface ResponseStatusInterceptorOptions {
63
63
  }
64
64
  declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
65
65
 
66
- export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMockInterceptorMappingValue, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor };
66
+ type RequestMd5InterceptorMappingValue = {
67
+ path: string[];
68
+ method?: string;
69
+ };
70
+ interface RequestMd5InterceptorOptions {
71
+ mapping?: Record<string, RequestMd5InterceptorMappingValue>;
72
+ include?: string[];
73
+ exclude?: string[];
74
+ axiosInterceptorOptions?: AxiosInterceptorOptions;
75
+ }
76
+ declare function requestMd5Interceptor(options?: RequestMd5InterceptorOptions): RequestInterceptor;
77
+
78
+ export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMappingValue, RequestMd5InterceptorOptions, RequestMockInterceptorMappingValue, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor };
package/es/index.js CHANGED
@@ -7,6 +7,25 @@ import {
7
7
  import axios from "axios";
8
8
  import qs from "qs";
9
9
  import { inBrowser } from "@varlet/shared";
10
+
11
+ // src/utils/index.ts
12
+ function isFormData(value) {
13
+ return typeof FormData !== "undefined" && value instanceof FormData;
14
+ }
15
+ function formDataToObject(formData) {
16
+ const normalizedData = {};
17
+ formData.forEach((value, key) => {
18
+ normalizedData[key] = value;
19
+ });
20
+ return normalizedData;
21
+ }
22
+ function objectToFormData(object) {
23
+ const formData = new FormData();
24
+ Object.keys(object).forEach((key) => formData.append(key, object[key]));
25
+ return formData;
26
+ }
27
+
28
+ // src/instance.ts
10
29
  function createFetchRunner(service, method, responseType) {
11
30
  return function(url, params, config) {
12
31
  return service[method](url, __spreadValues({
@@ -21,9 +40,7 @@ function createModifyRunner(service, method, contentType) {
21
40
  params = qs.stringify(params);
22
41
  }
23
42
  if (contentType === "multipart/form-data") {
24
- const formData = new FormData();
25
- Object.keys(params).forEach((key) => formData.append(key, params[key]));
26
- params = formData;
43
+ params = objectToFormData(params);
27
44
  }
28
45
  return service[method](url, params, __spreadValues({
29
46
  headers: {
@@ -319,6 +336,69 @@ function responseStatusInterceptor(options) {
319
336
  options: options.axiosInterceptorOptions
320
337
  };
321
338
  }
339
+
340
+ // src/interceptors/requestMd5Interceptor.ts
341
+ import qs2 from "qs";
342
+ import { minimatch as minimatch3 } from "minimatch";
343
+ import { set, get } from "lodash-es";
344
+ import { MD5 } from "crypto-js";
345
+ import { isString } from "@varlet/shared";
346
+ function withCtxMd5(ctx, mappingValue) {
347
+ mappingValue.path.forEach((path) => {
348
+ const targetValue = get(ctx, path);
349
+ if (targetValue != null) {
350
+ set(ctx, path, MD5(String(targetValue)).toString());
351
+ }
352
+ });
353
+ }
354
+ function withMd5(config, mappingValue) {
355
+ const { data = {}, params = {}, headers = {} } = config;
356
+ const ctx = {
357
+ data: isFormData(data) ? data : JSON.parse(JSON.stringify(data)),
358
+ params: JSON.parse(JSON.stringify(params)),
359
+ headers: JSON.parse(JSON.stringify(headers))
360
+ };
361
+ if (isString(ctx.data) && ctx.data.length > 0 && ctx.headers["Content-Type"] === "application/x-www-form-urlencoded") {
362
+ ctx.data = qs2.parse(ctx.data);
363
+ withCtxMd5(ctx, mappingValue);
364
+ ctx.data = qs2.stringify(ctx.data);
365
+ } else if (isFormData(ctx.data)) {
366
+ ctx.data = formDataToObject(ctx.data);
367
+ withCtxMd5(ctx, mappingValue);
368
+ ctx.data = objectToFormData(ctx.data);
369
+ } else {
370
+ withCtxMd5(ctx, mappingValue);
371
+ }
372
+ return __spreadValues(__spreadValues({}, config), ctx);
373
+ }
374
+ function requestMd5Interceptor(options = {}) {
375
+ return {
376
+ onFulfilled(config) {
377
+ var _a, _b;
378
+ const matcher = createMatcher(options.include, options.exclude);
379
+ if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
380
+ return config;
381
+ }
382
+ const findMappingRecord = () => {
383
+ var _a2;
384
+ return Object.entries((_a2 = options.mapping) != null ? _a2 : {}).find(
385
+ ([key, value]) => {
386
+ var _a3, _b2;
387
+ return minimatch3((_a3 = config.url) != null ? _a3 : "", key) && config.method === ((_b2 = value.method) != null ? _b2 : "get");
388
+ }
389
+ );
390
+ };
391
+ const mappingRecord = findMappingRecord();
392
+ if (!mappingRecord) {
393
+ return config;
394
+ }
395
+ const [, mappingValue] = mappingRecord;
396
+ return withMd5(config, mappingValue);
397
+ },
398
+ onRejected: (error) => Promise.reject(error),
399
+ options: options.axiosInterceptorOptions
400
+ };
401
+ }
322
402
  export {
323
403
  createAxle,
324
404
  createFetchRunner,
@@ -327,6 +407,7 @@ export {
327
407
  download,
328
408
  matchPattern,
329
409
  requestHeadersInterceptor,
410
+ requestMd5Interceptor,
330
411
  requestMockInterceptor,
331
412
  responseBlobInterceptor,
332
413
  responseRetryInterceptor,
package/es/use.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-KLNSLHAC.js";
4
4
 
5
5
  // src/use.ts
6
- import { ref as ref3, onUnmounted } from "vue";
6
+ import { ref as ref3 } from "vue";
7
7
  import { isFunction } from "@varlet/shared";
8
8
 
9
9
  // src/composables/useValues.ts
@@ -139,7 +139,6 @@ function createUseAxle(options) {
139
139
  config: initialConfig
140
140
  });
141
141
  }
142
- onUnmounted(abort);
143
142
  return [
144
143
  value,
145
144
  run,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.2.18-alpha.1702565170429",
3
+ "version": "0.2.18",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",
@@ -31,8 +31,11 @@
31
31
  "author": "haoziqaq <357229046@qq.com>",
32
32
  "license": "MIT",
33
33
  "dependencies": {
34
+ "@types/crypto-js": "^4.2.1",
34
35
  "@varlet/shared": "^2.19.2",
35
36
  "axios": "^1.4.0",
37
+ "crypto-js": "^4.2.0",
38
+ "lodash-es": "^4.17.21",
36
39
  "minimatch": "^9.0.3",
37
40
  "qs": "^6.11.0"
38
41
  },
@@ -40,11 +43,12 @@
40
43
  "vue": "^3.2.0"
41
44
  },
42
45
  "devDependencies": {
46
+ "@types/lodash-es": "^4.17.12",
47
+ "@types/minimatch": "^5.1.2",
48
+ "@types/qs": "^6.9.7",
43
49
  "tsup": "7.2.0",
44
50
  "typescript": "^5.1.5",
45
- "vue": "3.3.4",
46
- "@types/minimatch": "^5.1.2",
47
- "@types/qs": "^6.9.7"
51
+ "vue": "3.3.4"
48
52
  },
49
53
  "repository": {
50
54
  "type": "git",