home / skills / hoangnguyen0403 / agent-skills-standard / api-standards
This skill enforces standardized NestJS API responses, pagination, and error handling to improve consistency and developer experience.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill api-standardsReview the files below or copy the command above to add this skill to your agents.
---
name: NestJS API Standards
description: Response wrapping, pagination, and error standardization.
metadata:
labels: [nestjs, api, pagination, response]
triggers:
files: ['**/*.controller.ts', '**/*.dto.ts']
keywords: [ApiResponse, Pagination, TransformInterceptor]
---
# NestJS API Standards & Common Patterns
## **Priority: P1 (OPERATIONAL)**
Standardized API response patterns and common NestJS conventions.
## Generic Response Wrapper
- **Concept**: Standardize all successful API responses.
- **Implementation**: Use `TransformInterceptor` to wrap data in `{ statusCode, data, meta }`.
## Pagination Standards (Pro)
- **DTOs**: Use strict `PageOptionsDto` (page/take/order) and `PageDto<T>` (data/meta).
- **Swagger Logic**: Generics require `ApiExtraModels` and schema path resolution.
- **Reference**: See [Pagination Wrapper Implementation](references/pagination-wrapper.md) for the complete `ApiPaginatedResponse` decorator code.
## Custom Error Response
- **Standard Error Object**:
```typescript
export class ApiErrorResponse {
@ApiProperty()
statusCode: number;
@ApiProperty()
message: string;
@ApiProperty()
error: string;
@ApiProperty()
timestamp: string;
@ApiProperty()
path: string;
}
```
- **Docs**: Apply `@ApiBadRequestResponse({ type: ApiErrorResponse })` globally or per controller.
This skill captures NestJS API standards for consistent response wrapping, pagination, and error formatting. It codifies interceptors, DTO patterns, and Swagger integrations so APIs are predictable for clients and easier to document. The focus is practical: simple response envelopes, typed pagination DTOs, and a standard error contract.
The skill recommends using a TransformInterceptor to wrap all successful responses into a uniform envelope with fields like statusCode, data, and meta. For list endpoints it defines PageOptionsDto and a generic PageDto<T> to carry data and pagination metadata. For error handling it prescribes a shared ApiErrorResponse class and documenting bad responses with @ApiBadRequestResponse or global exception filters to ensure consistent error payloads.
How do I include generics in Swagger for paginated responses?
Register generic models with ApiExtraModels and use decorators that resolve schema paths to expose PageDto<T> correctly in Swagger.
Should I wrap error responses with the same envelope as success responses?
No. Use a dedicated ApiErrorResponse structure for errors to convey statusCode, message, error, timestamp, and path clearly.