@minson1994/ms-utils 1.0.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.
Files changed (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +696 -0
  3. package/dist/api/ApiConfig.d.ts +206 -0
  4. package/dist/api/ApiConfig.d.ts.map +1 -0
  5. package/dist/api/ApiError.d.ts +30 -0
  6. package/dist/api/ApiError.d.ts.map +1 -0
  7. package/dist/api/ApiRequest.d.ts +87 -0
  8. package/dist/api/ApiRequest.d.ts.map +1 -0
  9. package/dist/api/ApiResponse.d.ts +15 -0
  10. package/dist/api/ApiResponse.d.ts.map +1 -0
  11. package/dist/api/adapter/AjaxHttpAdapter.d.ts +22 -0
  12. package/dist/api/adapter/AjaxHttpAdapter.d.ts.map +1 -0
  13. package/dist/api/adapter/ApiAdapter.d.ts +56 -0
  14. package/dist/api/adapter/ApiAdapter.d.ts.map +1 -0
  15. package/dist/api/adapter/AxiosHttpAdapter.d.ts +43 -0
  16. package/dist/api/adapter/AxiosHttpAdapter.d.ts.map +1 -0
  17. package/dist/api/adapter/FetchHttpAdapter.d.ts +50 -0
  18. package/dist/api/adapter/FetchHttpAdapter.d.ts.map +1 -0
  19. package/dist/api/adapter/WxHttpAdapter.d.ts +33 -0
  20. package/dist/api/adapter/WxHttpAdapter.d.ts.map +1 -0
  21. package/dist/api/adapter/XhrHttpAdapter.d.ts +46 -0
  22. package/dist/api/adapter/XhrHttpAdapter.d.ts.map +1 -0
  23. package/dist/api/index.d.ts +22 -0
  24. package/dist/api/index.d.ts.map +1 -0
  25. package/dist/api/interceptor/ApiCacheInterceptor.d.ts +29 -0
  26. package/dist/api/interceptor/ApiCacheInterceptor.d.ts.map +1 -0
  27. package/dist/api/interceptor/ApiInterceptor.d.ts +18 -0
  28. package/dist/api/interceptor/ApiInterceptor.d.ts.map +1 -0
  29. package/dist/api/interceptor/ApiStatusInterceptor.d.ts +49 -0
  30. package/dist/api/interceptor/ApiStatusInterceptor.d.ts.map +1 -0
  31. package/dist/api/interceptor/ApiUIInterceptor.d.ts +37 -0
  32. package/dist/api/interceptor/ApiUIInterceptor.d.ts.map +1 -0
  33. package/dist/axios.cjs +151 -0
  34. package/dist/axios.d.ts +6 -0
  35. package/dist/axios.d.ts.map +1 -0
  36. package/dist/axios.js +58 -0
  37. package/dist/browser.cjs +102 -0
  38. package/dist/browser.d.ts +5 -0
  39. package/dist/browser.d.ts.map +1 -0
  40. package/dist/browser.js +75 -0
  41. package/dist/chunk-PV2X54HI.js +75 -0
  42. package/dist/index.cjs +1880 -0
  43. package/dist/index.d.ts +11 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +1753 -0
  46. package/dist/utils/AlgorithmUtils.d.ts +98 -0
  47. package/dist/utils/AlgorithmUtils.d.ts.map +1 -0
  48. package/dist/utils/Concurrency.d.ts +28 -0
  49. package/dist/utils/Concurrency.d.ts.map +1 -0
  50. package/dist/utils/CookieUtils.d.ts +38 -0
  51. package/dist/utils/CookieUtils.d.ts.map +1 -0
  52. package/dist/utils/Hook.d.ts +50 -0
  53. package/dist/utils/Hook.d.ts.map +1 -0
  54. package/dist/utils/TaskQueue.d.ts +159 -0
  55. package/dist/utils/TaskQueue.d.ts.map +1 -0
  56. package/dist/utils/TimeUtils.d.ts +82 -0
  57. package/dist/utils/TimeUtils.d.ts.map +1 -0
  58. package/dist/utils/ValidateUtils.d.ts +139 -0
  59. package/dist/utils/ValidateUtils.d.ts.map +1 -0
  60. package/dist/wx.cjs +169 -0
  61. package/dist/wx.d.ts +6 -0
  62. package/dist/wx.d.ts.map +1 -0
  63. package/dist/wx.js +76 -0
  64. package/package.json +76 -0
package/dist/wx.cjs ADDED
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/wx.ts
21
+ var wx_exports = {};
22
+ __export(wx_exports, {
23
+ WxHttpAdapter: () => WxHttpAdapter
24
+ });
25
+ module.exports = __toCommonJS(wx_exports);
26
+
27
+ // src/api/adapter/ApiAdapter.ts
28
+ var ApiAdapter = class {
29
+ constructor(options = {}) {
30
+ this.options = options;
31
+ }
32
+ /**
33
+ * 触发标准化进度回调。
34
+ */
35
+ emitProgress(handler, loaded, total, event) {
36
+ if (!handler) return;
37
+ const lengthComputable = typeof total === "number" && Number.isFinite(total) && total > 0;
38
+ const progress = {
39
+ loaded,
40
+ lengthComputable,
41
+ event
42
+ };
43
+ if (lengthComputable) {
44
+ progress.total = total;
45
+ progress.percent = Math.min(100, Math.round(loaded / total * 100));
46
+ }
47
+ handler(progress);
48
+ }
49
+ /**
50
+ * 将 fetch / axios / xhr 等运行时进度事件转换为标准进度对象。
51
+ */
52
+ emitProgressEvent(handler, event) {
53
+ const source = event;
54
+ const loaded = Number(source.loaded ?? 0);
55
+ const total = source.lengthComputable === false ? void 0 : source.total;
56
+ this.emitProgress(handler, loaded, total, event);
57
+ }
58
+ /**
59
+ * 将上传请求数据转换为 FormData。
60
+ */
61
+ formData(config, target) {
62
+ if (!config.isUpload) return;
63
+ const formData = this.options.formDataFactory?.() ?? this.createGlobalFormData();
64
+ const fileName = config.data.fileName ?? "file";
65
+ for (const [key, value] of Object.entries(config.data)) {
66
+ if (key !== "file" && key !== "fileName")
67
+ formData.append(key, value);
68
+ }
69
+ if (config.data.file !== void 0)
70
+ formData.append(fileName, config.data.file);
71
+ target.data = formData;
72
+ target.headers ?? (target.headers = {});
73
+ target.headers["Content-Type"] = "multipart/form-data";
74
+ target.method = "POST";
75
+ }
76
+ /**
77
+ * 使用全局 FormData 构造器。
78
+ */
79
+ createGlobalFormData() {
80
+ const FormDataCtor = globalThis.FormData;
81
+ if (!FormDataCtor) {
82
+ throw new Error(
83
+ "FormData is not available; pass formDataFactory to the adapter"
84
+ );
85
+ }
86
+ return new FormDataCtor();
87
+ }
88
+ };
89
+
90
+ // src/api/ApiResponse.ts
91
+ var ApiResponse = class {
92
+ constructor(status, data) {
93
+ this.status = status;
94
+ this.data = data;
95
+ }
96
+ };
97
+
98
+ // src/api/adapter/WxHttpAdapter.ts
99
+ var WxHttpAdapter = class extends ApiAdapter {
100
+ constructor(wx) {
101
+ super();
102
+ this.wx = wx;
103
+ }
104
+ /**
105
+ * 将 ApiConfig 转换为 wx.request / wx.uploadFile 或 uni.request / uni.uploadFile 配置并发起请求。
106
+ */
107
+ request(config) {
108
+ return new Promise((resolve, reject) => {
109
+ let requestTask;
110
+ const requestConfig = {
111
+ url: `${config.baseUrl}${config.url}`,
112
+ method: config.method,
113
+ data: config.data,
114
+ header: { ...config.headers },
115
+ success: (response) => {
116
+ resolve(
117
+ new ApiResponse(
118
+ response.statusCode ?? response.status ?? 200,
119
+ response.data
120
+ )
121
+ );
122
+ },
123
+ fail: (error) => reject(error)
124
+ };
125
+ const onCancel = config.onCancel ?? config.cancelCallback;
126
+ if (onCancel) {
127
+ onCancel(() => requestTask?.abort?.());
128
+ }
129
+ if (config.isUpload) {
130
+ requestConfig.filePath = config.data.file;
131
+ requestConfig.name = config.data.fileName ?? "file";
132
+ requestConfig.formData = Object.fromEntries(
133
+ Object.entries(config.data).filter(
134
+ ([key]) => key !== "file" && key !== "fileName"
135
+ )
136
+ );
137
+ requestTask = this.wx.uploadFile(requestConfig);
138
+ if (config.onUploadProgress && requestTask?.onProgressUpdate) {
139
+ requestTask.onProgressUpdate((event) => {
140
+ this.emitProgress(
141
+ config.onUploadProgress,
142
+ event.totalBytesSent ?? 0,
143
+ event.totalBytesExpectedToSend,
144
+ event
145
+ );
146
+ });
147
+ }
148
+ return;
149
+ }
150
+ if (config.isJsonBody) {
151
+ requestConfig.method = "POST";
152
+ requestConfig.header = {
153
+ ...config.headers,
154
+ "content-type": "application/json"
155
+ };
156
+ } else if (config.method.toUpperCase() === "POST") {
157
+ requestConfig.header = {
158
+ ...config.headers,
159
+ "content-type": "application/x-www-form-urlencoded"
160
+ };
161
+ }
162
+ requestTask = this.wx.request(requestConfig);
163
+ });
164
+ }
165
+ };
166
+ // Annotate the CommonJS export names for ESM import in node:
167
+ 0 && (module.exports = {
168
+ WxHttpAdapter
169
+ });
package/dist/wx.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 微信小程序适配器入口。
3
+ */
4
+ export { default as WxHttpAdapter } from './api/adapter/WxHttpAdapter';
5
+ export type { WxLike } from './api/adapter/WxHttpAdapter';
6
+ //# sourceMappingURL=wx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wx.d.ts","sourceRoot":"","sources":["../src/wx.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACvE,YAAY,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC"}
package/dist/wx.js ADDED
@@ -0,0 +1,76 @@
1
+ import {
2
+ ApiAdapter,
3
+ ApiResponse
4
+ } from "./chunk-PV2X54HI.js";
5
+
6
+ // src/api/adapter/WxHttpAdapter.ts
7
+ var WxHttpAdapter = class extends ApiAdapter {
8
+ constructor(wx) {
9
+ super();
10
+ this.wx = wx;
11
+ }
12
+ /**
13
+ * 将 ApiConfig 转换为 wx.request / wx.uploadFile 或 uni.request / uni.uploadFile 配置并发起请求。
14
+ */
15
+ request(config) {
16
+ return new Promise((resolve, reject) => {
17
+ let requestTask;
18
+ const requestConfig = {
19
+ url: `${config.baseUrl}${config.url}`,
20
+ method: config.method,
21
+ data: config.data,
22
+ header: { ...config.headers },
23
+ success: (response) => {
24
+ resolve(
25
+ new ApiResponse(
26
+ response.statusCode ?? response.status ?? 200,
27
+ response.data
28
+ )
29
+ );
30
+ },
31
+ fail: (error) => reject(error)
32
+ };
33
+ const onCancel = config.onCancel ?? config.cancelCallback;
34
+ if (onCancel) {
35
+ onCancel(() => requestTask?.abort?.());
36
+ }
37
+ if (config.isUpload) {
38
+ requestConfig.filePath = config.data.file;
39
+ requestConfig.name = config.data.fileName ?? "file";
40
+ requestConfig.formData = Object.fromEntries(
41
+ Object.entries(config.data).filter(
42
+ ([key]) => key !== "file" && key !== "fileName"
43
+ )
44
+ );
45
+ requestTask = this.wx.uploadFile(requestConfig);
46
+ if (config.onUploadProgress && requestTask?.onProgressUpdate) {
47
+ requestTask.onProgressUpdate((event) => {
48
+ this.emitProgress(
49
+ config.onUploadProgress,
50
+ event.totalBytesSent ?? 0,
51
+ event.totalBytesExpectedToSend,
52
+ event
53
+ );
54
+ });
55
+ }
56
+ return;
57
+ }
58
+ if (config.isJsonBody) {
59
+ requestConfig.method = "POST";
60
+ requestConfig.header = {
61
+ ...config.headers,
62
+ "content-type": "application/json"
63
+ };
64
+ } else if (config.method.toUpperCase() === "POST") {
65
+ requestConfig.header = {
66
+ ...config.headers,
67
+ "content-type": "application/x-www-form-urlencoded"
68
+ };
69
+ }
70
+ requestTask = this.wx.request(requestConfig);
71
+ });
72
+ }
73
+ };
74
+ export {
75
+ WxHttpAdapter
76
+ };
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@minson1994/ms-utils",
3
+ "version": "1.0.0",
4
+ "description": "小工具集合",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ },
15
+ "./browser": {
16
+ "types": "./dist/browser.d.ts",
17
+ "import": "./dist/browser.js",
18
+ "require": "./dist/browser.cjs"
19
+ },
20
+ "./wx": {
21
+ "types": "./dist/wx.d.ts",
22
+ "import": "./dist/wx.js",
23
+ "require": "./dist/wx.cjs"
24
+ },
25
+ "./axios": {
26
+ "types": "./dist/axios.d.ts",
27
+ "import": "./dist/axios.js",
28
+ "require": "./dist/axios.cjs"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "README.md",
35
+ "LICENSE"
36
+ ],
37
+ "sideEffects": false,
38
+ "scripts": {
39
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsup src/index.ts src/browser.ts src/wx.ts src/axios.ts --format esm,cjs --clean && tsc -p tsconfig.build.json",
40
+ "build:test": "node scripts/build-to-test.mjs",
41
+ "test": "vitest run",
42
+ "typecheck": "tsc --noEmit",
43
+ "prepublishOnly": "npm run typecheck && npm run test && npm run build"
44
+ },
45
+ "keywords": [
46
+ "utils",
47
+ "typescript",
48
+ "http",
49
+ "concurrency",
50
+ "hook"
51
+ ],
52
+ "author": "minson",
53
+ "license": "MIT",
54
+ "peerDependencies": {
55
+ "axios": ">=0.27 || >=1",
56
+ "qs": ">=6"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "axios": {
60
+ "optional": true
61
+ },
62
+ "qs": {
63
+ "optional": true
64
+ }
65
+ },
66
+ "devDependencies": {
67
+ "@types/crypto-js": "^4.2.2",
68
+ "@types/node": "^20.14.0",
69
+ "tsup": "^8.3.0",
70
+ "typescript": "^5.6.0",
71
+ "vitest": "^4.1.8"
72
+ },
73
+ "dependencies": {
74
+ "crypto-js": "^4.2.0"
75
+ }
76
+ }