create-skweb 3.0.1 → 3.1.0

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.
@@ -3,99 +3,212 @@ import { db } from '../services/database.js'
3
3
 
4
4
  const { SysUser } = db.sequelize.models
5
5
 
6
- export default [
7
- {
8
- method: 'get',
9
- path: '/api/users',
10
- async handler(req) {
11
- const { page = 1, limit = 10 } = req.query
12
- const users = await SysUser.findAndCountAll({
13
- offset: (Number(page) - 1) * Number(limit),
14
- limit: Number(limit)
15
- })
16
- return {
17
- list: users.rows,
18
- total: users.count
19
- }
6
+ /**
7
+ * @swagger
8
+ * /api/users:
9
+ * get:
10
+ * summary: 获取用户列表
11
+ * tags: [Users]
12
+ * parameters:
13
+ * - in: query
14
+ * name: page
15
+ * schema: { type: integer, minimum: 1, default: 1 }
16
+ * description: 页码
17
+ * - in: query
18
+ * name: limit
19
+ * schema: { type: integer, minimum: 1, default: 10 }
20
+ * description: 每页条数
21
+ * responses:
22
+ * 200:
23
+ * description: OK
24
+ */
25
+ const userList = {
26
+ method: 'get',
27
+ path: '/api/users',
28
+ async handler(req) {
29
+ const { page = 1, limit = 10 } = req.query
30
+ const users = await SysUser.findAndCountAll({
31
+ offset: (Number(page) - 1) * Number(limit),
32
+ limit: Number(limit)
33
+ })
34
+ return {
35
+ list: users.rows,
36
+ total: users.count
20
37
  }
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @swagger
43
+ * /api/users/{id}:
44
+ * get:
45
+ * summary: 获取单个用户
46
+ * tags: [Users]
47
+ * parameters:
48
+ * - in: path
49
+ * name: id
50
+ * required: true
51
+ * schema: { type: integer, minimum: 1 }
52
+ * responses:
53
+ * 200:
54
+ * description: OK
55
+ * 404:
56
+ * description: 用户不存在
57
+ */
58
+ const userGet = {
59
+ method: 'get',
60
+ path: '/api/users/:id',
61
+ paramsSchema: {
62
+ type: 'object',
63
+ required: ['id'],
64
+ properties: { id: { type: 'integer', minimum: 1 } }
21
65
  },
22
- {
23
- method: 'get',
24
- path: '/api/users/:id',
25
- paramsSchema: {
26
- type: 'object',
27
- required: ['id'],
28
- properties: { id: { type: 'integer', minimum: 1 } }
29
- },
30
- async handler(req) {
31
- const { id } = req.validatedParams
32
- const user = await SysUser.findByPk(id)
33
- if (!user) {
34
- throw new createHttpError.NotFound('User not found')
35
- }
36
- return user
66
+ async handler(req) {
67
+ const { id } = req.validatedParams
68
+ const user = await SysUser.findByPk(id)
69
+ if (!user) {
70
+ throw new createHttpError.NotFound('User not found')
71
+ }
72
+ return user
73
+ }
74
+ }
75
+
76
+ /**
77
+ * @swagger
78
+ * /api/users:
79
+ * post:
80
+ * summary: 创建用户
81
+ * tags: [Users]
82
+ * requestBody:
83
+ * required: true
84
+ * content:
85
+ * application/json:
86
+ * schema:
87
+ * type: object
88
+ * required: [username, password]
89
+ * properties:
90
+ * username: { type: string, minLength: 3 }
91
+ * password: { type: string, minLength: 6 }
92
+ * email: { type: string, format: email }
93
+ * phone: { type: string }
94
+ * responses:
95
+ * 200:
96
+ * description: 创建成功
97
+ */
98
+ const userCreate = {
99
+ method: 'post',
100
+ path: '/api/users',
101
+ bodySchema: {
102
+ type: 'object',
103
+ required: ['username', 'password'],
104
+ properties: {
105
+ username: { type: 'string', minLength: 3 },
106
+ password: { type: 'string', minLength: 6 },
107
+ email: { type: 'string', format: 'email' },
108
+ phone: { type: 'string' }
37
109
  }
38
110
  },
39
- {
40
- method: 'post',
41
- path: '/api/users',
42
- bodySchema: {
43
- type: 'object',
44
- required: ['username', 'password'],
45
- properties: {
46
- username: { type: 'string', minLength: 3 },
47
- password: { type: 'string', minLength: 6 },
48
- email: { type: 'string', format: 'email' },
49
- phone: { type: 'string' }
50
- }
51
- },
52
- async handler(req) {
53
- return await SysUser.create(req.validatedBody)
111
+ async handler(req) {
112
+ return await SysUser.create(req.validatedBody)
113
+ }
114
+ }
115
+
116
+ /**
117
+ * @swagger
118
+ * /api/users/{id}:
119
+ * put:
120
+ * summary: 更新用户
121
+ * tags: [Users]
122
+ * parameters:
123
+ * - in: path
124
+ * name: id
125
+ * required: true
126
+ * schema: { type: integer, minimum: 1 }
127
+ * requestBody:
128
+ * required: true
129
+ * content:
130
+ * application/json:
131
+ * schema:
132
+ * type: object
133
+ * properties:
134
+ * password: { type: string, minLength: 6 }
135
+ * email: { type: string, format: email }
136
+ * phone: { type: string }
137
+ * status: { type: integer }
138
+ * responses:
139
+ * 200:
140
+ * description: OK
141
+ * 404:
142
+ * description: 用户不存在
143
+ */
144
+ const userUpdate = {
145
+ method: 'put',
146
+ path: '/api/users/:id',
147
+ paramsSchema: {
148
+ type: 'object',
149
+ required: ['id'],
150
+ properties: { id: { type: 'integer', minimum: 1 } }
151
+ },
152
+ bodySchema: {
153
+ type: 'object',
154
+ properties: {
155
+ password: { type: 'string', minLength: 6 },
156
+ email: { type: 'string', format: 'email' },
157
+ phone: { type: 'string' },
158
+ status: { type: 'integer' }
54
159
  }
55
160
  },
56
- {
57
- method: 'put',
58
- path: '/api/users/:id',
59
- paramsSchema: {
60
- type: 'object',
61
- required: ['id'],
62
- properties: { id: { type: 'integer', minimum: 1 } }
63
- },
64
- bodySchema: {
65
- type: 'object',
66
- properties: {
67
- password: { type: 'string', minLength: 6 },
68
- email: { type: 'string', format: 'email' },
69
- phone: { type: 'string' },
70
- status: { type: 'integer' }
71
- }
72
- },
73
- async handler(req) {
74
- const { id } = req.validatedParams
75
- const user = await SysUser.findByPk(id)
76
- if (!user) {
77
- throw new createHttpError.NotFound('User not found')
78
- }
79
- await user.update(req.validatedBody)
80
- return user
161
+ async handler(req) {
162
+ const { id } = req.validatedParams
163
+ const user = await SysUser.findByPk(id)
164
+ if (!user) {
165
+ throw new createHttpError.NotFound('User not found')
81
166
  }
167
+ await user.update(req.validatedBody)
168
+ return user
169
+ }
170
+ }
171
+
172
+ /**
173
+ * @swagger
174
+ * /api/users/{id}:
175
+ * delete:
176
+ * summary: 删除用户
177
+ * tags: [Users]
178
+ * parameters:
179
+ * - in: path
180
+ * name: id
181
+ * required: true
182
+ * schema: { type: integer, minimum: 1 }
183
+ * responses:
184
+ * 200:
185
+ * description: OK
186
+ * 404:
187
+ * description: 用户不存在
188
+ */
189
+ const userDelete = {
190
+ method: 'delete',
191
+ path: '/api/users/:id',
192
+ paramsSchema: {
193
+ type: 'object',
194
+ required: ['id'],
195
+ properties: { id: { type: 'integer', minimum: 1 } }
82
196
  },
83
- {
84
- method: 'delete',
85
- path: '/api/users/:id',
86
- paramsSchema: {
87
- type: 'object',
88
- required: ['id'],
89
- properties: { id: { type: 'integer', minimum: 1 } }
90
- },
91
- async handler(req) {
92
- const { id } = req.validatedParams
93
- const user = await SysUser.findByPk(id)
94
- if (!user) {
95
- throw new createHttpError.NotFound('User not found')
96
- }
97
- await user.destroy()
98
- return { message: 'User deleted' }
197
+ async handler(req) {
198
+ const { id } = req.validatedParams
199
+ const user = await SysUser.findByPk(id)
200
+ if (!user) {
201
+ throw new createHttpError.NotFound('User not found')
99
202
  }
203
+ await user.destroy()
204
+ return { message: 'User deleted' }
100
205
  }
101
- ]
206
+ }
207
+
208
+ export default {
209
+ userList,
210
+ userGet,
211
+ userCreate,
212
+ userUpdate,
213
+ userDelete
214
+ }
@@ -57,14 +57,11 @@ async function startCronWorker() {
57
57
  }
58
58
  })
59
59
 
60
- await framework.runPluginBeforeSetup()
61
- await framework.setupModels(path.join(__dirname, './models'))
62
- await framework.loadPluginModels()
63
- await framework.runPluginAfterSetup()
64
-
65
- await framework.loadCronActions(path.join(__dirname, './cronActions'))
66
- await framework.startWorker()
67
- framework.start()
60
+ await framework.setup({
61
+ modelsDir: path.join(__dirname, './models'),
62
+ cronActionsDir: path.join(__dirname, './cronActions')
63
+ })
64
+ await framework.start()
68
65
  logger.info('[cron-worker] Cron worker started')
69
66
 
70
67
  // 未捕获异常:打印堆栈后直接退出。OS / 进程管理器负责回收资源。
@@ -92,13 +92,13 @@ async function bootstrap(): Promise<void> {
92
92
  }
93
93
  })
94
94
 
95
- await framework.runPluginBeforeSetup()
96
- await framework.setupModels(path.join(__dirname, './models'))
97
- await framework.loadPluginModels()
98
- await framework.runPluginAfterSetup()
99
-
100
95
  const app = framework.createApp() as express.Express & Record<string, any>
101
96
 
97
+ await framework.setup({
98
+ modelsDir: path.join(__dirname, './models'),
99
+ controllersDir: path.join(__dirname, './controllers')
100
+ })
101
+
102
102
  app.disable('x-powered-by')
103
103
  app.disable('etag')
104
104
 
@@ -135,8 +135,10 @@ async function bootstrap(): Promise<void> {
135
135
  logger.info(`[app] Swagger UI disabled (env: ${process.env.NODE_ENV || 'unset'})`)
136
136
  }
137
137
 
138
- await framework.loadControllers(path.join(__dirname, './controllers'))
139
- await framework.loadPluginControllers()
138
+ await framework.loadControllers()
139
+
140
+ // 错误日志中间件:挂在所有路由之后,记录业务错误并继续传递给默认错误处理。
141
+ app.use(expressErrorHandler())
140
142
 
141
143
  framework.start(Number(process.env.PORT) || 3000)
142
144
 
@@ -16,6 +16,21 @@
16
16
  * message: { type: string, example: 'Welcome to SKWeb!' }
17
17
  * version: { type: string, example: '1.0.0' }
18
18
  * timestamp: { type: string, format: date-time }
19
+ */
20
+ const helloIndex = {
21
+ method: 'get',
22
+ path: '/',
23
+ async handler() {
24
+ return {
25
+ message: 'Welcome to SKWeb!',
26
+ version: '1.0.0',
27
+ timestamp: new Date().toISOString()
28
+ }
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @swagger
19
34
  * /health:
20
35
  * get:
21
36
  * summary: 健康检查
@@ -30,6 +45,20 @@
30
45
  * properties:
31
46
  * status: { type: string, example: 'ok' }
32
47
  * timestamp: { type: string, format: date-time }
48
+ */
49
+ const helloHealth = {
50
+ method: 'get',
51
+ path: '/health',
52
+ async handler() {
53
+ return {
54
+ status: 'ok',
55
+ timestamp: new Date().toISOString()
56
+ }
57
+ }
58
+ }
59
+
60
+ /**
61
+ * @swagger
33
62
  * /echo:
34
63
  * post:
35
64
  * summary: 回显请求体
@@ -44,36 +73,19 @@
44
73
  * 200:
45
74
  * description: OK
46
75
  */
47
- export default [
48
- {
49
- method: 'get',
50
- path: '/',
51
- async handler() {
52
- return {
53
- message: 'Welcome to SKWeb!',
54
- version: '1.0.0',
55
- timestamp: new Date().toISOString()
56
- }
57
- }
58
- },
59
- {
60
- method: 'get',
61
- path: '/health',
62
- async handler() {
63
- return {
64
- status: 'ok',
65
- timestamp: new Date().toISOString()
66
- }
67
- }
68
- },
69
- {
70
- method: 'post',
71
- path: '/echo',
72
- async handler(req: any) {
73
- return {
74
- message: 'Echo received',
75
- data: req.body
76
- }
76
+ const helloEcho = {
77
+ method: 'post',
78
+ path: '/echo',
79
+ async handler(req: any) {
80
+ return {
81
+ message: 'Echo received',
82
+ data: req.body
77
83
  }
78
84
  }
79
- ]
85
+ }
86
+
87
+ export default {
88
+ helloIndex,
89
+ helloHealth,
90
+ helloEcho
91
+ }