Skip to content

Commit 027f776

Browse files
Ashish KumarAshish Kumar
authored andcommitted
chore: setting up the package
0 parents  commit 027f776

17 files changed

+9876
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/dist
3+
npm-debug.log

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Exclude test files from src
2+
/src/**/*.spec.ts
3+
/src/**/*.test.ts
4+
5+
# Exclude test files from dist
6+
/dist/test
7+
src
8+
jest.config.js

CONTRIBUTING.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Contributing to `@nodeflip/nest-axios-http`
2+
3+
Welcome to `@nodeflip/nest-axios-http`! We're excited that you're interested in contributing. This guide outlines how you can get involved and contribute to our project.
4+
5+
## Table of Contents
6+
7+
- [Code of Conduct](#code-of-conduct)
8+
- [Reporting Issues](#reporting-issues)
9+
- [Feature Requests](#feature-requests)
10+
- [Contributing Code](#contributing-code)
11+
- [Setting Up Your Development Environment](#setting-up-your-development-environment)
12+
- [Making Changes](#making-changes)
13+
- [Submitting Pull Requests](#submitting-pull-requests)
14+
- [Code Guidelines](#code-guidelines)
15+
- [Community](#community)
16+
17+
## Code of Conduct
18+
19+
We enforce a Code of Conduct in all interactions within our community. Please familiarize yourself with it before participating: [Code of Conduct](CODE_OF_CONDUCT.md).
20+
21+
## Reporting Issues
22+
23+
If you encounter a bug, inconsistency, or have a feature request, please check the [issue tracker](https://github.com/nodeflip/nest-axios-http/issues) to see if it has already been reported or discussed. If not, feel free to [create a new issue](https://github.com/nodeflip/nest-axios-http/issues/new).
24+
25+
## Feature Requests
26+
27+
We welcome suggestions for new features or improvements. Please ensure your feature request is clear and provides sufficient context. You can submit your ideas via [GitHub Issues](https://github.com/nodeflip/nest-axios-http/issues/new).
28+
29+
## Contributing Code
30+
31+
### Setting Up Your Development Environment
32+
33+
To start contributing to `@nodeflip/nest-axios-http`, follow these steps:
34+
35+
1. **Fork the repository**: Click on the "Fork" button on the top right corner of the repository page.
36+
37+
2. **Clone your fork**: Clone the repository to your local machine.
38+
39+
```bash
40+
git clone https://github.com/nodeflip/nest-axios-http.git
41+
cd nest-axios-http
42+
```
43+
44+
3. **Install dependencies**: Install the necessary dependencies.
45+
46+
```bash
47+
npm install
48+
```
49+
50+
### Making Changes
51+
52+
- Create a new branch for your feature or bug fix.
53+
54+
```bash
55+
git checkout -b feature-or-bugfix-name
56+
```
57+
58+
- Make your changes, following the [code guidelines](#code-guidelines).
59+
60+
### Submitting Pull Requests
61+
62+
1. **Commit your changes**:
63+
64+
```bash
65+
git commit -am 'Add new feature or fix issue #123'
66+
```
67+
68+
2. **Push to your fork**:
69+
70+
```bash
71+
git push origin feature-or-bugfix-name
72+
```
73+
74+
3. **Submit a Pull Request**:
75+
76+
- Go to the [Pull Requests](https://github.com/nodeflip/nest-axios-http/pulls) tab of the original repository.
77+
- Click on "New Pull Request".
78+
- Provide a clear title and description for your pull request, explaining the purpose of your changes.
79+
80+
## Code Guidelines
81+
82+
- Follow TypeScript and NestJS best practices.
83+
- Maintain consistent coding style and adhere to existing conventions.
84+
- Write clear commit messages and comments.
85+
86+
## Community
87+
88+
Join our community to connect with other contributors and users:
89+
90+
- [GitHub Discussions](https://github.com/nodeflip/nest-axios-http/discussions)
91+
92+
Thank you for contributing to `@nodeflip/nest-axios-http`! Your involvement helps improve our project and supports the community of NestJS developers. We look forward to reviewing your contributions!

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Ashish Kumar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
![test workflow](https://github.com/nodeflip/nest-axios-http/actions/workflows/test.yml/badge.svg)
2+
[![npm version](https://badge.fury.io/js/%40nodeflip%2Fnest-axios-http.svg)](https://badge.fury.io/js/%40nodeflip%2Fnestjs-axios-http)
3+
[![downloads](https://img.shields.io/npm/dm/@nodeflip/nest-axios-http.svg)](https://www.npmjs.com/package/@nodeflip/nestjs-axios-http)
4+
5+
# @nodeflip/nest-axios-http
6+
7+
## Description
8+
9+
`@nodeflip/nest-axios-http` is a NestJS module that simplifies HTTP requests using Axios within NestJS applications. It provides an easy-to-use interface for making HTTP calls while integrating seamlessly with NestJS dependency injection and module system.
10+
11+
## Features
12+
13+
- **Simplified HTTP Requests:** Easily make HTTP requests with Axios using a service-oriented approach.
14+
- **Dependency Injection:** Utilize NestJS's powerful dependency injection system to manage HTTP service instances.
15+
- **Customization Options:** Customize Axios instance configuration, logging, request/response interceptors, and error handling.
16+
- **Dynamic Module Configuration:** Dynamically configure HTTP services based on module options using `forRoot`, `forFeature`, and `forFeatureWithProvider` methods.
17+
18+
## Benefits
19+
20+
- **Integration with NestJS:** Seamlessly integrates with NestJS architecture, making it easy to manage HTTP services as injectable dependencies.
21+
- **Modular Design:** Allows for dynamic module configuration, enabling multiple HTTP service instances with different configurations within a single NestJS application.
22+
- **Logging and Interceptors:** Built-in support for custom logging and request/response interceptors, providing flexibility in handling HTTP interactions.
23+
- **Named Services:** Supports injecting named HTTP services using `serviceName` for different configurations or service instances within the same application.
24+
25+
## Installation
26+
27+
```bash
28+
npm install @nodeflip/nest-axios-http
29+
```
30+
31+
## Usage
32+
33+
### Importing the Module
34+
35+
Import `HttpModule` into your NestJS application's module (`AppModule`, for example).
36+
37+
```typescript
38+
import { Module } from '@nestjs/common';
39+
import { HttpModule } from '@nodeflip/nest-axios-http';
40+
41+
@Module({
42+
imports: [
43+
HttpModule.forRoot({
44+
config: {
45+
baseURL: 'https://api.example.com',
46+
enableLogging: true,
47+
onRequest: (config) => {
48+
// Customize request logging or modifications
49+
return config;
50+
},
51+
onResponse: (response) => {
52+
// Customize response logging or modifications
53+
return response;
54+
},
55+
onError: (error) => {
56+
// Customize error handling or logging
57+
return Promise.reject(error);
58+
},
59+
},
60+
}),
61+
],
62+
})
63+
export class AppModule {}
64+
```
65+
66+
### Using HTTP Service
67+
68+
Inject `HttpService` into your NestJS components or services to make HTTP requests.
69+
70+
```typescript
71+
import { Injectable } from '@nestjs/common';
72+
import { HttpService } from '@nodeflip/nest-axios-http';
73+
import { AxiosResponse } from 'axios';
74+
75+
@Injectable()
76+
export class MyService {
77+
constructor(private readonly httpService: HttpService) {}
78+
79+
async fetchData(): Promise<AxiosResponse<any>> {
80+
return this.httpService.get('/data');
81+
}
82+
83+
async postData(data: any): Promise<AxiosResponse<any>> {
84+
return this.httpService.post('/data', data);
85+
}
86+
}
87+
```
88+
89+
### Injecting Named HTTP Services
90+
91+
You can inject named HTTP services using the `serviceName` option and the `@Inject` decorator.
92+
93+
```typescript
94+
import { Module } from '@nestjs/common';
95+
import { HttpModule } from '@nodeflip/nest-axios-http';
96+
97+
@Module({
98+
imports: [
99+
HttpModule.forRoot({
100+
serviceName: 'CustomHttpService',
101+
config: {
102+
baseURL: 'https://api.example.com',
103+
enableLogging: true,
104+
onRequest: (config) => {
105+
// Customize request logging or modifications
106+
return config;
107+
},
108+
onResponse: (response) => {
109+
// Customize response logging or modifications
110+
return response;
111+
},
112+
onError: (error) => {
113+
// Customize error handling or logging
114+
return Promise.reject(error);
115+
},
116+
},
117+
}),
118+
],
119+
})
120+
export class AppModule {}
121+
```
122+
123+
```typescript
124+
import { Injectable, Inject } from '@nestjs/common';
125+
import { HttpService } from '@nodeflip/nest-axios-http';
126+
import { AxiosResponse } from 'axios';
127+
128+
@Injectable()
129+
export class AnotherService {
130+
constructor(
131+
@Inject('CustomHttpService') private readonly customHttpService: HttpService,
132+
@Inject(HttpService) private readonly defaultHttpService: HttpService,
133+
) {}
134+
135+
async fetchDataFromCustomService(): Promise<AxiosResponse<any>> {
136+
return this.customHttpService.get('/custom-data');
137+
}
138+
139+
async fetchDataFromDefaultService(): Promise<AxiosResponse<any>> {
140+
return this.defaultHttpService.get('/default-data');
141+
}
142+
}
143+
```
144+
145+
### Custom Logging and Interceptors
146+
147+
You can customize logging and request/response interceptors by providing `logger` and `config` options during module initialization.
148+
149+
```typescript
150+
import { Logger } from '@nestjs/common';
151+
import { Module } from '@nestjs/common';
152+
import { HttpModule } from '@nodeflip/nest-axios-http';
153+
154+
const customLogger = new Logger('CustomLogger');
155+
156+
@Module({
157+
imports: [
158+
HttpModule.forRoot({
159+
logger: customLogger, // Custom logger instance
160+
config: {
161+
baseURL: 'https://api.example.com',
162+
enableLogging: true,
163+
onRequest: (config) => {
164+
// Customize request logging or modifications
165+
return config;
166+
},
167+
onResponse: (response) => {
168+
// Customize response logging or modifications
169+
return response;
170+
},
171+
onError: (error) => {
172+
// Customize error handling or logging
173+
return Promise.reject(error);
174+
},
175+
},
176+
}),
177+
],
178+
})
179+
export class AppModule {}
180+
```
181+
### Configuring Multiple HTTP Services
182+
183+
You can configure multiple HTTP services using an array of options with forFeature.
184+
```typescript
185+
import { Module } from '@nestjs/common';
186+
import { HttpModule } from '@nodeflip/nest-axios-http';
187+
188+
@Module({
189+
imports: [
190+
HttpModule.forFeature([
191+
{
192+
serviceName: "HTTP_SERVICE_2",
193+
config: {
194+
baseURL: 'https://api.service1.com',
195+
enableLogging: true,
196+
onRequest: (config) => {
197+
// Optional: Customize request logging or modifications
198+
return config;
199+
},
200+
onResponse: (response) => {
201+
// Optional: Customize response logging or modifications
202+
return response;
203+
},
204+
onError: (error) => {
205+
// Optional: Customize error handling or logging
206+
return Promise.reject(error);
207+
},
208+
},
209+
},
210+
{
211+
serviceName: "HTTP_SERVICE_2",
212+
config: {
213+
baseURL: 'https://api.service2.com',
214+
enableLogging: true,
215+
onRequest: (config) => {
216+
// Optional: Customize request logging or modifications
217+
return config;
218+
},
219+
onResponse: (response) => {
220+
// Optional: Customize response logging or modifications
221+
return response;
222+
},
223+
onError: (error) => {
224+
// Optional: Customize error handling or logging
225+
return Promise.reject(error);
226+
},
227+
},
228+
},
229+
]),
230+
],
231+
})
232+
export class AppModule {}
233+
234+
```
235+
## License
236+
237+
This package is [MIT licensed](LICENSE).
238+
239+
## Issues
240+
241+
For any issues, bugs, or feature requests, please [file an issue](https://github.com/nodeflip/nest-axios-http/issues) on GitHub.
242+
243+
## Repository
244+
245+
Find this package on [npm](https://npmjs.com/nodeflip/nest-axios-http).

0 commit comments

Comments
 (0)