Gathering detailed insights and metrics for nestjs-undici
Gathering detailed insights and metrics for nestjs-undici
Gathering detailed insights and metrics for nestjs-undici
Gathering detailed insights and metrics for nestjs-undici
Undici utilities module based on the @nodejs undici package 🌐https://www.npmjs.com/package/nestjs-undici
npm install nestjs-undici
Typescript
Module System
Min. Node Version
Node Version
NPM Version
53.7
Supply Chain
98.4
Quality
84
Maintenance
100
Vulnerability
99.6
License
TypeScript (89.71%)
HTML (8.02%)
JavaScript (1.91%)
Shell (0.36%)
Total Downloads
4,758
Last Day
1
Last Week
3
Last Month
221
Last Year
1,087
14 Stars
439 Commits
3 Forks
1 Watching
1 Branches
4 Contributors
Latest Version
0.1.55
Package Id
nestjs-undici@0.1.55
Unpacked Size
31.21 kB
Size
8.13 kB
File Count
35
NPM Version
10.8.2
Node Version
18.20.5
Publised On
05 Dec 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-90.9%
3
Compared to previous week
Last month
550%
221
Compared to previous month
Last year
-26.5%
1,087
Compared to previous year
4
26
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
:
1# yarn 2yarn add nestjs-undici
Or NPM:
1# npm 2npm 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:
1import { 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.
1// app.module.ts 2 3import { Module } from '@nestjs/common'; 4import { HttpModule } from 'nestjs-undici'; 5 6import { AppController } from './app.controller'; 7import { AppService } from './app.service'; 8 9 10@Module({ 11 imports: [ 12 HttpModule.register({ 13 headers: { 14 'my-header': `foo-bar`, 15 }, 16 }), 17 ], 18 controllers: [AppController], 19 providers: [AppService], 20}) 21export 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:
1import { HttpService } from 'nestjs-undici'; 2 3export class AppComponent { 4 constructor(private httpService: HttpService) {} 5}
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:
1import { of } from 'rxjs'; 2 3export class AppService { 4 constructor(private httpService: HttpService) {} 5 6 public getUsers(): void { 7 this.httpService 8 .request 9 .get('/users') 10 .subscribe(response => { // Handle the response here }); 11}}
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 moduleThe 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:
1import { of } from 'rxjs'; 2 3export class AppService { 4 constructor(private httpService: HttpService) {} 5 6 public createUser(user: User): void { 7 this.httpService 8 .request('/users', { 9 method: 'POST', 10 body: JSON.stringify(user), 11 headers: { 12 'Content-Type': 'application/json', 13 }, 14 }) 15 .subscribe(response => { 16 // Handle the response here 17 }); 18 } 19}
No vulnerabilities found.
No security vulnerabilities found.