uniky 1.0.5
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/README.md +156 -0
- package/package.json +60 -0
- package/src/lib/hook/index.ts +2 -0
- package/src/lib/hook/useParam.ts +114 -0
- package/src/lib/http/index.ts +2 -0
- package/src/lib/http/ky.ts +454 -0
- package/src/lib/index.ts +3 -0
- package/src/plugin/_pages.md +37 -0
- package/src/plugin/global.defined.ts +333 -0
- package/src/plugin/index.ts +29 -0
- package/src/plugin/lib.defined.ts +2 -0
- package/src/plugin/pages.defined.ts +275 -0
- package/src/types/uni-app.d.ts +75 -0
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# @uniky/core
|
|
2
|
+
|
|
3
|
+
uni-app 开发工具库,提供常用的 hooks、http 请求封装和 vite 插件。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- ✅ 直接使用 TypeScript 源码,无需编译
|
|
8
|
+
- ✅ 与用户项目共享依赖,避免冲突
|
|
9
|
+
- ✅ 完整的类型支持
|
|
10
|
+
- ✅ 开箱即用的 Vite 插件
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install uniky
|
|
16
|
+
# 或
|
|
17
|
+
pnpm add uniky
|
|
18
|
+
# 或
|
|
19
|
+
yarn add uniky
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 使用
|
|
23
|
+
|
|
24
|
+
### 库功能
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { useParam } from '@uniky/core';
|
|
28
|
+
|
|
29
|
+
// 在页面中使用 hooks
|
|
30
|
+
const params = useParam();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Vite 插件
|
|
34
|
+
|
|
35
|
+
在 `vite.config.ts` 中使用:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { defineConfig } from 'vite';
|
|
39
|
+
import uni from '@dcloudio/vite-plugin-uni';
|
|
40
|
+
import { unikyPlugin } from 'uniky/plugin';
|
|
41
|
+
|
|
42
|
+
export default defineConfig({
|
|
43
|
+
plugins: [
|
|
44
|
+
uni(),
|
|
45
|
+
...unikyPlugin({
|
|
46
|
+
enablePages: true, // 启用页面路由生成
|
|
47
|
+
enableGlobal: true // 启用全局定义生成
|
|
48
|
+
})
|
|
49
|
+
]
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### 单独使用插件
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { pagesDefinedPlugin, globalDefinedPlugin } from 'uniky/plugin';
|
|
57
|
+
|
|
58
|
+
export default defineConfig({
|
|
59
|
+
plugins: [
|
|
60
|
+
uni(),
|
|
61
|
+
pagesDefinedPlugin(), // 仅使用页面路由插件
|
|
62
|
+
globalDefinedPlugin() // 仅使用全局定义插件
|
|
63
|
+
]
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 插件功能
|
|
68
|
+
|
|
69
|
+
### pagesDefinedPlugin
|
|
70
|
+
|
|
71
|
+
从 `pages.json` 自动生成类型安全的路由定义,生成 `src/autoGen/global/pages.ts` 文件。
|
|
72
|
+
|
|
73
|
+
生成的内容包括:
|
|
74
|
+
- `_Pages`: 所有页面路径的常量数组
|
|
75
|
+
- `_PagePath`: 页面路径类型
|
|
76
|
+
- `_To`: 类型安全的路由跳转方法
|
|
77
|
+
|
|
78
|
+
使用示例:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// 类型安全的路由跳转
|
|
82
|
+
_To.navigate('pages/index/index', {
|
|
83
|
+
query: { id: '123' },
|
|
84
|
+
json: { data: { name: 'test' } }
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
_To.redirect('pages/detail/detail');
|
|
88
|
+
_To.back();
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### globalDefinedPlugin
|
|
92
|
+
|
|
93
|
+
收集 `src/autoGen/global` 目录下的 TypeScript 文件导出,自动生成:
|
|
94
|
+
- `src/autoGen/global.d.ts`: 全局类型定义
|
|
95
|
+
- `src/autoGen/global.install.ts`: 全局变量安装文件
|
|
96
|
+
|
|
97
|
+
在 `main.ts` 中安装全局定义:
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { installGlobals } from './autoGen/global.install';
|
|
101
|
+
|
|
102
|
+
installGlobals();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## 架构说明
|
|
106
|
+
|
|
107
|
+
本库直接发布 TypeScript 源码,不进行编译。这样做的好处:
|
|
108
|
+
|
|
109
|
+
1. **避免依赖冲突**:使用项目自己的 vue、@dcloudio/uni-app 等依赖
|
|
110
|
+
2. **类型支持更好**:直接使用源码类型定义
|
|
111
|
+
3. **调试更方便**:可以直接查看和调试源码
|
|
112
|
+
4. **体积更小**:不包含编译后的代码
|
|
113
|
+
|
|
114
|
+
用户项目的构建工具(如 Vite)会自动处理这些 TypeScript 文件的编译。
|
|
115
|
+
|
|
116
|
+
## 发布
|
|
117
|
+
|
|
118
|
+
### 前置要求
|
|
119
|
+
|
|
120
|
+
需要安装 [gum](https://github.com/charmbracelet/gum) 用于交互式命令行:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
# macOS
|
|
124
|
+
brew install gum
|
|
125
|
+
|
|
126
|
+
# Linux
|
|
127
|
+
brew install gum
|
|
128
|
+
# 或
|
|
129
|
+
go install github.com/charmbracelet/gum@latest
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 发布流程
|
|
133
|
+
|
|
134
|
+
使用自动发布脚本:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm run publish:auto
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
或直接执行脚本:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
./publish.sh
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
脚本会自动:
|
|
147
|
+
1. 检查 npm 登录状态
|
|
148
|
+
2. 交互式选择版本更新类型(默认 patch 补丁版本)
|
|
149
|
+
3. 更新 package.json 版本号
|
|
150
|
+
4. 预览源码目录结构
|
|
151
|
+
5. 确认后发布到 npm
|
|
152
|
+
6. 可选自动提交到 git
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "uniky",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "uni-app 开发工具库,包含 hooks、http 请求和 vite 插件",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/lib/index.ts",
|
|
7
|
+
"module": "./src/lib/index.ts",
|
|
8
|
+
"types": "./src/lib/index.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/lib/index.ts",
|
|
12
|
+
"import": "./src/lib/index.ts"
|
|
13
|
+
},
|
|
14
|
+
"./plugin": {
|
|
15
|
+
"types": "./src/plugin/index.ts",
|
|
16
|
+
"import": "./src/plugin/index.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"publish:auto": "bash publish.sh"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"uni-app",
|
|
28
|
+
"uniapp",
|
|
29
|
+
"vue3",
|
|
30
|
+
"vite",
|
|
31
|
+
"plugin",
|
|
32
|
+
"hooks",
|
|
33
|
+
"http",
|
|
34
|
+
"pages",
|
|
35
|
+
"router",
|
|
36
|
+
"global"
|
|
37
|
+
],
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/zhuxietong/uniky.git"
|
|
41
|
+
},
|
|
42
|
+
"author": "zhuxietong",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"vite": ">=4.0.0",
|
|
46
|
+
"vue": ">=3.0.0",
|
|
47
|
+
"@dcloudio/uni-app": "*"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"vue": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"@dcloudio/uni-app": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"vite": {
|
|
57
|
+
"optional": true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { onLoad } from '@dcloudio/uni-app'
|
|
2
|
+
import { onMounted, ref } from 'vue'
|
|
3
|
+
|
|
4
|
+
export const useParam = <Q extends object = {}, J extends object = {}>(
|
|
5
|
+
callBack?: (params: { option: Partial<Q & J>; query: Partial<Q>; json: Partial<J> }) => void,
|
|
6
|
+
) => {
|
|
7
|
+
const query = ref<Partial<Q>>({})
|
|
8
|
+
const json = ref<Partial<J>>({})
|
|
9
|
+
const param = ref<Partial<Q & J>>({})
|
|
10
|
+
|
|
11
|
+
onLoad((op?: AnyObject) => {
|
|
12
|
+
let jsonObj: any = {}
|
|
13
|
+
let queryObj: any = {}
|
|
14
|
+
|
|
15
|
+
if (!op || Object.keys(op).length === 0) {
|
|
16
|
+
param.value = {} as any
|
|
17
|
+
query.value = {} as any
|
|
18
|
+
json.value = {} as any
|
|
19
|
+
callBack?.({ option: {} as any, query: {} as any, json: {} as any })
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const decodedOp: { [k: string]: any } = {}
|
|
24
|
+
for (const key in op) {
|
|
25
|
+
if (typeof op[key] === 'string') {
|
|
26
|
+
if (key === 'json') {
|
|
27
|
+
try {
|
|
28
|
+
decodedOp[key] = JSON.parse(decodeURIComponent(op[key]))
|
|
29
|
+
jsonObj = decodedOp[key]
|
|
30
|
+
json.value = jsonObj
|
|
31
|
+
} catch (e) {}
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
34
|
+
decodedOp[key] = decodeURIComponent(op[key])
|
|
35
|
+
} else {
|
|
36
|
+
decodedOp[key] = op[key]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
queryObj = { ...decodedOp }
|
|
42
|
+
delete queryObj.json
|
|
43
|
+
query.value = queryObj
|
|
44
|
+
} catch (e) {}
|
|
45
|
+
|
|
46
|
+
param.value = { ...jsonObj, ...queryObj }
|
|
47
|
+
callBack?.({ option: param.value as any, query: query.value as any, json: json.value as any })
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
query,
|
|
52
|
+
param,
|
|
53
|
+
json,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const useMountedLoad = <Q extends object = {}, J extends object = {}>(
|
|
58
|
+
callBack?: (params: { option: Partial<Q & J>; query: Partial<Q>; json: Partial<J> }) => void,
|
|
59
|
+
) => {
|
|
60
|
+
const query = ref<Partial<Q>>({})
|
|
61
|
+
const json = ref<Partial<J>>({})
|
|
62
|
+
const param = ref<Partial<Q & J>>({})
|
|
63
|
+
|
|
64
|
+
onLoad((op?: AnyObject) => {
|
|
65
|
+
let jsonObj: any = {}
|
|
66
|
+
let queryObj: any = {}
|
|
67
|
+
|
|
68
|
+
if (!op || Object.keys(op).length === 0) {
|
|
69
|
+
param.value = {} as any
|
|
70
|
+
query.value = {} as any
|
|
71
|
+
json.value = {} as any
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const decodedOp: { [k: string]: any } = {}
|
|
76
|
+
for (const key in op) {
|
|
77
|
+
if (typeof op[key] === 'string') {
|
|
78
|
+
if (key === 'json') {
|
|
79
|
+
try {
|
|
80
|
+
decodedOp[key] = JSON.parse(decodeURIComponent(op[key]))
|
|
81
|
+
jsonObj = decodedOp[key]
|
|
82
|
+
json.value = jsonObj
|
|
83
|
+
} catch (e) {}
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
decodedOp[key] = decodeURIComponent(op[key])
|
|
87
|
+
} else {
|
|
88
|
+
decodedOp[key] = op[key]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
queryObj = { ...decodedOp }
|
|
94
|
+
delete queryObj.json
|
|
95
|
+
query.value = queryObj
|
|
96
|
+
} catch (e) {}
|
|
97
|
+
|
|
98
|
+
param.value = { ...jsonObj, ...queryObj }
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
onMounted(() => {
|
|
102
|
+
callBack?.({
|
|
103
|
+
option: (param.value || {}) as any,
|
|
104
|
+
query: (query.value || {}) as any,
|
|
105
|
+
json: (json.value || {}) as any
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
query,
|
|
111
|
+
param,
|
|
112
|
+
json,
|
|
113
|
+
}
|
|
114
|
+
}
|