npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-undici

v0.1.54

Published

Undici utilities module based on the @nodejs/undici package 🌐

Downloads

37

Readme

nestjs-undici

nestjs-undici is a NestJS module that provides utility functions for working with the @nodejs/undici package. Undici is a lightweight HTTP/1.1 client for Node.js, designed to be used with the NestJs environment.

Running Code Coverage

Installation

To install nestjs-undici, you will need to have Node.js and npm (or yarn) installed on your system. Then, you can run the following command to install the module:

Install with yarn or npm: yarn or npm:

# yarn
yarn add nestjs-undici

Or NPM:

# npm
npm i nestjs-undici --save

Importing the module

To use nestjs-undici in your NestJS application, you will need to import it. You can do this by adding the following line to the top of the file where you want to use the module:

import { HttpModule } from 'nestjs-undici';

You will also need to include the HttpModule in the imports array of the root AppModule or the module where you want to use it.

// app.module.ts

import { Module } from '@nestjs/common';
import { HttpModule } from 'nestjs-undici';

import { AppController } from './app.controller';
import { AppService } from './app.service';

  
@Module({
    imports: [
        HttpModule.register({
            headers: {
                'my-header': `foo-bar`,
            },
        }),
    ],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule {}

Using the module

To use the nestjs-undici module, you will need to inject the HttpService into your component or controller. You can do this by adding it to the constructor arguments and adding a public or private property for it:

import { HttpService } from 'nestjs-undici';

export class AppComponent {
  constructor(private httpService: HttpService) {}
}

Once you have injected the HttpService, you can use it to make HTTP requests using the request() method. The request() method takes a URL and an options object as arguments, and returns an RxJS Observable that you can subscribe to in order to handle the response.

For example, here is how you could use the HttpService to make a GET request to the /users endpoint:

import { of } from 'rxjs';

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

  public getUsers(): void {
    this.httpService
      .request
      .get('/users')
      .subscribe(response => { // Handle the response here }); 
}}

You can also use the post, put, patch, delete, and head methods to send other types of HTTP requests.

Configuration

The nestjs-undici module supports configuration options through the register method. You can pass an options object to the register method to configure the Undici client. The available options are the same as the options for the @nodejs/undici client.

You can also use the registerAsync method to provide the options asynchronously, for example, from a configuration file. The registerAsync method accepts an object with the following properties:

  • useClass: a provider that returns the options object
  • useExisting: a provider that returns the options object
  • useFactory: a factory function that returns the options object
  • inject: an array of providers to inject into the factory function
  • imports: an array of modules to import
  • extraProviders: an array of additional providers to add to the module

Customizing the request options

The request() method also accepts an options object as its second argument. This options object can be used to customize the request, such as setting the HTTP method, adding headers, or setting the body of the request.

Here is an example of how you could use the options object to set the HTTP method to POST and add a JSON payload to the request body:

import { of } from 'rxjs';

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

  public createUser(user: User): void {
    this.httpService
      .request('/users', {
        method: 'POST',
        body: JSON.stringify(user),
        headers: {
          'Content-Type': 'application/json',
        },
      })
      .subscribe(response => {
        // Handle the response here
      });
  }
}