@biggora/claude-plugins 1.2.2 → 1.3.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/README.md +2 -0
- package/package.json +1 -1
- package/registry/registry.json +15 -0
- package/specs/coding.md +6 -0
- package/src/commands/skills/add.js +63 -7
- package/src/commands/skills/list.js +23 -52
- package/src/commands/skills/remove.js +26 -27
- package/src/commands/skills/resolve.js +155 -0
- package/src/commands/skills/update.js +58 -74
- package/src/skills/nest-best-practices/SKILL.md +251 -0
- package/src/skills/nest-best-practices/references/best-practices-request-lifecycle.md +158 -0
- package/src/skills/nest-best-practices/references/cli-monorepo.md +106 -0
- package/src/skills/nest-best-practices/references/cli-overview.md +157 -0
- package/src/skills/nest-best-practices/references/core-controllers.md +165 -0
- package/src/skills/nest-best-practices/references/core-dependency-injection.md +179 -0
- package/src/skills/nest-best-practices/references/core-middleware.md +139 -0
- package/src/skills/nest-best-practices/references/core-modules.md +138 -0
- package/src/skills/nest-best-practices/references/core-providers.md +188 -0
- package/src/skills/nest-best-practices/references/faq-raw-body-hybrid.md +122 -0
- package/src/skills/nest-best-practices/references/fundamentals-circular-dependency.md +89 -0
- package/src/skills/nest-best-practices/references/fundamentals-custom-decorators.md +107 -0
- package/src/skills/nest-best-practices/references/fundamentals-dynamic-modules.md +125 -0
- package/src/skills/nest-best-practices/references/fundamentals-exception-filters.md +202 -0
- package/src/skills/nest-best-practices/references/fundamentals-execution-context.md +107 -0
- package/src/skills/nest-best-practices/references/fundamentals-guards.md +136 -0
- package/src/skills/nest-best-practices/references/fundamentals-interceptors.md +187 -0
- package/src/skills/nest-best-practices/references/fundamentals-lazy-loading.md +89 -0
- package/src/skills/nest-best-practices/references/fundamentals-lifecycle-events.md +87 -0
- package/src/skills/nest-best-practices/references/fundamentals-module-reference.md +107 -0
- package/src/skills/nest-best-practices/references/fundamentals-pipes.md +197 -0
- package/src/skills/nest-best-practices/references/fundamentals-provider-scopes.md +92 -0
- package/src/skills/nest-best-practices/references/fundamentals-testing.md +142 -0
- package/src/skills/nest-best-practices/references/graphql-overview.md +233 -0
- package/src/skills/nest-best-practices/references/graphql-resolvers-mutations.md +199 -0
- package/src/skills/nest-best-practices/references/graphql-scalars-unions-enums.md +180 -0
- package/src/skills/nest-best-practices/references/graphql-subscriptions.md +228 -0
- package/src/skills/nest-best-practices/references/microservices-grpc.md +175 -0
- package/src/skills/nest-best-practices/references/microservices-overview.md +221 -0
- package/src/skills/nest-best-practices/references/microservices-transports.md +119 -0
- package/src/skills/nest-best-practices/references/openapi-swagger.md +207 -0
- package/src/skills/nest-best-practices/references/recipes-authentication.md +97 -0
- package/src/skills/nest-best-practices/references/recipes-cqrs.md +176 -0
- package/src/skills/nest-best-practices/references/recipes-crud-generator.md +87 -0
- package/src/skills/nest-best-practices/references/recipes-documentation.md +93 -0
- package/src/skills/nest-best-practices/references/recipes-mongoose.md +153 -0
- package/src/skills/nest-best-practices/references/recipes-prisma.md +98 -0
- package/src/skills/nest-best-practices/references/recipes-terminus.md +148 -0
- package/src/skills/nest-best-practices/references/recipes-typeorm.md +122 -0
- package/src/skills/nest-best-practices/references/security-authorization.md +196 -0
- package/src/skills/nest-best-practices/references/security-cors-helmet-rate-limiting.md +204 -0
- package/src/skills/nest-best-practices/references/security-encryption-hashing.md +93 -0
- package/src/skills/nest-best-practices/references/techniques-caching.md +142 -0
- package/src/skills/nest-best-practices/references/techniques-compression-streaming-sse.md +194 -0
- package/src/skills/nest-best-practices/references/techniques-configuration.md +132 -0
- package/src/skills/nest-best-practices/references/techniques-database.md +153 -0
- package/src/skills/nest-best-practices/references/techniques-events.md +163 -0
- package/src/skills/nest-best-practices/references/techniques-fastify.md +137 -0
- package/src/skills/nest-best-practices/references/techniques-file-upload.md +140 -0
- package/src/skills/nest-best-practices/references/techniques-http-module.md +176 -0
- package/src/skills/nest-best-practices/references/techniques-logging.md +146 -0
- package/src/skills/nest-best-practices/references/techniques-mvc-serve-static.md +132 -0
- package/src/skills/nest-best-practices/references/techniques-queues.md +162 -0
- package/src/skills/nest-best-practices/references/techniques-serialization.md +158 -0
- package/src/skills/nest-best-practices/references/techniques-sessions-cookies.md +167 -0
- package/src/skills/nest-best-practices/references/techniques-task-scheduling.md +166 -0
- package/src/skills/nest-best-practices/references/techniques-validation.md +126 -0
- package/src/skills/nest-best-practices/references/techniques-versioning.md +153 -0
- package/src/skills/nest-best-practices/references/websockets-advanced.md +96 -0
- package/src/skills/nest-best-practices/references/websockets-gateways.md +215 -0
- package/src/skills/typescript-expert/SKILL.md +145 -0
- package/src/skills/typescript-expert/commands/typescript-fix.md +65 -0
- package/src/skills/typescript-expert/references/advanced-conditional-types.md +190 -0
- package/src/skills/typescript-expert/references/advanced-decorators.md +243 -0
- package/src/skills/typescript-expert/references/advanced-mapped-types.md +223 -0
- package/src/skills/typescript-expert/references/advanced-template-literals.md +209 -0
- package/src/skills/typescript-expert/references/advanced-type-guards.md +308 -0
- package/src/skills/typescript-expert/references/best-practices-patterns.md +313 -0
- package/src/skills/typescript-expert/references/best-practices-performance.md +185 -0
- package/src/skills/typescript-expert/references/best-practices-tsconfig.md +242 -0
- package/src/skills/typescript-expert/references/core-generics.md +246 -0
- package/src/skills/typescript-expert/references/core-interfaces-types.md +231 -0
- package/src/skills/typescript-expert/references/core-type-system.md +261 -0
- package/src/skills/typescript-expert/references/core-utility-types.md +235 -0
- package/src/skills/typescript-expert/references/features-ts5x.md +370 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: nest-best-practices
|
|
3
|
+
description: NestJS framework best practices and production patterns. Use whenever working with NestJS — creating modules, controllers, services, DTOs, guards, interceptors, pipes, middleware, or building REST/GraphQL/microservice APIs. Also use when setting up authentication, authorization, validation, queues, health checks, WebSockets, caching, or any @nestjs/* package. Even for simple NestJS tasks, this skill ensures correct import paths, proper decorator usage, and production-ready patterns. Covers NestJS v11 with Express v5, native JWT auth, Zod validation, Keyv caching, and Suites testing.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
NestJS is a progressive Node.js framework for building efficient and scalable server-side applications. It uses TypeScript by default, supports both Express and Fastify, and provides an out-of-the-box application architecture inspired by Angular. NestJS combines elements of OOP, FP, and FRP, making it ideal for building enterprise-grade applications.
|
|
7
|
+
|
|
8
|
+
> Skill based on NestJS documentation, updated 2026-03-28. Covers NestJS v11 with Express v5.
|
|
9
|
+
|
|
10
|
+
## How to Use This Skill
|
|
11
|
+
|
|
12
|
+
When generating NestJS code, read the relevant reference files below for the specific topic. The references contain current API patterns, correct import paths, and production-ready examples.
|
|
13
|
+
|
|
14
|
+
**Always apply the production best practices below** — these are patterns that matter in production and are easy to miss without explicit guidance.
|
|
15
|
+
|
|
16
|
+
## NestJS v11 Breaking Changes
|
|
17
|
+
|
|
18
|
+
Be aware of these when generating code for NestJS v11+:
|
|
19
|
+
|
|
20
|
+
### Node.js v20+ Required
|
|
21
|
+
Node.js v16 and v18 are no longer supported. Always target Node.js v20+.
|
|
22
|
+
|
|
23
|
+
### Express v5 Route Matching
|
|
24
|
+
NestJS v11 uses Express v5 by default. Route patterns have changed:
|
|
25
|
+
|
|
26
|
+
| Express v4 (Old) | Express v5 (New) | Notes |
|
|
27
|
+
|---|---|---|
|
|
28
|
+
| `@Get('users/*')` | `@Get('users/*splat')` | Wildcards must be named |
|
|
29
|
+
| `forRoutes('*')` | `forRoutes('{*splat}')` | Braces make path optional (matches root) |
|
|
30
|
+
| `?` optional character | Not supported | Use braces `{}` instead |
|
|
31
|
+
| Regex in routes | Not supported | Regex characters no longer work |
|
|
32
|
+
|
|
33
|
+
### CacheModule Migration to Keyv
|
|
34
|
+
The `CacheModule` now uses Keyv adapters instead of `cache-manager` stores:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// OLD (pre-v11) — no longer works
|
|
38
|
+
CacheModule.register({ store: redisStore, host: 'localhost', port: 6379 });
|
|
39
|
+
|
|
40
|
+
// NEW (v11+) — uses @keyv/redis
|
|
41
|
+
import { KeyvRedis } from '@keyv/redis';
|
|
42
|
+
CacheModule.registerAsync({
|
|
43
|
+
useFactory: async () => ({
|
|
44
|
+
stores: [new KeyvRedis('redis://localhost:6379')],
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Production Best Practices
|
|
50
|
+
|
|
51
|
+
### Bootstrap & Application Setup
|
|
52
|
+
Configure `ValidationPipe` globally in `main.ts` — this is a security-critical step:
|
|
53
|
+
```typescript
|
|
54
|
+
app.useGlobalPipes(new ValidationPipe({
|
|
55
|
+
whitelist: true, // strips unknown properties
|
|
56
|
+
forbidNonWhitelisted: true, // rejects requests with unknown properties (400)
|
|
57
|
+
transform: true, // enables automatic type coercion for params
|
|
58
|
+
}));
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Entity & Schema Discipline
|
|
62
|
+
- Use explicit column lengths: `@Column({ length: 255 })`, not bare `@Column()`
|
|
63
|
+
- Name tables explicitly: `@Entity('books')` to avoid surprises with naming conventions
|
|
64
|
+
- Use `@CreateDateColumn()` and `@UpdateDateColumn()` for automatic timestamps
|
|
65
|
+
- Use specialized validators like `@IsISBN()`, `@IsEmail()`, `@IsUUID()` — not just `@IsString()`
|
|
66
|
+
- Align nullability between entity and DTO: if `@Column({ nullable: true })`, the DTO field should be `@IsOptional()`
|
|
67
|
+
|
|
68
|
+
### DTO Patterns
|
|
69
|
+
- Import `PartialType` from `@nestjs/swagger` (not `@nestjs/mapped-types`) when using Swagger — this preserves API documentation metadata on partial fields
|
|
70
|
+
- Zod validation is now an officially supported alternative to `class-validator` — use `ZodValidationPipe` with `z.infer<typeof schema>` for schema-first validation with type inference
|
|
71
|
+
|
|
72
|
+
### Guards & Auth
|
|
73
|
+
NestJS v11 documents two auth approaches:
|
|
74
|
+
1. **Native JWT** (recommended for simpler cases): Use `@nestjs/jwt` directly with a custom `CanActivate` guard that calls `JwtService.verifyAsync()`. No Passport dependency needed.
|
|
75
|
+
2. **Passport-based** (for complex strategies like OAuth2, SAML): Use `@nestjs/passport` with `PassportStrategy`. Now documented under "recipes" rather than primary auth docs.
|
|
76
|
+
|
|
77
|
+
Regardless of approach:
|
|
78
|
+
- Guard ordering matters: `@UseGuards(JwtAuthGuard, RolesGuard)` — JWT guard must run first to populate `request.user` before RolesGuard reads it
|
|
79
|
+
- Use `Reflector.getAllAndOverride()` (not just `.get()`) so roles can be set at both handler and class level with handler taking precedence
|
|
80
|
+
- RolesGuard must throw `ForbiddenException` with a descriptive message when the user lacks the required role — do NOT just return `false` from `canActivate()`. The generic 403 provides no diagnostic value. Include context: `throw new ForbiddenException(\`Requires roles: \${requiredRoles.join(', ')}\`)`
|
|
81
|
+
- Never hardcode JWT secrets — use `JwtModule.registerAsync()` with `ConfigService`
|
|
82
|
+
- Define a `Role` enum rather than raw strings for type safety
|
|
83
|
+
- In `JwtStrategy.validate()` (or native guard), validate the presence of required payload fields (`sub`, `username`, `roles`) before returning the user object. Define a `JwtPayload` interface for type safety.
|
|
84
|
+
|
|
85
|
+
### Error Messages
|
|
86
|
+
All thrown exceptions (`ForbiddenException`, `UnauthorizedException`, `NotFoundException`, etc.) should include descriptive messages that identify what was expected, what was found, and what action the consumer should take. Do not rely on framework default messages in production code.
|
|
87
|
+
|
|
88
|
+
### Microservices & Queues
|
|
89
|
+
- For hybrid apps (HTTP + microservice), use `NestFactory.create()` + `app.connectMicroservice()` pattern
|
|
90
|
+
- Always call `app.enableShutdownHooks()` when using Terminus health checks for graceful shutdown
|
|
91
|
+
- Use `@nestjs/bullmq` (not `@nestjs/bull`) — it wraps the newer `bullmq` library. Config uses `connection` key (not `redis`):
|
|
92
|
+
```typescript
|
|
93
|
+
BullModule.forRoot({ connection: { host: 'localhost', port: 6379 } })
|
|
94
|
+
```
|
|
95
|
+
- Use `BullModule.forRootAsync()` with `ConfigService` injection for production config
|
|
96
|
+
- BullMQ consumers extend `WorkerHost` and implement `process()` — do NOT use the `@Process()` decorator (that's the older `@nestjs/bull` API)
|
|
97
|
+
- Register `@OnWorkerEvent('completed')` and `@OnWorkerEvent('failed')` lifecycle hooks for observability
|
|
98
|
+
- Set `removeOnComplete: true` and configure retry with `backoff: { type: 'exponential' }` on jobs
|
|
99
|
+
- Capture and log `job.id` from `queue.add()` return value for traceability
|
|
100
|
+
- Use `job.updateProgress()` for long-running jobs to enable monitoring dashboards
|
|
101
|
+
- Define and export TypeScript interfaces for all event payloads (e.g., `OrderCreatedEvent`, `UserRegisteredEvent`) for type safety across service boundaries
|
|
102
|
+
|
|
103
|
+
### Health Checks
|
|
104
|
+
- Don't just check one thing — include multiple indicators: service connectivity (Redis/DB), memory (heap + RSS), and disk usage
|
|
105
|
+
- Use `MicroserviceHealthIndicator` for transport checks, `MemoryHealthIndicator` for heap/RSS, `DiskHealthIndicator` for storage
|
|
106
|
+
- Configure graceful shutdown timeout: `TerminusModule.forRoot({ gracefulShutdownTimeoutMs: 1000 })`
|
|
107
|
+
|
|
108
|
+
### OpenAPI / Swagger
|
|
109
|
+
The `@nestjs/swagger` CLI plugin can eliminate most manual `@ApiProperty()` annotations. Add to `nest-cli.json`:
|
|
110
|
+
```json
|
|
111
|
+
{
|
|
112
|
+
"compilerOptions": {
|
|
113
|
+
"plugins": [{
|
|
114
|
+
"name": "@nestjs/swagger",
|
|
115
|
+
"options": { "classValidatorShim": true, "introspectComments": true }
|
|
116
|
+
}]
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
With the plugin enabled, TypeScript types, default values, optional markers, and JSDoc comments are automatically inferred — you only need explicit `@ApiProperty()` for edge cases.
|
|
121
|
+
|
|
122
|
+
### Testing
|
|
123
|
+
- `@suites/unit` is now a recommended testing library for NestJS:
|
|
124
|
+
- `TestBed.solitary(Service).compile()` — all dependencies auto-mocked
|
|
125
|
+
- `TestBed.sociable(Service).expose(RealDep).compile()` — selected real deps
|
|
126
|
+
- `Mocked<T>` type for full IntelliSense on mock methods
|
|
127
|
+
- Supports Jest, Vitest, and Sinon
|
|
128
|
+
- For e2e testing, use `@nestjs/testing` with `Test.createTestingModule()` and `supertest`
|
|
129
|
+
|
|
130
|
+
### Custom Decorators
|
|
131
|
+
- For NestJS 10+, prefer `Reflector.createDecorator<Role[]>()` over `SetMetadata` for custom decorators — it provides better type inference and eliminates the need for a separate metadata key constant
|
|
132
|
+
- The `SetMetadata` pattern still works and is fine for simple cases
|
|
133
|
+
|
|
134
|
+
## CLI
|
|
135
|
+
|
|
136
|
+
| Topic | Description | Reference |
|
|
137
|
+
|-------|-------------|-----------|
|
|
138
|
+
| CLI Overview | Scaffolding, building, and running applications | [cli-overview](references/cli-overview.md) |
|
|
139
|
+
| Monorepo & Libraries | Workspaces, apps, shared libraries | [cli-monorepo](references/cli-monorepo.md) |
|
|
140
|
+
|
|
141
|
+
## Core References
|
|
142
|
+
|
|
143
|
+
| Topic | Description | Reference |
|
|
144
|
+
|-------|-------------|-----------|
|
|
145
|
+
| Controllers | Route handlers, HTTP methods, request/response handling | [core-controllers](references/core-controllers.md) |
|
|
146
|
+
| Modules | Application structure, feature modules, shared modules, dynamic modules | [core-modules](references/core-modules.md) |
|
|
147
|
+
| Providers | Services, dependency injection, custom providers | [core-providers](references/core-providers.md) |
|
|
148
|
+
| Dependency Injection | DI fundamentals, custom providers, scopes | [core-dependency-injection](references/core-dependency-injection.md) |
|
|
149
|
+
| Middleware | Request/response middleware, functional middleware | [core-middleware](references/core-middleware.md) |
|
|
150
|
+
|
|
151
|
+
## Fundamentals
|
|
152
|
+
|
|
153
|
+
| Topic | Description | Reference |
|
|
154
|
+
|-------|-------------|-----------|
|
|
155
|
+
| Pipes | Data transformation and validation pipes | [fundamentals-pipes](references/fundamentals-pipes.md) |
|
|
156
|
+
| Guards | Authorization guards, role-based access control | [fundamentals-guards](references/fundamentals-guards.md) |
|
|
157
|
+
| Interceptors | Aspect-oriented programming, response transformation | [fundamentals-interceptors](references/fundamentals-interceptors.md) |
|
|
158
|
+
| Exception Filters | Error handling, custom exception filters | [fundamentals-exception-filters](references/fundamentals-exception-filters.md) |
|
|
159
|
+
| Custom Decorators | Creating custom parameter decorators | [fundamentals-custom-decorators](references/fundamentals-custom-decorators.md) |
|
|
160
|
+
| Dynamic Modules | Configurable modules, module configuration | [fundamentals-dynamic-modules](references/fundamentals-dynamic-modules.md) |
|
|
161
|
+
| Execution Context | Accessing request context, metadata reflection | [fundamentals-execution-context](references/fundamentals-execution-context.md) |
|
|
162
|
+
| Provider Scopes | Singleton, request-scoped, transient providers | [fundamentals-provider-scopes](references/fundamentals-provider-scopes.md) |
|
|
163
|
+
| Lifecycle Events | Application and provider lifecycle hooks | [fundamentals-lifecycle-events](references/fundamentals-lifecycle-events.md) |
|
|
164
|
+
| Lazy Loading | Loading modules on-demand for serverless | [fundamentals-lazy-loading](references/fundamentals-lazy-loading.md) |
|
|
165
|
+
| Circular Dependency | Resolving circular dependencies with forwardRef | [fundamentals-circular-dependency](references/fundamentals-circular-dependency.md) |
|
|
166
|
+
| Module Reference | Accessing providers dynamically with ModuleRef | [fundamentals-module-reference](references/fundamentals-module-reference.md) |
|
|
167
|
+
| Testing | Unit testing and e2e testing with @nestjs/testing | [fundamentals-testing](references/fundamentals-testing.md) |
|
|
168
|
+
|
|
169
|
+
## Techniques
|
|
170
|
+
|
|
171
|
+
| Topic | Description | Reference |
|
|
172
|
+
|-------|-------------|-----------|
|
|
173
|
+
| Validation | ValidationPipe, class-validator, Zod validation | [techniques-validation](references/techniques-validation.md) |
|
|
174
|
+
| Configuration | Environment variables, ConfigModule, configuration management | [techniques-configuration](references/techniques-configuration.md) |
|
|
175
|
+
| Database | TypeORM, Prisma, MongoDB integration | [techniques-database](references/techniques-database.md) |
|
|
176
|
+
| Caching | Keyv-based cache manager, Redis integration | [techniques-caching](references/techniques-caching.md) |
|
|
177
|
+
| Logging | Built-in logger, custom loggers, JSON logging | [techniques-logging](references/techniques-logging.md) |
|
|
178
|
+
| File Upload | File upload handling with multer, validation | [techniques-file-upload](references/techniques-file-upload.md) |
|
|
179
|
+
| Versioning | URI, header, and media type API versioning | [techniques-versioning](references/techniques-versioning.md) |
|
|
180
|
+
| Serialization | Response serialization with class-transformer | [techniques-serialization](references/techniques-serialization.md) |
|
|
181
|
+
| Queues | Background job processing with BullMQ | [techniques-queues](references/techniques-queues.md) |
|
|
182
|
+
| Task Scheduling | Cron jobs, intervals, and timeouts | [techniques-task-scheduling](references/techniques-task-scheduling.md) |
|
|
183
|
+
| Events | Event-driven architecture with EventEmitter | [techniques-events](references/techniques-events.md) |
|
|
184
|
+
| HTTP Module | Making HTTP requests with Axios | [techniques-http-module](references/techniques-http-module.md) |
|
|
185
|
+
| Fastify | Using Fastify for better performance | [techniques-fastify](references/techniques-fastify.md) |
|
|
186
|
+
| Sessions & Cookies | HTTP sessions and cookies for stateful apps | [techniques-sessions-cookies](references/techniques-sessions-cookies.md) |
|
|
187
|
+
| Streaming & SSE | Compression, file streaming, Server-Sent Events | [techniques-compression-streaming-sse](references/techniques-compression-streaming-sse.md) |
|
|
188
|
+
| MVC & Serve Static | Template rendering (Handlebars) and SPA static serving | [techniques-mvc-serve-static](references/techniques-mvc-serve-static.md) |
|
|
189
|
+
|
|
190
|
+
## Security
|
|
191
|
+
|
|
192
|
+
| Topic | Description | Reference |
|
|
193
|
+
|-------|-------------|-----------|
|
|
194
|
+
| Authentication | Native JWT auth and Passport integration | [recipes-authentication](references/recipes-authentication.md) |
|
|
195
|
+
| Authorization | RBAC, claims-based, CASL integration | [security-authorization](references/security-authorization.md) |
|
|
196
|
+
| CORS & Rate Limiting | CORS, Helmet, ThrottlerModule | [security-cors-helmet-rate-limiting](references/security-cors-helmet-rate-limiting.md) |
|
|
197
|
+
| Encryption & Hashing | bcrypt, argon2, password hashing | [security-encryption-hashing](references/security-encryption-hashing.md) |
|
|
198
|
+
|
|
199
|
+
## OpenAPI
|
|
200
|
+
|
|
201
|
+
| Topic | Description | Reference |
|
|
202
|
+
|-------|-------------|-----------|
|
|
203
|
+
| Swagger | OpenAPI documentation generation, CLI plugin | [openapi-swagger](references/openapi-swagger.md) |
|
|
204
|
+
|
|
205
|
+
## WebSockets
|
|
206
|
+
|
|
207
|
+
| Topic | Description | Reference |
|
|
208
|
+
|-------|-------------|-----------|
|
|
209
|
+
| Gateways | Real-time communication with Socket.IO/ws | [websockets-gateways](references/websockets-gateways.md) |
|
|
210
|
+
| Guards & Exception Filters | WsException, BaseWsExceptionFilter, interceptors, pipes | [websockets-advanced](references/websockets-advanced.md) |
|
|
211
|
+
|
|
212
|
+
## Microservices
|
|
213
|
+
|
|
214
|
+
| Topic | Description | Reference |
|
|
215
|
+
|-------|-------------|-----------|
|
|
216
|
+
| Overview | Transport layers, message patterns, events | [microservices-overview](references/microservices-overview.md) |
|
|
217
|
+
| gRPC | Protocol Buffers, streaming, metadata, reflection | [microservices-grpc](references/microservices-grpc.md) |
|
|
218
|
+
| Transports | Redis, Kafka, NATS, RabbitMQ configuration | [microservices-transports](references/microservices-transports.md) |
|
|
219
|
+
|
|
220
|
+
## GraphQL
|
|
221
|
+
|
|
222
|
+
| Topic | Description | Reference |
|
|
223
|
+
|-------|-------------|-----------|
|
|
224
|
+
| Overview | Code-first and schema-first approaches | [graphql-overview](references/graphql-overview.md) |
|
|
225
|
+
| Resolvers & Mutations | Queries, mutations, field resolvers | [graphql-resolvers-mutations](references/graphql-resolvers-mutations.md) |
|
|
226
|
+
| Subscriptions | Real-time subscriptions with PubSub | [graphql-subscriptions](references/graphql-subscriptions.md) |
|
|
227
|
+
| Scalars, Unions & Enums | Interfaces, scalars, union types, enums | [graphql-scalars-unions-enums](references/graphql-scalars-unions-enums.md) |
|
|
228
|
+
|
|
229
|
+
## Recipes
|
|
230
|
+
|
|
231
|
+
| Topic | Description | Reference |
|
|
232
|
+
|-------|-------------|-----------|
|
|
233
|
+
| CRUD Generator | Nest CLI resource generator | [recipes-crud-generator](references/recipes-crud-generator.md) |
|
|
234
|
+
| Documentation | OpenAPI/Swagger integration | [recipes-documentation](references/recipes-documentation.md) |
|
|
235
|
+
| TypeORM | TypeORM integration and usage | [recipes-typeorm](references/recipes-typeorm.md) |
|
|
236
|
+
| Prisma | Prisma ORM integration | [recipes-prisma](references/recipes-prisma.md) |
|
|
237
|
+
| Mongoose | MongoDB with Mongoose ODM | [recipes-mongoose](references/recipes-mongoose.md) |
|
|
238
|
+
| CQRS | Command Query Responsibility Segregation | [recipes-cqrs](references/recipes-cqrs.md) |
|
|
239
|
+
| Terminus | Health checks and readiness/liveness probes | [recipes-terminus](references/recipes-terminus.md) |
|
|
240
|
+
|
|
241
|
+
## FAQ
|
|
242
|
+
|
|
243
|
+
| Topic | Description | Reference |
|
|
244
|
+
|-------|-------------|-----------|
|
|
245
|
+
| Raw Body & Hybrid | Webhook signature verification, HTTP + microservices | [faq-raw-body-hybrid](references/faq-raw-body-hybrid.md) |
|
|
246
|
+
|
|
247
|
+
## Best Practices
|
|
248
|
+
|
|
249
|
+
| Topic | Description | Reference |
|
|
250
|
+
|-------|-------------|-----------|
|
|
251
|
+
| Request Lifecycle | Understanding execution order and flow | [best-practices-request-lifecycle](references/best-practices-request-lifecycle.md) |
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: request-lifecycle
|
|
3
|
+
description: Understanding the request processing flow in NestJS
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Request Lifecycle
|
|
7
|
+
|
|
8
|
+
Understanding execution order helps debug and optimize NestJS applications.
|
|
9
|
+
|
|
10
|
+
## Lifecycle Flow
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
1. Incoming Request
|
|
14
|
+
↓
|
|
15
|
+
2. Middleware
|
|
16
|
+
├── 2.1 Global middleware
|
|
17
|
+
└── 2.2 Module middleware
|
|
18
|
+
↓
|
|
19
|
+
3. Guards
|
|
20
|
+
├── 3.1 Global guards
|
|
21
|
+
├── 3.2 Controller guards
|
|
22
|
+
└── 3.3 Route guards
|
|
23
|
+
↓
|
|
24
|
+
4. Interceptors (pre-controller)
|
|
25
|
+
├── 4.1 Global interceptors
|
|
26
|
+
├── 4.2 Controller interceptors
|
|
27
|
+
└── 4.3 Route interceptors
|
|
28
|
+
↓
|
|
29
|
+
5. Pipes
|
|
30
|
+
├── 5.1 Global pipes
|
|
31
|
+
├── 5.2 Controller pipes
|
|
32
|
+
├── 5.3 Route pipes
|
|
33
|
+
└── 5.4 Route parameter pipes
|
|
34
|
+
↓
|
|
35
|
+
6. Controller (method handler)
|
|
36
|
+
↓
|
|
37
|
+
7. Service
|
|
38
|
+
↓
|
|
39
|
+
8. Interceptors (post-request)
|
|
40
|
+
├── 8.1 Route interceptors
|
|
41
|
+
├── 8.2 Controller interceptors
|
|
42
|
+
└── 8.3 Global interceptors
|
|
43
|
+
↓
|
|
44
|
+
9. Exception Filters (on error)
|
|
45
|
+
├── 9.1 Route filters
|
|
46
|
+
├── 9.2 Controller filters
|
|
47
|
+
└── 9.3 Global filters
|
|
48
|
+
↓
|
|
49
|
+
10. Server Response
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Middleware
|
|
53
|
+
|
|
54
|
+
- **Global first**: `app.use()` runs before module-bound middleware
|
|
55
|
+
- **Sequential**: Runs in order of binding
|
|
56
|
+
- **Module order**: Follows module import order in `AppModule`
|
|
57
|
+
|
|
58
|
+
## Guards
|
|
59
|
+
|
|
60
|
+
- Execute in order: Global → Controller → Route
|
|
61
|
+
- Within same level: Order of decorator parameters
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
@UseGuards(Guard1, Guard2)
|
|
65
|
+
@Controller('cats')
|
|
66
|
+
export class CatsController {
|
|
67
|
+
@UseGuards(Guard3)
|
|
68
|
+
@Get()
|
|
69
|
+
findAll() {}
|
|
70
|
+
}
|
|
71
|
+
// Execution: Guard1 → Guard2 → Guard3
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Interceptors
|
|
75
|
+
|
|
76
|
+
- **Pre-controller**: Global → Controller → Route
|
|
77
|
+
- **Post-request**: Route → Controller → Global (reverse order)
|
|
78
|
+
- Uses RxJS Observables (first in, last out)
|
|
79
|
+
|
|
80
|
+
## Pipes
|
|
81
|
+
|
|
82
|
+
- Execute: Global → Controller → Route → Parameters
|
|
83
|
+
- **Parameters**: Last parameter first
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
@UsePipes(GeneralPipe)
|
|
87
|
+
@Controller()
|
|
88
|
+
export class CatsController {
|
|
89
|
+
@UsePipes(RoutePipe)
|
|
90
|
+
@Patch(':id')
|
|
91
|
+
update(
|
|
92
|
+
@Body() body, // Processed third
|
|
93
|
+
@Param() params, // Processed second
|
|
94
|
+
@Query() query, // Processed first
|
|
95
|
+
) {}
|
|
96
|
+
}
|
|
97
|
+
// GeneralPipe runs on query → params → body
|
|
98
|
+
// Then RoutePipe on query → params → body
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Exception Filters
|
|
102
|
+
|
|
103
|
+
- **Unique behavior**: Lowest level first (opposite of others)
|
|
104
|
+
- Route → Controller → Global
|
|
105
|
+
- Only one filter handles the exception (no chaining)
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
@UseFilters(GlobalFilter)
|
|
109
|
+
@Controller()
|
|
110
|
+
@UseFilters(ControllerFilter)
|
|
111
|
+
export class CatsController {
|
|
112
|
+
@UseFilters(RouteFilter)
|
|
113
|
+
@Get()
|
|
114
|
+
findAll() {}
|
|
115
|
+
}
|
|
116
|
+
// If exception: RouteFilter catches first
|
|
117
|
+
// If no route filter: ControllerFilter catches
|
|
118
|
+
// If no controller filter: GlobalFilter catches
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Best Practices
|
|
122
|
+
|
|
123
|
+
### Middleware
|
|
124
|
+
- Use for logging, authentication setup
|
|
125
|
+
- Modify request/response objects
|
|
126
|
+
- Call `next()` to continue
|
|
127
|
+
|
|
128
|
+
### Guards
|
|
129
|
+
- Authorization and authentication checks
|
|
130
|
+
- Return `true`/`false` or throw exception
|
|
131
|
+
- Access `ExecutionContext`
|
|
132
|
+
|
|
133
|
+
### Interceptors
|
|
134
|
+
- Transform response data
|
|
135
|
+
- Add extra logic before/after handler
|
|
136
|
+
- Handle timeouts, caching
|
|
137
|
+
|
|
138
|
+
### Pipes
|
|
139
|
+
- Validate and transform input data
|
|
140
|
+
- Use `ValidationPipe` for DTOs
|
|
141
|
+
- Throw `BadRequestException` on failure
|
|
142
|
+
|
|
143
|
+
### Exception Filters
|
|
144
|
+
- Catch and handle errors
|
|
145
|
+
- Format error responses
|
|
146
|
+
- Log errors
|
|
147
|
+
|
|
148
|
+
## Debugging Tips
|
|
149
|
+
|
|
150
|
+
1. Add logging at each layer to trace flow
|
|
151
|
+
2. Check guard order if authorization fails
|
|
152
|
+
3. Verify pipe order for validation issues
|
|
153
|
+
4. Use `try/catch` carefully (bypasses filters)
|
|
154
|
+
|
|
155
|
+
<!--
|
|
156
|
+
Source references:
|
|
157
|
+
- https://docs.nestjs.com/faq/request-lifecycle
|
|
158
|
+
-->
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: cli-monorepo
|
|
3
|
+
description: NestJS monorepo mode, workspaces, and libraries
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Monorepo and Libraries
|
|
7
|
+
|
|
8
|
+
## Enable Monorepo
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
nest new my-project
|
|
12
|
+
cd my-project
|
|
13
|
+
nest generate app my-app
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This converts to monorepo mode: apps move under `apps/`, shared config at root.
|
|
17
|
+
|
|
18
|
+
## Structure
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
apps/
|
|
22
|
+
my-app/
|
|
23
|
+
src/
|
|
24
|
+
tsconfig.app.json
|
|
25
|
+
my-project/ # default project
|
|
26
|
+
src/
|
|
27
|
+
tsconfig.app.json
|
|
28
|
+
nest-cli.json
|
|
29
|
+
package.json
|
|
30
|
+
tsconfig.json
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Generate Library
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
nest generate library shared
|
|
37
|
+
# Or: nest g lib shared
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Creates `libs/shared/` with module, service, index. Use `--no-build` to skip build config.
|
|
41
|
+
|
|
42
|
+
## Library Structure
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
libs/
|
|
46
|
+
shared/
|
|
47
|
+
src/
|
|
48
|
+
shared.module.ts
|
|
49
|
+
shared.service.ts
|
|
50
|
+
index.ts
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Using Library
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { SharedModule } from '@shared';
|
|
57
|
+
|
|
58
|
+
@Module({
|
|
59
|
+
imports: [SharedModule],
|
|
60
|
+
})
|
|
61
|
+
export class AppModule {}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Path alias `@shared` is configured in `tsconfig.json` (paths) and `nest-cli.json`.
|
|
65
|
+
|
|
66
|
+
## Library Options
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
nest g lib shared --prefix=@app
|
|
70
|
+
# Creates @app/shared import path
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Default Project
|
|
74
|
+
|
|
75
|
+
In monorepo, `nest build` and `nest start` target the default project. Set in `nest-cli.json`:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"projects": {
|
|
80
|
+
"my-app": { "type": "application", ... },
|
|
81
|
+
"my-project": { "type": "application", "root": "apps/my-project", "entryFile": "main" }
|
|
82
|
+
},
|
|
83
|
+
"defaultProject": "my-project"
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Target Specific Project
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
nest build my-app
|
|
91
|
+
nest start my-app
|
|
92
|
+
nest start my-app --watch
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Key Points
|
|
96
|
+
|
|
97
|
+
- Monorepo: shared `node_modules`, config, build pipeline
|
|
98
|
+
- Library: reusable modules, no `main.ts`
|
|
99
|
+
- Path aliases auto-configured for libraries
|
|
100
|
+
- Default project used when none specified
|
|
101
|
+
|
|
102
|
+
<!--
|
|
103
|
+
Source references:
|
|
104
|
+
- https://docs.nestjs.com/cli/monorepo
|
|
105
|
+
- https://docs.nestjs.com/cli/libraries
|
|
106
|
+
-->
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: cli
|
|
3
|
+
description: NestJS CLI for scaffolding, building, and running applications
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Nest CLI
|
|
7
|
+
|
|
8
|
+
The Nest CLI helps initialize, develop, and maintain NestJS applications.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install -g @nestjs/cli
|
|
14
|
+
# Or use npx
|
|
15
|
+
npx @nestjs/cli@latest
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Common Commands
|
|
19
|
+
|
|
20
|
+
| Command | Alias | Description |
|
|
21
|
+
|---------|-------|-------------|
|
|
22
|
+
| `nest new` | `n` | Create new application |
|
|
23
|
+
| `nest generate` | `g` | Generate components |
|
|
24
|
+
| `nest build` | | Compile application |
|
|
25
|
+
| `nest start` | | Run application |
|
|
26
|
+
| `nest add` | | Add library |
|
|
27
|
+
| `nest info` | `i` | Display system info |
|
|
28
|
+
|
|
29
|
+
## Creating a New Project
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
nest new my-project
|
|
33
|
+
cd my-project
|
|
34
|
+
npm run start:dev
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Generate Components
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Generate a module
|
|
41
|
+
nest g module users
|
|
42
|
+
|
|
43
|
+
# Generate a controller
|
|
44
|
+
nest g controller users
|
|
45
|
+
|
|
46
|
+
# Generate a service
|
|
47
|
+
nest g service users
|
|
48
|
+
|
|
49
|
+
# Generate a complete resource (CRUD)
|
|
50
|
+
nest g resource users
|
|
51
|
+
|
|
52
|
+
# Generate with specific path
|
|
53
|
+
nest g controller users/admin
|
|
54
|
+
|
|
55
|
+
# Dry run (preview without creating)
|
|
56
|
+
nest g controller users --dry-run
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Generator Schematics
|
|
60
|
+
|
|
61
|
+
| Schematic | Alias | Description |
|
|
62
|
+
|-----------|-------|-------------|
|
|
63
|
+
| `module` | `mo` | Module |
|
|
64
|
+
| `controller` | `co` | Controller |
|
|
65
|
+
| `service` | `s` | Service |
|
|
66
|
+
| `provider` | `pr` | Provider |
|
|
67
|
+
| `pipe` | `pi` | Pipe |
|
|
68
|
+
| `guard` | `gu` | Guard |
|
|
69
|
+
| `interceptor` | `itc` | Interceptor |
|
|
70
|
+
| `filter` | `f` | Exception filter |
|
|
71
|
+
| `decorator` | `d` | Custom decorator |
|
|
72
|
+
| `gateway` | `ga` | WebSocket gateway |
|
|
73
|
+
| `middleware` | `mi` | Middleware |
|
|
74
|
+
| `resource` | `res` | CRUD resource |
|
|
75
|
+
| `class` | `cl` | Class |
|
|
76
|
+
| `interface` | `itf` | Interface |
|
|
77
|
+
|
|
78
|
+
## Build and Run
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# Development with watch mode
|
|
82
|
+
npm run start:dev
|
|
83
|
+
|
|
84
|
+
# Production build
|
|
85
|
+
npm run build
|
|
86
|
+
npm run start:prod
|
|
87
|
+
|
|
88
|
+
# Debug mode
|
|
89
|
+
npm run start:debug
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Project Structure
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
src/
|
|
96
|
+
├── app.controller.ts # Root controller
|
|
97
|
+
├── app.controller.spec.ts # Controller tests
|
|
98
|
+
├── app.module.ts # Root module
|
|
99
|
+
├── app.service.ts # Root service
|
|
100
|
+
└── main.ts # Entry point
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## nest-cli.json Configuration
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"collection": "@nestjs/schematics",
|
|
108
|
+
"sourceRoot": "src",
|
|
109
|
+
"compilerOptions": {
|
|
110
|
+
"deleteOutDir": true,
|
|
111
|
+
"webpack": false,
|
|
112
|
+
"tsConfigPath": "tsconfig.build.json"
|
|
113
|
+
},
|
|
114
|
+
"generateOptions": {
|
|
115
|
+
"spec": true
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## SWC Compiler (10x Faster)
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
npm install -D @swc/cli @swc/core
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```json
|
|
127
|
+
// nest-cli.json
|
|
128
|
+
{
|
|
129
|
+
"compilerOptions": {
|
|
130
|
+
"builder": "swc"
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Monorepo Mode
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
# Convert to monorepo
|
|
139
|
+
nest generate app secondary-app
|
|
140
|
+
|
|
141
|
+
# Generate library
|
|
142
|
+
nest generate library shared
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Key Points
|
|
146
|
+
|
|
147
|
+
- Use `--dry-run` or `-d` flag to preview changes
|
|
148
|
+
- Use `--flat` flag to skip creating a subdirectory
|
|
149
|
+
- Use `--no-spec` flag to skip test file generation
|
|
150
|
+
- Use SWC for faster builds in development
|
|
151
|
+
- Generators automatically update module imports
|
|
152
|
+
|
|
153
|
+
<!--
|
|
154
|
+
Source references:
|
|
155
|
+
- https://docs.nestjs.com/cli/overview
|
|
156
|
+
- https://docs.nestjs.com/cli/usages
|
|
157
|
+
-->
|