@svton/cli 1.2.2 → 1.2.4

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.
Files changed (102) hide show
  1. package/dist/index.js +58 -27
  2. package/dist/index.mjs +58 -27
  3. package/package.json +1 -3
  4. package/templates/apps/admin/next-env.d.ts +0 -2
  5. package/templates/apps/admin/next.config.js +0 -15
  6. package/templates/apps/admin/package.json.tpl +0 -54
  7. package/templates/apps/admin/postcss.config.js +0 -6
  8. package/templates/apps/admin/src/app/globals.css +0 -37
  9. package/templates/apps/admin/src/app/layout.tsx +0 -19
  10. package/templates/apps/admin/src/app/login/page.tsx +0 -96
  11. package/templates/apps/admin/src/app/page.tsx +0 -8
  12. package/templates/apps/admin/src/app/users/page.tsx +0 -165
  13. package/templates/apps/admin/src/components/ui/switch.tsx +0 -29
  14. package/templates/apps/admin/src/hooks/useAPI.ts +0 -130
  15. package/templates/apps/admin/src/lib/api-client.ts +0 -112
  16. package/templates/apps/admin/src/lib/api-server.ts +0 -95
  17. package/templates/apps/admin/tailwind.config.js +0 -54
  18. package/templates/apps/admin/tsconfig.json +0 -22
  19. package/templates/apps/backend/.env.example +0 -29
  20. package/templates/apps/backend/nest-cli.json +0 -8
  21. package/templates/apps/backend/package.json.tpl +0 -57
  22. package/templates/apps/backend/prisma/schema.prisma +0 -72
  23. package/templates/apps/backend/prisma/seed.ts +0 -32
  24. package/templates/apps/backend/src/app.controller.ts +0 -15
  25. package/templates/apps/backend/src/app.module.ts +0 -85
  26. package/templates/apps/backend/src/app.service.ts +0 -12
  27. package/templates/apps/backend/src/auth/auth.controller.ts +0 -31
  28. package/templates/apps/backend/src/auth/auth.module.ts +0 -27
  29. package/templates/apps/backend/src/auth/auth.service.ts +0 -89
  30. package/templates/apps/backend/src/auth/jwt-auth.guard.ts +0 -5
  31. package/templates/apps/backend/src/auth/jwt.strategy.ts +0 -27
  32. package/templates/apps/backend/src/config/env.schema.ts +0 -35
  33. package/templates/apps/backend/src/main.ts +0 -51
  34. package/templates/apps/backend/src/object-storage/object-storage.controller.ts +0 -114
  35. package/templates/apps/backend/src/object-storage/object-storage.module.ts +0 -7
  36. package/templates/apps/backend/src/prisma/prisma.module.ts +0 -9
  37. package/templates/apps/backend/src/prisma/prisma.service.ts +0 -13
  38. package/templates/apps/backend/src/user/user.controller.ts +0 -50
  39. package/templates/apps/backend/src/user/user.module.ts +0 -12
  40. package/templates/apps/backend/src/user/user.service.ts +0 -117
  41. package/templates/apps/backend/tsconfig.json +0 -23
  42. package/templates/apps/mobile/babel.config.js +0 -8
  43. package/templates/apps/mobile/config/index.ts +0 -65
  44. package/templates/apps/mobile/package.json.tpl +0 -48
  45. package/templates/apps/mobile/project.config.json.tpl +0 -17
  46. package/templates/apps/mobile/src/app.config.ts +0 -9
  47. package/templates/apps/mobile/src/app.scss +0 -4
  48. package/templates/apps/mobile/src/app.ts +0 -8
  49. package/templates/apps/mobile/src/hooks/useAPI.ts +0 -285
  50. package/templates/apps/mobile/src/pages/index/index.scss +0 -7
  51. package/templates/apps/mobile/src/pages/index/index.tsx +0 -49
  52. package/templates/apps/mobile/src/services/api.ts +0 -155
  53. package/templates/apps/mobile/src/services/upload.service.ts +0 -41
  54. package/templates/apps/mobile/tsconfig.json +0 -21
  55. package/templates/configs/authz.config.ts +0 -10
  56. package/templates/configs/cache.config.ts +0 -14
  57. package/templates/configs/oauth.config.ts +0 -20
  58. package/templates/configs/payment.config.ts +0 -44
  59. package/templates/configs/queue.config.ts +0 -21
  60. package/templates/configs/rate-limit.config.ts +0 -16
  61. package/templates/configs/sms.config.ts +0 -11
  62. package/templates/configs/storage.config.ts +0 -14
  63. package/templates/examples/README.md +0 -258
  64. package/templates/examples/authz/README.md +0 -273
  65. package/templates/examples/authz/roles.guard.ts +0 -37
  66. package/templates/examples/authz/user.controller.ts +0 -116
  67. package/templates/examples/cache/README.md +0 -82
  68. package/templates/examples/cache/user.controller.ts +0 -42
  69. package/templates/examples/cache/user.service.ts +0 -78
  70. package/templates/examples/oauth/README.md +0 -192
  71. package/templates/examples/oauth/auth.controller.ts +0 -99
  72. package/templates/examples/oauth/auth.service.ts +0 -97
  73. package/templates/examples/payment/README.md +0 -151
  74. package/templates/examples/payment/order.controller.ts +0 -56
  75. package/templates/examples/payment/order.service.ts +0 -132
  76. package/templates/examples/payment/webhook.controller.ts +0 -73
  77. package/templates/examples/queue/README.md +0 -134
  78. package/templates/examples/queue/email.controller.ts +0 -34
  79. package/templates/examples/queue/email.processor.ts +0 -68
  80. package/templates/examples/queue/email.service.ts +0 -64
  81. package/templates/examples/rate-limit/README.md +0 -249
  82. package/templates/examples/rate-limit/api.controller.ts +0 -113
  83. package/templates/examples/sms/README.md +0 -121
  84. package/templates/examples/sms/sms.service.ts +0 -69
  85. package/templates/examples/sms/verification.controller.ts +0 -100
  86. package/templates/examples/storage/README.md +0 -224
  87. package/templates/examples/storage/upload.controller.ts +0 -117
  88. package/templates/examples/storage/upload.service.ts +0 -123
  89. package/templates/packages/types/package.json.tpl +0 -16
  90. package/templates/packages/types/src/api.ts +0 -88
  91. package/templates/packages/types/src/common.ts +0 -89
  92. package/templates/packages/types/src/index.ts +0 -3
  93. package/templates/packages/types/tsconfig.json +0 -16
  94. package/templates/skills/authz.skill.md +0 -42
  95. package/templates/skills/base.skill.md +0 -57
  96. package/templates/skills/cache.skill.md +0 -88
  97. package/templates/skills/oauth.skill.md +0 -41
  98. package/templates/skills/payment.skill.md +0 -129
  99. package/templates/skills/queue.skill.md +0 -140
  100. package/templates/skills/rate-limit.skill.md +0 -38
  101. package/templates/skills/sms.skill.md +0 -39
  102. package/templates/skills/storage.skill.md +0 -42
@@ -1,132 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { PaymentService } from '@svton/nestjs-payment';
3
-
4
- @Injectable()
5
- export class OrderService {
6
- constructor(private readonly paymentService: PaymentService) {}
7
-
8
- /**
9
- * 创建微信 JSAPI 支付订单(公众号/小程序)
10
- */
11
- async createWechatJsapiOrder(
12
- orderId: string,
13
- amount: number,
14
- openid: string,
15
- ) {
16
- const result = await this.paymentService.wechat.createOrder(
17
- {
18
- outTradeNo: orderId,
19
- totalAmount: amount,
20
- description: '商品购买',
21
- userId: openid,
22
- },
23
- 'jsapi',
24
- );
25
-
26
- return {
27
- orderId,
28
- paymentData: result,
29
- };
30
- }
31
-
32
- /**
33
- * 创建微信 Native 支付订单(扫码支付)
34
- */
35
- async createWechatNativeOrder(orderId: string, amount: number) {
36
- const result = await this.paymentService.wechat.createOrder(
37
- {
38
- outTradeNo: orderId,
39
- totalAmount: amount,
40
- description: '商品购买',
41
- },
42
- 'native',
43
- );
44
-
45
- return {
46
- orderId,
47
- qrCode: result.code_url, // 二维码链接
48
- };
49
- }
50
-
51
- /**
52
- * 创建支付宝电脑网站支付订单
53
- */
54
- async createAlipayPageOrder(orderId: string, amount: number) {
55
- const result = await this.paymentService.alipay.createOrder(
56
- {
57
- outTradeNo: orderId,
58
- totalAmount: amount,
59
- description: '商品购买',
60
- },
61
- 'page',
62
- );
63
-
64
- return {
65
- orderId,
66
- paymentUrl: result, // 支付页面 URL
67
- };
68
- }
69
-
70
- /**
71
- * 查询订单状态
72
- */
73
- async queryOrderStatus(orderId: string) {
74
- try {
75
- // 先尝试查询微信支付
76
- const wechatStatus = await this.paymentService.wechat.queryOrder(orderId);
77
- return {
78
- orderId,
79
- provider: 'wechat',
80
- status: wechatStatus.trade_state,
81
- data: wechatStatus,
82
- };
83
- } catch (wechatError) {
84
- // 如果微信查询失败,尝试支付宝
85
- try {
86
- const alipayStatus = await this.paymentService.alipay.queryOrder(orderId);
87
- return {
88
- orderId,
89
- provider: 'alipay',
90
- status: alipayStatus.trade_status,
91
- data: alipayStatus,
92
- };
93
- } catch (alipayError) {
94
- // 两个都失败,返回详细错误信息
95
- throw new Error(
96
- `Failed to query order ${orderId}. ` +
97
- `Wechat error: ${wechatError instanceof Error ? wechatError.message : 'Unknown'}. ` +
98
- `Alipay error: ${alipayError instanceof Error ? alipayError.message : 'Unknown'}`,
99
- );
100
- }
101
- }
102
- }
103
-
104
- /**
105
- * 申请退款
106
- */
107
- async refundOrder(
108
- orderId: string,
109
- refundId: string,
110
- amount: number,
111
- reason?: string,
112
- ) {
113
- // TODO: 从数据库查询订单信息,确定支付方式和总金额
114
- const totalAmount = amount; // 实际应从数据库获取
115
-
116
- // 这里假设是微信支付,实际应根据订单信息判断
117
- const result = await this.paymentService.wechat.refund({
118
- outTradeNo: orderId,
119
- outRefundNo: refundId,
120
- refundAmount: amount,
121
- totalAmount: totalAmount,
122
- reason: reason || '用户申请退款',
123
- });
124
-
125
- return {
126
- orderId,
127
- refundId,
128
- status: result.status,
129
- data: result,
130
- };
131
- }
132
- }
@@ -1,73 +0,0 @@
1
- import { Controller, Post, Req, Body, HttpCode } from '@nestjs/common';
2
- import { Request } from 'express';
3
- import { PaymentService } from '@svton/nestjs-payment';
4
-
5
- @Controller('examples/webhooks')
6
- export class WebhookController {
7
- constructor(private readonly paymentService: PaymentService) {}
8
-
9
- /**
10
- * 微信支付回调
11
- */
12
- @Post('wechat')
13
- @HttpCode(200)
14
- async wechatWebhook(@Req() req: Request) {
15
- try {
16
- const result = await this.paymentService.wechat.handleNotify(req);
17
-
18
- console.log('Wechat payment notification:', result);
19
-
20
- // TODO: 处理支付成功逻辑
21
- // 1. 验证订单状态
22
- // 2. 更新订单状态
23
- // 3. 发送通知给用户
24
- // 4. 触发后续业务流程
25
-
26
- const { out_trade_no, transaction_id, trade_state } = result;
27
-
28
- if (trade_state === 'SUCCESS') {
29
- console.log(`Order ${out_trade_no} paid successfully`);
30
- // await this.orderService.markAsPaid(out_trade_no, transaction_id);
31
- }
32
-
33
- // 返回成功响应
34
- return {
35
- code: 'SUCCESS',
36
- message: '成功',
37
- };
38
- } catch (error) {
39
- console.error('Wechat webhook error:', error);
40
- return {
41
- code: 'FAIL',
42
- message: error.message,
43
- };
44
- }
45
- }
46
-
47
- /**
48
- * 支付宝支付回调
49
- */
50
- @Post('alipay')
51
- @HttpCode(200)
52
- async alipayWebhook(@Body() body: any) {
53
- try {
54
- const result = await this.paymentService.alipay.handleNotify(body);
55
-
56
- console.log('Alipay payment notification:', result);
57
-
58
- // TODO: 处理支付成功逻辑
59
- const { out_trade_no, trade_no, trade_status } = result;
60
-
61
- if (trade_status === 'TRADE_SUCCESS') {
62
- console.log(`Order ${out_trade_no} paid successfully`);
63
- // await this.orderService.markAsPaid(out_trade_no, trade_no);
64
- }
65
-
66
- // 返回成功响应(支付宝要求返回 success 字符串)
67
- return 'success';
68
- } catch (error) {
69
- console.error('Alipay webhook error:', error);
70
- return 'fail';
71
- }
72
- }
73
- }
@@ -1,134 +0,0 @@
1
- # 消息队列示例
2
-
3
- 本示例展示如何使用 `@svton/nestjs-queue` 模块处理异步任务。
4
-
5
- ## 文件说明
6
-
7
- - `email.processor.ts` - 队列处理器,定义任务执行逻辑
8
- - `email.service.ts` - 服务层,添加任务到队列
9
- - `email.controller.ts` - 控制器,提供 API 接口
10
-
11
- ## 核心概念
12
-
13
- - **Queue(队列)**:任务的容器
14
- - **Job(任务)**:具体的执行单元
15
- - **Processor(处理器)**:任务的执行逻辑
16
- - **Worker(工作进程)**:执行任务的进程
17
-
18
- ## 使用方式
19
-
20
- ### 1. 定义处理器
21
-
22
- ```typescript
23
- @Processor({ name: 'email' })
24
- export class EmailProcessor {
25
- @Process('send')
26
- async handleSend(job: Job<EmailData>) {
27
- // 处理任务
28
- }
29
- }
30
- ```
31
-
32
- ### 2. 添加任务
33
-
34
- ```typescript
35
- await this.queueService.addJob('email', 'send', {
36
- to: 'user@example.com',
37
- subject: 'Welcome',
38
- body: 'Welcome to our platform!',
39
- });
40
- ```
41
-
42
- ## 任务选项
43
-
44
- ### 延迟执行
45
-
46
- ```typescript
47
- await this.queueService.addJob('email', 'send', data, {
48
- delay: 60000, // 延迟 60 秒
49
- });
50
- ```
51
-
52
- ### 重试策略
53
-
54
- ```typescript
55
- await this.queueService.addJob('email', 'send', data, {
56
- attempts: 3,
57
- backoff: {
58
- type: 'exponential',
59
- delay: 1000,
60
- },
61
- });
62
- ```
63
-
64
- ### 定时任务
65
-
66
- ```typescript
67
- await this.queueService.addJob('email', 'send', data, {
68
- repeat: {
69
- cron: '0 9 * * *', // 每天 9 点执行
70
- },
71
- });
72
- ```
73
-
74
- ### 优先级
75
-
76
- ```typescript
77
- await this.queueService.addJob('email', 'send', data, {
78
- priority: 1, // 数字越小优先级越高
79
- });
80
- ```
81
-
82
- ## 测试接口
83
-
84
- ```bash
85
- # 发送邮件
86
- curl -X POST http://localhost:3000/examples/emails/send \
87
- -H "Content-Type: application/json" \
88
- -d '{
89
- "to": "user@example.com",
90
- "subject": "Test Email",
91
- "body": "This is a test email"
92
- }'
93
-
94
- # 延迟发送
95
- curl -X POST http://localhost:3000/examples/emails/send-delayed \
96
- -H "Content-Type: application/json" \
97
- -d '{
98
- "to": "user@example.com",
99
- "subject": "Delayed Email",
100
- "body": "This email will be sent after 10 seconds",
101
- "delayMs": 10000
102
- }'
103
-
104
- # 批量发送
105
- curl -X POST http://localhost:3000/examples/emails/send-batch \
106
- -H "Content-Type: application/json" \
107
- -d '{
108
- "emails": [
109
- {"to": "user1@example.com", "subject": "Test 1", "body": "Body 1"},
110
- {"to": "user2@example.com", "subject": "Test 2", "body": "Body 2"}
111
- ]
112
- }'
113
-
114
- # 紧急邮件(高优先级)
115
- curl -X POST http://localhost:3000/examples/emails/send-urgent \
116
- -H "Content-Type: application/json" \
117
- -d '{
118
- "to": "admin@example.com",
119
- "subject": "Urgent",
120
- "body": "This is urgent!"
121
- }'
122
- ```
123
-
124
- ## 最佳实践
125
-
126
- 1. **任务幂等性**:确保任务可以安全重试
127
- 2. **合理设置重试**:根据业务场景设置重试次数和策略
128
- 3. **任务拆分**:大任务拆分成小任务,提高并发度
129
- 4. **监控告警**:监听失败事件,及时处理异常
130
- 5. **资源限制**:控制并发数,避免资源耗尽
131
-
132
- ## 更多信息
133
-
134
- 查看官方文档:https://751848178.github.io/svton/packages/nestjs-queue
@@ -1,34 +0,0 @@
1
- import { Controller, Post, Body } from '@nestjs/common';
2
- import { EmailService } from './email.service';
3
- import { EmailData } from './email.processor';
4
-
5
- @Controller('examples/emails')
6
- export class EmailController {
7
- constructor(private readonly emailService: EmailService) {}
8
-
9
- @Post('send')
10
- async send(@Body() data: EmailData) {
11
- await this.emailService.sendEmail(data);
12
- return { message: 'Email queued successfully' };
13
- }
14
-
15
- @Post('send-delayed')
16
- async sendDelayed(
17
- @Body() data: EmailData & { delayMs: number },
18
- ) {
19
- await this.emailService.sendEmailDelayed(data, data.delayMs);
20
- return { message: `Email will be sent after ${data.delayMs}ms` };
21
- }
22
-
23
- @Post('send-batch')
24
- async sendBatch(@Body() data: { emails: EmailData[] }) {
25
- await this.emailService.sendBatchEmails(data.emails);
26
- return { message: `${data.emails.length} emails queued successfully` };
27
- }
28
-
29
- @Post('send-urgent')
30
- async sendUrgent(@Body() data: EmailData) {
31
- await this.emailService.sendUrgentEmail(data);
32
- return { message: 'Urgent email queued successfully' };
33
- }
34
- }
@@ -1,68 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { Processor, Process, OnQueueCompleted, OnQueueFailed } from '@svton/nestjs-queue';
3
- import { Job } from 'bullmq';
4
-
5
- export interface EmailData {
6
- to: string;
7
- subject: string;
8
- body: string;
9
- }
10
-
11
- @Processor({ name: 'email' })
12
- @Injectable()
13
- export class EmailProcessor {
14
- /**
15
- * 处理发送邮件任务
16
- */
17
- @Process('send')
18
- async handleSend(job: Job<EmailData>) {
19
- const { to, subject, body } = job.data;
20
-
21
- console.log(`Sending email to ${to}...`);
22
- console.log(`Subject: ${subject}`);
23
- console.log(`Body: ${body}`);
24
-
25
- // TODO: 实际项目中调用邮件服务
26
- // await this.emailService.send(to, subject, body);
27
-
28
- // 模拟发送延迟
29
- await new Promise(resolve => setTimeout(resolve, 2000));
30
-
31
- console.log(`Email sent successfully to ${to}`);
32
-
33
- return { success: true, to };
34
- }
35
-
36
- /**
37
- * 处理批量发送任务
38
- */
39
- @Process('sendBatch')
40
- async handleSendBatch(job: Job<{ emails: EmailData[] }>) {
41
- const { emails } = job.data;
42
-
43
- console.log(`Sending ${emails.length} emails...`);
44
-
45
- for (const email of emails) {
46
- await this.handleSend({ data: email } as Job<EmailData>);
47
- }
48
-
49
- return { success: true, count: emails.length };
50
- }
51
-
52
- /**
53
- * 任务完成回调
54
- */
55
- @OnQueueCompleted({ name: 'email' })
56
- async onCompleted(job: Job) {
57
- console.log(`Job ${job.id} completed successfully`);
58
- }
59
-
60
- /**
61
- * 任务失败回调
62
- */
63
- @OnQueueFailed({ name: 'email' })
64
- async onFailed(job: Job, error: Error) {
65
- console.error(`Job ${job.id} failed:`, error.message);
66
- // TODO: 发送告警通知
67
- }
68
- }
@@ -1,64 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { QueueService } from '@svton/nestjs-queue';
3
- import { EmailData } from './email.processor';
4
-
5
- @Injectable()
6
- export class EmailService {
7
- constructor(private readonly queueService: QueueService) {}
8
-
9
- /**
10
- * 发送单个邮件
11
- */
12
- async sendEmail(data: EmailData): Promise<void> {
13
- await this.queueService.addJob('email', 'send', data);
14
- }
15
-
16
- /**
17
- * 延迟发送邮件
18
- */
19
- async sendEmailDelayed(data: EmailData, delayMs: number): Promise<void> {
20
- await this.queueService.addJob('email', 'send', data, {
21
- delay: delayMs,
22
- });
23
- }
24
-
25
- /**
26
- * 发送邮件(带重试)
27
- */
28
- async sendEmailWithRetry(data: EmailData): Promise<void> {
29
- await this.queueService.addJob('email', 'send', data, {
30
- attempts: 3,
31
- backoff: {
32
- type: 'exponential',
33
- delay: 1000,
34
- },
35
- });
36
- }
37
-
38
- /**
39
- * 批量发送邮件
40
- */
41
- async sendBatchEmails(emails: EmailData[]): Promise<void> {
42
- await this.queueService.addJob('email', 'sendBatch', { emails });
43
- }
44
-
45
- /**
46
- * 定时发送邮件
47
- */
48
- async scheduleEmail(data: EmailData, cron: string): Promise<void> {
49
- await this.queueService.addJob('email', 'send', data, {
50
- repeat: {
51
- cron, // 例如: '0 9 * * *' 每天 9 点
52
- },
53
- });
54
- }
55
-
56
- /**
57
- * 高优先级邮件
58
- */
59
- async sendUrgentEmail(data: EmailData): Promise<void> {
60
- await this.queueService.addJob('email', 'send', data, {
61
- priority: 1, // 数字越小优先级越高
62
- });
63
- }
64
- }