@tdi-mc/http
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

@tdi-mc/http

A NestJS library that provides a wrapper around the `axios` and `@nestjs/axios` libraries for making HTTP requests.

Table of Contents

  1. Introduction
  2. Installation
  3. Getting Started
  4. Reference
  5. Contributing

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

axios

@nestjs/axios

retry-axios

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.

Readme

Keywords

Package Sidebar

Install

npm i @tdi-mc/http

Weekly Downloads

7

Version

0.0.7

License

MIT

Unpacked Size

301 kB

Total Files

44

Last publish

Collaborators

  • duane.le
  • trangiabao1203
  • brian.tran