@varlet/axle 0.2.17 → 0.2.18-alpha.1702624592933

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,64 @@ 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 withConfigMd5(config, mappingValue) {
347
+ mappingValue.path.forEach((path) => {
348
+ const targetValue = get(config, path);
349
+ if (targetValue != null) {
350
+ set(config, path, MD5(String(targetValue)).toString());
351
+ }
352
+ });
353
+ }
354
+ function withMd5(config, mappingValue) {
355
+ const { data, headers = {} } = config;
356
+ if (isString(data) && data.length > 0 && headers["Content-Type"] === "application/x-www-form-urlencoded") {
357
+ config.data = qs2.parse(config.data);
358
+ withConfigMd5(config, mappingValue);
359
+ config.data = qs2.stringify(config.data);
360
+ } else if (isFormData(data)) {
361
+ config.data = formDataToObject(data);
362
+ withConfigMd5(config, mappingValue);
363
+ config.data = objectToFormData(config.data);
364
+ } else {
365
+ withConfigMd5(config, mappingValue);
366
+ }
367
+ return config;
368
+ }
369
+ function requestMd5Interceptor(options = {}) {
370
+ return {
371
+ onFulfilled(config) {
372
+ var _a, _b;
373
+ const matcher = createMatcher(options.include, options.exclude);
374
+ if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
375
+ return config;
376
+ }
377
+ const findMappingRecord = () => {
378
+ var _a2;
379
+ return Object.entries((_a2 = options.mapping) != null ? _a2 : {}).find(
380
+ ([key, value]) => {
381
+ var _a3, _b2;
382
+ return minimatch3((_a3 = config.url) != null ? _a3 : "", key) && config.method === ((_b2 = value.method) != null ? _b2 : "get");
383
+ }
384
+ );
385
+ };
386
+ const mappingRecord = findMappingRecord();
387
+ if (!mappingRecord) {
388
+ return config;
389
+ }
390
+ const [, mappingValue] = mappingRecord;
391
+ return withMd5(config, mappingValue);
392
+ },
393
+ onRejected: (error) => Promise.reject(error),
394
+ options: options.axiosInterceptorOptions
395
+ };
396
+ }
322
397
  export {
323
398
  createAxle,
324
399
  createFetchRunner,
@@ -327,6 +402,7 @@ export {
327
402
  download,
328
403
  matchPattern,
329
404
  requestHeadersInterceptor,
405
+ requestMd5Interceptor,
330
406
  requestMockInterceptor,
331
407
  responseBlobInterceptor,
332
408
  responseRetryInterceptor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.2.17",
3
+ "version": "0.2.18-alpha.1702624592933",
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",