|
| 1 | +# Coding Guidelines for Serverless TypeScript Application |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This repository is a serverless application boilerplate using AWS Lambda functions, TypeScript, and the Serverless Framework. It follows modern serverless patterns and best practices for building scalable applications on AWS infrastructure. |
| 6 | + |
| 7 | +## Code Architecture |
| 8 | + |
| 9 | +### Key Patterns |
| 10 | +- **Serverless Functions**: Individual Lambda functions with specific responsibilities, each in its own directory |
| 11 | +- **Microservices approach**: Each function handles a specific business capability |
| 12 | +- **Dependency Injection**: Functions receive dependencies as parameters for better testability |
| 13 | +- **Middleware-based processing**: Uses Middy middleware for cross-cutting concerns |
| 14 | +- **Schema validation**: Zod schemas for input validation and type safety |
| 15 | + |
| 16 | +### Project Structure |
| 17 | +``` |
| 18 | +functions/ |
| 19 | +├── [function-name]/ # Each function has its own directory |
| 20 | +│ ├── handler.ts # Main lambda handler with middleware |
| 21 | +│ ├── service.ts # Business logic (optional) |
| 22 | +│ ├── service.spec.ts # Unit tests for service |
| 23 | +│ ├── event.schema.ts # Zod validation schema |
| 24 | +│ ├── function.yml # Serverless configuration |
| 25 | +│ └── config/ |
| 26 | +│ └── index.ts # Environment configuration |
| 27 | +shared/ |
| 28 | +├── config/ # Database and shared configuration |
| 29 | +├── errors/ # Custom error classes |
| 30 | +├── middleware/ # Reusable middleware functions |
| 31 | +├── models/ # TypeORM database models |
| 32 | +├── repositories/ # Data access layer |
| 33 | +├── utils/ # Shared utility functions |
| 34 | +└── logger/ # Winston logging setup |
| 35 | +workflows/ |
| 36 | +├── [workflow-name]/ # Step Functions workflows |
| 37 | +│ ├── workflow.yml # Step Function definition |
| 38 | +│ └── workflow.asl.yml # Amazon States Language |
| 39 | +migrations/ # TypeORM database migrations |
| 40 | +``` |
| 41 | + |
| 42 | +### Key Libraries and Functionalities |
| 43 | +- **TypeScript**: For static typing and modern JavaScript features |
| 44 | +- **Serverless Framework**: For deployment and infrastructure management |
| 45 | +- **AWS Lambda**: Function compute platform |
| 46 | +- **AWS Step Functions**: Workflow orchestration |
| 47 | +- **Middy**: Lambda middleware framework for Node.js |
| 48 | +- **TypeORM**: ORM for PostgreSQL database interactions |
| 49 | +- **Zod**: Schema validation and type inference |
| 50 | +- **Winston**: Structured logging |
| 51 | +- **AWS SDK v3**: AWS service clients |
| 52 | +- **PostgreSQL**: Primary database |
| 53 | +- **DynamoDB**: NoSQL database for high-performance data storage |
| 54 | +- **Mocha + Sinon**: Testing framework and mocking |
| 55 | + |
| 56 | +### Key Development Guidelines |
| 57 | + |
| 58 | +#### General Guidelines |
| 59 | +- Always use TypeScript for type safety |
| 60 | +- Follow serverless best practices and patterns |
| 61 | +- Use dependency injection for all external dependencies |
| 62 | +- All environment variables must be extracted and validated using Zod in function config files |
| 63 | +- Use the provided middleware stack for all Lambda functions |
| 64 | +- Keep functions focused on single responsibilities |
| 65 | + |
| 66 | +#### Naming Guidelines |
| 67 | +- Function directories: Use **kebab-case** (e.g., `create-transaction`, `get-exchange-rates`) |
| 68 | +- File names: Use **kebab-case** with appropriate suffixes (e.g., `handler.ts`, `service.ts`) |
| 69 | +- Variables and functions: Use **camelCase** (e.g., `getUserById`, `transactionData`) |
| 70 | +- Classes and interfaces: Use **PascalCase** (e.g., `TransactionModel`, `PaymentService`) |
| 71 | +- Database columns: Use **snake_case** for TypeORM entity properties |
| 72 | +- Schema names: Use **camelCase** with "Schema" suffix (e.g., `createTransactionSchema`) |
| 73 | +- AWS resources: Use descriptive names with service prefixes |
| 74 | + |
| 75 | +#### Lambda Function Guidelines |
| 76 | +- All handlers must use the standard Middy middleware stack |
| 77 | +- Initialize database connections and AWS clients outside the handler function |
| 78 | +- Use dependency injection - pass all dependencies to service functions |
| 79 | +- Validate all inputs with Zod schemas |
| 80 | +- Use existing error classes from `shared/errors/` |
| 81 | +- Keep handlers thin - move business logic to service functions |
| 82 | +- Never create tests for handlers - only test service logic |
| 83 | + |
| 84 | +#### Database Guidelines |
| 85 | +- Use **TypeORM** for PostgreSQL interactions |
| 86 | +- Use **UUID v4** for primary keys, avoid auto-increment IDs |
| 87 | +- Models must have `.model.ts` suffix and include factory methods |
| 88 | +- Always generate migrations |
| 89 | +- Migrations are auto-executed at application start |
| 90 | +- Pass repositories to service functions, never import directly |
| 91 | + |
| 92 | +#### AWS Integration Guidelines |
| 93 | +- Use AWS SDK v3 with command pattern |
| 94 | +- Initialize AWS clients outside Lambda handlers for connection reuse |
| 95 | +- Use Step Functions for complex workflows |
| 96 | +- Follow AWS security best practices |
| 97 | + |
| 98 | +#### Testing Guidelines |
| 99 | +- Create unit tests for service logic only (`.spec.ts` files) |
| 100 | +- Use Sinon for mocking AWS clients and external dependencies |
| 101 | +- Mock all external dependencies (databases, APIs, AWS services) |
| 102 | +- Test both success and error scenarios |
| 103 | +- Place tests next to the files they test |
| 104 | + |
| 105 | +#### Error Handling Guidelines |
| 106 | +- Use existing error classes |
| 107 | +- Throw errors as exceptions - middleware handles responses |
| 108 | +- Include meaningful error messages with context |
| 109 | + |
| 110 | +#### Security Guidelines |
| 111 | +- Never expose sensitive information in error messages |
| 112 | +- Validate all inputs with Zod schemas |
| 113 | +- Use proper IAM roles and permissions |
| 114 | +- Store secrets in AWS Parameter Store or Secrets Manager |
| 115 | +- Enable CORS only when necessary and configure properly |
| 116 | + |
| 117 | +#### Middleware Guidelines |
| 118 | +- Always use the standard middleware stack in correct order |
| 119 | +- For non-HTTP triggers, omit HTTP-specific middleware |
| 120 | +- Use custom middleware sparingly and document well |
| 121 | + |
| 122 | +#### Workflow Guidelines |
| 123 | +- Use Step Functions for complex business processes |
| 124 | +- Define workflows in Amazon States Language (ASL) |
| 125 | +- Each step should be a focused Lambda function |
| 126 | +- Handle errors and retries at the workflow level |
| 127 | +- Use Task Tokens for human-in-the-loop processes |
0 commit comments