|
1 |
| -[](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici) |
| 1 | +# nestjs-undici |
2 | 2 |
|
3 |
| -[](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici) |
4 |
| -[](https://codecov.io/gh/hebertcisco/nestjs-undici) |
| 3 | +[**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*. |
5 | 4 |
|
6 | 5 | [](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici) [](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici)
|
7 | 6 |
|
8 |
| -[](https://github.com/hebertcisco/nestjs-undici/actions/workflows/npm-publish.yml) |
9 |
| - |
10 |
| -[](https://github.com/hebertcisco/nestjs-undici/actions/workflows/coverage.yml) |
11 |
| -[](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici) |
12 |
| - |
13 |
| -[](https://www.typescriptlang.org/) |
14 |
| -[](https://nestjs.com/) |
15 |
| -[](https://code.visualstudio.com/) |
16 |
| -[](https://github.com/hebertcisco/nestjs-undici/actions) |
17 |
| - |
18 | 7 | ## Installation
|
19 | 8 |
|
| 9 | +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: |
| 10 | + |
20 | 11 | > Install with yarn or npm: `yarn` or `npm`:
|
21 | 12 |
|
22 | 13 | ```bash
|
23 | 14 | # yarn
|
24 | 15 | yarn add nestjs-undici
|
25 | 16 | ```
|
26 | 17 |
|
| 18 | +Or NPM: |
| 19 | + |
27 | 20 | ```bash
|
28 | 21 | # npm
|
29 | 22 | npm i nestjs-undici --save
|
30 | 23 | ```
|
31 | 24 |
|
32 |
| -### Usage example: |
| 25 | +#### Importing the module |
33 | 26 |
|
34 |
| -> Project example: [https://github.com/hebertcisco/nestjs-techniques-undici](https://github.com/hebertcisco/nestjs-techniques-undici) |
| 27 | +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: |
| 28 | + |
| 29 | +```ts |
| 30 | +import { HttpModule } from 'nestjs-undici'; |
| 31 | +``` |
| 32 | + |
| 33 | +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. |
35 | 34 |
|
36 | 35 | ```ts
|
37 | 36 | // app.module.ts
|
| 37 | + |
38 | 38 | import { Module } from '@nestjs/common';
|
39 | 39 | import { HttpModule } from 'nestjs-undici';
|
40 | 40 |
|
41 | 41 | import { AppController } from './app.controller';
|
42 | 42 | import { AppService } from './app.service';
|
43 | 43 |
|
| 44 | + |
44 | 45 | @Module({
|
45 |
| - imports: [ |
46 |
| - HttpModule.register({ |
47 |
| - headers: { |
48 |
| - 'my-header': `foo-bar`, |
49 |
| - }, |
50 |
| - }), |
51 |
| - ], |
52 |
| - controllers: [AppController], |
53 |
| - providers: [AppService], |
| 46 | + imports: [ |
| 47 | + HttpModule.register({ |
| 48 | + headers: { |
| 49 | + 'my-header': `foo-bar`, |
| 50 | + }, |
| 51 | + }), |
| 52 | + ], |
| 53 | + controllers: [AppController], |
| 54 | + providers: [AppService], |
54 | 55 | })
|
55 | 56 | export class AppModule {}
|
56 |
| - |
57 | 57 | ```
|
58 | 58 |
|
59 |
| -```ts |
60 |
| -// app.service.ts |
61 |
| -import { lastValueFrom } from 'rxjs'; |
| 59 | +#### Using the module |
| 60 | + |
| 61 | +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: |
62 | 62 |
|
63 |
| -import { Injectable } from '@nestjs/common'; |
| 63 | +```ts |
64 | 64 | import { HttpService } from 'nestjs-undici';
|
65 | 65 |
|
66 |
| -@Injectable() |
67 |
| -export class AppService { |
68 |
| - constructor(private httpService: HttpService) {} |
69 |
| - public fetchExternalInfo = async () => { |
70 |
| - const baseURL = 'https://api.github.com'; |
71 |
| - try { |
72 |
| - const result = this.httpService.request(`${baseURL}/repos/hebertcisco/undici`); |
73 |
| - |
74 |
| - const response = await lastValueFrom(result); |
75 |
| - |
76 |
| - return response.body.json(); |
77 |
| - } catch (error) { |
78 |
| - throw error; |
79 |
| - } |
80 |
| - }; |
| 66 | +export class AppComponent { |
| 67 | + constructor(private httpService: HttpService) {} |
81 | 68 | }
|
82 | 69 | ```
|
83 | 70 |
|
84 |
| -## 🤝 Contributing |
| 71 | +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. |
| 72 | + |
| 73 | +For example, here is how you could use the `HttpService` to make a GET request to the `/users` endpoint: |
| 74 | + |
| 75 | +```ts |
| 76 | +import { of } from 'rxjs'; |
| 77 | + |
| 78 | +export class AppService { |
| 79 | + constructor(private httpService: HttpService) {} |
| 80 | + |
| 81 | + public getUsers(): void { |
| 82 | + this.httpService |
| 83 | + .request |
| 84 | + .get('/users') |
| 85 | + .subscribe(response => { // Handle the response here }); |
| 86 | +}} |
| 87 | +``` |
| 88 | +
|
| 89 | +> You can also use the `post`, `put`, `patch`, `delete`, and `head` methods to send other types of HTTP requests. |
| 90 | +
|
| 91 | +## Configuration |
85 | 92 |
|
86 |
| -Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](issues). |
| 93 | +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. |
87 | 94 |
|
88 |
| -## Show your support |
| 95 | +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: |
89 | 96 |
|
90 |
| -Give a ⭐️ if this project helped you! |
| 97 | +- `useClass`: a provider that returns the options object |
| 98 | +- `useExisting`: a provider that returns the options object |
| 99 | +- `useFactory`: a factory function that returns the options object |
| 100 | +- `inject`: an array of providers to inject into the factory function |
| 101 | +- `imports`: an array of modules to import |
| 102 | +- `extraProviders`: an array of additional providers to add to the module |
91 | 103 |
|
92 |
| -Or buy me a coffee 🙌🏾 |
| 104 | +### Customizing the request options |
93 | 105 |
|
94 |
| -<a href="https://www.buymeacoffee.com/hebertcisco"> |
95 |
| - <img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=hebertcisco&button_colour=FFDD00&font_colour=000000&font_family=Inter&outline_colour=000000&coffee_colour=ffffff" /> |
96 |
| -</a> |
| 106 | +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. |
97 | 107 |
|
98 |
| -## 📝 License |
| 108 | +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: |
| 109 | +
|
| 110 | +```ts |
| 111 | +import { of } from 'rxjs'; |
99 | 112 |
|
100 |
| -Copyright © 2022 [Hebert F Barros](https://github.com/hebertcisco).<br /> |
101 |
| -This project is [MIT](LICENSE) licensed. |
| 113 | +export class AppService { |
| 114 | + constructor(private httpService: HttpService) {} |
| 115 | + |
| 116 | + public createUser(user: User): void { |
| 117 | + this.httpService |
| 118 | + .request('/users', { |
| 119 | + method: 'POST', |
| 120 | + body: JSON.stringify(user), |
| 121 | + headers: { |
| 122 | + 'Content-Type': 'application/json', |
| 123 | + }, |
| 124 | + }) |
| 125 | + .subscribe(response => { |
| 126 | + // Handle the response here |
| 127 | + }); |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
0 commit comments