Http-Typedi Documentation

website·Indexed 30 days ago

https://fkc-alt.github.io/http-typedi/

A framework for building HTTP services featuring dependency injection, modular architecture, and a robust set of decorators. It includes built-in support for guards, interceptors, pipes (such as ParseIntPipe and ValidationPipe), and controllers with versioning and routing capabilities.

Tokens
14.2K
Snippets
76
Records
92
Agent score
75%

What's inside Http-Typedi

  1. Overview of http-typedi Utility Services

    The http-typedi library provides a set of modular utility services designed to handle common client-side development tasks and reduce boilerplate code. The available utility interfaces include:

    • UploadService: For handling file uploads.
    • RequestService: For managing network requests.
    • UtilsService: For general-purpose utility functions.
  2. Overview of http-typedi features

    http-typedi is a lightweight, out-of-the-box TypeScript library for HTTP requests. Key features include:

    • Middleware configuration for specific routes
    • Request and response interceptors (pre- and post-processing)
    • DTO (Data Transfer Object) support with interface validation
    • Built-in exception handlers
    • Full TypeScript implementation providing strong type hinting
    • Compatibility with modern build tools including Vite and Webpack
  3. Overview of http-typedi

    http-typedi is a TypeScript library designed to help developers write modular, efficient, scalable, and maintainable code for HTTP requests. It addresses common pain points in business development where request methods lack modularity and inconsistent technical encapsulation make long-term maintenance difficult—specifically reducing the need to manually verify API contracts with backend developers when interfaces change.
  4. Understand the purpose of Interceptors in http-typedi

    Interceptors in http-typedi are inspired by Aspect-Oriented Programming (AOP) and allow developers to inject cross-cutting logic into function executions. They can be used to:

    • Bind additional logic before or after a function executes.
    • Transform the results returned by a function.
    • Transform exceptions thrown by a function.
    • Extend the basic behavior of a function.
  5. Understand the role of Controllers in http-typedi

    Controllers in http-typedi are responsible for processing incoming request parameters, issuing requests to the server, and returning responses. They act as the entry point for specific application requests. Routing mechanisms determine which controller handles which request, and a single controller typically contains multiple routes to perform different operations. Controllers are implemented using classes and decorators, which associate the class with the metadata required by http-typedi to create route mappings.
  6. Use ExecutionContext in canActivate guards

    The canActivate() function in guards receives an instance of ExecutionContext. ExecutionContext extends ArgumentsHost, meaning it provides access to the request object and other arguments, while adding detailed information about the current execution process. This allows developers to build generic guards that can operate across multiple controllers, methods, and different execution contexts.
  7. Implement a service layer with RequestService for data handling

    To maintain a clean architecture, controllers should only trigger requests and not process data. Data processing and server communication should be handled in a service layer. Use the @Injectable() decorator to mark a class as a provider and inject the built-in RequestService to perform the actual server requests.
    import { Injectable, RequestService } from 'http-typedi'
    import { DemoReq, DemoRes } from './interfaces/demo.interface'
    
    @Injectable()
    export default class DemoService {
      constructor(private requestService: RequestService) {}
      private readonly url: string = 'http://localhost:8080'
    
      public getDemoDetail(param: DemoReq): ServerRes<DemoRes> {
         return this.requestService.request(param)
      }
    }
  8. Understand the purpose and benefits of Data Transfer Objects (DTO)

    A Data Transfer Object (DTO) is an object specifically designed to transport data between different layers of an application. By using DTOs, developers can decouple the internal data structures of a layer from the data being transmitted, which leads to a clearer system architecture.

    Key benefits include:

    • Improved Maintainability: Changes to data requirements (such as adding a new field) only need to be applied to the DTO object rather than every layer that consumes the data.
    • Enhanced Scalability: The system can evolve its data transport needs without requiring cascading changes across all architectural layers.
  9. Configure global settings in http-typedi

    http-typedi allows for global configuration of the application's behavior. These settings are typically defined within the app.module.ts file. Available global configuration areas include route mapping, route prefixes, interceptors, exception handlers, and delay timers.
  10. Delay route request processing using @Sleep()

    Use the @Sleep() decorator to introduce a delay in processing a route request. The decorator accepts a single number argument representing the delay duration in milliseconds (ms). This decorator must be imported from the http-typedi package.
    import { Controller, PostMapping, Sleep } from 'http-typedi';
    import { DemoService } from './demo.service';
    import { DemoReq, DemoRes } from './interfaces/demo.interface';
    
    @Controller('demo')
    export class DemoController {
      constructor(private demoService: DemoService) {}
    
      @PostMapping('updateDetail')
      @Sleep(1000) // Delays the request by 1000ms (1 second)
      async updateDemoDetail(demo: DemoReq): ServerRes<DemoRes> {
        return this.demoService.updateDemoDetail(demo);
      }
    }
  11. Organize application structure using @Module()

    In http-typedi, a Module is a class decorated with @Module(). This decorator provides the metadata necessary for the framework to organize the application tree. Every application must have at least one root module, which serves as the entry point for the application structure. For larger applications, multiple modules should be used to group closely related features.

    By default, modules encapsulate their providers. A provider can only be injected if it is a direct part of the current module or if it has been exported from an imported module. Exported providers act as the module's public API.

  12. Perform custom network requests with RequestConfig

    To perform a manual network request using RequestService, pass a RequestConfig object to the .request() method.

    Data Transmission Rule:

    • If the method is GET, use the params field to send data.
    • For other HTTP methods, use the data field to send data.
    const result = this.requestService.request<TData, Record<string, any>>(configure);