Skip to content

Commit 86c9150

Browse files
Merge pull request #181 from filipedeschamps/brasil-api
Implementa o serviço BrasilAPI
2 parents 0a2d8de + c9a3b52 commit 86c9150

16 files changed

+807
-336
lines changed

.babelrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"presets": ["@babel/preset-env"],
33
"plugins": [
44
[
5-
"babel-plugin-transform-builtin-extend",
6-
{ "globals": ["Error"] }
5+
"@babel/plugin-transform-runtime"
76
]
87
]
98
}

.mocharc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"timeout": 60000,
3+
"require": "@babel/register"
4+
}

dist/cep-promise-browser.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,50 @@
540540
throw serviceError;
541541
}
542542

543+
function fetchBrasilAPIService(cepWithLeftPad) {
544+
var url = "https://brasilapi.com.br/api/cep/v1/".concat(cepWithLeftPad);
545+
var options = {
546+
method: 'GET',
547+
mode: 'cors',
548+
headers: {
549+
'content-type': 'application/json;charset=utf-8'
550+
}
551+
};
552+
return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3);
553+
}
554+
555+
function parseResponse(response) {
556+
if (response.ok === false || response.status !== 200) {
557+
throw new Error('CEP não encontrado na base do BrasilAPI.');
558+
}
559+
560+
return response.json();
561+
}
562+
563+
function extractCepValuesFromResponse$2(response) {
564+
return {
565+
cep: response.cep,
566+
state: response.state,
567+
city: response.city,
568+
neighborhood: response.neighborhood,
569+
street: response.street,
570+
service: 'brasilapi'
571+
};
572+
}
573+
574+
function throwApplicationError$3(error) {
575+
var serviceError = new ServiceError({
576+
message: error.message,
577+
service: 'brasilapi'
578+
});
579+
580+
if (error.name === 'FetchError') {
581+
serviceError.message = 'Erro ao se conectar com o serviço BrasilAPI.';
582+
}
583+
584+
throw serviceError;
585+
}
586+
543587
var PROXY_URL = 'https://proxier.now.sh/api?url=';
544588

545589
/* istanbul ignore next */
@@ -559,6 +603,7 @@
559603
var CorreiosService = isBrowser() ? injectProxy(fetchCorreiosService) : fetchCorreiosService;
560604
var ViaCepService = fetchViaCepService;
561605
var WideNetService = fetchWideNetService;
606+
var BrasilAPIService = fetchBrasilAPIService;
562607

563608
var reverse = function reverse(promise) {
564609
return new Promise(function (resolve, reject) {
@@ -581,11 +626,11 @@
581626
return cepRawValue;
582627
}).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(function (cepWithLeftPad) {
583628
return fetchCepFromServices(cepWithLeftPad, configurations);
584-
})["catch"](handleServicesError)["catch"](throwApplicationError$3);
629+
})["catch"](handleServicesError)["catch"](throwApplicationError$4);
585630
}
586631

587632
function validateProviders(providers) {
588-
var availableProviders = ['correios', 'viacep', 'widenet'];
633+
var availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet'];
589634

590635
if (!Array.isArray(providers)) {
591636
throw new CepPromiseError({
@@ -610,7 +655,7 @@
610655
message: 'Erro ao inicializar a instância do CepPromise.',
611656
type: 'validation_error',
612657
errors: [{
613-
message: "O provider \"".concat(provider, "\" \xE9 inv\xE1lido. Os providers dispon\xEDveis s\xE3o: ").concat(availableProviders.join(", "), "."),
658+
message: "O provider \"".concat(provider, "\" \xE9 inv\xE1lido. Os providers dispon\xEDveis s\xE3o: [\"").concat(availableProviders.join('", "'), "\"]."),
614659
service: 'providers_validation'
615660
}]
616661
});
@@ -667,9 +712,10 @@
667712

668713
function fetchCepFromServices(cepWithLeftPad, configurations) {
669714
var providersServices = {
670-
correios: CorreiosService,
715+
brasilapi: BrasilAPIService,
716+
viacep: ViaCepService,
671717
widenet: WideNetService,
672-
viacep: ViaCepService
718+
correios: CorreiosService
673719
};
674720

675721
if (configurations.providers.length === 0) {
@@ -695,7 +741,7 @@
695741
throw aggregatedErrors;
696742
}
697743

698-
function throwApplicationError$3(_ref) {
744+
function throwApplicationError$4(_ref) {
699745
var message = _ref.message,
700746
type = _ref.type,
701747
errors = _ref.errors;

dist/cep-promise-browser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cep-promise.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,50 @@
491491
throw serviceError;
492492
}
493493

494+
function fetchBrasilAPIService(cepWithLeftPad) {
495+
var url = "https://brasilapi.com.br/api/cep/v1/".concat(cepWithLeftPad);
496+
var options = {
497+
method: 'GET',
498+
mode: 'cors',
499+
headers: {
500+
'content-type': 'application/json;charset=utf-8'
501+
}
502+
};
503+
return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3);
504+
}
505+
506+
function parseResponse(response) {
507+
if (response.ok === false || response.status !== 200) {
508+
throw new Error('CEP não encontrado na base do BrasilAPI.');
509+
}
510+
511+
return response.json();
512+
}
513+
514+
function extractCepValuesFromResponse$2(response) {
515+
return {
516+
cep: response.cep,
517+
state: response.state,
518+
city: response.city,
519+
neighborhood: response.neighborhood,
520+
street: response.street,
521+
service: 'brasilapi'
522+
};
523+
}
524+
525+
function throwApplicationError$3(error) {
526+
var serviceError = new ServiceError({
527+
message: error.message,
528+
service: 'brasilapi'
529+
});
530+
531+
if (error.name === 'FetchError') {
532+
serviceError.message = 'Erro ao se conectar com o serviço BrasilAPI.';
533+
}
534+
535+
throw serviceError;
536+
}
537+
494538
var PROXY_URL = 'https://proxier.now.sh/api?url=';
495539

496540
/* istanbul ignore next */
@@ -510,6 +554,7 @@
510554
var CorreiosService = isBrowser() ? injectProxy(fetchCorreiosService) : fetchCorreiosService;
511555
var ViaCepService = fetchViaCepService;
512556
var WideNetService = fetchWideNetService;
557+
var BrasilAPIService = fetchBrasilAPIService;
513558

514559
var reverse = function reverse(promise) {
515560
return new Promise(function (resolve, reject) {
@@ -532,11 +577,11 @@
532577
return cepRawValue;
533578
}).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(function (cepWithLeftPad) {
534579
return fetchCepFromServices(cepWithLeftPad, configurations);
535-
})["catch"](handleServicesError)["catch"](throwApplicationError$3);
580+
})["catch"](handleServicesError)["catch"](throwApplicationError$4);
536581
}
537582

538583
function validateProviders(providers) {
539-
var availableProviders = ['correios', 'viacep', 'widenet'];
584+
var availableProviders = ['brasilapi', 'correios', 'viacep', 'widenet'];
540585

541586
if (!Array.isArray(providers)) {
542587
throw new CepPromiseError({
@@ -561,7 +606,7 @@
561606
message: 'Erro ao inicializar a instância do CepPromise.',
562607
type: 'validation_error',
563608
errors: [{
564-
message: "O provider \"".concat(provider, "\" \xE9 inv\xE1lido. Os providers dispon\xEDveis s\xE3o: ").concat(availableProviders.join(", "), "."),
609+
message: "O provider \"".concat(provider, "\" \xE9 inv\xE1lido. Os providers dispon\xEDveis s\xE3o: [\"").concat(availableProviders.join('", "'), "\"]."),
565610
service: 'providers_validation'
566611
}]
567612
});
@@ -618,9 +663,10 @@
618663

619664
function fetchCepFromServices(cepWithLeftPad, configurations) {
620665
var providersServices = {
621-
correios: CorreiosService,
666+
brasilapi: BrasilAPIService,
667+
viacep: ViaCepService,
622668
widenet: WideNetService,
623-
viacep: ViaCepService
669+
correios: CorreiosService
624670
};
625671

626672
if (configurations.providers.length === 0) {
@@ -646,7 +692,7 @@
646692
throw aggregatedErrors;
647693
}
648694

649-
function throwApplicationError$3(_ref) {
695+
function throwApplicationError$4(_ref) {
650696
var message = _ref.message,
651697
type = _ref.type,
652698
errors = _ref.errors;

dist/cep-promise.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)