UNPKG

viacep

Version:

Procurando um webservice gratuito e de alto desempenho para consultar Códigos de Endereçamento Postal (CEP) do Brasil? Utilize o serviço, melhore a qualidade de suas aplicações web e colabore para manter esta base de dados atualizada.

3 lines (2 loc) 1.17 kB
import t,{Response as e}from"node-fetch";var r=function(t){this.status=t.status,this.statusText=t.statusText},n=function(){function n(){}return n.prototype.makeApiRequest=function(n){try{return Promise.resolve(function(r,o){try{var s=Promise.resolve(t("https://viacep.com.br/ws/"+n+"/json",{headers:{"Content-Type":"application/json; charset=utf-8"}})).then(function(t){if(!t.ok)throw t;return Promise.resolve(t.json()).then(function(t){if(t.erro)throw new e(null,{status:404,statusText:"A pesquisa não retornou dados"});return t})})}catch(t){return o(t)}return s&&s.then?s.then(void 0,o):s}(0,function(t){switch(t.status){case 400:case 404:throw new r(t);default:throw t}}))}catch(t){return Promise.reject(t)}},n}(),o=function(){function t(){this.request=new n}return t.prototype.cep=function(t){try{var e,r=this,n=function(n){return e?n:Promise.resolve(r.request.makeApiRequest(encodeURI(t.state+"/"+t.city+"/"+t.street)))},o=function(){if("string"==typeof t)return e=1,Promise.resolve(r.request.makeApiRequest(""+t))}();return Promise.resolve(o&&o.then?o.then(n):n(o))}catch(t){return Promise.reject(t)}},t}();export{o as ViaCEP}; //# sourceMappingURL=index.m.js.map