|
| 1 | + |
| 2 | +[](https://badge.fury.io/js/%40nodeflip%2Fnestjs-axios-http) |
| 3 | +[](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