@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
@@ -0,0 +1,22 @@
1
+ /**
2
+ * API 请求模块入口。
3
+ *
4
+ * 使用 Api* 命名,强调这是“跨运行时业务 API 请求编排器”,
5
+ * 不是要替代 axios/fetch/xhr 的底层 HTTP Client。
6
+ */
7
+ export { default as ApiRequest } from "./ApiRequest";
8
+ export { ApiError, ApiCancelError, ApiTimeoutError, ApiNetworkError, isApiCancelError, } from "./ApiError";
9
+ export { ApiConfig, ApiGlobalConfig, useApiConfig, useApiConfig as $Api, useApiConfig as $Use, } from "./ApiConfig";
10
+ export type { ApiConfigOptions, ApiCancel, ApiCancelHandler, ApiGlobalConfigOptions, ApiHeaders, ApiMethod, ApiProgress, ApiProgressHandler, } from "./ApiConfig";
11
+ export { default as ApiResponse } from "./ApiResponse";
12
+ export { default as ApiInterceptor } from "./interceptor/ApiInterceptor";
13
+ export { default as ApiUIInterceptor } from "./interceptor/ApiUIInterceptor";
14
+ export { default as ApiStatusInterceptor } from "./interceptor/ApiStatusInterceptor";
15
+ export type { ApiStatusInterceptorOptions } from "./interceptor/ApiStatusInterceptor";
16
+ export { default as ApiCacheInterceptor } from "./interceptor/ApiCacheInterceptor";
17
+ export { default as ApiAdapter } from "./adapter/ApiAdapter";
18
+ export { default as FetchHttpAdapter } from "./adapter/FetchHttpAdapter";
19
+ export type { FetchHttpAdapterOptions, FetchLike } from "./adapter/FetchHttpAdapter";
20
+ export { default as XhrHttpAdapter } from "./adapter/XhrHttpAdapter";
21
+ export type { XhrHttpAdapterOptions, XMLHttpRequestFactory } from "./adapter/XhrHttpAdapter";
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EACH,QAAQ,EACR,cAAc,EACd,eAAe,EACf,eAAe,EACf,gBAAgB,GACnB,MAAM,YAAY,CAAC;AAEpB,OAAO,EACH,SAAS,EACT,eAAe,EACf,YAAY,EACZ,YAAY,IAAI,IAAI,EACpB,YAAY,IAAI,IAAI,GACvB,MAAM,aAAa,CAAC;AAErB,YAAY,EACR,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,sBAAsB,EACtB,UAAU,EACV,SAAS,EACT,WAAW,EACX,kBAAkB,GACrB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACzE,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAC7E,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AACrF,YAAY,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AACtF,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AACnF,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAE7D,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AACzE,YAAY,EAAE,uBAAuB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAErF,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACrE,YAAY,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,29 @@
1
+ import ApiInterceptor from './ApiInterceptor';
2
+ import type { ApiConfig } from '../ApiConfig';
3
+ import type ApiResponse from '../ApiResponse';
4
+ /**
5
+ * 缓存 key 生成函数。
6
+ */
7
+ export type CacheKeyFactory = (config: ApiConfig) => string;
8
+ /**
9
+ * 默认缓存 key:按 baseUrl、url、method、data 区分请求。
10
+ */
11
+ export declare const defaultCacheKey: CacheKeyFactory;
12
+ /**
13
+ * HTTP 响应缓存拦截器。
14
+ */
15
+ export default class ApiCacheInterceptor extends ApiInterceptor {
16
+ private readonly getKey;
17
+ /** 缓存数据映射。 */
18
+ readonly cacheMap: Map<string, unknown>;
19
+ constructor(getKey?: CacheKeyFactory);
20
+ /**
21
+ * 请求前命中缓存则直接返回缓存值。
22
+ */
23
+ before(config: ApiConfig): Promise<unknown | void>;
24
+ /**
25
+ * 请求后按 key 写入缓存。
26
+ */
27
+ after(config: ApiConfig, response: ApiResponse): Promise<void>;
28
+ }
29
+ //# sourceMappingURL=ApiCacheInterceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiCacheInterceptor.d.ts","sourceRoot":"","sources":["../../../src/api/interceptor/ApiCacheInterceptor.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,MAAM,CAAC;AAE5D;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,eAMxB,CAAC;AAEP;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,mBAAoB,SAAQ,cAAc;IAI/C,OAAO,CAAC,QAAQ,CAAC,MAAM;IAHnC,cAAc;IACd,QAAQ,CAAC,QAAQ,uBAA8B;gBAElB,MAAM,GAAE,eAAiC;IAItE;;OAEG;IACY,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAMjE;;OAEG;IACY,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAGhF"}
@@ -0,0 +1,18 @@
1
+ import type { ApiConfig } from '../ApiConfig';
2
+ import type ApiResponse from '../ApiResponse';
3
+ /**
4
+ * HTTP 拦截器基类。
5
+ *
6
+ * before 返回非 undefined 数据时,请求链会直接使用该数据作为结果。
7
+ */
8
+ export default abstract class ApiInterceptor<TData = unknown> {
9
+ /**
10
+ * 请求前置处理。
11
+ */
12
+ abstract before(config: ApiConfig): Promise<TData | void>;
13
+ /**
14
+ * 请求后置处理。
15
+ */
16
+ abstract after(config: ApiConfig, response?: ApiResponse): Promise<void>;
17
+ }
18
+ //# sourceMappingURL=ApiInterceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiInterceptor.d.ts","sourceRoot":"","sources":["../../../src/api/interceptor/ApiInterceptor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAE9C;;;;GAIG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,cAAc,CAAC,KAAK,GAAG,OAAO;IACxD;;OAEG;IACH,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;IAEzD;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAC3E"}
@@ -0,0 +1,49 @@
1
+ import ApiInterceptor from './ApiInterceptor';
2
+ import type { ApiConfig } from '../ApiConfig';
3
+ import type ApiResponse from '../ApiResponse';
4
+ /**
5
+ * HTTP 状态拦截器配置。
6
+ */
7
+ export interface ApiStatusInterceptorOptions {
8
+ /** 业务状态码字段名。 */
9
+ status?: string;
10
+ /** 业务消息字段名。 */
11
+ message?: string;
12
+ /** 业务数据字段名。 */
13
+ data?: string;
14
+ /** 成功业务状态码。 */
15
+ success?: string | number;
16
+ /** 认证失效回调。 */
17
+ onUnauthorized?: (message: string, response: ApiResponse, config: ApiConfig) => void | Promise<void>;
18
+ }
19
+ /**
20
+ * HTTP 状态和业务状态拦截器。
21
+ *
22
+ * 负责检查 HTTP 状态码、业务状态码、认证失效回调,以及按配置剥离 data 字段。
23
+ */
24
+ export default class ApiStatusInterceptor extends ApiInterceptor {
25
+ /** 业务状态码字段名。 */
26
+ status: string;
27
+ /** 业务消息字段名。 */
28
+ message: string;
29
+ /** 业务数据字段名。 */
30
+ data: string;
31
+ /** 成功业务状态码。 */
32
+ success: string | number;
33
+ /** 认证失效回调。 */
34
+ onUnauthorized?: ApiStatusInterceptorOptions['onUnauthorized'];
35
+ constructor(options?: ApiStatusInterceptorOptions);
36
+ /**
37
+ * 状态拦截器不需要请求前处理。
38
+ */
39
+ before(_config: ApiConfig): Promise<void>;
40
+ /**
41
+ * 检查响应状态,并按配置改写 response.data。
42
+ */
43
+ after(config: ApiConfig, response: ApiResponse<Record<string, unknown>>): Promise<void>;
44
+ /**
45
+ * 更新业务字段映射和回调配置。
46
+ */
47
+ useConfig(options: ApiStatusInterceptorOptions): this;
48
+ }
49
+ //# sourceMappingURL=ApiStatusInterceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiStatusInterceptor.d.ts","sourceRoot":"","sources":["../../../src/api/interceptor/ApiStatusInterceptor.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,2BAA2B;IACxC,gBAAgB;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe;IACf,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,cAAc;IACd,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,SAAS,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxG;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,oBAAqB,SAAQ,cAAc;IAC5D,gBAAgB;IAChB,MAAM,SAAU;IAChB,eAAe;IACf,OAAO,SAAa;IACpB,eAAe;IACf,IAAI,SAAU;IACd,eAAe;IACf,OAAO,EAAE,MAAM,GAAG,MAAM,CAAO;IAC/B,cAAc;IACd,cAAc,CAAC,EAAE,2BAA2B,CAAC,gBAAgB,CAAC,CAAC;gBAEnD,OAAO,GAAE,2BAAgC;IAKrD;;OAEG;IACY,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAIxD;;OAEG;IACY,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAyBtG;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,2BAA2B,GAAG,IAAI;CAQxD"}
@@ -0,0 +1,37 @@
1
+ import ApiInterceptor from './ApiInterceptor';
2
+ import type { ApiConfig } from '../ApiConfig';
3
+ import type ApiResponse from '../ApiResponse';
4
+ /**
5
+ * loading 显示/隐藏回调。
6
+ */
7
+ export type LoadingHandler = () => void | Promise<void>;
8
+ /**
9
+ * 错误消息回调。
10
+ */
11
+ export type ErrorMessageHandler = (message: string, config: ApiConfig) => void | Promise<void>;
12
+ /**
13
+ * HTTP UI 拦截器。
14
+ *
15
+ * 只保存回调,不直接绑定具体 UI 框架。
16
+ */
17
+ export default class ApiUIInterceptor extends ApiInterceptor {
18
+ /** 显示 loading 回调。 */
19
+ loading?: LoadingHandler;
20
+ /** 隐藏 loading 回调。 */
21
+ loadingHide?: LoadingHandler;
22
+ /** 错误消息回调。 */
23
+ errorMessage?: ErrorMessageHandler;
24
+ /** 请求前显示 loading。 */
25
+ before(config: ApiConfig): Promise<void>;
26
+ /** 请求结束后隐藏 loading。 */
27
+ after(config: ApiConfig, _response?: ApiResponse): Promise<void>;
28
+ /** 按请求配置显示错误消息。 */
29
+ showErrorMessage(config: ApiConfig, message: string): Promise<void>;
30
+ /** 设置显示 loading 回调。 */
31
+ useLoading(loading: LoadingHandler): this;
32
+ /** 设置隐藏 loading 回调。 */
33
+ useLoadingHide(loadingHide: LoadingHandler): this;
34
+ /** 设置错误消息回调。 */
35
+ useErrorMessage(errorMessage: ErrorMessageHandler): this;
36
+ }
37
+ //# sourceMappingURL=ApiUIInterceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiUIInterceptor.d.ts","sourceRoot":"","sources":["../../../src/api/interceptor/ApiUIInterceptor.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE/F;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,gBAAiB,SAAQ,cAAc;IACxD,qBAAqB;IACrB,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,qBAAqB;IACrB,WAAW,CAAC,EAAE,cAAc,CAAC;IAC7B,cAAc;IACd,YAAY,CAAC,EAAE,mBAAmB,CAAC;IAEnC,qBAAqB;IACN,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvD,uBAAuB;IACR,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/E,mBAAmB;IACb,gBAAgB,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMzE,uBAAuB;IACvB,UAAU,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IAKzC,uBAAuB;IACvB,cAAc,CAAC,WAAW,EAAE,cAAc,GAAG,IAAI;IAKjD,gBAAgB;IAChB,eAAe,CAAC,YAAY,EAAE,mBAAmB,GAAG,IAAI;CAI3D"}
package/dist/axios.cjs ADDED
@@ -0,0 +1,151 @@
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/axios.ts
21
+ var axios_exports = {};
22
+ __export(axios_exports, {
23
+ AxiosHttpAdapter: () => AxiosHttpAdapter
24
+ });
25
+ module.exports = __toCommonJS(axios_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/AxiosHttpAdapter.ts
99
+ var AxiosHttpAdapter = class extends ApiAdapter {
100
+ constructor(axios, qs) {
101
+ super();
102
+ this.axios = axios;
103
+ this.qs = qs;
104
+ }
105
+ /**
106
+ * 将 ApiConfig 转换为 axios 配置并发起请求。
107
+ */
108
+ async request(config) {
109
+ const requestSource = this.axios.CancelToken?.source();
110
+ const requestConfig = {
111
+ url: `${config.baseUrl}${config.url}`,
112
+ method: config.method,
113
+ headers: { ...config.headers },
114
+ timeout: config.timeout * 1e3,
115
+ withCredentials: config.withCredentials
116
+ };
117
+ if (config.onUploadProgress) {
118
+ requestConfig.onUploadProgress = (event) => this.emitProgressEvent(config.onUploadProgress, event);
119
+ }
120
+ if (config.onDownloadProgress) {
121
+ requestConfig.onDownloadProgress = (event) => this.emitProgressEvent(config.onDownloadProgress, event);
122
+ }
123
+ if (config.isUpload) {
124
+ this.formData(
125
+ config,
126
+ requestConfig
127
+ );
128
+ } else if (config.isJsonBody) {
129
+ requestConfig.data = config.data;
130
+ } else if (["get", "delete"].includes(config.method.toLowerCase()) || !config.method) {
131
+ requestConfig.params = config.data;
132
+ } else {
133
+ requestConfig.data = this.qs?.stringify(config.data, { indices: false }) ?? config.data;
134
+ }
135
+ const onCancel = config.onCancel ?? config.cancelCallback;
136
+ if (onCancel && requestSource)
137
+ onCancel(requestSource.cancel.bind(requestSource));
138
+ try {
139
+ const response = await this.axios.request(requestConfig);
140
+ return new ApiResponse(response.status, response.data);
141
+ } catch (error) {
142
+ const response = error.response;
143
+ if (response) return new ApiResponse(response.status, response.data);
144
+ throw error;
145
+ }
146
+ }
147
+ };
148
+ // Annotate the CommonJS export names for ESM import in node:
149
+ 0 && (module.exports = {
150
+ AxiosHttpAdapter
151
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * axios 适配器入口。
3
+ */
4
+ export { default as AxiosHttpAdapter } from './api/adapter/AxiosHttpAdapter';
5
+ export type { AxiosLike, QsLike } from './api/adapter/AxiosHttpAdapter';
6
+ //# sourceMappingURL=axios.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"axios.d.ts","sourceRoot":"","sources":["../src/axios.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAC7E,YAAY,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC"}
package/dist/axios.js ADDED
@@ -0,0 +1,58 @@
1
+ import {
2
+ ApiAdapter,
3
+ ApiResponse
4
+ } from "./chunk-PV2X54HI.js";
5
+
6
+ // src/api/adapter/AxiosHttpAdapter.ts
7
+ var AxiosHttpAdapter = class extends ApiAdapter {
8
+ constructor(axios, qs) {
9
+ super();
10
+ this.axios = axios;
11
+ this.qs = qs;
12
+ }
13
+ /**
14
+ * 将 ApiConfig 转换为 axios 配置并发起请求。
15
+ */
16
+ async request(config) {
17
+ const requestSource = this.axios.CancelToken?.source();
18
+ const requestConfig = {
19
+ url: `${config.baseUrl}${config.url}`,
20
+ method: config.method,
21
+ headers: { ...config.headers },
22
+ timeout: config.timeout * 1e3,
23
+ withCredentials: config.withCredentials
24
+ };
25
+ if (config.onUploadProgress) {
26
+ requestConfig.onUploadProgress = (event) => this.emitProgressEvent(config.onUploadProgress, event);
27
+ }
28
+ if (config.onDownloadProgress) {
29
+ requestConfig.onDownloadProgress = (event) => this.emitProgressEvent(config.onDownloadProgress, event);
30
+ }
31
+ if (config.isUpload) {
32
+ this.formData(
33
+ config,
34
+ requestConfig
35
+ );
36
+ } else if (config.isJsonBody) {
37
+ requestConfig.data = config.data;
38
+ } else if (["get", "delete"].includes(config.method.toLowerCase()) || !config.method) {
39
+ requestConfig.params = config.data;
40
+ } else {
41
+ requestConfig.data = this.qs?.stringify(config.data, { indices: false }) ?? config.data;
42
+ }
43
+ const onCancel = config.onCancel ?? config.cancelCallback;
44
+ if (onCancel && requestSource)
45
+ onCancel(requestSource.cancel.bind(requestSource));
46
+ try {
47
+ const response = await this.axios.request(requestConfig);
48
+ return new ApiResponse(response.status, response.data);
49
+ } catch (error) {
50
+ const response = error.response;
51
+ if (response) return new ApiResponse(response.status, response.data);
52
+ throw error;
53
+ }
54
+ }
55
+ };
56
+ export {
57
+ AxiosHttpAdapter
58
+ };
@@ -0,0 +1,102 @@
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/browser.ts
21
+ var browser_exports = {};
22
+ __export(browser_exports, {
23
+ CookieUtils: () => CookieUtils
24
+ });
25
+ module.exports = __toCommonJS(browser_exports);
26
+
27
+ // src/utils/CookieUtils.ts
28
+ var CookieUtils = class {
29
+ /**
30
+ * 读取指定 Cookie。
31
+ */
32
+ static read(name) {
33
+ const cookies = document.cookie ? document.cookie.split("; ") : [];
34
+ for (const item of cookies) {
35
+ const [cookieName, ...valueParts] = item.split("=");
36
+ if (cookieName === name) return valueParts.join("=");
37
+ }
38
+ return null;
39
+ }
40
+ /**
41
+ * 写入 Cookie。
42
+ *
43
+ * @param expires number 表示多少秒后过期;Date/string 表示绝对过期时间
44
+ */
45
+ static write(name, value, expires = 86400, path, domain, secure) {
46
+ let cookie = `${name}=${value}`;
47
+ if (expires !== void 0) {
48
+ const expiresAt = typeof expires === "number" ? new Date(Date.now() + expires * 1e3) : expires instanceof Date ? expires : new Date(expires);
49
+ cookie += `; expires=${expiresAt.toUTCString()}`;
50
+ }
51
+ if (path) cookie += `; path=${path}`;
52
+ if (domain) cookie += `; domain=${domain}`;
53
+ if (secure) cookie += "; secure";
54
+ document.cookie = cookie;
55
+ }
56
+ /**
57
+ * 删除指定 Cookie。
58
+ */
59
+ static delete(name, path, domain, secure) {
60
+ this.write(name, "", /* @__PURE__ */ new Date(0), path, domain, secure);
61
+ }
62
+ /**
63
+ * 将对象序列化为 JSON 后写入 Cookie。
64
+ */
65
+ static writeJSON(name, value, expires = 86400, path, domain, secure) {
66
+ this.write(name, JSON.stringify(value), expires, path, domain, secure);
67
+ }
68
+ /**
69
+ * 读取 Cookie 并按 JSON 反序列化。
70
+ */
71
+ static readJSON(name) {
72
+ const value = this.read(name);
73
+ if (!value) return null;
74
+ try {
75
+ return JSON.parse(value);
76
+ } catch {
77
+ return null;
78
+ }
79
+ }
80
+ /**
81
+ * 读取所有 Cookie。
82
+ */
83
+ static readAll() {
84
+ const cookies = {};
85
+ if (!document.cookie) return cookies;
86
+ for (const item of document.cookie.split("; ")) {
87
+ const [name, ...valueParts] = item.split("=");
88
+ if (name) cookies[name] = valueParts.join("=");
89
+ }
90
+ return cookies;
91
+ }
92
+ /**
93
+ * 判断指定 Cookie 是否存在。
94
+ */
95
+ static has(name) {
96
+ return this.read(name) !== null;
97
+ }
98
+ };
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ CookieUtils
102
+ });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 浏览器专用入口。
3
+ */
4
+ export { default as CookieUtils } from './utils/CookieUtils';
5
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,qBAAqB,CAAC"}
@@ -0,0 +1,75 @@
1
+ // src/utils/CookieUtils.ts
2
+ var CookieUtils = class {
3
+ /**
4
+ * 读取指定 Cookie。
5
+ */
6
+ static read(name) {
7
+ const cookies = document.cookie ? document.cookie.split("; ") : [];
8
+ for (const item of cookies) {
9
+ const [cookieName, ...valueParts] = item.split("=");
10
+ if (cookieName === name) return valueParts.join("=");
11
+ }
12
+ return null;
13
+ }
14
+ /**
15
+ * 写入 Cookie。
16
+ *
17
+ * @param expires number 表示多少秒后过期;Date/string 表示绝对过期时间
18
+ */
19
+ static write(name, value, expires = 86400, path, domain, secure) {
20
+ let cookie = `${name}=${value}`;
21
+ if (expires !== void 0) {
22
+ const expiresAt = typeof expires === "number" ? new Date(Date.now() + expires * 1e3) : expires instanceof Date ? expires : new Date(expires);
23
+ cookie += `; expires=${expiresAt.toUTCString()}`;
24
+ }
25
+ if (path) cookie += `; path=${path}`;
26
+ if (domain) cookie += `; domain=${domain}`;
27
+ if (secure) cookie += "; secure";
28
+ document.cookie = cookie;
29
+ }
30
+ /**
31
+ * 删除指定 Cookie。
32
+ */
33
+ static delete(name, path, domain, secure) {
34
+ this.write(name, "", /* @__PURE__ */ new Date(0), path, domain, secure);
35
+ }
36
+ /**
37
+ * 将对象序列化为 JSON 后写入 Cookie。
38
+ */
39
+ static writeJSON(name, value, expires = 86400, path, domain, secure) {
40
+ this.write(name, JSON.stringify(value), expires, path, domain, secure);
41
+ }
42
+ /**
43
+ * 读取 Cookie 并按 JSON 反序列化。
44
+ */
45
+ static readJSON(name) {
46
+ const value = this.read(name);
47
+ if (!value) return null;
48
+ try {
49
+ return JSON.parse(value);
50
+ } catch {
51
+ return null;
52
+ }
53
+ }
54
+ /**
55
+ * 读取所有 Cookie。
56
+ */
57
+ static readAll() {
58
+ const cookies = {};
59
+ if (!document.cookie) return cookies;
60
+ for (const item of document.cookie.split("; ")) {
61
+ const [name, ...valueParts] = item.split("=");
62
+ if (name) cookies[name] = valueParts.join("=");
63
+ }
64
+ return cookies;
65
+ }
66
+ /**
67
+ * 判断指定 Cookie 是否存在。
68
+ */
69
+ static has(name) {
70
+ return this.read(name) !== null;
71
+ }
72
+ };
73
+ export {
74
+ CookieUtils
75
+ };
@@ -0,0 +1,75 @@
1
+ // src/api/adapter/ApiAdapter.ts
2
+ var ApiAdapter = class {
3
+ constructor(options = {}) {
4
+ this.options = options;
5
+ }
6
+ /**
7
+ * 触发标准化进度回调。
8
+ */
9
+ emitProgress(handler, loaded, total, event) {
10
+ if (!handler) return;
11
+ const lengthComputable = typeof total === "number" && Number.isFinite(total) && total > 0;
12
+ const progress = {
13
+ loaded,
14
+ lengthComputable,
15
+ event
16
+ };
17
+ if (lengthComputable) {
18
+ progress.total = total;
19
+ progress.percent = Math.min(100, Math.round(loaded / total * 100));
20
+ }
21
+ handler(progress);
22
+ }
23
+ /**
24
+ * 将 fetch / axios / xhr 等运行时进度事件转换为标准进度对象。
25
+ */
26
+ emitProgressEvent(handler, event) {
27
+ const source = event;
28
+ const loaded = Number(source.loaded ?? 0);
29
+ const total = source.lengthComputable === false ? void 0 : source.total;
30
+ this.emitProgress(handler, loaded, total, event);
31
+ }
32
+ /**
33
+ * 将上传请求数据转换为 FormData。
34
+ */
35
+ formData(config, target) {
36
+ if (!config.isUpload) return;
37
+ const formData = this.options.formDataFactory?.() ?? this.createGlobalFormData();
38
+ const fileName = config.data.fileName ?? "file";
39
+ for (const [key, value] of Object.entries(config.data)) {
40
+ if (key !== "file" && key !== "fileName")
41
+ formData.append(key, value);
42
+ }
43
+ if (config.data.file !== void 0)
44
+ formData.append(fileName, config.data.file);
45
+ target.data = formData;
46
+ target.headers ?? (target.headers = {});
47
+ target.headers["Content-Type"] = "multipart/form-data";
48
+ target.method = "POST";
49
+ }
50
+ /**
51
+ * 使用全局 FormData 构造器。
52
+ */
53
+ createGlobalFormData() {
54
+ const FormDataCtor = globalThis.FormData;
55
+ if (!FormDataCtor) {
56
+ throw new Error(
57
+ "FormData is not available; pass formDataFactory to the adapter"
58
+ );
59
+ }
60
+ return new FormDataCtor();
61
+ }
62
+ };
63
+
64
+ // src/api/ApiResponse.ts
65
+ var ApiResponse = class {
66
+ constructor(status, data) {
67
+ this.status = status;
68
+ this.data = data;
69
+ }
70
+ };
71
+
72
+ export {
73
+ ApiAdapter,
74
+ ApiResponse
75
+ };