-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(address): implementation of address search with viacep
- Loading branch information
Lucas Eduardo
committed
Nov 22, 2020
1 parent
0223668
commit 09339cb
Showing
12 changed files
with
129 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface IAddressDto { | ||
cep: number; | ||
publicPlace: string; | ||
neighborhood: string; | ||
city: string; | ||
state: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface IViaCepDto { | ||
cep: string; | ||
logradouro: string; | ||
complemento: string; | ||
bairro: string; | ||
localidade: string; | ||
uf: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { IAddressDto } from 'modules/Address/dtos/IAddressDto'; | ||
import { IViaCepDto } from 'modules/Address/dtos/IViaCepDto'; | ||
import { IAddressRepository } from 'modules/Address/repositories/IAddressRepository'; | ||
|
||
import { axios } from 'shared/infra/http/HttpAxios'; | ||
|
||
class AxiosRepository implements IAddressRepository { | ||
searchAddress(cep: string): Promise<IAddressDto> { | ||
const url = process.env.REACT_APP_SEARCH_ADDRESS?.replace( | ||
'{CEP}', | ||
cep, | ||
) as string; | ||
|
||
const address: Promise<IAddressDto> = axios | ||
.get<IViaCepDto>(url) | ||
.then(({ data }) => ({ | ||
cep: Number(data.cep.replace('-', '')), | ||
publicPlace: data.logradouro, | ||
neighborhood: data.bairro, | ||
city: data.localidade, | ||
state: data.uf, | ||
})) | ||
.catch(err => { | ||
throw new Error(err); | ||
}); | ||
|
||
return address; | ||
} | ||
} | ||
|
||
export default AxiosRepository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { IAddressDto } from '../dtos/IAddressDto'; | ||
|
||
export interface IAddressRepository { | ||
searchAddress(cep: string): Promise<IAddressDto>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { IAddressDto } from '../dtos/IAddressDto'; | ||
import AxiosRepository from '../infra/http/AxiosRepository'; | ||
import { IAddressRepository } from '../repositories/IAddressRepository'; | ||
|
||
class SearchAddressService { | ||
constructor(private readonly addressRepository: IAddressRepository) {} | ||
|
||
execute(cep: string): Promise<IAddressDto> { | ||
const address = this.addressRepository | ||
.searchAddress(cep) | ||
.then(data => data) | ||
.catch(err => { | ||
throw new Error(err); | ||
}); | ||
|
||
return address; | ||
} | ||
} | ||
|
||
export default new SearchAddressService(new AxiosRepository()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import axios from 'axios'; | ||
|
||
const api = axios.create({ | ||
baseURL: process.env.REACT_APP_API, | ||
}); | ||
|
||
export { api, axios }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters