@sycsq/common 0.0.1 → 0.0.3
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/LICENSE +21 -0
- package/README.md +279 -64
- package/lib/index.esm.js +360 -359
- package/lib/index.umd.js +8 -8
- package/package.json +56 -15
- package/types/http/index.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 sycsq
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,27 +1,164 @@
|
|
|
1
1
|
# Personal Utils
|
|
2
2
|
|
|
3
|
-
一个基于
|
|
3
|
+
一个基于 Vite 构建的 TypeScript 工具库,提供 HTTP 请求封装和常用工具函数,支持完整的类型声明。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## ✨ 特性
|
|
6
|
+
|
|
7
|
+
- 🚀 基于 Vite 构建,支持 ES Module 和 UMD 格式
|
|
8
|
+
- 📦 完整的 TypeScript 类型支持
|
|
9
|
+
- 🔧 封装的 HTTP 请求工具,基于 Axios
|
|
10
|
+
- 🛠️ 丰富的工具函数集合
|
|
11
|
+
- 📱 支持浏览器和 Node.js 环境
|
|
12
|
+
- 🎯 零配置,开箱即用
|
|
13
|
+
|
|
14
|
+
## 📦 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @sycsq/common
|
|
18
|
+
# 或
|
|
19
|
+
yarn add @sycsq/common
|
|
20
|
+
# 或
|
|
21
|
+
pnpm add @sycsq/common
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 🚀 快速开始
|
|
25
|
+
|
|
26
|
+
### HTTP 工具使用
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { http } from '@sycsq/common';
|
|
30
|
+
|
|
31
|
+
// GET 请求
|
|
32
|
+
const getData = async () => {
|
|
33
|
+
try {
|
|
34
|
+
const response = await http.get('/api/users');
|
|
35
|
+
console.log(response.data);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error('请求失败:', error);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// POST 请求
|
|
42
|
+
const createUser = async (userData: any) => {
|
|
43
|
+
try {
|
|
44
|
+
const response = await http.post('/api/users', userData);
|
|
45
|
+
console.log('用户创建成功:', response.data);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error('创建失败:', error);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 工具函数使用
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { isString, isEmpty, isUrl } from '@sycsq/common';
|
|
56
|
+
|
|
57
|
+
// 类型检查
|
|
58
|
+
console.log(isString('hello')); // true
|
|
59
|
+
console.log(isString(123)); // false
|
|
60
|
+
|
|
61
|
+
// 空值检查
|
|
62
|
+
console.log(isEmpty('')); // true
|
|
63
|
+
console.log(isEmpty([])); // true
|
|
64
|
+
console.log(isEmpty({})); // true
|
|
65
|
+
|
|
66
|
+
// URL 验证
|
|
67
|
+
console.log(isUrl('https://example.com')); // true
|
|
68
|
+
console.log(isUrl('not-a-url')); // false
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 📚 API 文档
|
|
72
|
+
|
|
73
|
+
### HTTP 模块
|
|
74
|
+
|
|
75
|
+
#### 配置选项
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
interface RequestOptions {
|
|
79
|
+
joinPrefix?: boolean; // 是否添加 URL 前缀
|
|
80
|
+
isReturnNativeResponse?: boolean; // 是否返回原生响应
|
|
81
|
+
isTransformResponse?: boolean; // 是否转换响应数据
|
|
82
|
+
joinParamsToUrl?: boolean; // POST 请求是否将参数添加到 URL
|
|
83
|
+
formatDate?: boolean; // 是否格式化日期参数
|
|
84
|
+
apiUrl?: string; // API 基础地址
|
|
85
|
+
urlPrefix?: string; // URL 前缀
|
|
86
|
+
joinTime?: boolean; // 是否添加时间戳
|
|
87
|
+
ignoreCancelToken?: boolean; // 是否忽略重复请求
|
|
88
|
+
withToken?: boolean; // 是否携带 Token
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### 请求方法
|
|
93
|
+
|
|
94
|
+
- `http.get(url, config?)` - GET 请求
|
|
95
|
+
- `http.post(url, data?, config?)` - POST 请求
|
|
96
|
+
- `http.put(url, data?, config?)` - PUT 请求
|
|
97
|
+
- `http.delete(url, config?)` - DELETE 请求
|
|
98
|
+
- `http.patch(url, data?, config?)` - PATCH 请求
|
|
99
|
+
|
|
100
|
+
### 工具函数模块
|
|
101
|
+
|
|
102
|
+
#### 类型检查函数
|
|
103
|
+
|
|
104
|
+
- `is(val, type)` - 检查值是否为指定类型
|
|
105
|
+
- `isString(val)` - 检查是否为字符串
|
|
106
|
+
- `isNumber(val)` - 检查是否为数字
|
|
107
|
+
- `isBoolean(val)` - 检查是否为布尔值
|
|
108
|
+
- `isObject(val)` - 检查是否为对象
|
|
109
|
+
- `isArray(val)` - 检查是否为数组
|
|
110
|
+
- `isFunction(val)` - 检查是否为函数
|
|
111
|
+
- `isDate(val)` - 检查是否为日期
|
|
112
|
+
- `isPromise(val)` - 检查是否为 Promise
|
|
113
|
+
- `isRegExp(val)` - 检查是否为正则表达式
|
|
114
|
+
|
|
115
|
+
#### 值检查函数
|
|
116
|
+
|
|
117
|
+
- `isDef(val)` - 检查是否已定义
|
|
118
|
+
- `isUnDef(val)` - 检查是否未定义
|
|
119
|
+
- `isNull(val)` - 检查是否为 null
|
|
120
|
+
- `isEmpty(val)` - 检查是否为空值
|
|
121
|
+
- `isNullOrUnDef(val)` - 检查是否为 null 或 undefined
|
|
122
|
+
|
|
123
|
+
#### 环境检查函数
|
|
124
|
+
|
|
125
|
+
- `isServer` - 是否为服务端环境
|
|
126
|
+
- `isClient` - 是否为客户端环境
|
|
127
|
+
- `isWindow(val)` - 检查是否为 Window 对象
|
|
128
|
+
- `isElement(val)` - 检查是否为 DOM 元素
|
|
129
|
+
- `isUrl(path)` - 检查是否为有效 URL
|
|
130
|
+
|
|
131
|
+
## 🏗️ 项目结构
|
|
6
132
|
|
|
7
133
|
```
|
|
8
134
|
personal-utils/
|
|
9
|
-
├── packages/
|
|
10
|
-
│ ├── index.ts
|
|
11
|
-
│
|
|
12
|
-
├──
|
|
13
|
-
│ ├──
|
|
14
|
-
│ ├──
|
|
15
|
-
│ ├──
|
|
16
|
-
│
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
└──
|
|
135
|
+
├── packages/ # 源代码
|
|
136
|
+
│ ├── index.ts # 主入口文件
|
|
137
|
+
│ ├── http/ # HTTP 工具模块
|
|
138
|
+
│ │ ├── Axios.ts # Axios 封装类
|
|
139
|
+
│ │ ├── axiosTransform.ts # 请求/响应转换器
|
|
140
|
+
│ │ ├── axiosCancel.ts # 请求取消处理
|
|
141
|
+
│ │ ├── helper.ts # 辅助函数
|
|
142
|
+
│ │ ├── types.ts # 类型定义
|
|
143
|
+
│ │ └── index.ts # HTTP 模块入口
|
|
144
|
+
│ └── utils/ # 工具函数模块
|
|
145
|
+
│ └── index.ts # 工具函数集合
|
|
146
|
+
├── lib/ # 构建输出
|
|
147
|
+
│ ├── index.esm.js # ES 模块格式
|
|
148
|
+
│ ├── index.umd.js # UMD 格式
|
|
149
|
+
│ └── index.d.ts # 类型声明
|
|
150
|
+
├── types/ # 类型声明文件
|
|
151
|
+
├── example/ # Vue 3 示例项目
|
|
152
|
+
├── pnpm-workspace.yaml # pnpm workspace 配置
|
|
153
|
+
└── package.json # 项目配置
|
|
20
154
|
```
|
|
21
155
|
|
|
22
|
-
##
|
|
156
|
+
## 🛠️ 开发
|
|
23
157
|
|
|
24
|
-
|
|
158
|
+
### 环境要求
|
|
159
|
+
|
|
160
|
+
- Node.js >= 16
|
|
161
|
+
- pnpm >= 7
|
|
25
162
|
|
|
26
163
|
### 安装依赖
|
|
27
164
|
|
|
@@ -29,84 +166,162 @@ personal-utils/
|
|
|
29
166
|
pnpm install
|
|
30
167
|
```
|
|
31
168
|
|
|
32
|
-
###
|
|
169
|
+
### 开发命令
|
|
33
170
|
|
|
34
171
|
```bash
|
|
172
|
+
# 构建库
|
|
35
173
|
pnpm build
|
|
36
|
-
```
|
|
37
174
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
```bash
|
|
175
|
+
# 仅构建类型声明
|
|
41
176
|
pnpm build:types
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### 开发模式
|
|
45
177
|
|
|
46
|
-
|
|
178
|
+
# 开发模式
|
|
47
179
|
pnpm dev
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## 示例项目
|
|
51
180
|
|
|
52
|
-
|
|
181
|
+
# 启动示例项目
|
|
182
|
+
pnpm example:dev
|
|
53
183
|
|
|
54
|
-
```bash
|
|
55
184
|
# 一键设置(推荐)
|
|
56
185
|
pnpm example:setup
|
|
57
|
-
|
|
58
|
-
# 或者分步执行
|
|
59
|
-
pnpm example:install # 安装示例项目依赖
|
|
60
|
-
pnpm example:dev # 启动示例开发服务器
|
|
61
186
|
```
|
|
62
187
|
|
|
63
|
-
###
|
|
188
|
+
### 示例项目
|
|
189
|
+
|
|
190
|
+
项目包含一个完整的 Vue 3 示例,展示如何使用库中的功能:
|
|
64
191
|
|
|
65
192
|
```bash
|
|
66
|
-
|
|
67
|
-
pnpm example:
|
|
68
|
-
|
|
193
|
+
# 启动示例项目
|
|
194
|
+
pnpm example:dev
|
|
195
|
+
|
|
196
|
+
# 构建示例项目
|
|
197
|
+
pnpm example:build
|
|
198
|
+
|
|
199
|
+
# 预览构建结果
|
|
200
|
+
pnpm example:preview
|
|
69
201
|
```
|
|
70
202
|
|
|
71
|
-
##
|
|
203
|
+
## 📝 类型支持
|
|
204
|
+
|
|
205
|
+
本库提供完整的 TypeScript 类型支持,包括:
|
|
206
|
+
|
|
207
|
+
- 所有工具函数的类型定义
|
|
208
|
+
- HTTP 请求配置的类型定义
|
|
209
|
+
- 响应数据的类型定义
|
|
210
|
+
- 完整的 IntelliSense 支持
|
|
211
|
+
|
|
212
|
+
## 🤝 贡献
|
|
213
|
+
|
|
214
|
+
欢迎提交 Issue 和 Pull Request!
|
|
215
|
+
|
|
216
|
+
## 📄 许可证
|
|
217
|
+
|
|
218
|
+
MIT License
|
|
219
|
+
|
|
220
|
+
## 🔗 相关链接
|
|
221
|
+
|
|
222
|
+
- [Vite](https://vitejs.dev/) - 构建工具
|
|
223
|
+
- [Axios](https://axios-http.com/) - HTTP 客户端
|
|
224
|
+
- [TypeScript](https://www.typescriptlang.org/) - 编程语言
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
# GitHub Actions 自动发布配置
|
|
229
|
+
|
|
230
|
+
本项目配置了 GitHub Actions 工作流,实现提交到 main 分支时自动构建和发布到 NPM。
|
|
72
231
|
|
|
73
|
-
|
|
232
|
+
## 🚀 工作流说明
|
|
233
|
+
|
|
234
|
+
### 触发条件
|
|
235
|
+
- 推送到 `main` 分支
|
|
236
|
+
- 忽略以下文件的变更:
|
|
237
|
+
- `*.md` 文件
|
|
238
|
+
- `.gitignore`
|
|
239
|
+
- `LICENSE`
|
|
240
|
+
- `.github/**`
|
|
241
|
+
- `example/**`
|
|
242
|
+
|
|
243
|
+
### 执行步骤
|
|
244
|
+
1. **检出代码** - 获取最新代码
|
|
245
|
+
2. **设置环境** - 配置 Node.js 18 和 pnpm 8
|
|
246
|
+
3. **安装依赖** - 使用 pnpm 安装项目依赖
|
|
247
|
+
4. **构建项目** - 执行 TypeScript 编译和 Vite 构建
|
|
248
|
+
5. **生成类型** - 生成 TypeScript 类型声明文件
|
|
249
|
+
6. **版本管理** - 自动检查并更新版本号
|
|
250
|
+
7. **发布到 NPM** - 自动发布到 npm 仓库
|
|
251
|
+
8. **创建 Release** - 在 GitHub 上创建发布版本
|
|
252
|
+
|
|
253
|
+
## ⚙️ 环境变量配置
|
|
254
|
+
|
|
255
|
+
### 必需的环境变量
|
|
256
|
+
|
|
257
|
+
在 GitHub 仓库的 Settings → Secrets and variables → Actions 中添加以下密钥:
|
|
258
|
+
|
|
259
|
+
#### `NPM_TOKEN`
|
|
260
|
+
- **获取方式**: 在 [npmjs.com](https://www.npmjs.com/) 登录后,进入 Profile → Access Tokens
|
|
261
|
+
- **权限**: 选择 "Automation" 类型,确保有发布权限
|
|
262
|
+
- **用途**: 用于自动发布包到 NPM
|
|
263
|
+
|
|
264
|
+
### 自动提供的环境变量
|
|
265
|
+
|
|
266
|
+
- `GITHUB_TOKEN` - GitHub 自动提供,用于创建 Release
|
|
267
|
+
|
|
268
|
+
## 🔧 手动发布命令
|
|
269
|
+
|
|
270
|
+
如果需要在本地手动发布,可以使用以下命令:
|
|
74
271
|
|
|
75
272
|
```bash
|
|
76
|
-
|
|
77
|
-
|
|
273
|
+
# 增加 patch 版本并发布
|
|
274
|
+
pnpm publish:patch
|
|
78
275
|
|
|
79
|
-
|
|
276
|
+
# 增加 minor 版本并发布
|
|
277
|
+
pnpm publish:minor
|
|
80
278
|
|
|
81
|
-
|
|
82
|
-
|
|
279
|
+
# 增加 major 版本并发布
|
|
280
|
+
pnpm publish:major
|
|
83
281
|
|
|
84
|
-
|
|
85
|
-
|
|
282
|
+
# 仅更新版本号
|
|
283
|
+
pnpm version:patch
|
|
284
|
+
pnpm version:minor
|
|
285
|
+
pnpm version:major
|
|
86
286
|
```
|
|
87
287
|
|
|
88
|
-
|
|
288
|
+
## 📋 版本管理规则
|
|
89
289
|
|
|
90
|
-
|
|
290
|
+
- **首次发布**: 使用 `package.json` 中的初始版本
|
|
291
|
+
- **后续发布**: 如果本地版本与 NPM 版本相同,自动增加 patch 版本
|
|
292
|
+
- **版本格式**: 遵循语义化版本 (SemVer) 规范
|
|
91
293
|
|
|
92
|
-
|
|
93
|
-
import { http } from 'personal-utils';
|
|
294
|
+
## 🚨 注意事项
|
|
94
295
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
296
|
+
1. **权限检查**: 确保 GitHub Actions 有足够的权限访问仓库
|
|
297
|
+
2. **NPM 权限**: 确保 NPM_TOKEN 有发布 `@sycsq/common` 包的权限
|
|
298
|
+
3. **版本冲突**: 避免手动修改版本号,让工作流自动管理
|
|
299
|
+
4. **构建失败**: 如果构建失败,检查依赖和构建配置
|
|
300
|
+
|
|
301
|
+
## 🔍 故障排除
|
|
302
|
+
|
|
303
|
+
### 常见问题
|
|
304
|
+
|
|
305
|
+
1. **构建失败**
|
|
306
|
+
- 检查 `pnpm install` 是否成功
|
|
307
|
+
- 验证 TypeScript 配置
|
|
308
|
+
- 检查 Vite 构建配置
|
|
309
|
+
|
|
310
|
+
2. **发布失败**
|
|
311
|
+
- 验证 NPM_TOKEN 是否正确
|
|
312
|
+
- 检查包名是否可用
|
|
313
|
+
- 确认版本号是否冲突
|
|
314
|
+
|
|
315
|
+
3. **权限问题**
|
|
316
|
+
- 检查 GitHub Actions 权限设置
|
|
317
|
+
- 验证 NPM 账户权限
|
|
98
318
|
|
|
99
|
-
|
|
319
|
+
### 查看日志
|
|
100
320
|
|
|
101
|
-
|
|
102
|
-
2. 运行 `pnpm build` 构建库和类型声明
|
|
103
|
-
3. 在示例项目中测试功能
|
|
104
|
-
4. 提交代码
|
|
321
|
+
在 GitHub 仓库的 Actions 标签页中查看工作流执行日志,定位具体问题。
|
|
105
322
|
|
|
106
|
-
##
|
|
323
|
+
## 📚 相关链接
|
|
107
324
|
|
|
108
|
-
-
|
|
109
|
-
-
|
|
110
|
-
-
|
|
111
|
-
- **示例框架**: Vue 3 + TypeScript
|
|
112
|
-
- **类型支持**: 完整的 TypeScript 类型声明
|
|
325
|
+
- [GitHub Actions 文档](https://docs.github.com/en/actions)
|
|
326
|
+
- [NPM 发布指南](https://docs.npmjs.com/cli/v8/commands/npm-publish)
|
|
327
|
+
- [语义化版本](https://semver.org/)
|