Skip to content

Commit

Permalink
via-cep: user-agent cep-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Correa committed Sep 27, 2020
1 parent 83a943d commit a554a75
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/services/viacep.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fetch from 'node-fetch'
import ServiceError from '../errors/service.js'

export default function fetchViaCepService (cepWithLeftPad, proxyURL = '') {
export default function fetchViaCepService(cepWithLeftPad, proxyURL = '') {
const url = `${proxyURL}https://viacep.com.br/ws/${cepWithLeftPad}/json/`
const options = {
method: 'GET',
Expand All @@ -14,7 +14,7 @@ export default function fetchViaCepService (cepWithLeftPad, proxyURL = '') {
}

if (typeof window == 'undefined') {
options.headers['user-agent'] = ''
options.headers['user-agent'] = 'cep-promise'
}

return fetch(url, options)
Expand All @@ -24,23 +24,23 @@ export default function fetchViaCepService (cepWithLeftPad, proxyURL = '') {
.catch(throwApplicationError)
}

function analyzeAndParseResponse (response) {
function analyzeAndParseResponse(response) {
if (response.ok) {
return response.json()
}

throw Error('Erro ao se conectar com o serviço ViaCEP.')
}

function checkForViaCepError (responseObject) {
function checkForViaCepError(responseObject) {
if (responseObject.erro === true) {
throw new Error('CEP não encontrado na base do ViaCEP.')
}

return responseObject
}

function extractCepValuesFromResponse (responseObject) {
function extractCepValuesFromResponse(responseObject) {
return {
cep: responseObject.cep.replace('-', ''),
state: responseObject.uf,
Expand All @@ -51,7 +51,7 @@ function extractCepValuesFromResponse (responseObject) {
}
}

function throwApplicationError (error) {
function throwApplicationError(error) {
const serviceError = new ServiceError({
message: error.message,
service: 'viacep'
Expand Down

0 comments on commit a554a75

Please sign in to comment.