nestjs-undici
TypeScript icon, indicating that this package has built-in type declarations

0.2.57Β β€’Β PublicΒ β€’Β Published

NestJS Undici

npm version Running Code Coverage License: MIT

NestJS Undici is a powerful HTTP client module for NestJS applications, built on top of @nodejs/undici. It provides a simple and efficient way to make HTTP requests in your NestJS applications.

Features

  • πŸš€ Built on top of @nodejs/undici
  • πŸ”„ Full TypeScript support
  • ⚑ High-performance HTTP client
  • πŸ”’ Secure by default
  • πŸ› οΈ Easy to configure and use
  • πŸ“¦ Lightweight and dependency-free
  • πŸ” Built-in request/response interceptors
  • πŸ”„ Automatic retry mechanism
  • πŸ“ Comprehensive documentation

Installation

# Using npm
npm install nestjs-undici

# Using yarn
yarn add nestjs-undici

Quick Start

  1. Import the HttpModule in your root module:
import { Module } from '@nestjs/common';
import { HttpModule } from 'nestjs-undici';

@Module({
  imports: [
    HttpModule.register({
      // Optional configuration
      headers: {
        'Content-Type': 'application/json',
      },
    }),
  ],
})
export class AppModule {}
  1. Inject and use the HttpService in your service:
import { Injectable } from '@nestjs/common';
import { HttpService } from 'nestjs-undici';

@Injectable()
export class AppService {
  constructor(private readonly httpService: HttpService) {}

  async getUsers() {
    const response = await this.httpService
      .request('https://api.example.com/users')
      .toPromise();
    
    return response.data;
  }
}

Configuration

The HttpModule can be configured using the register or registerAsync methods:

Synchronous Configuration

HttpModule.register({
  headers: {
    'Content-Type': 'application/json',
  },
  timeout: 5000,
  retry: {
    attempts: 3,
    delay: 1000,
  },
});

Asynchronous Configuration

HttpModule.registerAsync({
  useFactory: async (configService: ConfigService) => ({
    headers: {
      'Authorization': await configService.get('API_KEY'),
    },
  }),
  inject: [ConfigService],
});

Advanced Usage

Making HTTP Requests

// GET request
const response = await this.httpService
  .request('https://api.example.com/users')
  .toPromise();

// POST request
const response = await this.httpService
  .request('https://api.example.com/users', {
    method: 'POST',
    body: JSON.stringify({ name: 'John Doe' }),
  })
  .toPromise();

Using Interceptors

import { Injectable } from '@nestjs/common';
import { HttpService, HttpInterceptor } from 'nestjs-undici';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  intercept(request: Request) {
    request.headers.set('Authorization', 'Bearer token');
    return request;
  }
}

// Register the interceptor
HttpModule.register({
  interceptors: [AuthInterceptor],
});

API Reference

For detailed API documentation, please visit our documentation site.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you find this package useful, please consider giving it a ⭐️ on GitHub.

Package Sidebar

Install

npm i nestjs-undici

Weekly Downloads

39

Version

0.2.57

License

MIT

Unpacked Size

36.1 kB

Total Files

36

Last publish

Collaborators

  • tecnobert