# nestjs-http-package

> You must import it into your module and enter the name and value of your base url as configuration. It will create the client instance for your bases url (Currently it only has the axios client)

Latest version **1.0.0** (published 2023-10-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-http-package
pnpm add nestjs-http-package
yarn add nestjs-http-package
bun add nestjs-http-package
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-10-07 |
| First published | 2023-10-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 182.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | andrewronscki |

## Links

- npm: https://www.npmjs.com/package/nestjs-http-package
- npm.io page: https://npm.io/package/nestjs-http-package

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^1.5.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.0.0

## Recent versions

- 1.0.0 (latest) — 2023-10-07
- 0.0.5 — 2023-10-07
- 0.0.4 — 2023-10-07
- 0.0.3 — 2023-10-07
- 0.0.2 — 2023-10-07
- 0.0.1 — 2023-10-06

## README

# How to use

You must import it into your module and enter the name and value of your base url as configuration. It will create the client instance for your bases url (Currently it only has the axios client)

## Example

In the module I must import the `HttpModule` and configure the clients, example of use:

```ts
import { Module } from '@nestjs/common';

import { HttpModule } from 'nestjs-http-package';

import { HttpCompanyRepository } from '../infra';
import { CompanyRepositoryToken } from '../domain';

@Module({
  imports: [
    HttpModule.forRoot([
      { name: 'brasilApi', value: 'https://brasilapi.com.br/api/cnpj/v1' },
      { name: 'viaCep', value: 'https://viacep.com.br/ws' },
    ]),
  ],
  providers: [
    HttpCompanyRepository,
    {
      provide: CompanyRepositoryToken,
      useExisting: HttpCompanyRepository,
    },
  ],
  exports: [CompanyRepositoryToken],
})
export class HttpCompanyInfraModule {}
```

I generated two clients, one pointing to BrasilApi and the other to ViaCep. One to search for information about a Brazilian company, and another to search for address information based on zip code.

In my repository, I need to inject the `HttpService` and use it normally:

```ts
import { Inject, Injectable } from '@nestjs/common';

import { HttpService } from 'nestjs-http-package';

import { CompanyEntity, CompanyRepository } from '@/company/domain';

@Injectable()
export class HttpCompanyRepository implements CompanyRepository {
  constructor(@Inject(HttpService) private readonly httpService: HttpService) {}

  async findCompany(document: string): Promise<any> {
    try {
      const client = this.httpService.getClient('brasilApi');
      const response = await client.get(`/${document}`);

      return response.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAddress(cep: string): Promise<any> {
    try {
      const client = this.httpService.getClient('viaCep');
      const response = await client.get(`/${cep}/json`);

      return response.data;
    } catch (error) {
      throw new Error(error);
    }
  }
}
```

In the `findCompany` method, you searched for the client configured for `brasilApi` and in the `findAddress` method, you searched for the client configured for `viaCep`, making your respective requests.

---
_Source: https://npm.io/package/nestjs-http-package · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
