streamline-code-by-ikun2274 0.12.6 → 0.12.71
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/package.json +2 -2
- package/src/Request/StreamlineCode.js +12 -9
- package/types/index.d.ts +62 -68
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "streamline-code-by-ikun2274",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.71",
|
|
4
4
|
"description": "A streamline Express route builder with database operations, pagination, and authentication",
|
|
5
5
|
"main": "src/StreamlineCodeIndex.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
9
|
"import": "./src/StreamlineCodeIndex.js",
|
|
10
|
-
"
|
|
10
|
+
"types": "./types/index.d.ts"
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"types": "./types/index.d.ts",
|
|
@@ -193,15 +193,18 @@ export function DefineRoutePlus_A(options) {
|
|
|
193
193
|
// 处理创建操作
|
|
194
194
|
let createData = req.body;
|
|
195
195
|
|
|
196
|
-
//
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
196
|
+
// 如果需要自动生成ID
|
|
197
|
+
if (autoGenerateId) {
|
|
198
|
+
// 直接使用generateId中间件的逻辑
|
|
199
|
+
const records = await ToolDb.find(table);
|
|
200
|
+
const ids = Array.isArray(records)
|
|
201
|
+
? records.map(record => record[primaryKey])
|
|
202
|
+
.filter(id => id !== null && id !== undefined && typeof id === 'number')
|
|
203
|
+
: [];
|
|
204
|
+
const maxId = ids.length > 0 ? Math.max(...ids) : 0;
|
|
205
|
+
const newId = maxId + 1;
|
|
206
|
+
createData = { [primaryKey]: newId, ...createData };
|
|
207
|
+
}
|
|
205
208
|
|
|
206
209
|
await ToolDb.create(table, createData);
|
|
207
210
|
break;
|
package/types/index.d.ts
CHANGED
|
@@ -1,83 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 类型定义文件
|
|
3
|
+
*/
|
|
3
4
|
import express from 'express';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// 数据库配置类型
|
|
6
|
+
/**
|
|
7
|
+
* 数据库配置接口
|
|
8
|
+
*/
|
|
9
9
|
export interface DbConfig {
|
|
10
|
+
/** 数据库主机地址 */
|
|
10
11
|
host: string;
|
|
12
|
+
/** 数据库用户名 */
|
|
11
13
|
user: string;
|
|
14
|
+
/** 数据库密码 */
|
|
12
15
|
password: string;
|
|
16
|
+
/** 数据库名称 */
|
|
13
17
|
database: string;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
/**
|
|
21
|
+
* 服务器配置接口
|
|
22
|
+
*/
|
|
17
23
|
export interface ServerConfig {
|
|
24
|
+
/** 服务器端口 */
|
|
18
25
|
port: number;
|
|
26
|
+
/** 允许的域名列表 */
|
|
19
27
|
allowedOrigins: string[];
|
|
20
28
|
}
|
|
21
29
|
|
|
22
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 路由配置接口
|
|
32
|
+
*/
|
|
23
33
|
export interface RouteConfig {
|
|
34
|
+
/** 路由路径 */
|
|
24
35
|
url: string;
|
|
36
|
+
/** HTTP 方法 */
|
|
25
37
|
method: 'get' | 'post' | 'put' | 'delete';
|
|
38
|
+
/** 中间件数组 */
|
|
26
39
|
middlewares?: Array<Function>;
|
|
40
|
+
/** 内置中间件配置 */
|
|
27
41
|
BuiltinMid?: {
|
|
42
|
+
/** 中间件参数 */
|
|
28
43
|
Parameter?: string | string[];
|
|
29
44
|
};
|
|
45
|
+
/** 额外处理选项 */
|
|
30
46
|
PlusHandler: {
|
|
47
|
+
/** 数据库操作配置 */
|
|
31
48
|
db: {
|
|
49
|
+
/** 数据库表名 */
|
|
32
50
|
table: string;
|
|
51
|
+
/** 操作类型 */
|
|
33
52
|
operation: 'select' | 'read' | 'create' | 'update' | 'delete';
|
|
53
|
+
/** 主键字段 */
|
|
34
54
|
primaryKey?: string;
|
|
55
|
+
/** 查询字段 */
|
|
35
56
|
fields?: string;
|
|
57
|
+
/** 分类字段 */
|
|
36
58
|
categoryField?: string;
|
|
37
59
|
};
|
|
60
|
+
/** 模式配置 */
|
|
38
61
|
Mode?: {
|
|
62
|
+
/** 是否启用分页 */
|
|
39
63
|
pagination?: boolean;
|
|
64
|
+
/** 是否需要认证 */
|
|
40
65
|
requireAuth?: boolean;
|
|
66
|
+
/** 是否打乱数据 */
|
|
41
67
|
shuffle?: boolean;
|
|
68
|
+
/** 分页大小 */
|
|
42
69
|
pageSize?: number;
|
|
43
70
|
};
|
|
71
|
+
/** 其他配置 */
|
|
44
72
|
other: {
|
|
73
|
+
/** 成功消息 */
|
|
45
74
|
successMessage?: string;
|
|
75
|
+
/** 错误消息 */
|
|
46
76
|
errorMessage?: string;
|
|
77
|
+
/** 自定义逻辑函数 */
|
|
47
78
|
customLogic?: Function;
|
|
48
79
|
};
|
|
49
80
|
};
|
|
50
81
|
}
|
|
51
82
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
export function DefineRoutePlus_A(config: RouteConfig): void;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
/** 数据库配置接口 */
|
|
61
|
-
export interface setDbConfig {
|
|
62
|
-
/** 数据库主机地址 */
|
|
63
|
-
host: string;
|
|
64
|
-
/** 数据库用户名 */
|
|
65
|
-
user: string;
|
|
66
|
-
/** 数据库密码 */
|
|
67
|
-
password: string;
|
|
68
|
-
/** 数据库名称 */
|
|
69
|
-
database: string;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** 服务器配置接口 */
|
|
73
|
-
export interface ServerConfig {
|
|
74
|
-
/** 服务器端口 */
|
|
75
|
-
port: number;
|
|
76
|
-
/** 数据库配置 */
|
|
77
|
-
db: DbConfig;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** 数据库操作配置接口 */
|
|
83
|
+
/**
|
|
84
|
+
* 数据库操作配置接口
|
|
85
|
+
*/
|
|
81
86
|
export interface DbOperationConfig {
|
|
82
87
|
/** 数据库表名 */
|
|
83
88
|
table: string;
|
|
@@ -91,7 +96,9 @@ export interface DbOperationConfig {
|
|
|
91
96
|
categoryField?: string;
|
|
92
97
|
}
|
|
93
98
|
|
|
94
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* 其他配置接口
|
|
101
|
+
*/
|
|
95
102
|
export interface OtherConfig {
|
|
96
103
|
/** 自定义逻辑函数 */
|
|
97
104
|
customLogic?: (req: express.Request, res: express.Response, ToolDb: any, successMessage: string, errorMessage: string) => Promise<any>;
|
|
@@ -101,13 +108,17 @@ export interface OtherConfig {
|
|
|
101
108
|
errorMessage?: string;
|
|
102
109
|
}
|
|
103
110
|
|
|
104
|
-
/**
|
|
111
|
+
/**
|
|
112
|
+
* 内置中间件配置接口
|
|
113
|
+
*/
|
|
105
114
|
export interface BuiltinMidConfig {
|
|
106
115
|
/** 中间件参数,可以是字符串或字符串数组 */
|
|
107
116
|
Parameter: string | string[];
|
|
108
117
|
}
|
|
109
118
|
|
|
110
|
-
/**
|
|
119
|
+
/**
|
|
120
|
+
* 路由配置接口(新版)
|
|
121
|
+
*/
|
|
111
122
|
export interface RouteConfig {
|
|
112
123
|
/** 路由路径 */
|
|
113
124
|
url: string;
|
|
@@ -128,7 +139,9 @@ export interface RouteConfig {
|
|
|
128
139
|
};
|
|
129
140
|
}
|
|
130
141
|
|
|
131
|
-
/**
|
|
142
|
+
/**
|
|
143
|
+
* 数据库操作工具类
|
|
144
|
+
*/
|
|
132
145
|
export interface ToolDb {
|
|
133
146
|
/**
|
|
134
147
|
* 查询单条记录
|
|
@@ -178,39 +191,16 @@ export interface ToolDb {
|
|
|
178
191
|
/**
|
|
179
192
|
* 二次封装 defineRoute 函数
|
|
180
193
|
* @param options 路由配置选项
|
|
181
|
-
*
|
|
182
|
-
* /**
|
|
183
|
-
* 二次封装 defineRoute
|
|
184
|
-
* @param {Object} options - 路由配置选项
|
|
185
|
-
* @param {string} options.url - 路由路径(必填)
|
|
186
|
-
* @param {string} options.method - HTTP 方法(GET/POST/PUT/DELETE,必填)
|
|
187
|
-
* @param {Array<Function>} [options.middlewares=[]] - 中间件数组(可选 用户自定义中间件)--作者会尽可能集成中间件
|
|
188
|
-
* @param {Object} options.PlusHandler - 额外处理选项(必填)
|
|
189
|
-
* @param {Object} options.PlusHandler.db - 数据库操作(必填)
|
|
190
|
-
* @param {string} options.PlusHandler.db.table - 数据库表名(必填)
|
|
191
|
-
* @param {string} options.PlusHandler.db.operation - 数据表操作(必填)select/insert/update/update/delete
|
|
192
|
-
* @param {string} [options.PlusHandler.db.primaryKey='id'] - 主键字段(可选)
|
|
193
|
-
* @param {string} [options.PlusHandler.db.fields='*'] - 查询字段(可选)
|
|
194
|
-
* @param {string} [options.PlusHandler.db.categoryField='category_id'] - 分类字段(可选)
|
|
195
|
-
* @param {object} [options.BuiltinMid.Parameter=[]] - 中间件参数(可选的内置中间件)填一个激活一个
|
|
196
|
-
* 'requireAuth': () => verifyToken,验证用户身份中间件
|
|
197
|
-
* 'login': () => [validateLogin, verifyUser, generateToken],登陆中间件集合
|
|
198
|
-
* 'register': () => [validateRegister, checkUsername],注册中间件集合
|
|
199
|
-
* 'categoryFilter': (field = categoryField) => categoryFilter(field),分类筛选中间件
|
|
200
|
-
* 'pagination': (size = 10) => Dpagination(Number(size)),分页中间件
|
|
201
|
-
* 'shuffle': () => shuffleData,打乱数据中间件
|
|
202
|
-
* 'unifiedResponse': (message = successMessage) => unifiedResponse(message),统一响应中间件
|
|
203
|
-
* @param {Object} options.PlusHandler.other - 其他选项(必填)
|
|
204
|
-
* @param {string} [options.PlusHandler.other.successMessage='操作成功'] - 成功消息(可选)
|
|
205
|
-
* @param {string} [options.PlusHandler.other.errorMessage='操作失败'] - 错误消息(可选)
|
|
206
|
-
*
|
|
207
194
|
*/
|
|
208
|
-
|
|
209
195
|
export function DefineRoutePlus_A(options: RouteConfig): void;
|
|
210
196
|
|
|
211
197
|
/**
|
|
212
198
|
* 二次封装 Express 路由注册
|
|
213
199
|
* @param options 路由配置选项
|
|
200
|
+
* @param options.url 路由路径
|
|
201
|
+
* @param options.method HTTP 方法
|
|
202
|
+
* @param options.middlewares 中间件数组
|
|
203
|
+
* @param options.handler 业务逻辑处理函数
|
|
214
204
|
*/
|
|
215
205
|
export function defineRoute(options: {
|
|
216
206
|
url: string;
|
|
@@ -227,6 +217,10 @@ export const ToolDb: ToolDb;
|
|
|
227
217
|
/**
|
|
228
218
|
* 设置数据库配置
|
|
229
219
|
* @param config 数据库配置
|
|
220
|
+
* @param config.host 数据库主机地址
|
|
221
|
+
* @param config.user 数据库用户名
|
|
222
|
+
* @param config.password 数据库密码
|
|
223
|
+
* @param config.database 数据库名称
|
|
230
224
|
*/
|
|
231
225
|
export function setDbConfig(config: Partial<DbConfig>): void;
|
|
232
226
|
|