Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
470840e
chore: migrate from yarn to bun
KevinEdry Jan 17, 2026
e074993
feat(nestjs-trpc): add rust cli for schema generation
KevinEdry Jan 17, 2026
a493175
chore(nestjs-trpc): update ci, hooks, and docs for bun and rust cli
KevinEdry Jan 17, 2026
33023f7
feat(nestjs-trpc): add auto-generated file header to schema output
KevinEdry Jan 17, 2026
2b14292
feat(nestjs-trpc): decouple generation from runtime
KevinEdry Jan 20, 2026
672df02
refactor(nestjs-trpc): apply early return pattern to rust cli
KevinEdry Jan 20, 2026
f772e03
chore(nestjs-trpc): update fastify example for cli testing
KevinEdry Jan 20, 2026
ed28eb8
chore(nestjs-trpc): remove obsolete cli and config files
KevinEdry Jan 20, 2026
8ffd57e
docs: map existing codebase
KevinEdry Jan 20, 2026
ad1c76b
docs: initialize project
KevinEdry Jan 20, 2026
47a95ee
chore: add project config
KevinEdry Jan 20, 2026
4d0ea22
docs: complete project research
KevinEdry Jan 20, 2026
e359c9a
docs: define v1 requirements
KevinEdry Jan 20, 2026
de90380
docs: create roadmap (6 phases)
KevinEdry Jan 20, 2026
e391a0f
docs(phase-1): research foundation phase domain
KevinEdry Jan 20, 2026
8fd2ad3
docs(01-foundation): create phase plan
KevinEdry Jan 20, 2026
1ab953a
chore(01-02): add test dependencies and create fixture structure
KevinEdry Jan 20, 2026
1e69af0
test(01-02): add cli integration tests
KevinEdry Jan 20, 2026
b297f80
feat(01-01): add miette and CLI verbosity flags with proper exit codes
KevinEdry Jan 20, 2026
662a38f
docs(01-02): complete integration test infrastructure plan
KevinEdry Jan 20, 2026
1983772
feat(01-01): add diagnostic error module with miette source snippets
KevinEdry Jan 20, 2026
bcee7c8
feat(01-01): wire diagnostic error reporting into CLI
KevinEdry Jan 20, 2026
b0523d3
docs(01-01): complete error handling infrastructure plan
KevinEdry Jan 20, 2026
1fe642c
chore(01-03): add criterion benchmark configuration
KevinEdry Jan 20, 2026
448c537
chore(01-03): generate benchmark fixture files
KevinEdry Jan 20, 2026
16a665b
feat(01-03): add criterion benchmark suite
KevinEdry Jan 20, 2026
4de00a1
docs(01-03): complete benchmark infrastructure plan
KevinEdry Jan 20, 2026
61ba4ce
chore(01-foundation): update Cargo.lock after benchmark dependencies
KevinEdry Jan 20, 2026
6ab31fe
docs(01-foundation): complete Foundation phase
KevinEdry Jan 20, 2026
a11960f
docs(02): capture phase context
KevinEdry Jan 20, 2026
87e0a7e
docs(02): research code organization phase domain
KevinEdry Jan 20, 2026
f22234c
docs(02): create phase plan
KevinEdry Jan 20, 2026
f282fa5
docs(02): create phase plans
KevinEdry Jan 20, 2026
7ed5d12
refactor(02-03): split main.rs into focused cli modules
KevinEdry Jan 20, 2026
ba97cfb
docs(02-03): complete split main.rs plan
KevinEdry Jan 20, 2026
efb3995
refactor(02-02): split middleware.rs into focused submodules
KevinEdry Jan 20, 2026
442b97b
refactor(02-01): split flatten.rs into focused submodules
KevinEdry Jan 20, 2026
21a299a
docs(02-02): complete split middleware.rs plan
KevinEdry Jan 20, 2026
4b83017
docs(02-01): complete split flatten.rs plan
KevinEdry Jan 20, 2026
19c5e37
chore(02-04): add clippy lint thresholds for code quality
KevinEdry Jan 20, 2026
b6522a8
chore(02-04): add CI file size check and fix workflow path
KevinEdry Jan 20, 2026
2f26b70
docs(02-04): complete CI enforcement plan
KevinEdry Jan 20, 2026
808326d
docs(02): complete code organization phase
KevinEdry Jan 20, 2026
858f825
docs(03): capture phase context
KevinEdry Jan 20, 2026
f020604
docs(03): research watch mode implementation
KevinEdry Jan 20, 2026
840d1b2
docs(03): create phase plan
KevinEdry Jan 20, 2026
606c258
feat(03-01): add watch mode dependencies
KevinEdry Jan 20, 2026
5873c65
feat(03-01): create watcher module with path filtering
KevinEdry Jan 20, 2026
d296d72
docs(03-01): complete watch mode foundation plan
KevinEdry Jan 20, 2026
f164015
feat(03-02): create progress module with spinner and timestamp format…
KevinEdry Jan 20, 2026
ef93b47
feat(03-02): create event loop with file watching and shutdown handling
KevinEdry Jan 20, 2026
e43f118
docs(03-02): complete watch mode core mechanics plan
KevinEdry Jan 20, 2026
9938209
feat(03-03): add watch CLI subcommand
KevinEdry Jan 20, 2026
94f8423
feat(03-03): implement WatchSession::run() with full watch loop
KevinEdry Jan 20, 2026
52edf5a
chore(03-03): update Cargo.lock for watch mode dependencies
KevinEdry Jan 20, 2026
35705c9
fix(cli): improve watch mode UX with gray timestamps
KevinEdry Jan 20, 2026
87fa40d
docs(03-03): complete CLI integration plan
KevinEdry Jan 20, 2026
d5d77d1
docs(03): complete watch mode phase
KevinEdry Jan 20, 2026
55023e8
docs(04): capture phase context
KevinEdry Jan 20, 2026
16f2585
docs(04): research phase domain
KevinEdry Jan 20, 2026
1f4fa4b
docs(04): create phase plan
KevinEdry Jan 20, 2026
43c68d6
fix(04): revise plans based on checker feedback
KevinEdry Jan 20, 2026
2007af5
chore(04-01): upgrade tRPC dependencies to v11
KevinEdry Jan 20, 2026
12df12c
feat(04-02): add Insta snapshot testing infrastructure
KevinEdry Jan 20, 2026
0c3c46f
feat(04-02): add edge case test fixtures for snapshot tests
KevinEdry Jan 20, 2026
4cca8e0
test(04-02): add snapshot tests for all fixtures
KevinEdry Jan 20, 2026
2130ada
feat(04-01): migrate to tRPC v11 APIs
KevinEdry Jan 20, 2026
7bf427d
docs(04-02): complete snapshot testing plan
KevinEdry Jan 20, 2026
6eff084
test(04-01): update tests for tRPC v11 API changes
KevinEdry Jan 20, 2026
95770ff
docs(04-01): complete tRPC v11 migration plan
KevinEdry Jan 20, 2026
24c98b3
refactor(04-04): Remove ImportsScanner and generator interfaces
KevinEdry Jan 20, 2026
715f5e2
chore(04-04): Remove ts-morph dependency
KevinEdry Jan 20, 2026
aad356f
docs(04-04): Complete remove ts-morph plan
KevinEdry Jan 20, 2026
c390881
test(04-03): add TypeScript validation tests for generated code
KevinEdry Jan 20, 2026
9e87987
test(04-03): add negative test to verify error detection
KevinEdry Jan 20, 2026
5d27ee8
docs(04-03): complete TypeScript validation plan
KevinEdry Jan 20, 2026
9989db8
docs(04-05): Create tRPC v10 to v11 migration guide
KevinEdry Jan 20, 2026
7f5aa99
docs(04-05): Create TypeScript to Rust CLI migration guide
KevinEdry Jan 20, 2026
9043f7e
docs(04-05): Add v2.0.0 breaking changes to CHANGELOG
KevinEdry Jan 20, 2026
ddf2e18
docs(04-05): Complete migration documentation plan
KevinEdry Jan 20, 2026
83abafe
chore(04): update Cargo.lock and file permissions
KevinEdry Jan 20, 2026
71dd7f7
docs(04): complete migration compatibility phase
KevinEdry Jan 20, 2026
20063cf
docs(05): capture phase context
KevinEdry Jan 20, 2026
e9ddcef
docs(05): research phase domain
KevinEdry Jan 20, 2026
892745b
docs(05): create phase plan
KevinEdry Jan 20, 2026
ccf45b6
feat(05-01): add dependencies and improve CLI help text
KevinEdry Jan 21, 2026
a080bda
feat(05-01): add NO_COLOR environment variable support
KevinEdry Jan 21, 2026
098066b
docs(05-01): complete CLI polish and dependencies plan
KevinEdry Jan 21, 2026
dfccde6
feat(05-02): create validation module with tsc and diff support
KevinEdry Jan 21, 2026
46f14e3
feat(05-02): add CLI output module with JSON serializable types
KevinEdry Jan 21, 2026
c532b6e
docs(05-02): complete validation infrastructure plan
KevinEdry Jan 21, 2026
cfacdab
feat(05-03): implement dry-run execution logic
KevinEdry Jan 21, 2026
cbb9bda
test(05-03): add dry-run integration tests
KevinEdry Jan 21, 2026
966e1de
docs(05-03): complete dry-run wiring plan
KevinEdry Jan 21, 2026
a95ccc5
docs(05): complete advanced features phase
KevinEdry Jan 21, 2026
c9cef1e
docs(06): capture phase context
KevinEdry Jan 21, 2026
8b22af1
docs(06): research phase domain
KevinEdry Jan 21, 2026
6e275f9
docs(06): create phase plan
KevinEdry Jan 21, 2026
d920842
fix(06): revise plans based on checker feedback
KevinEdry Jan 21, 2026
58032a1
fix(06): revise plan 03 based on checker feedback
KevinEdry Jan 21, 2026
2468a90
docs(06-05): create v2.0.0 release checklist
KevinEdry Jan 21, 2026
df2714f
docs(06-05): link release checklist from readme
KevinEdry Jan 21, 2026
556030a
docs(06-02): Update README with CLI-based workflow
KevinEdry Jan 21, 2026
20825c2
docs(06-05): complete release checklist plan
KevinEdry Jan 21, 2026
1642bc7
docs(06-02): Complete documentation updates plan
KevinEdry Jan 21, 2026
44c3c94
fix(06-01): use constructor injection for factories and driver
KevinEdry Jan 21, 2026
97ea7ad
docs(06-01): complete di fix plan
KevinEdry Jan 21, 2026
abfbb3a
feat(06-04): update next.js client to trpc v11 api
KevinEdry Jan 21, 2026
ef862fd
docs(06-04): complete next.js client v11 plan
KevinEdry Jan 21, 2026
6962469
test(06-03): add E2E test infrastructure to Express example
KevinEdry Jan 21, 2026
ae3ef47
test(06-03): add E2E test infrastructure to Fastify example
KevinEdry Jan 21, 2026
2bf3e81
test(06-03): add TypeScript compilation verification tests
KevinEdry Jan 21, 2026
eeb06cc
ci(06-03): add E2E test job to verify workflow
KevinEdry Jan 21, 2026
76414b3
docs(06-03): complete E2E integration tests plan
KevinEdry Jan 21, 2026
8422a2e
fix(06-03): exclude test dir from tsconfig.build
KevinEdry Jan 21, 2026
664644d
fix(06-01): resolve bun workspace nestjs duplicate deps
KevinEdry Jan 21, 2026
74c11ed
fix(06-01): add cargo build fallback to cli bin script
KevinEdry Jan 21, 2026
34c67d9
docs(06): complete production-ready phase
KevinEdry Jan 21, 2026
5a4b659
docs: start milestone v2.0 quality sweep
KevinEdry Jan 21, 2026
9b2d08d
docs: define milestone v2.0 requirements
KevinEdry Jan 21, 2026
9c655a7
docs: create milestone v2.0 roadmap (3 phases)
KevinEdry Jan 21, 2026
66b0858
docs(07): research phase domain
KevinEdry Jan 21, 2026
6c72fa0
docs(07): create phase plan
KevinEdry Jan 21, 2026
52bd594
docs(07-01): add SAFETY comments to all 6 production expect calls
KevinEdry Jan 21, 2026
96aac57
docs(07-01): update REQUIREMENTS.md with corrected error handling counts
KevinEdry Jan 21, 2026
61409f7
docs(07-01): complete error handling documentation plan
KevinEdry Jan 21, 2026
77f8399
docs(07): complete error-handling phase
KevinEdry Jan 21, 2026
b69dfd4
docs(08): research clippy cleanup phase domain
KevinEdry Jan 21, 2026
1c059c9
docs(08): create phase plan
KevinEdry Jan 21, 2026
642222c
fix(08-01): resolve unused variable and self warnings
KevinEdry Jan 21, 2026
31badb8
fix(08-01): resolve miscellaneous style warnings
KevinEdry Jan 21, 2026
af54ca9
docs(08-01): complete non-nesting clippy warnings plan
KevinEdry Jan 21, 2026
0361d7f
refactor(08-03): reduce nesting in module.rs and context.rs
KevinEdry Jan 21, 2026
609f411
refactor(08-02): reduce nesting in config, diagnostic, discovery
KevinEdry Jan 21, 2026
a4c8438
refactor(08-02): reduce nesting in generation and generator modules
KevinEdry Jan 21, 2026
23f37af
refactor(08-02): reduce nesting in scanner, watcher, cli modules
KevinEdry Jan 21, 2026
89f1149
docs(08-02): complete infrastructure nesting cleanup plan
KevinEdry Jan 21, 2026
400a0cd
refactor(08-03): reduce nesting in middleware, imports, router, and d…
KevinEdry Jan 21, 2026
8fd0d57
refactor(08-03): reduce nesting in schema modules
KevinEdry Jan 21, 2026
ba3d22b
docs(08-03): complete parser nesting cleanup plan
KevinEdry Jan 21, 2026
a55e26d
test(08-04): expand error handling integration tests
KevinEdry Jan 21, 2026
2eec962
test(08-04): add parser unit tests for Result/Option propagation
KevinEdry Jan 21, 2026
71bbb4a
test(08-04): add generator unit tests for edge cases
KevinEdry Jan 21, 2026
00e0f9f
docs(08-04): complete error handling test coverage plan
KevinEdry Jan 21, 2026
5f94fb7
fix(08): orchestrator corrections for clippy cleanup
KevinEdry Jan 21, 2026
59507f9
docs(08): complete clippy-cleanup phase
KevinEdry Jan 21, 2026
96f6a6c
docs(09): Research CI enforcement phase domain
KevinEdry Jan 21, 2026
2a1b829
docs(09): create phase plan
KevinEdry Jan 21, 2026
5060abd
feat(09-01): enable strict unwrap_used and expect_used lints
KevinEdry Jan 21, 2026
f7eed44
feat(09-01): add allow attributes to documented production expects
KevinEdry Jan 21, 2026
b3a119f
docs(09-01): complete strict lint enforcement plan
KevinEdry Jan 21, 2026
6c5002f
docs(09): Complete CI enforcement phase
KevinEdry Jan 21, 2026
1f91570
chore: complete v2.0 milestone
KevinEdry Jan 21, 2026
805c424
chore(docs): add vercel.json with bun version config
KevinEdry Jan 22, 2026
9ae2b5d
fix(docs): disable corepack for bun compatibility
KevinEdry Jan 22, 2026
dcc86ca
fix(docs): downgrade eslint to v8 for next.js compatibility
KevinEdry Jan 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,39 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
submodules: false
- name: Enable Corepack
run: corepack enable
- name: Setup Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: yarn install --frozen-lockfile
run: bun install --frozen-lockfile
- name: Lint
run: yarn lint --no-fix
run: bun run lint --no-fix
- name: Test
run: yarn test
run: bun test
- name: Build
run: yarn build
run: bun run build

verify-rust-cli:
runs-on: ubuntu-latest
name: verify-rust-cli
defaults:
run:
working-directory: packages/nestjs-trpc-cli
steps:
- name: Checkout the repo
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
submodules: false
- name: Setup Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
components: rustfmt, clippy
- name: Check formatting
run: cargo fmt --check
- name: Run clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Run tests
run: cargo test
- name: Build
run: cargo build --release
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ test/**/dist
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
!.yarn/versions

# Rust CLI (packages/nestjs-trpc/cli)
**/target/
**/*.rs.bk

# Development tools
.auto-claude/
.worktrees/
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1 +1 @@
yarn commitlint -c --config .commitlintrc.json -E --edit
bun run commitlint -c --config .commitlintrc.json -E --edit
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
yarn lint-staged
bun run lint-staged
3 changes: 0 additions & 3 deletions .yarnrc.yml

This file was deleted.

269 changes: 269 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

**NestJS tRPC** is a library that integrates tRPC into the NestJS framework using an opinionated, decorator-based approach that aligns with NestJS conventions. It provides end-to-end typesafety for APIs with automatic AppRouter generation and full dependency injection support.

## Development Commands

### Build & Development
```bash
# Build all packages (uses TypeScript project references)
bun run build

# Build nestjs-trpc package only
cd packages/nestjs-trpc && bun run build

# Watch mode for development
cd packages/nestjs-trpc && bun run start:dev

# Debug with Node inspector
cd packages/nestjs-trpc && bun run debug:dev

# Clean build artifacts
bun run clean
```

### Testing
```bash
# Run all tests (workspace-wide)
bun test

# Run tests for nestjs-trpc package only
cd packages/nestjs-trpc && bun test

# Run tests with coverage
cd packages/nestjs-trpc && bun test --coverage

# Run related tests for changed files (used by lint-staged)
bun test --bail --findRelatedTests <file>
```

### Linting & Formatting
```bash
# Lint and auto-fix
bun run lint

# Lint without fixing (CI mode)
bun run lint --no-fix

# Format code
bun run format
```

### Running Examples
```bash
# Run Express example
cd examples/nestjs-express && bun run start:dev

# Run Fastify example
cd examples/nestjs-fastify && bun run start:dev

# Run Next.js example (client-side)
cd examples/nextjs-trpc && bun run dev
```

### Git Workflow
- Commits use conventional commits enforced by commitlint
- Allowed types: `build`, `chore`, `ci`, `docs`, `feat`, `fix`, `perf`, `refactor`, `revert`, `style`, `test`, `sample`
- Lint-staged runs on pre-commit: prettier, eslint, and related tests
- Tests must pass for affected files before committing

## Architecture

### Core Concepts

The library follows a factory-based architecture where decorators mark classes and methods, then factories extract metadata at runtime to build the tRPC router:

1. **Decorators** (`lib/decorators/`) - Metadata markers for routers and procedures:
- `@Router()` - Marks classes as tRPC routers, stores file path and optional alias
- `@Query()` / `@Mutation()` - Marks methods as tRPC procedures with input/output schemas
- `@UseMiddlewares()` - Attaches middleware to routers or procedures
- `@Context()` / `@RawInput()` - Parameter decorators for dependency injection

2. **Factories** (`lib/factories/`) - Extract metadata and build tRPC structures:
- `RouterFactory` - Scans NestJS modules to find `@Router()` classes via reflection
- `ProcedureFactory` - Extracts `@Query()` and `@Mutation()` methods from router instances
- `MiddlewareFactory` - Processes `@UseMiddlewares()` metadata
- `TRPCFactory` - Top-level orchestrator that calls `RouterFactory.serializeRoutes()`

3. **Generators** (`lib/generators/`) - TypeScript code generation for client types:
- `TRPCGenerator` - Main entry point, orchestrates schema file generation
- `RouterGenerator` - Generates router type definitions from metadata
- `ProcedureGenerator` - Generates procedure type definitions with input/output schemas
- `MiddlewareGenerator` / `ContextGenerator` - Generate context type augmentations
- Uses `ts-morph` to build TypeScript AST and write generated files

4. **Scanners** (`lib/scanners/`) - File system and import analysis:
- `FileScanner` - Resolves caller file paths from stack traces
- `ImportsScanner` - Builds import dependency maps using `ts-morph`

5. **Drivers** (`lib/drivers/`) - HTTP adapter integration:
- `ExpressDriver` - Integrates with Express via `@trpc/server/adapters/express`
- `FastifyDriver` - Integrates with Fastify via `@trpc/server/adapters/fastify`
- Auto-detected based on NestJS HttpAdapter

### Runtime Flow

1. **Module Initialization** (`TRPCModule.forRoot()`):
- Accepts `TRPCModuleOptions` including `autoSchemaFile` path
- If `autoSchemaFile` is provided, registers `GeneratorModule` for code generation
- Registers core providers: factories, drivers, `AppRouterHost`

2. **Application Bootstrap** (`onModuleInit`):
- `TRPCDriver.start()` initializes tRPC with `initTRPC.context().create()`
- `TRPCFactory.serializeAppRoutes()` calls `RouterFactory` to build the router tree
- Generated `appRouter` stored in `AppRouterHost` (injectable singleton)
- Appropriate driver (Express/Fastify) mounts the router at `basePath` (default: `/trpc`)

3. **Schema Generation** (if `autoSchemaFile` enabled):
- `TRPCGenerator.generateSchemaFile()` runs after module init
- Scans all routers/procedures, extracts Zod schemas and return types
- Generates TypeScript file with `export type AppRouter = typeof appRouter`
- Clients import this type for end-to-end typesafety

### Key Design Patterns

- **Reflection-based Discovery**: Uses `Reflect.getMetadata()` to find decorated classes/methods across all NestJS modules
- **Metadata Keys** (`lib/trpc.constants.ts`): `ROUTER_METADATA_KEY`, `MIDDLEWARES_KEY`, etc.
- **Lazy Router Construction**: Routers built at runtime from discovered metadata, not compile-time
- **DI Integration**: Router/middleware instances resolved from NestJS DI container
- **File Path Tracking**: Each `@Router()` stores its source file path for import resolution during generation

## Monorepo Structure

This is a **Bun workspaces** monorepo with the following structure:

- **`packages/nestjs-trpc/`** - Main library package published to npm
- `lib/` - Source code (decorators, factories, generators, drivers, etc.)
- `dist/` - Compiled output

- **`examples/`** - Integration examples demonstrating library usage
- `nestjs-express/` - Example using Express adapter
- `nestjs-fastify/` - Example using Fastify adapter
- `nextjs-trpc/` - Next.js client consuming tRPC endpoints

- **`docs/`** - Documentation website (https://nestjs-trpc.io)

- **Build System**: Uses TypeScript project references for efficient incremental compilation
- Root `tsconfig.json` files coordinate workspace builds
- Each package/example has its own `tsconfig.json` and `tsconfig.build.json`
- Build command: `tsc -b -v packages`

## Testing

- **Framework**: Jest with ts-jest preset
- **Location**: Tests co-located in `__tests__` directories next to source files
- **Config**: Uses `tsconfig.spec.json` with `ignoreCodes: [151001]` for TS diagnostics
- **Coverage**: Enabled by default with `--coverage` flag
- **Patterns**: `**/__tests__/**/*.ts`, `**/*.spec.ts`, `**/*.test.ts`

## Important Implementation Notes

1. **ts-morph Usage**: The generator heavily relies on `ts-morph` (TypeScript compiler API wrapper) to parse source files and build import maps. Changes to decorator metadata must be reflected in the corresponding generator.

2. **Runtime vs Codegen**: The library has two distinct code paths:
- **Runtime**: Factories build the actual tRPC router from decorated classes
- **Codegen**: Generators analyze the same decorators to emit TypeScript types
These must stay in sync - a change to how a decorator works requires updating both.

3. **Import Resolution**: `ImportsScanner` builds a map from class/type names to their source file paths by traversing import statements. This enables the generator to write correct import statements in generated files. If imports are missing, check `buildSourceFileImportsMap()` logic.

4. **File Path Tracking**: `FileScanner.getCallerFilePath()` uses stack traces to determine where decorators are applied. This is fragile - avoid deeply nested decorator factories or the path resolution may break.

5. **NestJS Module Scanning**: `RouterFactory.getRouters()` iterates `ModulesContainer` to find providers. Routers must be registered as providers in a NestJS module or they won't be discovered.

6. **v11 Migration Note**: Comment in `router.factory.ts:102` indicates the `router()` wrapping call needs removal for tRPC v11 compatibility.

## Coding Principles

### Comments Philosophy

**If code needs "how" or "what" comments to be understood, it's a sign the code needs refactoring.**

Guidelines:
- ✅ **Self-documenting code** - Use clear naming, proper abstractions, and logical structure
- ✅ **"Why" comments only** - Explain non-obvious decisions, constraints, or trade-offs
- ❌ **No "how" comments** - Don't explain step-by-step what the code does
- ❌ **No "what" comments** - Don't describe what a function/variable is (the name should do that)
- 🔧 **Refactor unclear code** - If you need to explain how/what, refactor instead

Examples:
```typescript
// ❌ BAD - "what" comment
// This function gets the user by ID
function getUserById(id: string) { ... }

// ❌ BAD - "how" comment
// This function:
// 1. Validates the input
// 2. Queries the database
// 3. Transforms the result
function processUser(data) { ... }

// ✅ GOOD - "why" comment
// Using setTimeout instead of setImmediate because Node.js
// event loop phases cause race conditions with DB connections
setTimeout(checkConnection, 0);

// ✅ GOOD - no comment needed, self-documenting
function getUserById(id: string) { ... }
function validateAndTransformUser(rawData: unknown): User { ... }
```

This principle applies to both the TypeScript library code and the Rust CLI.

### File Size Limits

**Keep files focused and maintainable by limiting file size to ~200-300 lines of production code.**

Guidelines:
- ✅ **Target: 200-300 lines** - Aim for files in this range (excluding tests)
- ✅ **Split at logical boundaries** - Break large files into cohesive submodules
- ✅ **Single Responsibility** - Each file should have one clear purpose
- ❌ **Avoid mega-files** - Files over 500 lines are usually doing too much
- 🔧 **Refactor when needed** - If a file grows beyond 300 lines, look for natural split points

When counting:
- Count only production code (exclude tests in `#[cfg(test)]` blocks)
- Blank lines and imports don't count toward cognitive load
- Focus on keeping the logic focused and navigable

Example structure:
```
parser/
├── mod.rs (~150 lines) - Public API + core orchestration
├── router.rs (~220 lines) - Router extraction logic
├── procedure.rs (~200 lines)- Procedure extraction logic
└── schema/
├── mod.rs (~150 lines) - Schema public API
├── flatten.rs (~200 lines)- Flattening logic
└── traverse.rs (~200 lines)- AST traversal
```

This guideline applies primarily to the Rust CLI. TypeScript files can be larger if needed.

## Package Manager

- This project uses **Bun** as specified in `packageManager` field
- `bun.lock` contains dependency lock information
- Bun provides fast package installation and native TypeScript/JSX support

## Publishing

The library uses Lerna for version management and publishing:

```bash
# Prepare release (builds and copies changelog)
bun run prepublish:npm

# Publish to npm
bun run publish:npm

# Publish next/canary version
bun run publish:next
```

The library is published as `nestjs-trpc` on npm with the main entry point at `./dist/index.js`.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
To install **NestJS tRPC** with your preferred package manager, you can use any of the following commands:

```shell
# bun
bun add nestjs-trpc zod @trpc/server

# npm
npm install nestjs-trpc zod @trpc/server

Expand Down
Loading