Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General Enchancements #1

Merged
merged 6 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ lerna-debug.log*
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# ASDF
.tool-versions
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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<any> {
try {
const client = this.httpService.getClient('brasilApi');
const client = this.factory.createClient('brasilApi');
const response = await client.get(`/${document}`);

return response.data;
Expand All @@ -61,7 +71,7 @@ export class HttpCompanyRepository implements CompanyRepository {

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

return response.data;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nest-http",
"name": "@mitz-it/nest-http",
"version": "1.0.0",
"description": "",
"author": "",
Expand Down Expand Up @@ -53,4 +53,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
31 changes: 31 additions & 0 deletions src/http-factory.service.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
}[] = [];

constructor(configs: { name: string; config: CreateAxiosDefaults<any> }[]) {
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;
}
}
15 changes: 9 additions & 6 deletions src/http.module.ts
Original file line number Diff line number Diff line change
@@ -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<any> }[],
): DynamicModule {
const factory = new HttpClientFactory(configs);
return {
module: HttpModule,
providers: [
{
provide: HttpService,
useValue: httpService,
provide: HttpClientFactory,
useValue: factory,
},
],
exports: [HttpService],
exports: [HttpClientFactory],
};
}
}
22 changes: 0 additions & 22 deletions src/http.service.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './http.module';
export * from './http.service';
export * from './http-factory.service';