@tdi-mc/http
v0.0.7
Published
TDI MC - Http Client Service
Downloads
50
Readme
Table of Contents
Introduction
@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.
Installation
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
Getting Started
Configuration
@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.
Module
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 {}
Service
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));
}
}
Reference
Contributing
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.