@buka/nestjs-kit 4.7.1 → 4.8.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.
- package/.agents/skills/ask-matt/PHASE-BOUNDARIES.md +55 -0
- package/.agents/skills/ask-matt/SKILL.md +97 -0
- package/.agents/skills/ask-matt/agents/openai.yaml +5 -0
- package/.agents/skills/code-review/SKILL.md +89 -0
- package/.agents/skills/code-review/agents/openai.yaml +3 -0
- package/.agents/skills/codebase-design/DEEPENING.md +37 -0
- package/.agents/skills/codebase-design/DESIGN-IT-TWICE.md +44 -0
- package/.agents/skills/codebase-design/SKILL.md +114 -0
- package/.agents/skills/codebase-design/agents/openai.yaml +3 -0
- package/.agents/skills/diagnosing-bugs/SKILL.md +138 -0
- package/.agents/skills/diagnosing-bugs/agents/openai.yaml +3 -0
- package/.agents/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +44 -0
- package/.agents/skills/domain-modeling/ADR-FORMAT.md +47 -0
- package/.agents/skills/domain-modeling/GLOSSARY-FORMAT.md +60 -0
- package/.agents/skills/domain-modeling/SKILL.md +74 -0
- package/.agents/skills/domain-modeling/agents/openai.yaml +3 -0
- package/.agents/skills/grill-me/SKILL.md +7 -0
- package/.agents/skills/grill-me/agents/openai.yaml +5 -0
- package/.agents/skills/grill-with-docs/SKILL.md +7 -0
- package/.agents/skills/grill-with-docs/agents/openai.yaml +5 -0
- package/.agents/skills/grilling/SKILL.md +30 -0
- package/.agents/skills/grilling/agents/openai.yaml +3 -0
- package/.agents/skills/handoff/SKILL.md +16 -0
- package/.agents/skills/handoff/agents/openai.yaml +5 -0
- package/.agents/skills/implement/SKILL.md +17 -0
- package/.agents/skills/implement/agents/openai.yaml +5 -0
- package/.agents/skills/implement-spec/SKILL.md +42 -0
- package/.agents/skills/implement-spec/agents/openai.yaml +5 -0
- package/.agents/skills/improve-codebase-architecture/HTML-REPORT.md +123 -0
- package/.agents/skills/improve-codebase-architecture/SKILL.md +71 -0
- package/.agents/skills/improve-codebase-architecture/agents/openai.yaml +5 -0
- package/.agents/skills/pr/CREDITS.md +3 -0
- package/.agents/skills/pr/SKILL.md +170 -0
- package/.agents/skills/pr/agents/openai.yaml +3 -0
- package/.agents/skills/prototype/LOGIC.md +67 -0
- package/.agents/skills/prototype/SKILL.md +26 -0
- package/.agents/skills/prototype/UI.md +112 -0
- package/.agents/skills/prototype/agents/openai.yaml +3 -0
- package/.agents/skills/research/SKILL.md +12 -0
- package/.agents/skills/research/agents/openai.yaml +3 -0
- package/.agents/skills/retro/SKILL.md +44 -0
- package/.agents/skills/retro/agents/openai.yaml +5 -0
- package/.agents/skills/setup-matt-pocock-skills/SKILL.md +120 -0
- package/.agents/skills/setup-matt-pocock-skills/agents/openai.yaml +5 -0
- package/.agents/skills/setup-matt-pocock-skills/domain.md +51 -0
- package/.agents/skills/setup-matt-pocock-skills/issue-tracker-github.md +50 -0
- package/.agents/skills/setup-matt-pocock-skills/issue-tracker-gitlab.md +46 -0
- package/.agents/skills/setup-matt-pocock-skills/issue-tracker-local.md +30 -0
- package/.agents/skills/setup-matt-pocock-skills/triage-labels.md +15 -0
- package/.agents/skills/tdd/SKILL.md +38 -0
- package/.agents/skills/tdd/agents/openai.yaml +3 -0
- package/.agents/skills/tdd/mocking.md +59 -0
- package/.agents/skills/tdd/tests.md +77 -0
- package/.agents/skills/teach/GLOSSARY-FORMAT.md +35 -0
- package/.agents/skills/teach/LEARNING-RECORD-FORMAT.md +46 -0
- package/.agents/skills/teach/MISSION-FORMAT.md +31 -0
- package/.agents/skills/teach/RESOURCES-FORMAT.md +32 -0
- package/.agents/skills/teach/SKILL.md +140 -0
- package/.agents/skills/teach/agents/openai.yaml +5 -0
- package/.agents/skills/to-questionnaire/SKILL.md +54 -0
- package/.agents/skills/to-questionnaire/agents/openai.yaml +5 -0
- package/.agents/skills/to-spec/SKILL.md +75 -0
- package/.agents/skills/to-spec/agents/openai.yaml +5 -0
- package/.agents/skills/to-tickets/SKILL.md +105 -0
- package/.agents/skills/to-tickets/agents/openai.yaml +5 -0
- package/.agents/skills/triage/AGENT-BRIEF.md +207 -0
- package/.agents/skills/triage/OUT-OF-SCOPE.md +105 -0
- package/.agents/skills/triage/SKILL.md +112 -0
- package/.agents/skills/triage/agents/openai.yaml +5 -0
- package/.agents/skills/wait-what/SKILL.md +7 -0
- package/.agents/skills/wait-what/agents/openai.yaml +5 -0
- package/.agents/skills/wayfinder/SKILL.md +128 -0
- package/.agents/skills/wayfinder/agents/openai.yaml +5 -0
- package/.agents/skills/wizard/SKILL.md +44 -0
- package/.agents/skills/wizard/agents/openai.yaml +3 -0
- package/.agents/skills/wizard/template.sh +223 -0
- package/.agents/skills/writing-for-agents/SKILL-MECHANICS.md +22 -0
- package/.agents/skills/writing-for-agents/SKILL.md +81 -0
- package/.agents/skills/writing-for-agents/agents/openai.yaml +3 -0
- package/AGENTS.md +13 -0
- package/CHANGELOG.md +7 -0
- package/GLOSSARY.md +21 -0
- package/README.md +1 -1
- package/dist/index.d.mts +91 -1
- package/dist/index.d.ts +91 -1
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +66 -1
- package/dist/index.mjs.map +1 -1
- package/docs/agents/domain.md +51 -0
- package/docs/agents/issue-tracker.md +50 -0
- package/docs/agents/triage-labels.md +15 -0
- package/docs/exception.md +26 -1
- package/package.json +1 -1
- package/skills/nestjs-kit-coding-standards/SKILL.md +94 -44
- package/skills/nestjs-kit-coding-standards/references/controller.md +2 -2
- package/skills/nestjs-kit-coding-standards/references/dto.md +2 -2
- package/skills/nestjs-kit-coding-standards/references/entity.md +2 -2
- package/skills/nestjs-kit-coding-standards/references/query.md +2 -2
- package/skills/nestjs-kit-coding-standards/references/service.md +4 -3
- package/skills/nestjs-kit-crypto/SKILL.md +1 -5
- package/skills/nestjs-kit-exception/SKILL.md +15 -8
- package/skills/nestjs-kit-http/SKILL.md +1 -5
- package/skills/nestjs-kit-logger/SKILL.md +1 -4
- package/skills/nestjs-kit-object-storage/SKILL.md +1 -5
- package/skills/nestjs-kit-open-bao/SKILL.md +1 -5
- package/skills/nestjs-kit-swagger/SKILL.md +1 -6
- package/skills/nestjs-kit-urn/SKILL.md +1 -3
- package/skills-lock.json +167 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Domain Docs
|
|
2
|
+
|
|
3
|
+
How the engineering skills should consume this repo's domain documentation when exploring the codebase.
|
|
4
|
+
|
|
5
|
+
## Before exploring, read these
|
|
6
|
+
|
|
7
|
+
- **`GLOSSARY.md`** at the repo root, or
|
|
8
|
+
- **`GLOSSARY-MAP.md`** at the repo root if it exists: it points at one `GLOSSARY.md` per context. Read each one relevant to the topic.
|
|
9
|
+
- **`docs/adr/`**: read ADRs that touch the area you're about to work in. In multi-context repos, also check `src/<context>/docs/adr/` for context-scoped decisions.
|
|
10
|
+
|
|
11
|
+
If any of these files don't exist, **proceed silently**. Don't flag their absence; don't suggest creating them upfront. The `/domain-modeling` skill (reached via `/grill-with-docs` and `/improve-codebase-architecture`) creates them lazily when terms or decisions actually get resolved.
|
|
12
|
+
|
|
13
|
+
## File structure
|
|
14
|
+
|
|
15
|
+
Single-context repo (most repos):
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
/
|
|
19
|
+
├── GLOSSARY.md
|
|
20
|
+
├── docs/adr/
|
|
21
|
+
│ ├── 0001-event-sourced-orders.md
|
|
22
|
+
│ └── 0002-postgres-for-write-model.md
|
|
23
|
+
└── src/
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Multi-context repo (presence of `GLOSSARY-MAP.md` at the root):
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
/
|
|
30
|
+
├── GLOSSARY-MAP.md
|
|
31
|
+
├── docs/adr/ ← system-wide decisions
|
|
32
|
+
└── src/
|
|
33
|
+
├── ordering/
|
|
34
|
+
│ ├── GLOSSARY.md
|
|
35
|
+
│ └── docs/adr/ ← context-specific decisions
|
|
36
|
+
└── billing/
|
|
37
|
+
├── GLOSSARY.md
|
|
38
|
+
└── docs/adr/
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Use the glossary's vocabulary
|
|
42
|
+
|
|
43
|
+
When your output names a domain concept (in an issue title, a refactor proposal, a hypothesis, a test name), use the term as defined in `GLOSSARY.md`. Don't drift to synonyms the glossary explicitly avoids.
|
|
44
|
+
|
|
45
|
+
If the concept you need isn't in the glossary yet, that's a signal: either you're inventing language the project doesn't use (reconsider) or there's a real gap (note it for `/domain-modeling`).
|
|
46
|
+
|
|
47
|
+
## Flag ADR conflicts
|
|
48
|
+
|
|
49
|
+
If your output contradicts an existing ADR, surface it explicitly rather than silently overriding:
|
|
50
|
+
|
|
51
|
+
> _Contradicts ADR-0007 (event-sourced orders), but worth reopening because…_
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Issue tracker: GitHub
|
|
2
|
+
|
|
3
|
+
Issues and specs for this repo live as GitHub issues. Use the `gh` CLI for all operations.
|
|
4
|
+
|
|
5
|
+
## Conventions
|
|
6
|
+
|
|
7
|
+
- **Create an issue**: `gh issue create --title "..." --body "..."`. Use a heredoc for multi-line bodies.
|
|
8
|
+
- **Read an issue**: `gh issue view <number> --json number,title,body,labels,comments`.
|
|
9
|
+
- **List issues**: `gh issue list --state open --json number,title,body,labels,comments --jq '[.[] | {number, title, body, labels: [.labels[].name], comments: [.comments[].body]}]'` with appropriate `--label` and `--state` filters.
|
|
10
|
+
- **Make an issue a sub-issue of a parent**: `gh issue create --parent <parent> ...`, or `gh issue edit <parent> --add-sub-issue <child>` afterwards (`gh` 2.94+). Older `gh`: `gh api --method POST repos/<owner>/<repo>/issues/<parent>/sub_issues -F sub_issue_id=<child-db-id>` (database id, as in **Blocking** below). Without sub-issues, put `Part of #<parent>` at the top of the child body.
|
|
11
|
+
- **Blocking**: GitHub's **native issue dependencies**, the canonical, UI-visible representation. Add an edge with `gh issue create --blocked-by <n>,<n> ...` or `gh issue edit <child> --add-blocked-by <blocker>` (`gh` 2.94+). Older `gh`: `gh api --method POST repos/<owner>/<repo>/issues/<child>/dependencies/blocked_by -F issue_id=<blocker-db-id>`, where `<blocker-db-id>` is the blocker's numeric **database id** (`gh api repos/<owner>/<repo>/issues/<n> --jq .id`, _not_ the `#number` or `node_id`). Where dependencies aren't available, fall back to a `Blocked by: #<n>, #<n>` line at the top of the child body. A ticket is unblocked when every blocker is closed.
|
|
12
|
+
- **Count open blockers**: `gh api repos/<owner>/<repo>/issues/<n> --jq .issue_dependencies_summary.blocked_by` (open blockers only; 0 means startable). `gh issue view <n> --json blockedBy` also lists closed blockers, so filter it: `--jq '[.blockedBy.nodes[] | select(.state == "OPEN")] | length'`.
|
|
13
|
+
- **Comment on an issue**: `gh issue comment <number> --body "..."`
|
|
14
|
+
- **Apply / remove labels**: `gh issue edit <number> --add-label "..."` / `--remove-label "..."`
|
|
15
|
+
- **Close**: post the explanation with `gh issue comment <number> --body "..."`, then `gh issue close <number>`. On an already-closed issue, `gh issue close --comment` drops the comment and still exits 0.
|
|
16
|
+
|
|
17
|
+
Infer the repo from `git remote -v`; `gh` does this automatically when run inside a clone.
|
|
18
|
+
|
|
19
|
+
File issues only into the user's own repo. Never file into `mattpocock/skills` (the repo these skills come from) unless the user owns it. In a fork, `gh` can default to the upstream parent, so before the first `gh issue create` of a session, check the target with `gh repo view --json nameWithOwner` and confirm it with the user if it is not their repo.
|
|
20
|
+
|
|
21
|
+
## Pull requests as a triage surface
|
|
22
|
+
|
|
23
|
+
**PRs as a request surface: no.** _(Set to `yes` if this repo treats external PRs as feature requests; `/triage` reads this flag.)_
|
|
24
|
+
|
|
25
|
+
When set to `yes`, PRs run through the same labels and states as issues, using the `gh pr` equivalents:
|
|
26
|
+
|
|
27
|
+
- **Read a PR**: `gh pr view <number>`, then `gh pr view <number> --comments` (off a TTY, `--comments` prints only the comments, not the body), and `gh pr diff <number>` for the diff.
|
|
28
|
+
- **List external PRs for triage**: `gh api --paginate 'repos/{owner}/{repo}/pulls?state=open' --jq '.[] | select(.author_association | IN("OWNER","MEMBER","COLLABORATOR") | not) | {number, title, author: .user.login, author_association, labels: [.labels[].name]}'`.
|
|
29
|
+
- **Comment / label / close**: `gh pr comment`, `gh pr edit --add-label`/`--remove-label`, `gh pr close`.
|
|
30
|
+
|
|
31
|
+
GitHub shares one number space across issues and PRs, so a bare `#42` may be either: resolve with `gh pr view 42` and fall back to `gh issue view 42`.
|
|
32
|
+
|
|
33
|
+
## When a skill says "publish to the issue tracker"
|
|
34
|
+
|
|
35
|
+
Create a GitHub issue.
|
|
36
|
+
|
|
37
|
+
## When a skill says "fetch the relevant ticket"
|
|
38
|
+
|
|
39
|
+
Read it as in **Read an issue** above.
|
|
40
|
+
|
|
41
|
+
## Wayfinding operations
|
|
42
|
+
|
|
43
|
+
Used by `/wayfinder`. The **map** is a single issue with **child** issues as tickets.
|
|
44
|
+
|
|
45
|
+
- **Map**: a single issue labelled `wayfinder:map`, holding the Notes / Decisions-so-far / Fog body. `gh issue create --label wayfinder:map`.
|
|
46
|
+
- **Child ticket**: an issue linked to the map as a GitHub sub-issue (see **Make an issue a sub-issue of a parent**). Where sub-issues aren't enabled, add the child to a task list in the map body and put `Part of #<map>` at the top of the child body. Labels: `wayfinder:<type>` (`research`/`prototype`/`grilling`/`task`). Once claimed, the ticket is assigned to the driving dev.
|
|
47
|
+
- **Blocking**: see **Blocking** under Conventions.
|
|
48
|
+
- **Frontier query**: list the map's open children (`gh issue list --state open`, scoped to the map's sub-issues / task list), drop any with an open blocker (see **Count open blockers**, or an open issue in the `Blocked by` line) or an assignee; first in map order wins.
|
|
49
|
+
- **Claim**: `gh issue edit <n> --add-assignee @me`, the session's first write.
|
|
50
|
+
- **Resolve**: `gh issue comment <n> --body "<answer>"`, then `gh issue close <n>`, then append a context pointer (gist + link) to the map's Decisions-so-far.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Triage Labels
|
|
2
|
+
|
|
3
|
+
The skills speak in terms of five canonical triage roles. This file maps those roles to the actual label strings used in this repo's issue tracker.
|
|
4
|
+
|
|
5
|
+
| Label in mattpocock/skills | Label in our tracker | Meaning |
|
|
6
|
+
| -------------------------- | -------------------- | ---------------------------------------- |
|
|
7
|
+
| `needs-triage` | `needs-triage` | Maintainer needs to evaluate this issue |
|
|
8
|
+
| `needs-info` | `needs-info` | Waiting on reporter for more information |
|
|
9
|
+
| `ready-for-agent` | `ready-for-agent` | Fully specified, ready for an AFK agent |
|
|
10
|
+
| `ready-for-human` | `ready-for-human` | Requires human implementation |
|
|
11
|
+
| `wontfix` | `wontfix` | Will not be actioned |
|
|
12
|
+
|
|
13
|
+
When a skill mentions a role (e.g. "apply the AFK-ready triage label"), use the corresponding label string from this table.
|
|
14
|
+
|
|
15
|
+
Edit the "Label in our tracker" column to match whatever vocabulary you actually use.
|
package/docs/exception.md
CHANGED
|
@@ -211,7 +211,7 @@ NestJS 内置 HTTP 异常会被映射到预留错误码(`moduleId: 0`):
|
|
|
211
211
|
| 503 | `DEGRADE` | 503 |
|
|
212
212
|
| 504 | `THIRD_PARTY` | 504 |
|
|
213
213
|
|
|
214
|
-
> `moduleId: 0
|
|
214
|
+
> `moduleId: 0`(内置 HTTP 语义)与 `1`(客户端门禁)是公共号段,由 kit 占用;业务模块在 `1000` - `FZZZ` 范围内声明,见公司 `docs.specifications` 的 error-codes.md。
|
|
215
215
|
|
|
216
216
|
### BukaExceptions — 内置异常
|
|
217
217
|
|
|
@@ -243,6 +243,31 @@ throw new BukaExceptions.Unauthorized();
|
|
|
243
243
|
| `BukaExceptions.GatewayTimeout` | `THIRD_PARTY` | 504 |
|
|
244
244
|
| `BukaExceptions.ServiceUnavailable` | `DEGRADE` | 503 |
|
|
245
245
|
|
|
246
|
+
### ClientGateExceptions — 客户端门禁
|
|
247
|
+
|
|
248
|
+
跨应用通用的「用户此刻做不了这件事,必须完成一个固定动作才能恢复」语义。它占用公共号段、任何系统都可抛出,不属于任何系统的业务语义:
|
|
249
|
+
|
|
250
|
+
| 异常 | 语义 | sequenceId |
|
|
251
|
+
| ------------------------------------------- | ---------------- | ---------- |
|
|
252
|
+
| `ClientGateExceptions.ClientVersionRejected` | 客户端版本过低 | 1 |
|
|
253
|
+
| `ClientGateExceptions.EntitlementRequired` | 需要付费资格 | 2 |
|
|
254
|
+
| `ClientGateExceptions.EntitlementExpired` | 付费资格已失效 | 3 |
|
|
255
|
+
| `ClientGateExceptions.Maintenance` | 服务维护中 | 4 |
|
|
256
|
+
| `ClientGateExceptions.CapabilityUnavailable` | 功能未开放 | 5 |
|
|
257
|
+
| `ClientGateExceptions.PlanLimitReached` | 已达当前方案上限 | 6 |
|
|
258
|
+
|
|
259
|
+
全部属于 `FEATURE` 类别(HTTP 403)。参数走 `details`,作为**最后一个参数**传入详情数组:
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
import { ClientGateExceptions } from "@buka/nestjs-kit";
|
|
263
|
+
|
|
264
|
+
throw new ClientGateExceptions.PlanLimitReached("成员", 15, 10, [
|
|
265
|
+
{ type: "client_gate", resource: "members", current: 15, max: 10 },
|
|
266
|
+
]);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
详情统一以 `type: 'client_gate'` 标识,字段按 `seq` 取(类型见 `PlanLimitDetail` 等导出)。
|
|
270
|
+
|
|
246
271
|
如需禁用全局过滤器以自定义异常处理:
|
|
247
272
|
|
|
248
273
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buka/nestjs-kit",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.0",
|
|
4
4
|
"description": "Buka Nestjs Kit",
|
|
5
5
|
"packageManager": "pnpm@10.33.4+sha512.1c67b3b359b2d408119ba1ed289f34b8fc3c6873412bec6fd264fbdc82489e510fcbecb9ce9d22dae7f3b76269d8441046014bdca53b9979cd7a561ad631b800",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-coding-standards
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
PickType/OmitType/PartialType/IntersectionType、ResponseBodyType、EntityDto/@EntityRef/PrimaryKeyType)、
|
|
6
|
-
定义 MikroORM 实体(DiscreteEntity/LinearEntity/TimestampedEntity 基类、Column.*、Cardinality.*)、
|
|
7
|
-
编写 service(command 单参契约、Ref<Entity> 传递、em.flush 事务约定)与 controller(operationId 命名、中文 JSDoc、@IsBrowserRequest)、
|
|
8
|
-
实现 list 查询(@FilterQuery/OrderQueryType/@PageQuery/Slice/ParseUUIDv7Pipe)、
|
|
9
|
-
组织模块目录与命名。当用户编写或修改 .dto.ts/.entity.ts/.service.ts/.controller.ts 文件、
|
|
10
|
-
新增业务模块或 CRUD 接口、移动或重命名业务源码文件时使用本 skill;
|
|
11
|
-
只要在项目中新增或修改 NestJS 业务代码,即使用户未提到具体文件名也应使用。
|
|
12
|
-
异常抛出、日志、加密存储等模块级能力由对应 skill 覆盖,本 skill 只规定代码结构与文件级规范。
|
|
4
|
+
本项目 NestJS 编码规约(强制)。改代码前先加载本 skill:新增、修改、重构、移动重命名、按 review 或 lint 修复,即使请求未点名文件。规约覆盖目录与命名、模块与 provider 组织、DTO/实体/service/controller 写法、列表查询、事务与异常。按改动涉及的文件类型读对应 reference,不必通读。
|
|
13
5
|
---
|
|
14
6
|
|
|
15
|
-
#
|
|
7
|
+
# 编码规约(coding standards)
|
|
16
8
|
|
|
17
|
-
|
|
9
|
+
本规约是 @buka/nestjs-kit 业务项目里全部 NestJS 代码的强制规范:动手改代码前先加载本 skill,按被改动文件的类型读对应 reference,改完对照规约自查一遍。
|
|
18
10
|
|
|
19
|
-
##
|
|
11
|
+
## 适用前提
|
|
20
12
|
|
|
21
13
|
以下机制由项目接入阶段统一提供,编写业务文件时直接依赖:
|
|
22
14
|
|
|
@@ -24,31 +16,9 @@ description: >
|
|
|
24
16
|
- `DatabaseConfig` 默认 `flushMode: FlushMode.COMMIT`(查询前隐式 flush 已跟踪变更)。事务提交点在 controller 末尾统一 `em.flush()`(见 [references/controller.md](references/controller.md) 与 [references/service.md](references/service.md))。
|
|
25
17
|
- Swagger 文档由 `@nestjs/swagger` CLI 插件从 controller/DTO 元数据生成,`SwaggerPatcher` 只做文档后处理(见 [nestjs-kit-swagger](../nestjs-kit-swagger/SKILL.md))。
|
|
26
18
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
| 要做什么 | 用什么 |
|
|
30
|
-
|---|---|
|
|
31
|
-
| kit API 导入 | `import { X } from '@buka/nestjs-kit'`(细则见「通用约定·导入来源」);`Ref`/`Collection`/`Cursor`/`EntityManager` 自 `@mikro-orm/core`;`@Entity` 自 `@mikro-orm/decorators/legacy` |
|
|
32
|
-
| 声明 service/controller | 普通 `@Injectable()`/`@Controller()` 类,无需继承基类 |
|
|
33
|
-
| 请求体校验转换 | 全局 `BukaValidationPipe`(由 `BukaModule` 注册;`@EntityRef` 自动转 wrapped `Ref`) |
|
|
34
|
-
| 事务提交 | controller handler 末尾统一 `await this.em.flush()`;service 非计数器等原子场景不 flush |
|
|
35
|
-
| 抛出异常 | `@ModuleExceptions` 声明的异常工厂(规范见 [nestjs-kit-exception](../nestjs-kit-exception/SKILL.md)) |
|
|
36
|
-
| 响应结构 | 统一 `{ data, meta }`:单条 `ResponseBodyType.from()`,列表 `ListResponseBodyType.fromSlice()` |
|
|
37
|
-
|
|
38
|
-
## references 导航
|
|
39
|
-
|
|
40
|
-
本 skill 的 references/ 目录按文件类型拆分,按需加载——写某类文件时只读对应的 reference;新增一条完整 CRUD 链路(跨 5 类文件)时才通读全部。
|
|
41
|
-
|
|
42
|
-
| 要写什么文件 | 读哪个 reference |
|
|
43
|
-
|---|---|
|
|
44
|
-
| 写 DTO(请求/响应) | [references/dto.md](references/dto.md) |
|
|
45
|
-
| 定义实体 | [references/entity.md](references/entity.md) |
|
|
46
|
-
| 实现 list 查询(过滤/排序/分页) | [references/query.md](references/query.md) |
|
|
47
|
-
| 写 service(业务逻辑) | [references/service.md](references/service.md) |
|
|
48
|
-
| 写 controller(HTTP 层) | [references/controller.md](references/controller.md) |
|
|
49
|
-
| 文件放在哪个目录、如何命名 | 见下方「目录组织」章 |
|
|
19
|
+
未接入 @buka/nestjs-kit 的项目,依赖这些机制的条款不适用。
|
|
50
20
|
|
|
51
|
-
##
|
|
21
|
+
## 硬红线(跨文件通用)
|
|
52
22
|
|
|
53
23
|
以"该事实是否 ≥2 类文件共用"为判据——以下五条红线与一条全局数据流链路对所有文件类型成立:
|
|
54
24
|
|
|
@@ -85,6 +55,32 @@ CreateXxxRequestDto(显式声明字段,@Model/@Property/@EntityRef 等)
|
|
|
85
55
|
|
|
86
56
|
请求体中 `@EntityRef`/`PrimaryKeyType` 字段由全局 `BukaValidationPipe` 在运行时转成 `em.getReference(Entity, id)` 的 wrapped `Ref`,到 service 入参时已是可直接使用的 `Ref<Entity>`(见 [references/dto.md](references/dto.md) 的实体派生 DTO 章)。
|
|
87
57
|
|
|
58
|
+
## 按文件类型读 reference
|
|
59
|
+
|
|
60
|
+
本规约的 references/ 目录按文件类型拆分,按需加载——写某类文件时只读对应的 reference;新增一条完整 CRUD 链路(跨 5 类文件)时才通读全部。
|
|
61
|
+
|
|
62
|
+
| 要写什么文件 | 读哪个 reference |
|
|
63
|
+
|---|---|
|
|
64
|
+
| 写 DTO(请求/响应) | [references/dto.md](references/dto.md) |
|
|
65
|
+
| 定义实体 | [references/entity.md](references/entity.md) |
|
|
66
|
+
| 实现 list 查询(过滤/排序/分页) | [references/query.md](references/query.md) |
|
|
67
|
+
| 写 service(业务逻辑) | [references/service.md](references/service.md) |
|
|
68
|
+
| 写 controller(HTTP 层) | [references/controller.md](references/controller.md) |
|
|
69
|
+
| 文件放在哪个目录、如何命名 | 见下方「业务模块目录组织」与「跨模块共享层」两章 |
|
|
70
|
+
|
|
71
|
+
改完自查:被改动的每类文件都对照过对应 reference,文件放在正确目录、类名与文件名一一对应、导入来源正确。
|
|
72
|
+
|
|
73
|
+
## 速查表(跨文件通用规则)
|
|
74
|
+
|
|
75
|
+
| 要做什么 | 用什么 |
|
|
76
|
+
|---|---|
|
|
77
|
+
| kit API 导入 | `import { X } from '@buka/nestjs-kit'`(细则见「硬红线·导入来源」);`Ref`/`Collection`/`Cursor`/`EntityManager` 自 `@mikro-orm/core`;`@Entity` 自 `@mikro-orm/decorators/legacy` |
|
|
78
|
+
| 声明 service/controller | 普通 `@Injectable()`/`@Controller()` 类,无需继承基类 |
|
|
79
|
+
| 请求体校验转换 | 全局 `BukaValidationPipe`(由 `BukaModule` 注册;`@EntityRef` 自动转 wrapped `Ref`) |
|
|
80
|
+
| 事务提交 | controller handler 末尾统一 `await this.em.flush()`;service 非计数器等原子场景不 flush |
|
|
81
|
+
| 抛出异常 | `@ModuleExceptions` 声明的异常工厂(规范见 [nestjs-kit-exception](../nestjs-kit-exception/SKILL.md)) |
|
|
82
|
+
| 响应结构 | 统一 `{ data, meta }`:单条 `ResponseBodyType.from()`,列表 `ListResponseBodyType.fromSlice()` |
|
|
83
|
+
|
|
88
84
|
## 业务模块目录组织
|
|
89
85
|
|
|
90
86
|
### 目录结构
|
|
@@ -96,25 +92,44 @@ src/modules/<module-name>/
|
|
|
96
92
|
├── <module-name>.exceptions.ts # 模块专属异常(@ModuleExceptions 集中声明)
|
|
97
93
|
├── commands/ # Command interface —— 聚合 service 方法的参数(写法见 references/service.md)
|
|
98
94
|
├── constants/ # 枚举和常量
|
|
99
|
-
├── decorators/ # 模块内自定义装饰器(可跨模块 import
|
|
95
|
+
├── decorators/ # 模块内自定义装饰器(可跨模块 import 共享,复用度高的下沉共享层)
|
|
100
96
|
├── dto/ # 对外暴露的数据结构
|
|
101
97
|
│ ├── requests/ # 请求参数 DTO
|
|
102
98
|
│ └── responses/ # 响应体 DTO({FunctionName}ResponseDto extends ResponseBodyType(...))
|
|
103
99
|
├── entities/ # MikroORM 实体
|
|
104
|
-
├──
|
|
100
|
+
├── features/ # 模块内功能分组(按需创建,见下文「features 子目录」)
|
|
101
|
+
│ └── <feature-name>/
|
|
102
|
+
│ ├── dto/ # 该功能独占的 DTO
|
|
103
|
+
│ ├── constants/ # 该功能独占的常量/策略
|
|
104
|
+
│ ├── schema/ # 该功能对外的 OpenAPI schema 片段(可选)
|
|
105
|
+
│ └── <feature-entry>.ts # 功能实现入口(如 provider)
|
|
106
|
+
├── services/ # 模块自身的业务逻辑(主 service + 按复杂度拆出的 provider;写法见 references/service.md)
|
|
105
107
|
├── subscribers/ # MikroORM 订阅者(可选)
|
|
106
108
|
├── types/ # 模块内部类型(可跨模块 import 共享)
|
|
107
109
|
└── utils/ # 模块内工具函数(可选)
|
|
108
110
|
```
|
|
109
111
|
|
|
112
|
+
### features 子目录
|
|
113
|
+
|
|
114
|
+
默认仍按文件类型扁平组织;仅当**同一模块承载多个可独立演进的能力**(如一种凭据实现、一个子域)时,才为该能力建 `features/<feature-name>/`,把它的实现、`dto/`、`constants/`、`schema/` 一起收拢。只有一个能力的模块保持扁平,不要预先套壳。
|
|
115
|
+
|
|
116
|
+
- **判据**:能力自身有独立的对外契约(自己的请求 DTO、策略/常量、schema),且与模块内其它能力可以分别演进;或该能力以插件方式注册(如 `CredentialProvider`),宿主模块不应出现它的类型分支。
|
|
117
|
+
- **按能力分组,不按文件类型分组**:是 `features/sms-credential/`,不是 `features/dto/`、`features/services/`;子目录名 kebab-case,用能力名。
|
|
118
|
+
- **能力内沿用文件类型目录**:`features/<feature-name>/` 下仍是 `dto/`、`constants/`、`schema/`(可选)加实现入口文件;需要时再加 `commands/`、`services/`、`types/`、`utils/`,不发明新的目录名。
|
|
119
|
+
- **只分层一级**:`features/<feature-name>/` 之下不再嵌套 `features/`;能力继续膨胀到需要独立注册时,拆成独立模块。
|
|
120
|
+
- **归属与注册**:feature 内不建 `<module-name>.module.ts` 与 controller;宿主模块的 `<module-name>.module.ts` 从 `./features/<feature-name>/...` import 实现,并放进 `providers`/`exports`。
|
|
121
|
+
- **对外导入面**:feature 的 `dto/`、`types/`、`schema/` 可被其他模块以完整路径 import(`~/modules/<module>/features/<feature-name>/...`);实现(provider、内部工具)不对外。被多个模块共用、又不属于任何模块的纯契约,按「跨模块共享层」下沉到 `src/shared/`。
|
|
122
|
+
- **模块级目录只留公共部分**:`entities/`、模块级 `commands/`、`services/` 与共享常量留在模块根;feature 独占的 DTO、常量/策略、schema 与实现搬进 feature 目录,搬移后删除空目录并更新所有引用路径。
|
|
123
|
+
|
|
110
124
|
### 各目录职责边界
|
|
111
125
|
|
|
112
126
|
- **commands/**:只放 `interface`(聚合某个 service 方法的全部参数);不放 handler、不放 DTO。command 的设计与写法见 [references/service.md](references/service.md)
|
|
113
|
-
- **constants
|
|
114
|
-
- **decorators/**:自定义装饰器(可直接跨模块 import 共享)
|
|
115
|
-
- **dto
|
|
127
|
+
- **constants/**:模块级枚举与常量;某 feature 独占的常量/策略放该 feature 的 `constants/`
|
|
128
|
+
- **decorators/**:自定义装饰器(可直接跨模块 import 共享);复用度高的下沉共享层(`src/shared/`,见「跨模块共享层」)
|
|
129
|
+
- **dto/**:模块自身(不属于某个 feature)的对外数据结构(请求/响应),内部类型放 `types/`;requests 与 responses 分开放;feature 独占的 DTO 放该 feature 的 `dto/`
|
|
116
130
|
- **entities/**:只放 MikroORM 实体
|
|
117
|
-
- **
|
|
131
|
+
- **features/**:模块内可独立演进能力的收拢目录(如 `sms-credential`),按需创建;创建后该能力的实现、dto、constants、schema 不再散落在模块级目录
|
|
132
|
+
- **services/**:模块自身(不属于某个 feature)的业务逻辑;主 service 与按复杂度拆出的子 service/provider 同放此目录——拆分信号与不拆时机见 [references/service.md](references/service.md)(本条目只定目录归属)
|
|
118
133
|
- **exceptions** 不建目录:集中一个文件 `<module-name>.exceptions.ts`,所有 `@ModuleExceptions` 声明都写在这里
|
|
119
134
|
- **types/**:模块内部类型,可直接跨模块 import 共享;**utils/**:模块私有工具函数,通常不共享,只在有多个文件共用的工具函数时才建
|
|
120
135
|
|
|
@@ -123,18 +138,53 @@ src/modules/<module-name>/
|
|
|
123
138
|
- **一个 DTO 一个文件**:每个请求/响应 DTO 独立成文件(`get-student-response.dto.ts`),全部 DTO 集中在一个 `dtos.ts` 里会破坏按需加载与搜索效率
|
|
124
139
|
- 一个实体一个文件(`.entity.ts`),一个 controller 一个文件;controller 的方法命名、拆分上限(约 10 个)与职责边界见 [references/controller.md](references/controller.md)
|
|
125
140
|
- service 拆分以复杂度为准绳:子资源具备多个功能拆 `XxxSubresourceService`、可独立理解的复杂职责(通知、审计、同步)拆独立 provider、简单 service 不拆;完整信号清单见 [references/service.md](references/service.md)
|
|
126
|
-
- 跨模块共享是允许的:entities、装饰器、dto、types 可直接被其他模块 import
|
|
141
|
+
- 跨模块共享是允许的:entities、装饰器、dto、types 可直接被其他模块 import,features 内的 dto/types/schema 同理;commands、utils 与 feature 的实现属模块私有,通常不共享(确需跨模块复用的纯契约再下沉 `src/shared/`,见「跨模块共享层」)
|
|
142
|
+
- 能力分组按需引入:模块内出现第二个可独立演进的能力时才建 `features/`,把该能力的文件整体搬入并更新所有引用路径(含跨模块的完整路径);只有一个能力时保持扁平
|
|
143
|
+
- 共享层按真实复用提取:能力先留在模块内,确认第二个模块也要用时才下沉到 `src/shared/<name>/`;不要因为"将来可能复用"提前抽象
|
|
127
144
|
|
|
128
145
|
### 命名约定
|
|
129
146
|
|
|
130
147
|
- 文件名 kebab-case,类名 PascalCase,一一对应(`GetStudentResponseDto` → `get-student-response.dto.ts`)
|
|
148
|
+
- 目录名 kebab-case、用能力名:`features/sms-credential/`、`src/shared/access-token-claims/`;不用 `dtos/`、`utils/`、`common/` 这类泛型名
|
|
131
149
|
- MikroORM 仓储注入(`@InjectRepository(X)`)的属性命名:`{EntityName}Repo`(如 `courseRepo`、`enrollmentRepo`),不写 `courseRepository`、`repo` 等变体
|
|
132
150
|
- 变量与属性命名使用完整单词,禁止自造无法识别含义的极简缩写——如把 `ServiceDescriptor` 缩成 `sd`、把 `service` 缩成 `svc`;规范中已约定的惯用缩写(`em`、`id`、`url` 等)除外
|
|
133
151
|
- Controller 方法命名(operationId = 动词 + 资源名、全局唯一)、路由参数命名(`:studentId`)与中文 JSDoc 约定见 [references/controller.md](references/controller.md)
|
|
134
152
|
|
|
153
|
+
## 跨模块共享层
|
|
154
|
+
|
|
155
|
+
`src/shared/<name>/` 与 `src/modules/` 平级,存放**被 ≥2 个模块共用、且不属于任何一个模块的纯契约**;模块内部仍按文件类型组织,只有真正跨模块复用时才下沉到这里。
|
|
156
|
+
|
|
157
|
+
### 目录结构
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
src/shared/<name>/
|
|
161
|
+
├── index.ts # 唯一出口,只 re-export 公开契约
|
|
162
|
+
├── <name>.ts # 契约主体:接口/类型/纯函数
|
|
163
|
+
├── <name>.dto.ts # wire 表示(需要进入 Swagger/SDK 时)
|
|
164
|
+
└── *.spec.ts # 与实现同目录
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
目录内可按主题拆多个文件(如契约主体、`projections.ts`、附加 DTO),但对外只经 `index.ts` 暴露。
|
|
168
|
+
|
|
169
|
+
### 判据与边界
|
|
170
|
+
|
|
171
|
+
- **下沉判据**:同一契约/能力被 ≥2 个模块引用,且责任不属于其中任何一个模块;只有一个模块使用时留在该模块(可用模块内 `types/`、`utils/` 或 feature 目录)。
|
|
172
|
+
- **只放纯契约**:类型、接口、DTO、常量、纯函数与框架无关的 Error 类;带状态、带 DI、带生命周期的东西不进来——`@Injectable()` provider、`@Module`、controller、ORM 实体都不放。
|
|
173
|
+
- **依赖方向单向**:shared 只依赖 `@buka/nestjs-kit`、SDK 与第三方包,**禁止 import `~/modules/**`**;否则模块与共享层互相依赖,抽取即产生循环。
|
|
174
|
+
- **一个能力一个子目录**:`src/shared/<name>/` 对应一个内聚契约(如 `access-token-claims`);不建 `src/shared/utils/`、`src/shared/common/` 这类大杂烩目录,第二个不相关能力出现时新开子目录。
|
|
175
|
+
- **唯一出口**:目录内 `index.ts` 汇总公开符号;其他模块一律 `import { X } from '~/shared/<name>'`,不深入具体文件(目录内部文件之间才用相对路径 import)。
|
|
176
|
+
- **与 features 的分工**:`features/` 属于模块内部,所有权在宿主模块;`shared/` 不属于任何模块,是模块之间的公共契约层。两者都不是"暂时没地方放"的收容所。
|
|
177
|
+
|
|
178
|
+
### 命名约定
|
|
179
|
+
|
|
180
|
+
- 子目录名 kebab-case、用能力名(`access-token-claims`),不用文件类型名
|
|
181
|
+
- `index.ts` 只做 re-export,不写实现;DTO 的 wire 表示用 `<name>.dto.ts`
|
|
182
|
+
|
|
135
183
|
## 注意事项
|
|
136
184
|
|
|
137
185
|
- DTO 应放入对应子目录:`requests/` 用于请求参数,`responses/` 用于响应体——两者混放会破坏对外结构的一致性
|
|
186
|
+
- `features/` 是模块内分组而非拆模块:不新增 NestJS 模块与 controller,注册与导出仍由宿主模块负责
|
|
187
|
+
- `src/shared/` 只放跨模块纯契约,不放"什么都能塞"的公共工具箱;只有一个模块使用的能力留在模块内
|
|
138
188
|
- 移动/重命名文件时同步检查:类名与文件名的一一对应、operationId 是否仍全局唯一、JSDoc 是否随迁、command→DTO 映射是否同步(operationId/JSDoc 规则见 [references/controller.md](references/controller.md))
|
|
139
189
|
|
|
140
190
|
## 相关 skills
|
|
@@ -146,4 +196,4 @@ src/modules/<module-name>/
|
|
|
146
196
|
- [nestjs-kit-logger](../nestjs-kit-logger/SKILL.md) — LoggerModule、@InjectLogger
|
|
147
197
|
- [nestjs-kit-http](../nestjs-kit-http/SKILL.md) — keq 服务间调用的错误处理
|
|
148
198
|
- [nestjs-kit-object-storage](../nestjs-kit-object-storage/SKILL.md) — S3 兼容对象存储
|
|
149
|
-
- [nestjs-kit-open-bao](../nestjs-kit-open-bao/SKILL.md) — OpenBao/Vault 集成
|
|
199
|
+
- [nestjs-kit-open-bao](../nestjs-kit-open-bao/SKILL.md) — OpenBao/Vault 集成
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Controller 编写与事务提交(HTTP 层)
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 本文件是 [编码规约](../SKILL.md) 的按需加载部分(写 controller 时读本文件)。导入来源、事务与异常等硬红线见 [SKILL.md 「硬红线」章](../SKILL.md#硬红线跨文件通用);目录归属见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)。
|
|
4
4
|
|
|
5
5
|
## 前置条件
|
|
6
6
|
|
|
@@ -15,7 +15,7 @@ src/modules/<module-name>/<module-name>.controller.ts # 主 controller
|
|
|
15
15
|
src/modules/<module-name>/<subresource>.controller.ts # 拆出的子 controller
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
目录与文件命名见 [SKILL.md
|
|
18
|
+
目录与文件命名见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)(一个 controller 一个文件)。
|
|
19
19
|
|
|
20
20
|
## 速查表
|
|
21
21
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# DTO 与响应体(Model 体系)
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 本文件是 [编码规约](../SKILL.md) 的按需加载部分(写/改 DTO 时读本文件)。导入来源、事务与异常等硬红线见 [SKILL.md 「硬红线」章](../SKILL.md#硬红线跨文件通用);目录归属见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)。
|
|
4
4
|
|
|
5
5
|
## 前置条件
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
## 命名与放置
|
|
10
10
|
|
|
11
|
-
- 请求 DTO 放 `<module>/dto/requests/`,响应 DTO 放 `<module>/dto/responses
|
|
11
|
+
- 请求 DTO 放 `<module>/dto/requests/`,响应 DTO 放 `<module>/dto/responses/`;跨模块共享的纯契约放 `src/shared/<name>/`(见 [SKILL.md 「跨模块共享层」章](../SKILL.md#跨模块共享层)),feature 独占的 DTO 放该 feature 的 `dto/`(见 [SKILL.md 「features 子目录」](../SKILL.md#features-子目录))。目录规范见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)
|
|
12
12
|
- **一个 DTO 一个文件**,文件名与类名一一对应:`CreateStudentRequestDto` → `create-student-request.dto.ts`
|
|
13
13
|
- 请求命名:`CreateXxxRequestDto` / `UpdateXxxRequestDto` / `XxxFilterQueryDto`;响应命名:`{FunctionName}ResponseDto`(pack 一个 `ResponseBodyType(...)`),列表元素用 `XxxBriefDto` / `XxxDetailDto`
|
|
14
14
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 实体与关系(MikroORM 集成)
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 本文件是 [编码规约](../SKILL.md) 的按需加载部分(定义实体时读本文件)。导入来源、事务与异常等硬红线见 [SKILL.md 「硬红线」章](../SKILL.md#硬红线跨文件通用);目录归属见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)。
|
|
4
4
|
|
|
5
5
|
## 前置条件
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## 命名与放置
|
|
11
11
|
|
|
12
|
-
实体存放于 `src/modules/<module-name>/entities/<name>.entity.ts`(目录规范见 [SKILL.md
|
|
12
|
+
实体存放于 `src/modules/<module-name>/entities/<name>.entity.ts`(目录规范见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织))。一个实体一个文件,后缀 `.entity.ts`。若项目使用 `@nestjs/swagger` CLI 插件,`tsconfig.json` 的 `dtoFileNameSuffix` **不要包含 `.entity.ts`**——该插件对 MikroORM `Ref/Collection` 推断出的装饰器是错误的;实体的 Swagger 一律由 `Column.*`/`Cardinality.*` 自动生成。
|
|
13
13
|
|
|
14
14
|
## 速查表
|
|
15
15
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# list 查询三件套(过滤 / 排序 / 分页)
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 本文件是 [编码规约](../SKILL.md) 的按需加载部分(实现 list 查询时读本文件)。导入来源、事务与异常等硬红线见 [SKILL.md 「硬红线」章](../SKILL.md#硬红线跨文件通用);目录归属见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)。
|
|
4
4
|
|
|
5
5
|
## 命名与放置
|
|
6
6
|
|
|
7
|
-
过滤 DTO(`XxxFilterQueryDto`)、排序类(`XxxOrderQuery`)与分页参数类型随请求参数 DTO 存放于 `dto/requests/`(命名与拆分规则见 [references/dto.md](dto.md) 与 [SKILL.md
|
|
7
|
+
过滤 DTO(`XxxFilterQueryDto`)、排序类(`XxxOrderQuery`)与分页参数类型随请求参数 DTO 存放于 `dto/requests/`(命名与拆分规则见 [references/dto.md](dto.md) 与 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织))。
|
|
8
8
|
|
|
9
9
|
## 速查表
|
|
10
10
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Service 编写与事务约定(业务逻辑层)
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 本文件是 [编码规约](../SKILL.md) 的按需加载部分(写 service 时读本文件)。导入来源、事务与异常等硬红线见 [SKILL.md 「硬红线」章](../SKILL.md#硬红线跨文件通用);目录归属见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)。
|
|
4
4
|
|
|
5
5
|
## 前置条件
|
|
6
6
|
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
- `services/<module-name>.service.ts` 主 service
|
|
14
14
|
- `services/<subresource>.service.ts` 子 service
|
|
15
15
|
- `commands/<action>.command.ts`(只放 interface,不放 handler)
|
|
16
|
-
-
|
|
16
|
+
- 模块按能力分组时,该能力的 service/provider 放 `features/<feature-name>/`(见 [SKILL.md 「features 子目录」](../SKILL.md#features-子目录))
|
|
17
|
+
- 目录归属与命名约定见 [SKILL.md 「业务模块目录组织」章](../SKILL.md#业务模块目录组织)
|
|
17
18
|
|
|
18
19
|
## 速查表
|
|
19
20
|
|
|
@@ -70,7 +71,7 @@ export class CourseService {
|
|
|
70
71
|
| 可独立理解的复杂职责(通知发送、审计日志、外部同步) | 独立 provider(`@Injectable()`),由主 service 注入 |
|
|
71
72
|
| 公共方法逼近 10 个,且可按资源/职责切出明确主题组 | 按主题拆子 service |
|
|
72
73
|
| 单文件数百行仍持续膨胀 | 切分 |
|
|
73
|
-
| 多模块共需的能力(如权限判断、ID 生成) |
|
|
74
|
+
| 多模块共需的能力(如权限判断、ID 生成) | 下沉到 `src/shared/<name>/`(见 [SKILL.md 「跨模块共享层」](../SKILL.md#跨模块共享层)) |
|
|
74
75
|
|
|
75
76
|
### 不拆信号(保持单 service)
|
|
76
77
|
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-crypto
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
KeyEnvelope/EncryptedPayload)、为加密字段建立等值查询(BlindIndexModule 盲索引)、
|
|
6
|
-
存储密码或凭证(SaltedHashModule 加盐哈希)、在实体中嵌入加密列(Column.Embedded)、
|
|
7
|
-
或用 reencrypt 执行密钥轮换时使用本 skill。
|
|
8
|
-
信封加密依赖 OpenBao transit 前置注册,见 nestjs-kit-open-bao;实体与嵌入列声明见 nestjs-kit-coding-standards。
|
|
4
|
+
在 NestJS 代码里加密存储敏感字段、给加密字段建等值查询、存密码或凭证、执行密钥轮换时加载本 skill。覆盖 EnvelopeEncryptionModule(信封加密、KeyEnvelope/EncryptedPayload)、BlindIndexModule(盲索引)、SaltedHashModule(加盐哈希)、实体中嵌入加密列(Column.Embedded)与 reencrypt。
|
|
9
5
|
---
|
|
10
6
|
|
|
11
7
|
# 加密三件套(信封加密 / 盲索引 / 加盐哈希)
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-exception
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
用 9 个异常工厂抛出业务异常(BusinessException/SystemException/ThirdPartyException/AuthException/
|
|
6
|
-
ValidationException/ConflictException/FeatureException/RateLimitException/DegradeException)、
|
|
7
|
-
构造统一错误响应 {error:{code,message,details}}、编写 ExceptionDetail 结构化详情、
|
|
8
|
-
为二方包认领 moduleId、或查询 GET /error-codes 错误码时使用本 skill。
|
|
9
|
-
写业务代码时选择抛哪种异常、异常声明文件的位置同时涉及 nestjs-kit-coding-standards;
|
|
10
|
-
上游服务响应转异常涉及 nestjs-kit-http。
|
|
4
|
+
在 NestJS 代码里抛出业务异常、声明或修改模块错误码时加载本 skill。覆盖 @ModuleExceptions 错误码声明、9 个异常工厂(Business/System/ThirdParty/Auth/Validation/Conflict/Feature/RateLimit/Degrade)、统一错误响应 {error:{code,message,details}}、ExceptionDetail 结构化详情、二方包认领 moduleId 与 GET /error-codes 查询。
|
|
11
5
|
---
|
|
12
6
|
|
|
13
7
|
# 异常与错误码(ExceptionModule)
|
|
@@ -28,6 +22,7 @@ description: >
|
|
|
28
22
|
| 参数校验错误 | `ValidationException`(400) |
|
|
29
23
|
| 限流/降级 | `RateLimitException`(429) / `DegradeException`(503) |
|
|
30
24
|
| 直接抛内置常用异常 | `BukaExceptions.NotFound` 等 |
|
|
25
|
+
| 客户端门禁(版本过低 / 维护 / 付费资格 / 方案上限) | `ClientGateExceptions.*` |
|
|
31
26
|
| 结构化错误详情 | `ExceptionDetail`(details 参数) |
|
|
32
27
|
|
|
33
28
|
## 文件位置
|
|
@@ -117,6 +112,18 @@ throw new BukaExceptions.BadRequest('自定义消息')
|
|
|
117
112
|
|
|
118
113
|
完整列表:`BadRequest`(400)、`Unauthorized`(401)、`Forbidden`(403)、`NotFound`(404)、`Conflict`(409)、`UnsupportedMediaType`(415)、`TooManyRequests`(429)、`InternalServerError`(500)、`BadGateway`(502)、`GatewayTimeout`(504)、`ServiceUnavailable`(503)。
|
|
119
114
|
|
|
115
|
+
`ClientGateExceptions`(moduleId: 1)是**客户端门禁**:用户此刻做不了某件事、必须完成一个固定动作才能恢复。它占用公共号段,任何系统都可抛出:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
throw new ClientGateExceptions.PlanLimitReached('成员', 15, 10, [
|
|
119
|
+
{ type: 'client_gate', resource: 'members', current: 15, max: 10 },
|
|
120
|
+
])
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
列表:`ClientVersionRejected`(1 版本过低)、`EntitlementRequired`(2 需要付费资格)、`EntitlementExpired`(3 付费资格失效)、`Maintenance`(4 维护中)、`CapabilityUnavailable`(5 功能未开放)、`PlanLimitReached`(6 已达方案上限);全部是 `FEATURE`/403。
|
|
124
|
+
|
|
125
|
+
参数走 `details`(最后一个参数传详情数组),详情以 `type: 'client_gate'` 标识。
|
|
126
|
+
|
|
120
127
|
> 内置 NestJS HttpException 也能被 filter 隐式映射,但业务代码统一走 `@ModuleExceptions` 工厂——错误码可检索、类别可控,裸 `throw new HttpException`(NestJS 内建类)跳过了错误码体系。
|
|
121
128
|
|
|
122
129
|
## 结构化详情(ExceptionDetail)
|
|
@@ -160,7 +167,7 @@ export class PaymentExceptions {
|
|
|
160
167
|
|
|
161
168
|
## 注意事项
|
|
162
169
|
|
|
163
|
-
- 必须先登记再使用:`sequenceId` 模块内自增管理;`moduleId` 业务模块在 1000-FZZZ 范围内声明(跨服务复用的模块保持一致)
|
|
170
|
+
- 必须先登记再使用:`sequenceId` 模块内自增管理;`moduleId` 业务模块在 1000-FZZZ 范围内声明(跨服务复用的模块保持一致);`moduleId: 0`(内置 HTTP 语义)与 `1`(客户端门禁)由 kit 占用,不要认领
|
|
164
171
|
- `description` 必写——它是 `GET /error-codes` 错误码查询接口的排障材料
|
|
165
172
|
- 内部错误使用 `SystemException`;第三方调用失败使用 `ThirdPartyException`(保留上游原始错误码,不包装成 SYSTEM,以正确告警归类)
|
|
166
173
|
- 找不到资源用 `BusinessException`/`BukaExceptions.NotFound`
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-http
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
把上游 4xx/5xx 响应自动转成带错误码的异常(throwOnResponseError 中间件)、
|
|
6
|
-
按上游错误码分发不同异常类(errorDispatchers)、捕获并处理 BukaRequestException(code/errorCode/details/fatal)、
|
|
7
|
-
或实现尊重 fatal 标记的重试策略时使用本 skill。
|
|
8
|
-
本服务作为被调用方的错误响应结构见 nestjs-kit-exception;本 skill 只覆盖调用方一侧。
|
|
4
|
+
在 NestJS 代码里调用下游或第三方服务、处理 keq 请求错误时加载本 skill。覆盖 throwOnResponseError 中间件(上游 4xx/5xx 自动转成带错误码的异常)、errorDispatchers 按上游错误码分发异常类、捕获并处理 BukaRequestException(code/errorCode/details/fatal)、尊重 fatal 标记的重试策略。
|
|
9
5
|
---
|
|
10
6
|
|
|
11
7
|
# 服务间调用(keq 错误处理)
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-logger
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
配置 LoggerModule(serviceName/level/pretty)、用 LoggerConfig 读取 LOG_* 环境变量、
|
|
6
|
-
理解 pino JSON 结构化日志(含 HTTP 请求自动日志、reqId 链路、Loki 检索字段)时使用本 skill。
|
|
7
|
-
写任何带日志的业务代码(含 catch 中记日志后 rethrow 的写法)时都应参考本 skill 的注入与脱敏约定。
|
|
4
|
+
在 NestJS 代码里注入 logger、写日志、配置日志模块时加载本 skill。覆盖 @InjectLogger(以类名为上下文)、LoggerModule 注册(serviceName/level/pretty)、LoggerConfig 读取 LOG_* 环境变量、pino JSON 结构化日志(HTTP 请求日志、reqId 链路、Loki 检索字段)、脱敏约定。
|
|
8
5
|
---
|
|
9
6
|
|
|
10
7
|
# 统一日志(LoggerModule)
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-object-storage
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
生成预签名下载 URL(getSignedUrl)、查询对象元数据/存在性或删除对象、
|
|
6
|
-
配置 ObjectStorageModule(endpoint/bucket/prefix/forcePathStyle)、
|
|
7
|
-
或用 ObjectStorageModuleConfig 从环境变量读取配置时使用本 skill。
|
|
8
|
-
涉及 Readable 流的消费与销毁、prefix 两级语义、MinIO 联调 400 等对象存储问题也应使用。
|
|
4
|
+
在 NestJS 代码里上传或下载文件、生成预签名 URL、查询元数据与存在性、删除对象、配置对象存储时加载本 skill。覆盖 S3 与 MinIO、getSignedUrl、ObjectStorageModule(endpoint/bucket/prefix/forcePathStyle)、ObjectStorageModuleConfig 读环境变量、Readable 流的消费与销毁、prefix 两级语义。
|
|
9
5
|
---
|
|
10
6
|
|
|
11
7
|
# 对象存储(ObjectStorageModule)
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nestjs-kit-open-bao
|
|
3
3
|
description: >
|
|
4
|
-
|
|
5
|
-
(OpenBaoHttpClient 的 kvReadDataPath/kvWriteDataPath)、配置认证方式(token/userpass/approle/kubernetes)、
|
|
6
|
-
管理 transit 加密密钥、使用 OpenBaoTokenManager 获取或续期 token、
|
|
7
|
-
或给自建 keq 客户端透传 token(setOpenBaoToken)时使用本 skill。
|
|
8
|
-
信封加密依赖 OpenBao transit,相关场景同时参考 nestjs-kit-crypto。
|
|
4
|
+
在项目里读写 KV 秘密、配置 OpenBao/Vault 认证、管理 transit 密钥、获取或续期 token 时加载本 skill。覆盖 OpenBaoHttpClient 的 kvReadDataPath/kvWriteDataPath、token/userpass/approle/kubernetes 认证、OpenBaoTokenManager、setOpenBaoToken 给自建 keq 客户端透传 token。
|
|
9
5
|
---
|
|
10
6
|
|
|
11
7
|
# OpenBao / Vault 集成(OpenBaoModule)
|