Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hebertcisco/nestjs-undici

Undici utilities module based on the @nodejs undici package 🌐https://www.npmjs.com/package/nestjs-undici
https://github.com/hebertcisco/nestjs-undici

nest nestjs nestjs-module typescript undici

Last synced: about 2 months ago
JSON representation

Undici utilities module based on the @nodejs undici package 🌐https://www.npmjs.com/package/nestjs-undici

Awesome Lists containing this project

README

        

# nestjs-undici

[**nestjs-undici**](/) is a **NestJS** module that provides utility functions for working with the [@nodejs/undici](https://github.com/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](https://github.com/hebertcisco/nestjs-undici/actions/workflows/coverage.yml/badge.svg)](https://github.com/hebertcisco/nestjs-undici/actions/workflows/coverage.yml)

## Installation

To install [nestjs-undici](https://www.npmjs.com/package/nestjs-undici), you will need to have [Node.js](https://nodejs.org/en/download/) and npm (or [yarn](https://classic.yarnpkg.com/lang/en/docs/install/#windows-stable)) installed on your system. Then, you can run the following command to install the module:

> Install with yarn or npm: `yarn` or `npm`:

```bash
# yarn
yarn add nestjs-undici
```

Or NPM:

```bash
# 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:

```js
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.

```js
// 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:

```js
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](https://rxjs.dev/api/index/class/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:

```js
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](https://github.com/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:

```js
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
});
}
}
```