|
434 | 434 | throw serviceError;
|
435 | 435 | }
|
436 | 436 |
|
| 437 | + function fetchCorreiosAltAPIService(cepWithLeftPad, configurations) { |
| 438 | + var url = 'https://buscacepinter.correios.com.br/app/cep/carrega-cep.php'; |
| 439 | + var options = { |
| 440 | + method: 'POST', |
| 441 | + mode: 'cors', |
| 442 | + headers: { |
| 443 | + 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' |
| 444 | + }, |
| 445 | + body: "cep=".concat(cepWithLeftPad), |
| 446 | + timeout: configurations.timeout || 30000 |
| 447 | + }; |
| 448 | + return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse)["catch"](throwApplicationError$1); |
| 449 | + } |
| 450 | + |
| 451 | + function parseResponse(response) { |
| 452 | + return response.json().then(function (result) { |
| 453 | + if (result.total === 0 || result.erro || result.dados[0].cep === "") { |
| 454 | + throw new Error('CEP não encontrado na base dos Correios.'); |
| 455 | + } |
| 456 | + |
| 457 | + return result; |
| 458 | + }); |
| 459 | + } |
| 460 | + |
| 461 | + function extractCepValuesFromResponse(response) { |
| 462 | + var firstCep = response.dados[0]; |
| 463 | + return { |
| 464 | + cep: firstCep.cep, |
| 465 | + state: firstCep.uf, |
| 466 | + city: firstCep.localidade, |
| 467 | + neighborhood: firstCep.bairro, |
| 468 | + street: firstCep.logradouroDNEC, |
| 469 | + service: 'correios-alt' |
| 470 | + }; |
| 471 | + } |
| 472 | + |
| 473 | + function throwApplicationError$1(error) { |
| 474 | + var serviceError = new ServiceError({ |
| 475 | + message: error.message, |
| 476 | + service: 'correios-alt' |
| 477 | + }); |
| 478 | + |
| 479 | + if (error.name === 'FetchError') { |
| 480 | + serviceError.message = 'Erro ao se conectar com o serviço dos Correios Alt.'; |
| 481 | + } |
| 482 | + |
| 483 | + throw serviceError; |
| 484 | + } |
| 485 | + |
437 | 486 | function fetchViaCepService(cepWithLeftPad, configurations) {
|
438 | 487 | var url = "https://viacep.com.br/ws/".concat(cepWithLeftPad, "/json/");
|
439 | 488 | var options = {
|
|
449 | 498 | options.headers['user-agent'] = 'cep-promise';
|
450 | 499 | }
|
451 | 500 |
|
452 |
| - return fetch(url, options).then(analyzeAndParseResponse$1).then(checkForViaCepError).then(extractCepValuesFromResponse)["catch"](throwApplicationError$1); |
| 501 | + return fetch(url, options).then(analyzeAndParseResponse$1).then(checkForViaCepError).then(extractCepValuesFromResponse$1)["catch"](throwApplicationError$2); |
453 | 502 | }
|
454 | 503 |
|
455 | 504 | function analyzeAndParseResponse$1(response) {
|
|
468 | 517 | return responseObject;
|
469 | 518 | }
|
470 | 519 |
|
471 |
| - function extractCepValuesFromResponse(responseObject) { |
| 520 | + function extractCepValuesFromResponse$1(responseObject) { |
472 | 521 | return {
|
473 | 522 | cep: responseObject.cep.replace('-', ''),
|
474 | 523 | state: responseObject.uf,
|
|
479 | 528 | };
|
480 | 529 | }
|
481 | 530 |
|
482 |
| - function throwApplicationError$1(error) { |
| 531 | + function throwApplicationError$2(error) { |
483 | 532 | var serviceError = new ServiceError({
|
484 | 533 | message: error.message,
|
485 | 534 | service: 'viacep'
|
|
502 | 551 | },
|
503 | 552 | timeout: configurations.timeout || 30000
|
504 | 553 | };
|
505 |
| - return fetch(url, options).then(analyzeAndParseResponse$2).then(checkForWideNetError).then(extractCepValuesFromResponse$1)["catch"](throwApplicationError$2); |
| 554 | + return fetch(url, options).then(analyzeAndParseResponse$2).then(checkForWideNetError).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3); |
506 | 555 | }
|
507 | 556 |
|
508 | 557 | function analyzeAndParseResponse$2(response) {
|
|
521 | 570 | return object;
|
522 | 571 | }
|
523 | 572 |
|
524 |
| - function extractCepValuesFromResponse$1(object) { |
| 573 | + function extractCepValuesFromResponse$2(object) { |
525 | 574 | return {
|
526 | 575 | cep: object.code.replace('-', ''),
|
527 | 576 | state: object.state,
|
|
532 | 581 | };
|
533 | 582 | }
|
534 | 583 |
|
535 |
| - function throwApplicationError$2(error) { |
| 584 | + function throwApplicationError$3(error) { |
536 | 585 | var serviceError = new ServiceError({
|
537 | 586 | message: error.message,
|
538 | 587 | service: 'widenet'
|
|
555 | 604 | },
|
556 | 605 | timeout: configurations.timeout || 30000
|
557 | 606 | };
|
558 |
| - return fetch(url, options).then(parseResponse).then(extractCepValuesFromResponse$2)["catch"](throwApplicationError$3); |
| 607 | + return fetch(url, options).then(parseResponse$1).then(extractCepValuesFromResponse$3)["catch"](throwApplicationError$4); |
559 | 608 | }
|
560 | 609 |
|
561 |
| - function parseResponse(response) { |
| 610 | + function parseResponse$1(response) { |
562 | 611 | if (response.ok === false || response.status !== 200) {
|
563 | 612 | throw new Error('CEP não encontrado na base do BrasilAPI.');
|
564 | 613 | }
|
565 | 614 |
|
566 | 615 | return response.json();
|
567 | 616 | }
|
568 | 617 |
|
569 |
| - function extractCepValuesFromResponse$2(response) { |
| 618 | + function extractCepValuesFromResponse$3(response) { |
570 | 619 | return {
|
571 | 620 | cep: response.cep,
|
572 | 621 | state: response.state,
|
|
577 | 626 | };
|
578 | 627 | }
|
579 | 628 |
|
580 |
| - function throwApplicationError$3(error) { |
| 629 | + function throwApplicationError$4(error) { |
581 | 630 | var serviceError = new ServiceError({
|
582 | 631 | message: error.message,
|
583 | 632 | service: 'brasilapi'
|
|
603 | 652 |
|
604 | 653 | return {
|
605 | 654 | correios: fetchCorreiosService,
|
| 655 | + 'correios-alt': fetchCorreiosAltAPIService, |
606 | 656 | viacep: fetchViaCepService,
|
607 | 657 | widenet: fetchWideNetService,
|
608 | 658 | brasilapi: fetchBrasilAPIService
|
|
630 | 680 | return cepRawValue;
|
631 | 681 | }).then(removeSpecialCharacters).then(validateInputLength).then(leftPadWithZeros).then(function (cepWithLeftPad) {
|
632 | 682 | return fetchCepFromServices(cepWithLeftPad, configurations);
|
633 |
| - })["catch"](handleServicesError)["catch"](throwApplicationError$4); |
| 683 | + })["catch"](handleServicesError)["catch"](throwApplicationError$5); |
634 | 684 | }
|
635 | 685 |
|
636 | 686 | function validateProviders(providers) {
|
|
738 | 788 | throw aggregatedErrors;
|
739 | 789 | }
|
740 | 790 |
|
741 |
| - function throwApplicationError$4(_ref) { |
| 791 | + function throwApplicationError$5(_ref) { |
742 | 792 | var message = _ref.message,
|
743 | 793 | type = _ref.type,
|
744 | 794 | errors = _ref.errors;
|
|
0 commit comments