@fefeding/common
v1.0.60
Published
models utils
Readme
@fefeding/common
通用工具库,提供数据模型、HTTP 请求封装、日志记录、腾讯云 COS 操作等常用功能。
安装
npm install @fefeding/common
# 或
pnpm add @fefeding/common功能模块
📦 数据模型 (Models)
基础模型 (src/models/base/)
| 模块 | 说明 |
|------|------|
| model | 基础模型类,支持 JSON 对象复制和数组转换 |
| baseORM | TypeORM 实体基类,包含通用字段(valid、creator、updater、createTime、modifyTime) |
| request | API 请求基类,包含 api_token、timestamp、request_id |
| response | API 响应基类,包含 ret、msg、data |
| pagination | 分页请求/响应模型(PageRequest、PageResponse) |
| enumType | 通用枚举类型(EValid、EStatus) |
| cos | 腾讯云 COS 文本审核相关类型 |
账户模型 (src/models/account/)
| 模块 | 说明 |
|------|------|
| account | 登录账号模型 |
| user | 用户信息模型(含性别、启用状态枚举) |
| session | 会话管理模型(含多种登录方式请求/响应) |
| message | 消息模型 |
| app | 应用配置模型(支持微信、企业微信、QQ、百度等应用类型) |
| verificationCode | 验证码模型(图片、手机、邮件验证码) |
| wx | 微信相关接口请求/响应模型 |
AI 模型 (src/models/ai/)
| 模块 | 说明 |
|------|------|
| message | AI 消息体接口定义 |
🛠 工具类 (Utils)
日志记录器 (src/utils/logger.ts)
基于 pino 的远程日志记录器,支持本地日志和远程日志同步发送。
import { RemotePinoLogger } from '@fefeding/common';
const logger = new RemotePinoLogger({
remoteUrl: 'https://your-log-server.com/api/log',
serviceName: 'my-service',
logLevel: 'info',
apiKey: 'your-api-key'
});
logger.info('操作成功', { userId: 123 });
logger.error('发生错误', { error: err.message });HTTP 请求 (src/utils/axios.ts)
Axios 封装,支持直接传入请求模型对象。
import { requestApi, requestServer } from '@fefeding/common';
// 使用请求模型
const res = await requestApi(new LoginReq({ account, password }));
// 或直接使用 URL
const data = await requestApi('/api/user/info', { method: 'GET' });API Token 生成 (src/utils/api.ts)
基于 MD5 的 API Token 生成工具。
import api from '@fefeding/common';
const token = api.createApiToken('your-access-key');
// 返回 { sign: '...', timestamp: '...' }装饰器 (src/utils/decorator.ts)
提供 API 路由、权限校验等装饰器。
import decorators from '@fefeding/common';
// 标记 API 路由
@decorators.api({ url: '/api/user/save' })
export class SaveUserReq extends Request {
data: User;
}
// 标记需要 Token 校验
@decorators.checkApiToken()
async saveUser() { ... }
// 标记需要登录态校验
@decorators.checkApiLogin()
async getUserInfo() { ... }腾讯云 COS 操作 (src/utils/txCos.ts)
腾讯云对象存储操作封装。
import { createClient, uploadFile, getFile, getObjectUrl, checkExists } from '@fefeding/common';
const cos = createClient({
SecretId: 'your-secret-id',
SecretKey: 'your-secret-key'
});
// 上传文件
await uploadFile({
Bucket: 'your-bucket',
Region: 'ap-guangzhou',
Key: 'file.txt',
Body: Buffer.from('content')
}, cos);
// 获取文件预签名 URL
const url = await getObjectUrl({
Bucket: 'your-bucket',
Region: 'ap-guangzhou',
Key: 'file.txt',
Sign: true
}, cos);
// 检查文件是否存在
const exists = await checkExists({
Bucket: 'your-bucket',
Region: 'ap-guangzhou',
Key: 'file.txt'
}, cos);随机字符串生成 (src/utils/rand.ts)
import { randString, stringToNumber } from '@fefeding/common';
// 生成随机短码
const code = randString(123, 8); // 基于ID生成唯一短码
// 字符串转数字(用于哈希计算)
const num = stringToNumber('abc');开发
环境要求
- Node.js >= 16
- pnpm (推荐)
构建
# 安装依赖
pnpm install
# 构建
pnpm build
# 清理构建产物
pnpm clean测试
# 运行测试
pnpm test
# 监听模式
pnpm test:watch
# 测试覆盖率
pnpm test:coverage项目结构
├── src/
│ ├── models/ # 数据模型
│ │ ├── base/ # 基础模型
│ │ ├── account/ # 账户相关模型
│ │ └── ai/ # AI 相关模型
│ └── utils/ # 工具类
│ ├── api.ts # API Token 生成
│ ├── axios.ts # HTTP 请求封装
│ ├── decorator.ts # 装饰器
│ ├── logger.ts # 日志记录器
│ ├── pdf.ts # PDF 处理(已禁用)
│ ├── rand.ts # 随机字符串生成
│ └── txCos.ts # 腾讯云 COS 操作
├── test/ # 测试文件
├── dist/ # 构建产物
├── build.js # 构建脚本
├── gulpfile.js # Gulp 构建配置
└── tsconfig.json # TypeScript 配置License
MIT © fefeding
