A NestJS library that provides a wrapper around the `axios` and `@nestjs/axios` libraries for making HTTP requests.
@tdi-mc/http
is a library used for making HTTP requests, based on NestJS. It is a wrapper for axios
and @nestjs/axios
, providing an easy-to-use interface for creating HTTP clients with NestJS.
To install this library, use either npm
or yarn
:
npm install -S @tdi-mc/core @tdi-mc/http
# or
yarn add -S @tdi-mc/core @tdi-mc/http
@tdi-mc/http
uses a configuration file to set up the default options for the HTTP client. Create a config.yml file in the root of your project, and add the following configuration options:
http:
baseURL: https://mydomain.com
method: GET
timeout: 30000
auth:
username: myuser
password: mypass
raxConfig:
retry: 3
Update the values according to your HTTP client details. The raxConfig option is using the retry-axios package to automatically retry failed requests.
Once you have provided the configuration, you can import the MongoModule
in your AppModule
:
import { CoreModule, Module } from '@tdi-mc/core';
import { HttpModule } from '@tdi-mc/http';
@Module({
imports: [CoreModule, HttpModule],
})
export class AppModule {}
You can then use the HttpService and do any request:
import { Injectable } from '@tdi-mc/core';
import { HttpService, Client, HttpResponse, HttpFormData } from '@tdi-mc/http';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class MyService {
constructor(private readonly httpService: HttpService) {}
async request(): Promise<HttpResponse<T>> {
return firstValueFrom(this.httpService.request<T>({
// ... input config
}));
}
async upload(): Observable<HttpResponse<T>> {
const data: HttpFormData = new HttpFormData();
return firstValueFrom(this.httpService.upload<T>({
// ... input config
}, data));
}
}
Contributions to @tdi-mc/http
are welcome. If you would like to contribute, please fork the repository, make your changes, and submit a pull request.
Please make sure to update tests as appropriate.