Skip to content

Commit e7fed64

Browse files
authored
Merge pull request #31 from hebertcisco/9-add-documentation-showing-how-all-methods-and-functions-work
Add documentation showing how all methods and functions work to close #9
2 parents b2c90eb + e669934 commit e7fed64

17 files changed

+506
-93
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"plugin:jest/recommended"
99
],
1010
"rules": {
11-
"@typescript-eslint/no-explicit-any": "off"
11+
"@typescript-eslint/no-explicit-any": "off",
12+
"@typescript-eslint/no-unused-vars": "off"
1213
}
1314
}

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# These are supported funding model platforms
22

3-
github: hebertcisco # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
3+
github: [hebertcisco]
44
patreon: # Replace with a single Patreon username
55
open_collective: # Replace with a single Open Collective username
66
ko_fi: # Replace with a single Ko-fi username

.github/dependabot.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
version: 2
22
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"
7+
open-pull-requests-limit: 10
8+
39
- package-ecosystem: "npm"
410
directory: "/"
511
schedule:
6-
interval: "daily"
7-
target-branch: "develop"
8-
labels:
9-
- "dependencies"
12+
interval: "weekly"
13+
open-pull-requests-limit: 10

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ npm-debug.log
1515
/coverage
1616
/.nyc_output
1717

18-
# dist
19-
dist
18+
# lib
19+
lib
2020

2121
# enviroment
2222
.env

.husky/pre-commit

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npm run lint
5+
npm run test

README.md

+84-55
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,130 @@
1-
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=hebertcisco_nestjs-undici&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici)
1+
# nestjs-undici
22

3-
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=hebertcisco_nestjs-undici&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici)
4-
[![codecov](https://codecov.io/gh/hebertcisco/nestjs-undici/branch/master/graph/badge.svg?token=J79GA9DGDT)](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*.
54

65
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=hebertcisco_nestjs-undici&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici) [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=hebertcisco_nestjs-undici&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici)
76

8-
[![Node.js build and publish package](https://github.com/hebertcisco/nestjs-undici/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/hebertcisco/nestjs-undici/actions/workflows/npm-publish.yml)
9-
10-
[![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)
11-
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=hebertcisco_nestjs-undici&metric=bugs)](https://sonarcloud.io/summary/new_code?id=hebertcisco_nestjs-undici)
12-
13-
[![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=flat&logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
14-
[![Nestjs](https://img.shields.io/badge/Nestjs-ea2845?style=flat&logo=nestjs&logoColor=white)](https://nestjs.com/)
15-
[![Free. Built on open source. Runs everywhere.](https://img.shields.io/badge/VS_Code-0078D4?style=flat&logo=visual%20studio%20code&logoColor=white)](https://code.visualstudio.com/)
16-
[![GitHub Actions](https://img.shields.io/badge/github%20actions-%232671E5.svg?style=flat&logo=githubactions&logoColor=white)](https://github.com/hebertcisco/nestjs-undici/actions)
17-
187
## Installation
198

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+
2011
> Install with yarn or npm: `yarn` or `npm`:
2112
2213
```bash
2314
# yarn
2415
yarn add nestjs-undici
2516
```
2617

18+
Or NPM:
19+
2720
```bash
2821
# npm
2922
npm i nestjs-undici --save
3023
```
3124

32-
### Usage example:
25+
#### Importing the module
3326

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.
3534

3635
```ts
3736
// app.module.ts
37+
3838
import { Module } from '@nestjs/common';
3939
import { HttpModule } from 'nestjs-undici';
4040

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

44+
4445
@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],
5455
})
5556
export class AppModule {}
56-
5757
```
5858

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:
6262

63-
import { Injectable } from '@nestjs/common';
63+
```ts
6464
import { HttpService } from 'nestjs-undici';
6565

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) {}
8168
}
8269
```
8370

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
8592
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.
8794
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:
8996
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
91103
92-
Or buy me a coffee 🙌🏾
104+
### Customizing the request options
93105
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.
97107
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';
99112

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+
```

docs/http/http.module.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Configuration
2+
3+
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.
4+
5+
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:
6+
7+
- `useClass`: a provider that returns the options object
8+
- `useExisting`: a provider that returns the options object
9+
- `useFactory`: a factory function that returns the options object
10+
- `inject`: an array of providers to inject into the factory function
11+
- `imports`: an array of modules to import
12+
- `extraProviders`: an array of additional providers to add to the module
13+
14+
### Setting the global dispatcher
15+
16+
The `HttpModule` uses the `dispatcher` option of the `request()` method to specify the [Dispatcher](https://github.com/nodejs/undici#dispatcher) that should be used to make the request. By default, the `HttpModule` uses the `createDispatcher()` function from the `@nodejs/undici` package to create

docs/http/http.service.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#### Using the module
2+
3+
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:
4+
5+
```ts
6+
import { HttpService } from 'nestjs-undici';
7+
8+
export class AppComponent {
9+
constructor(private httpService: HttpService) {}
10+
}
11+
```
12+
13+
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.
14+
15+
For example, here is how you could use the `HttpService` to make a GET request to the `/users` endpoint:
16+
17+
```ts
18+
import { of } from 'rxjs';
19+
20+
export class AppComponent {
21+
constructor(private httpService: HttpService) {}
22+
23+
public getUsers(): void {
24+
this.httpService
25+
.request
26+
.get('/users')
27+
.subscribe(response => { // Handle the response here });
28+
}}
29+
```
30+
31+
> You can also use the `post`, `put`, `patch`, `delete`, and `head` methods to send other types of HTTP requests.
32+
33+
### Customizing the request options
34+
35+
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.
36+
37+
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:
38+
39+
```ts
40+
import { of } from 'rxjs';
41+
42+
export class AppComponent {
43+
constructor(private httpService: HttpService) {}
44+
45+
public createUser(user: User): void {
46+
this.httpService
47+
.request('/users', {
48+
method: 'POST',
49+
body: JSON.stringify(user),
50+
headers: {
51+
'Content-Type': 'application/json',
52+
},
53+
})
54+
.subscribe(response => {
55+
// Handle the response here
56+
});
57+
}
58+
}
59+
```

docsify/sidebar.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- Sidebar for Docsify -->
2+
3+
* [**Home**](/ "NestJs Undici")
4+
* API
5+
* [HttpModule](docs/http/http.module.md "NestJs Undici - HttpModule")
6+
* [HttpService](docs/http/http.service.md "NestJs Undici - HttpService")

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './dist';
1+
export * from './lib';

index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>NestJs Undici</title>
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
8+
<meta name="description" content="Undici utilities module based on the @nodejs undici package 🌐">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
10+
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css">
11+
<link rel="icon" type="image/png"
12+
href="https://d33wubrfki0l68.cloudfront.net/e937e774cbbe23635999615ad5d7732decad182a/26072/logo-small.ede75a6b.svg" />
13+
<link rel="icon" type="image/png"
14+
href="https://d33wubrfki0l68.cloudfront.net/e937e774cbbe23635999615ad5d7732decad182a/26072/logo-small.ede75a6b.svg" />
15+
</head>
16+
17+
<body>
18+
<div id="app"></div>
19+
<script>
20+
window.$docsify = {
21+
name: 'NestJs Undici',
22+
repo: 'https://github.com/hebertcisco/nestjs-undici',
23+
loadSidebar: 'docsify/sidebar.md',
24+
auto2top: true,
25+
subMaxLevel: 3,
26+
maxLevel: 3,
27+
themeColor: '#ea2845',
28+
noCompileLinks: [
29+
'benchmarks/.*'
30+
],
31+
relativePath: true
32+
}
33+
</script>
34+
<!-- Docsify v4 -->
35+
<script src="https://cdn.jsdelivr.net/npm/docsify@4"
36+
integrity="sha256-ycM1gQUpS76Yjdc8fQA5WlyWGQ7YtDR68v+cbMEhdLY=">
37+
</script>
38+
</body>
39+
40+
</html>

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ function __export(m) {
33
for (const p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
44
}
55
exports.__esModule = true;
6-
__export(require("./dist"));
6+
__export(require("./lib"));

index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './dist';
1+
export * from './lib';

jest.config.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
2-
transform: {
3-
'^.+\\.(t|j)s$': 'ts-jest',
4-
},
5-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(t|j)s$',
6-
moduleFileExtensions: ['ts', 'js', 'json', 'node']
7-
};
2+
transform: {
3+
'^.+\\.(t|j)s$': 'ts-jest',
4+
},
5+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(t|j)s$',
6+
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
7+
};

0 commit comments

Comments
 (0)