@zero-library/common 2.1.9 → 2.1.11

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/dist/index.d.ts CHANGED
@@ -284,7 +284,7 @@ interface RenderMarkdownProps {
284
284
  * Markdown渲染组件
285
285
  * 将Markdown文本转换为HTML并渲染为React组件
286
286
  * 支持自定义组件、搜索高亮、内容编辑等功能
287
- * @param props - 组件属性
287
+ * @param RenderMarkdownProps - 组件属性
288
288
  */
289
289
  declare const _default$1: ({ content, searchValue, customComponents, onChange, onPartialChange }: RenderMarkdownProps) => react_jsx_runtime.JSX.Element;
290
290
 
@@ -392,9 +392,15 @@ declare function useIframeRelayBridge(allowedOrigins?: string[]): {
392
392
  };
393
393
 
394
394
  /**
395
- * 通用的 Valtio Context 工厂
396
- * 支持:
397
- * - useStore() 获取 store 实例
395
+ * 通用的 Valtio Context 创建工厂 Hook
396
+ * 创建 Valtio 状态管理的 Context 和 Provider
397
+ *
398
+ * 支持的操作:
399
+ * - ValtioProvider:包裹组件以提供 store 状态
400
+ * - useValtioStore:在组件中获取 store 实例
401
+ *
402
+ * @template T - Store 对象类型
403
+ * @returns Valtio Context 相关的工具函数
398
404
  */
399
405
  declare function useCreateValtioContext<T extends object>(): {
400
406
  ValtioProvider: ({ store, children }: {
@@ -402,27 +408,37 @@ declare function useCreateValtioContext<T extends object>(): {
402
408
  children?: ReactNode;
403
409
  }) => react_jsx_runtime.JSX.Element;
404
410
  useValtioStore: () => T;
411
+ Context: react.Context<T>;
405
412
  };
406
413
 
407
414
  /**
408
- * 防抖hook
409
- * @param func 需要执行的函数
410
- * @param wait 延迟时间
415
+ * 防抖 Hook
416
+ * 在指定时间内多次调用只执行最后一次,延迟指定时间后执行
417
+ *
418
+ * @param func - 需要防抖的函数
419
+ * @param wait - 延迟时间(毫秒),默认为400ms
420
+ * @returns 防抖处理后的函数,包含 flush 和 cancel 方法
411
421
  */
412
422
  declare function useDebounce<A extends Array<any>, R = void>(func: (..._args: A) => R, wait?: number): {
413
423
  (..._args: A): Promise<R>;
424
+ /** 添加额外方法到防抖函数 */
414
425
  flush: () => any;
415
426
  cancel: () => void;
416
427
  };
417
428
 
418
429
  /**
419
- * useDeepEffect
420
- * 深层对比依赖项的 useEffect
430
+ * 深度比较 useEffect Hook
431
+ * 使用深度比较算法来检测依赖项变化,避免不必要的重新渲染
432
+ *
433
+ * @param effect - 要执行的副作用函数
434
+ * @param deps - 依赖项数组
421
435
  */
422
436
  declare function useDeepEffect(effect: React.EffectCallback, deps: any[]): void;
423
437
 
424
438
  /**
425
- * 创建同时具有 ref 和 state 特性的状态
439
+ * 创建同时具有 ref 和 state 特性的状态 Hook
440
+ * 解决了 useState 在异步闭包中获取不到最新值的问题
441
+ *
426
442
  * @param init - 初始值
427
443
  * @returns [state, setState, getState] 元组,包含状态值、设置函数和获取函数
428
444
  */
@@ -431,6 +447,8 @@ declare function useRefState<T>(init: T): readonly [T, (newVal: T) => void, () =
431
447
  /**
432
448
  * 同步输入框组件和外部状态的 Hook
433
449
  * 解决输入中文时因连续渲染关闭中文输入法的问题
450
+ *
451
+ * @template T - 值类型
434
452
  * @param storeValue - 外部存储的值
435
453
  * @param setStoreValue - 设置外部存储值的函数
436
454
  * @returns 包含 inputValue 和 setInputValue 的对象
@@ -441,12 +459,18 @@ declare const useSyncInput: <T>(storeValue: T, setStoreValue: (value: T) => void
441
459
  };
442
460
 
443
461
  /**
444
- * 节流hook
445
- * @param func 需要执行的函数
446
- * @param wait 延迟时间
462
+ * 节流 Hook
463
+ * 在指定时间间隔内只执行一次函数,节流控制函数调用频率
464
+ *
465
+ * @template A - 函数参数类型
466
+ * @template R - 返回值类型
467
+ * @param func - 需要节流的函数
468
+ * @param wait - 节流间隔时间(毫秒)
469
+ * @returns 节流处理后的函数,包含 flush 和 cancel 方法
447
470
  */
448
471
  declare function useThrottle<A extends Array<any>, R = void>(func: (..._args: A) => R, wait: number): {
449
472
  (..._args: A): Promise<R>;
473
+ /** 添加额外方法到节流函数 */
450
474
  flush: () => any;
451
475
  cancel: () => void;
452
476
  };
@@ -508,18 +532,19 @@ declare const objToOptions: (obj: ObjectType<string>) => {
508
532
  declare const arrToObj: <T>(arr: T[], key: string) => {
509
533
  [key: string]: T;
510
534
  };
511
- /**
512
- * 使用 setTimeout 模拟 setInterval,避免 setInterval 的问题
513
- * @param fn - 要重复执行的函数
514
- * @param t - 间隔时间(毫秒)
515
- * @returns 控制对象,包含 isRun 和 cancel 方法
516
- */
517
535
  interface NsSetIntervalReturnType {
518
536
  /** 检查是否正在运行 */
519
537
  isRun: () => boolean;
520
538
  /** 取消定时器 */
521
539
  cancel: () => void;
522
540
  }
541
+ /**
542
+ * 使用 setTimeout 模拟 setInterval 的返回类型接口
543
+ * 避免 setInterval 的内存泄漏问题
544
+ * @param fn - 要重复执行的函数
545
+ * @param t - 间隔时间(毫秒)
546
+ * @returns 控制对象,包含 isRun 和 cancel 方法
547
+ */
523
548
  declare const nsSetInterval: (fn: () => void, t: number) => NsSetIntervalReturnType;
524
549
  /**
525
550
  * 生成不重复的唯一ID
@@ -530,14 +555,22 @@ declare const genNonDuplicateID: () => string;
530
555
  * 复制文本到剪贴板
531
556
  * 优先使用 navigator.clipboard API,降级使用 execCommand
532
557
  * @param text - 要复制的文本
533
- * @returns Promise
558
+ * @param prompt - 提示语
559
+ * @returns void
534
560
  */
535
- declare const copyText: (text: string) => Promise<void>;
561
+ declare const copyText: (text: string, prompt?: string) => void;
536
562
  /**
537
563
  * 把后台接口字段,返回的关于金额的数字,转换为千分符号分隔的数字
538
564
  * return string
539
565
  */
540
566
  declare function formatNumberWithCommas(number: number | string): string | number;
567
+ /**
568
+ * 生成指定范围内的不重复随机数
569
+ * @param min - 最小值
570
+ * @param max - 最大值
571
+ * @param count - 生成数量
572
+ * @returns 不重复的随机数数组
573
+ */
541
574
  declare const generateRandomNumbers: (min: number, max: number, count: number) => number[];
542
575
  /**
543
576
  * 从文件路径中提取文件名
@@ -564,30 +597,55 @@ declare const textAreaView: (con?: string) => string;
564
597
  * @returns URL 查询参数字符串
565
598
  */
566
599
  declare const buildUrlParams: (obj: Record<string, any>, url?: string) => string;
600
+ /**
601
+ * 下载文件到本地
602
+ * @param url - 文件下载地址
603
+ * @param name - 下载文件名,默认为'图片下载'
604
+ */
567
605
  declare const downloadFile: (url: string, name?: string) => void;
568
606
  /**
569
- * 给URL路径最后加/
607
+ * 给URL路径最后添加斜杠
608
+ * @param url - 要处理的URL,默认为空字符串
609
+ * @returns 处理后的URL
570
610
  */
571
611
  declare const addUrlLastSlash: (url?: string) => string;
572
612
  /**
573
- * 给url增加mainSource参数
613
+ * 给URL增加mainSource参数
614
+ * @param url - 目标URL
615
+ * @param preHref - 参考URL,用于获取mainSource参数
616
+ * @returns 处理后的URL
574
617
  */
575
618
  declare const getUrlMainSource: (url: string, preHref?: string) => string;
576
619
  /**
577
- * 获取 WebSocket 地址
578
- * @param path WebSocket 服务路径,比如 "/ws" 或 "/socket"
579
- * @param customHost 可选,指定 host:port;默认取当前 window.location.host
620
+ * 获取 WebSocket 连接地址
621
+ * @param path - WebSocket 服务路径,比如 "/ws" 或 "/socket"
622
+ * @param customHost - 可选,指定 host:port;默认取当前 window.location.host
623
+ * @returns 完整的 WebSocket URL
580
624
  */
581
625
  declare const getWebSocketUrl: (path: string, customHost?: string) => string;
626
+ /**
627
+ * 对象转换函数,将源对象转换为目标对象
628
+ * @param source - 源对象
629
+ * @param fieldMap - 字段映射配置
630
+ * @returns 转换后的目标对象
631
+ */
582
632
  declare function transform<T extends object, U extends object>(source: T, fieldMap: {
583
633
  [K in keyof U]: keyof T | ((source: T) => U[K]) | U[K];
584
634
  }): U;
635
+ /**
636
+ * 批量对象转换函数
637
+ * @param sources - 源对象数组
638
+ * @param fieldMap - 字段映射配置
639
+ * @returns 转换后的目标对象数组
640
+ */
585
641
  declare function transforms<T extends object, U extends object>(sources: T[], fieldMap: {
586
642
  [K in keyof U]: keyof T | ((source: T) => U[K]) | U[K];
587
643
  }): U[];
588
644
 
589
645
  type DateType = string | number | Date | dayjs.Dayjs | null;
646
+ /** 默认日期时间格式:年-月-日 时:分:秒 */
590
647
  declare const DateFormatType = "YYYY-MM-DD HH:mm:ss";
648
+ /** 默认日期格式:年-月-日 */
591
649
  declare const DateFormatType2 = "YYYY-MM-DD";
592
650
  /**
593
651
  * 获取一天的最早时间 '2023/10/17 00:00:00' 的时间戳
@@ -652,7 +710,9 @@ declare function isEmpty<T = unknown>(val: T): val is T;
652
710
  declare function isWindow(val: any): val is Window;
653
711
  declare function isElement(val: unknown): val is Element;
654
712
  declare function isMap(val: unknown): val is Map<any, any>;
713
+ /** 判断是否为服务端环境(无 window 对象) */
655
714
  declare const isServer: boolean;
715
+ /** 判断是否为客户端环境(有 window 对象) */
656
716
  declare const isClient: boolean;
657
717
  /**
658
718
  * 判断是否是外链
@@ -664,60 +724,191 @@ declare const isExternal: (path: string) => boolean;
664
724
  declare const isBlob: (val: unknown) => boolean;
665
725
  declare const isLocalhost: (host?: string) => boolean;
666
726
 
667
- interface UserInfo {
668
- id: number;
669
- orgName: string;
670
- name: string;
671
- username: string;
672
- mobile: string;
673
- avatar: string;
674
- myInviteCode: string;
675
- email?: string;
676
- remark?: string;
677
- roles: number[];
678
- permissions: string[];
679
- }
727
+ /**
728
+ * 设置当前用户信息到 sessionStorage(AES 加密存储)
729
+ * @param userInfo - 用户信息对象
730
+ */
680
731
  declare function setCurrentUser(userInfo: any): void;
732
+ /**
733
+ * 从 sessionStorage 获取当前用户信息(AES 解密)
734
+ * @returns 用户信息对象或 null
735
+ */
681
736
  declare function getCurrentUser(): any;
737
+ /**
738
+ * 清除 sessionStorage 中的用户信息
739
+ */
682
740
  declare function clearCurrentUser(): void;
741
+ /**
742
+ * 获取或生成设备唯一标识
743
+ * @returns 设备ID字符串
744
+ */
683
745
  declare function getDeviceId(): string;
684
746
 
747
+ /** 数学运算符类型 */
685
748
  type OperatorType = '+' | '-' | '*' | '/';
749
+ /**
750
+ * 通用数学计算函数
751
+ * @param operator - 运算符:+、-、*、/
752
+ * @param args - 要计算的数值数组
753
+ * @returns 计算结果字符串
754
+ */
686
755
  declare const calculate: (operator: OperatorType, ...args: Decimal.Value[]) => string;
756
+ /**
757
+ * 精确加法运算
758
+ * 调用:plus(arg1,arg2)
759
+ * 返回值:arg1加arg2的精确结果
760
+ */
687
761
  declare const plus: (...args: Decimal.Value[]) => string;
762
+ /**
763
+ * 精确减法运算
764
+ * 调用:minus(arg1,arg2)
765
+ * 返回值:arg1减arg2的精确结果
766
+ */
688
767
  declare const minus: (...args: Decimal.Value[]) => string;
768
+ /**
769
+ * 精确乘法运算
770
+ * 调用:times(arg1,arg2)
771
+ * 返回值:arg1乘以arg2的精确结果
772
+ */
689
773
  declare const times: (...args: Decimal.Value[]) => string;
774
+ /**
775
+ * 精确除法运算
776
+ * 调用:dividedBy(arg1,arg2)
777
+ * 返回值:arg1除以arg2的精确结果
778
+ */
690
779
  declare const dividedBy: (...args: Decimal.Value[]) => string;
780
+ /**
781
+ * 查看小数位数(不计算小数点最后末尾的0)
782
+ * @param arg1 - 要检查的数值
783
+ * @returns 小数位数
784
+ */
691
785
  declare const decimalPlaces: (arg1: Decimal.Value) => number;
786
+ /**
787
+ * 查看数值总位数(包括整数和小数部分)
788
+ * @param arg1 - 要检查的数值
789
+ * @returns 总位数
790
+ */
692
791
  declare const precision: (arg1: Decimal.Value) => number;
792
+ /**
793
+ * 取绝对值
794
+ * @param arg1 - 要处理的数值
795
+ * @returns 绝对值字符串
796
+ */
693
797
  declare const absVal: (arg1: Decimal.Value) => string;
798
+ /**
799
+ * 数值比较函数
800
+ * @param arg1 - 第一个数值
801
+ * @param arg2 - 第二个数值
802
+ * @param type - 比较类型:>、>=、<、<=、==
803
+ * @returns 比较结果布尔值
804
+ */
694
805
  declare const compareNum: (arg1: Decimal.Value, arg2: Decimal.Value, type: string) => boolean;
806
+ /**
807
+ * 四舍五入处理
808
+ * @param num - 要处理的数值
809
+ * @param decimals - 保留小数位数,默认为2
810
+ * @param fill - 是否补0,默认为true
811
+ * @returns 处理后的数字字符串
812
+ */
695
813
  declare const toFixed: (num: Decimal.Value, decimals?: number, fill?: boolean) => string;
814
+ /**
815
+ * 判断是否为整数
816
+ * @param num - 要判断的数值
817
+ * @returns 是否为整数
818
+ */
696
819
  declare const isInteger: (num: Decimal.Value) => boolean;
820
+ /**
821
+ * 判断是否为负数
822
+ * @param num - 要判断的数值
823
+ * @returns 是否为负数
824
+ */
697
825
  declare const isNegative: (num: Decimal.Value) => boolean;
698
826
 
827
+ /** HTTP 状态码常量:成功 */
699
828
  declare const OK = 200;
829
+ /** HTTP 状态码常量:服务器错误 */
700
830
  declare const ERROR = 500;
831
+ /** HTTP 状态码常量:缺少参数 */
701
832
  declare const MISSING_PARAMETER = 400;
833
+ /** HTTP 状态码常量:未授权 */
702
834
  declare const UNAUTHORIZED = 401;
835
+ /** HTTP 状态码常量:禁止访问 */
703
836
  declare const FORBIDDEN = 403;
837
+ /** HTTP 状态码常量:未找到资源 */
704
838
  declare const NOT_FOUND = 404;
839
+ /** 业务错误码常量:权限拒绝 */
705
840
  declare const PERMISSION_DENIED = 13001;
841
+ /**
842
+ * 创建 Axios 请求实例
843
+ * @param baseURL - API 基础路径,默认为 '/api'
844
+ * @returns 请求实例对象
845
+ */
706
846
  declare function createRequest(baseURL?: string): {
847
+ /**
848
+ * GET 请求方法
849
+ * @param url - 请求地址
850
+ * @param params - 查询参数
851
+ * @param options - Axios 配置选项
852
+ * @returns 响应数据
853
+ */
707
854
  get: (url: string, params?: any, options?: AxiosRequestConfig) => Promise<any>;
855
+ /**
856
+ * POST 请求方法
857
+ * @param url - 请求地址
858
+ * @param data - 请求数据
859
+ * @param options - Axios 配置选项
860
+ * @returns 响应数据
861
+ */
708
862
  post: (url: string, data?: any, options?: AxiosRequestConfig) => Promise<any>;
863
+ /**
864
+ * PUT 请求方法
865
+ * @param url - 请求地址
866
+ * @param data - 请求数据
867
+ * @param options - Axios 配置选项
868
+ * @returns 响应数据
869
+ */
709
870
  put: (url: string, data?: any, options?: AxiosRequestConfig) => Promise<any>;
871
+ /**
872
+ * DELETE 请求方法
873
+ * @param url - 请求地址
874
+ * @param data - 请求数据
875
+ * @param options - Axios 配置选项
876
+ * @returns 响应数据
877
+ */
710
878
  delete: (url: string, data?: any, options?: AxiosRequestConfig) => Promise<any>;
711
879
  };
712
880
 
881
+ /** Token 存储键名 */
713
882
  declare const TOKEN_KEY = "NS-TOKEN";
883
+ /**
884
+ * 设置访问令牌到 localStorage
885
+ * @param accessToken - 访问令牌
886
+ */
714
887
  declare function setToken(accessToken: string): void;
888
+ /**
889
+ * 从 URL 查询参数中获取 Token
890
+ * @returns Token 字符串或空字符串
891
+ */
715
892
  declare function getUrlToken(): string;
893
+ /**
894
+ * 获取当前有效的访问令牌
895
+ * 优先从 URL 参数获取,其次从 localStorage 获取
896
+ * @returns Token 字符串或空字符串
897
+ */
716
898
  declare function getToken(): string;
899
+ /**
900
+ * 清除 localStorage 中的 Token
901
+ */
717
902
  declare function clearToken(): void;
903
+ /**
904
+ * 获取登录页面路径
905
+ * @returns 登录路径,默认为 '/sign-in'
906
+ */
718
907
  declare function getSignPath(): string;
719
908
 
909
+ /** 项目主色调 */
720
910
  declare const LgPrimaryColor: string;
911
+ /** Ant Design 主题配置 */
721
912
  declare const themeConfig: ThemeConfig;
722
913
 
723
- export { _default$b as AudioPlayer, type AudioPlayerProps, type ComponentMapType, DateFormatType, DateFormatType2, ERROR, FORBIDDEN, _default$a as FileIcon, type FileIconProps, _default$9 as FilePreview, _default$8 as FilePreviewDrawer, type FilePreviewDrawerProps, type FilePreviewProps, _default$4 as Iframe, type IframeProps, _default$3 as LazyComponent, type LazyComponentProps, LgPrimaryColor, MISSING_PARAMETER, _default$2 as MarkdownEditor, type MarkdownEditorProps, _default$7 as MarkdownPreview, type MarkdownPreviewProps, NOT_FOUND, type NsSetIntervalReturnType, OK, PERMISSION_DENIED, type Params, _default$6 as PdfPreview, type PdfPreviewProps, type RenderControl, type RenderControlObj, _default$1 as RenderMarkdown, type RenderMarkdownProps, RenderWrapper, type RenderWrapperProps, TOKEN_KEY, UNAUTHORIZED, _default as UserAvatar, type UserAvatarProps, type UserInfo, _default$5 as VideoPlayer, type VideoPlayerProps, absVal, addUrlLastSlash, arrToObj, buildUrlParams, calculate, clearCurrentUser, clearToken, compareNum, copyText, createRequest, decimalPlaces, deepCopy, deepEqual, deepMerge, dividedBy, downloadFile, emit, emitToChild, formatDate, formatNumberWithCommas, genNonDuplicateID, generateRandomNumbers, getCurrentUser, getDeviceId, getEndOfTimestamp, getFileName, getFileSuffixName, getSignPath, getStartOfTimestamp, getTimestamp, getToken, getUrlMainSource, getUrlToken, getWebSocketUrl, is, isArray, isBlob, isBoolean, isClient, isDate, isDef, isElement, isEmpty, isEmptyObj, isExpire, isExternal, isFunction, isInteger, isLocalhost, isMap, isNegative, isNull, isNullOrUnDef, isNumber, isNumberNoNaN, isObject, isPromise, isRegExp, isServer, isString, isUnDef, isWindow, minus, nsSetInterval, objToOptions, plus, precision, propsMerge, setCurrentUser, setToken, shouldRender, textAreaView, themeConfig, times, toFixed, transform, transforms, useCreateValtioContext, useDebounce, useDeepEffect, useIframeRelayBridge, useRefState, useSyncInput, useThrottle, useWebSocket };
914
+ export { _default$b as AudioPlayer, type AudioPlayerProps, type ComponentMapType, DateFormatType, DateFormatType2, ERROR, FORBIDDEN, _default$a as FileIcon, type FileIconProps, _default$9 as FilePreview, _default$8 as FilePreviewDrawer, type FilePreviewDrawerProps, type FilePreviewProps, _default$4 as Iframe, type IframeProps, _default$3 as LazyComponent, type LazyComponentProps, LgPrimaryColor, MISSING_PARAMETER, _default$2 as MarkdownEditor, type MarkdownEditorProps, _default$7 as MarkdownPreview, type MarkdownPreviewProps, NOT_FOUND, type NsSetIntervalReturnType, OK, PERMISSION_DENIED, type Params, _default$6 as PdfPreview, type PdfPreviewProps, type RenderControl, type RenderControlObj, _default$1 as RenderMarkdown, type RenderMarkdownProps, RenderWrapper, type RenderWrapperProps, TOKEN_KEY, UNAUTHORIZED, _default as UserAvatar, type UserAvatarProps, _default$5 as VideoPlayer, type VideoPlayerProps, absVal, addUrlLastSlash, arrToObj, buildUrlParams, calculate, clearCurrentUser, clearToken, compareNum, copyText, createRequest, decimalPlaces, deepCopy, deepEqual, deepMerge, dividedBy, downloadFile, emit, emitToChild, formatDate, formatNumberWithCommas, genNonDuplicateID, generateRandomNumbers, getCurrentUser, getDeviceId, getEndOfTimestamp, getFileName, getFileSuffixName, getSignPath, getStartOfTimestamp, getTimestamp, getToken, getUrlMainSource, getUrlToken, getWebSocketUrl, is, isArray, isBlob, isBoolean, isClient, isDate, isDef, isElement, isEmpty, isEmptyObj, isExpire, isExternal, isFunction, isInteger, isLocalhost, isMap, isNegative, isNull, isNullOrUnDef, isNumber, isNumberNoNaN, isObject, isPromise, isRegExp, isServer, isString, isUnDef, isWindow, minus, nsSetInterval, objToOptions, plus, precision, propsMerge, setCurrentUser, setToken, shouldRender, textAreaView, themeConfig, times, toFixed, transform, transforms, useCreateValtioContext, useDebounce, useDeepEffect, useIframeRelayBridge, useRefState, useSyncInput, useThrottle, useWebSocket };