diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..c3aceb3 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,38 @@ +name: Publish on Release + +on: + release: + types: [created] + +jobs: + publish: + name: Publish Package to npmjs.org + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup NodeJS + uses: actions/setup-node@v3 + with: + node-version: 18.18.0 + + - name: Setup Registry + shell: bash + run: | + echo //registry.npmjs.org/:_authToken=${{ secrets.MITZ_IT_NPM_TOKEN }} >> ~/.npmrc | + echo @mitz-it:registry=https://registry.npmjs.org/ >> ~/.npmrc + + - name: Install + shell: bash + run: npm install + + - name: Build + shell: bash + run: npm run build + + - name: Publish + shell: bash + run: npm publish --access public diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..b850a2a --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,27 @@ +name: Pull Request + +on: + pull_request: + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup NodeJS + uses: actions/setup-node@v3 + with: + node-version: 18.18.0 + + - name: Install + shell: bash + run: npm install + + - name: Build + shell: bash + run: npm run build diff --git a/.gitignore b/.gitignore index da0ca36..e153ba9 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ lerna-debug.log* !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json + +# ASDF +.tool-versions \ No newline at end of file diff --git a/README.md b/README.md index e01e1e2..2d0721f 100644 --- a/README.md +++ b/README.md @@ -9,16 +9,26 @@ In the module I must import the `HttpModule` and configure the clients, example ```ts import { Module } from '@nestjs/common'; -import { HttpModule } from 'nestjs-http-package'; +import { HttpModule } from '@mitz-it/nestjs-http'; 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' }, + HttpModule.register([ + { + name: 'brasilApi', + config: { + baseURL: 'https://brasilapi.com.br/api/cnpj/v1', + }, + }, + { + name: 'viaCep', + config: { + baseURL: 'https://viacep.com.br/ws', + }, + }, ]), ], providers: [ @@ -40,17 +50,17 @@ 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 { HttpClientFactory } from '@mitz-it/nestjs-http'; import { CompanyEntity, CompanyRepository } from '@/company/domain'; @Injectable() export class HttpCompanyRepository implements CompanyRepository { - constructor(@Inject(HttpService) private readonly httpService: HttpService) {} + constructor(private readonly factory: HttpClientFactory) {} async findCompany(document: string): Promise { try { - const client = this.httpService.getClient('brasilApi'); + const client = this.factory.createClient('brasilApi'); const response = await client.get(`/${document}`); return response.data; @@ -61,7 +71,7 @@ export class HttpCompanyRepository implements CompanyRepository { async findAddress(cep: string): Promise { try { - const client = this.httpService.getClient('viaCep'); + const client = this.factory.createClient('viaCep'); const response = await client.get(`/${cep}/json`); return response.data; diff --git a/package-lock.json b/package-lock.json index 0d72fb8..891d068 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,7 @@ "": { "name": "nest-http", "version": "1.0.0", - "license": "UNLICENSED", + "license": "MIT", "dependencies": { "@nestjs/common": "^10.0.0", "axios": "^1.5.0" diff --git a/package.json b/package.json index 796dd41..9669824 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "nest-http", + "name": "@mitz-it/nest-http", "version": "1.0.0", "description": "", "author": "", @@ -53,4 +53,4 @@ "coverageDirectory": "../coverage", "testEnvironment": "node" } -} +} \ No newline at end of file diff --git a/src/http-factory.service.ts b/src/http-factory.service.ts new file mode 100644 index 0000000..23abeb1 --- /dev/null +++ b/src/http-factory.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@nestjs/common'; +import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios'; + +@Injectable() +export class HttpClientFactory { + private readonly clients: { [key: string]: AxiosInstance } = {}; + private readonly configs: { + name: string; + config: CreateAxiosDefaults; + }[] = []; + + constructor(configs: { name: string; config: CreateAxiosDefaults }[]) { + this.configs = configs; + } + + createClient(name?: string): AxiosInstance { + if (!name) { + return axios.create(); + } + + const client = this.clients[name]; + const config = this.configs.find((c) => c.name === name)?.config ?? {}; + + if (!client) { + this.clients[name] = axios.create(config); + return this.clients[name]; + } + + return client; + } +} diff --git a/src/http.module.ts b/src/http.module.ts index 490ee55..469ee99 100644 --- a/src/http.module.ts +++ b/src/http.module.ts @@ -1,19 +1,22 @@ import { Module, DynamicModule } from '@nestjs/common'; -import { HttpService } from './http.service'; +import { CreateAxiosDefaults } from 'axios'; +import { HttpClientFactory } from './http-factory.service'; @Module({}) export class HttpModule { - static forRoot(urls: { name: string; value: string }[]): DynamicModule { - const httpService = new HttpService(urls); + static register( + configs: { name: string; config: CreateAxiosDefaults }[], + ): DynamicModule { + const factory = new HttpClientFactory(configs); return { module: HttpModule, providers: [ { - provide: HttpService, - useValue: httpService, + provide: HttpClientFactory, + useValue: factory, }, ], - exports: [HttpService], + exports: [HttpClientFactory], }; } } diff --git a/src/http.service.ts b/src/http.service.ts deleted file mode 100644 index f3f5dc8..0000000 --- a/src/http.service.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import axios, { AxiosInstance } from 'axios'; - -@Injectable() -export class HttpService { - private readonly httpClients: { [key: string]: AxiosInstance } = {}; - - constructor(private readonly urls: { name: string; value: string }[]) { - urls.forEach((url) => { - this.httpClients[url.name] = axios.create({ - baseURL: url.value, - }); - }); - } - - getClient(name: string = this.urls[0].name): AxiosInstance { - if (!this.httpClients[name]) { - throw new Error(`Base URL ${name} not configured.`); - } - return this.httpClients[name]; - } -} diff --git a/src/index.ts b/src/index.ts index c13030e..f6c9d12 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ export * from './http.module'; -export * from './http.service'; +export * from './http-factory.service';