Posts

Showing posts with the label swagger

NestJS Controller and Swagger Guide

Image
Introduction NestJS is a progressive Node.js framework built with TypeScript, facilitating the development of efficient and scalable server-side applications. By default, NestJS uses Express as its core HTTP processing library. Key advantages include a tight modular architecture inspired by Angular and strong TypeScript support to reduce code errors. Controllers serve as the layer for processing incoming requests and returning responses to the client side. Their primary responsibility is to receive HTTP requests, route data to business logic services, and coordinate the returned result in the correct format. Swagger is a powerful suite of tools used for designing, building, and documenting RESTful APIs developed based on the OpenAPI data format specification. In NestJS, it helps automatically generate an intuitive UI interface for testing endpoints, enabling developers and stakeholders to understand the API structure without directly reading the source code. Detail After creating the N...

RESTful API Design

Image
Introduction REST (Representational State Transfer) working with JSON over HTTP/1.1 or HTTP/2 remains the default choice for public APIs and B2B integrations due to its popularity, debuggability and broad ecosystem. API Design & Resource Modeling When adopting REST , the design must maintain consistency, including the following characteristics: Resource-oriented Use plural nouns ( /api/v1/orders, /api/v1/users ). Placing actions in the URL, such as /orders/{id}/cancel , is a bad practice. Instead, use POST /orders/{id}/cancellations or PATCH /orders/{id} with a payload that updates state. Idempotency Ensure GET, PUT, DELETE are always idempotent. For POST (creating resources or processing payments), enforcing an Idempotency-Key in the Header is mandatory to prevent duplicate transactions during network glitches and client retries. Versioning Implement versioning from day one. Prefer URL Versioning (/api/v1/...) because it is explicit, easy to route at the Gateway layer (Nginx...