UNPKG

correios-brasil

Version:

Módulo completo consultar informações sobre o CEP, calcular o preço e os prazos das entregas das encomendas e também realizar o seu rastreio

3 lines (2 loc) 5.43 kB
"use strict";var t=require("axios"),n=require("iconv-lite"),e=require("xml-js"),r=require("crypto");function o(t){var n=new RegExp(/[^0-9]|[/ /]/g,""),e=t.trim().replace(n,"");if(8!==e.length)throw Error("Cep: ".concat(t," inválido!"));return e}var a,c=function(){return c=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t},c.apply(this,arguments)};function i(t,n,e,r){return new(e||(e=Promise))((function(o,a){function c(t){try{u(r.next(t))}catch(t){a(t)}}function i(t){try{u(r.throw(t))}catch(t){a(t)}}function u(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(c,i)}u((r=r.apply(t,n||[])).next())}))}function u(t,n){var e,r,o,a,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(i){return function(u){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;a&&(a=0,i[0]&&(c=0)),c;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(o=c.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){c=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){c.label=i[1];break}if(6===i[0]&&c.label<o[1]){c.label=o[1],o=i;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(i);break}o[2]&&c.ops.pop(),c.trys.pop();continue}i=n.call(t,c)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}function s(n,e){return i(this,void 0,void 0,(function(){return u(this,(function(r){return[2,new Promise((function(r,o){t(c(c({},e),{url:n})).then((function(t){return r(t)})).catch((function(t){o(t)}))}))]}))}))}!function(t){t.BASECEP="https://viacep.com.br/ws",t.BASECORREIOS="http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx",t.BASERASTREIO="https://www.linkcorreios.com.br",t.WIDENET="https://cep.widenet.host/busca-cep/api/cep",t.PROXYAPP_RASTREAR="https://proxyapp.correios.com.br/v1/sro-rastro",t.PROXYAPP_TOKEN="https://proxyapp.correios.com.br/v2/app-validation"}(a||(a={}));var l=a;var p=new Date,f=String(p.getDate()).padStart(2,"0"),d=String(p.getMonth()+1).padStart(2,"0"),h=String(p.getFullYear()),m=String(p.getHours()).padStart(2,"0"),S=String(p.getMinutes()).padStart(2,"0"),v=String(p.getSeconds()).padStart(2,"0"),b="YW5kcm9pZDtici5jb20uY29ycmVpb3MucHJlYXRlbmRpbWVudG87RjMyRTI5OTc2NzA5MzU5ODU5RTBCOTdGNkY4QTQ4M0I5Qjk1MzU3ODs1LjEuMTQ=",P="".concat(f,"/").concat(d,"/").concat(h," ").concat(m,":").concat(S,":").concat(v),g=r.createHash("md5").update("requestToken".concat(b,"data").concat(P)).digest("hex"),y=null,w=0,E=null;exports.calcularPrecoPrazo=function(t){var r=t.nCdServico;return Promise.all(r.map((function(r){return function(t,r){var a=this;return delete t.nCdServico,new Promise((function(p,f){return i(a,void 0,void 0,(function(){var a;return u(this,(function(i){return a=c(c({},t),{nCdServico:r,sCepOrigem:o(t.sCepOrigem),sCepDestino:o(t.sCepDestino),nCdEmpresa:null==t.nCdEmpresa?"":t.nCdEmpresa,sDsSenha:null==t.sDsSenha?"":t.sDsSenha,sCdMaoPropria:null==t.sCdMaoPropria?"n":t.sCdMaoPropria,nVlValorDeclarado:null==t.nVlValorDeclarado?0:t.nVlValorDeclarado,sCdAvisoRecebimento:null==t.sCdAvisoRecebimento?"n":t.sCdAvisoRecebimento,StrRetorno:"xml",nIndicaCalculo:null==t.nIndicaCalculo?3:t.nIndicaCalculo}),[2,s("".concat(l.BASECORREIOS,"?").concat(new URLSearchParams(a).toString()),{method:"GET",headers:{"content-type":"text/xml"},responseType:"arraybuffer"}).then((function(t){var r,o,a=(r=function(t,e){return n.decode(Buffer.from(t),e).toString()}(t.data,"iso-8859-1"),JSON.parse(e.xml2json(r,{compact:!0})));return p((o=a.Servicos.cServico,Object.keys(o).reduce((function(t,n){return t[n]=o[n]._text?o[n]._text:o[n]._cdata,t}),{})))})).catch((function(t){f(t)}))]}))}))}))}(t,r)}))).then((function(t){return t}))},exports.consultarCep=function(t){return new Promise((function(n,e){return s("".concat(l.BASECEP,"/").concat(o(t),"/json"),{method:"GET",headers:{"content-type":"application/json"}}).then((function(t){return n(t.data)})).catch((function(t){e(t)}))}))},exports.rastrearEncomendas=function(t){return Promise.all(t.map((function(t){return function(t){return new Promise((function(n,e){(function(){if(y&&w>Date.now())return Promise.resolve(y);if(E)return E;return E=new Promise((function(t,n){s(l.PROXYAPP_TOKEN,{method:"POST",headers:{"content-type":"application/json","user-agent":"Dart/2.18 (dart:io)"},data:{requestToken:b,data:P,sign:g}}).then((function(n){E=null;var e=n.data.token,r=e.split(".")[1],o=Buffer.from(r,"base64").toString("ascii"),a=JSON.parse(o);y=e,w=1e3*a.exp-12e4,t(e)})).catch((function(t){y=null,w=0,E=null,n(new Error("Falha ao autenticar requisição"))}))})),E})().then((function(r){s("".concat(l.PROXYAPP_RASTREAR,"/").concat(t),{method:"GET",headers:{"content-type":"application/json","user-agent":"Dart/2.18 (dart:io)","app-check-token":r}}).then((function(t){return n(t.data.objetos[0])})).catch((function(t){e(t)}))})).catch(e)}))}(t)}))).then((function(t){return t}))}; //# sourceMappingURL=index.js.map