@jnrs/shared

v1.1.11

Published

巨能前端工程化开发,通用工具包,与框架无关的纯逻辑、类型、常量、工具函数,完全不依赖任何 UI 框架或运行时环境。

Readme

@jnrs/shared

✨ 介绍

巨能前端工程化开发通用工具库,包含纯逻辑、类型、常量、工具函数等,不依赖任何 UI 框架。

  • 网络请求及相关类型和接口
  • 错误处理机制
  • 业务逻辑服务
  • 缓存策略
  • 通用类型、接口、枚举、常量
  • 纯函数工具库
  • 通用算法
  • CSS 变量
  • (已提供国际化支持)

💻 技术栈

TypeScript

🧩 安装教程

pnpm add @jnrs/shared

🔍 使用示例

import '@jnrs/shared/styles/theme.scss'
import type { User, Locale } from '@jnrs/shared'
import type { BusinessRequest, BusinessResponse } from '@jnrs/shared/request'
import { createAxiosInstance } from '@jnrs/shared/request'
import { Fullscreen } from '@jnrs/shared'
import { debounce } from '@jnrs/shared/lodash'
import { uuidv4 } from '@jnrs/shared/uuid'

📋 API

@jnrs/shared 模块

类型 & 接口

  • User
  • DictItem
  • Dict
  • FileItem
  • Locale
  • ThemeMode

  • Fullscreen

函数

  • hasPermissionWithSuffix 判断用户是否拥有指定后缀类型的权限(如 '*', ':view', ':edit')
  • objectToFormData 普通对象转 FormData 对象
  • setDefaultLocale
  • formatDateTime
  • formatWeekday

@jnrs/shared/locales 模块

函数

  • changeLocales 修改 @jnrs/shared 语言('zhCn' | 'en')

@jnrs/shared/request 模块

接口

  • Options

| 属性名 | 类型 | 说明 | |--------|------|------| | baseURL | string | 请求的根路径 | | timeout | number | 请求超时时间(ms) | | showErrorMsg | boolean | 是否显示错误信息 | | headers | Record<string, string> | 请求头 |

  • MessageOptions

| 属性名 | 类型 | 说明 | |--------|------|------| | message | string | 消息内容 | | type | 'success' \| 'error' \| 'warning' | 消息类型 | | grouping | boolean | 是否开启消息分组 | | duration | number | 消息显示持续时间(ms) | | showClose | boolean | 是否显示关闭按钮 |

  • CreateRequestOptions

| 属性名 | 类型 | 说明 | |--------|------|------| | options | Options | 请求配置项 | | responseMap | Record<number, string> | HTTP 状态码与提示信息的映射 | | handleMessageFn | (opts: MessageOptions) => void | 自定义消息处理函数 | | handleGetTokenFn | () => string \| null \| undefined | 获取 token 的函数 | | handleNoAuthFn | () => void | 未授权时的处理函数 |

  • BusinessRequest(泛型 D 默认为 Data)

| 属性名 | 类型 | 说明 | |--------|------|------| | url | string | 请求地址 | | mockUrl | string | MOCK 请求地址 | | method | 'get' \| 'post' \| 'put' \| 'delete' \| 'patch' \| 'head' \| 'options' | 请求方法 | | headers | Record<string, string> | 请求头 | | params | Record<string, unknown> | URL 查询参数 | | data | D | 请求体数据 | | showErrorMsg | boolean | 是否显示错误信息 | | noAuth | boolean | 是否跳过授权验证 | | responseType | 'json' \| 'blob' \| 'arraybuffer' \| 'text' \| 'stream' | 响应数据类型 |

  • BusinessResponse(泛型 T 默认为 unknown)

| 属性名 | 类型 | 说明 | |--------|------|------| | code | number | 业务状态码 | | msg | string | 提示信息 | | data | T | 业务数据 |

函数

  • createAxiosInstance
/**
 * 创建带默认配置的 axios 实例(已将副作用抽离为可配置项函数)
 * @param options - 实例的配置项
 * @param responseMap - 响应代码映射表
 * @param handleMessageFn - 提示信息函数
 * @param handleGetTokenFn - 获取 token 函数
 * @param handleNoAuthFn - 登录失败处理函数
 * @returns axiosInstance
 */

@jnrs/shared/date-fns 模块

@jnrs/shared/lodash 模块

@jnrs/shared/uuid 模块

@jnrs/shared/mitt 模块

@jnrs/shared/validate 校验规则模块

@jnrs/shared/validator 校验方法模块