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.
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
ornpm
:
# yarn
yarn add nestjs-undici
Or NPM:
# npm
npm i nestjs-undici --save
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 {}
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
, andhead
methods to send other types of HTTP requests.
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 objectuseExisting
: a provider that returns the options objectuseFactory
: a factory function that returns the options objectinject
: an array of providers to inject into the factory functionimports
: an array of modules to importextraProviders
: an array of additional providers to add to the module
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
});
}
}