-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from vitorleal/feature/update-lib
Update lib
- Loading branch information
Showing
5 changed files
with
2,813 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,84 @@ | ||
'use strict'; | ||
|
||
var soap = require('soap'), | ||
extend = require('extend'), | ||
request = require('request'); | ||
|
||
|
||
/** | ||
* Correios class | ||
* @example | ||
* ``` | ||
* var Correios = require('node-correios'), | ||
* correios = new Correios(); | ||
* | ||
* correios.calcPreco(args, function (err, result) { | ||
* console.log(result); | ||
* }); | ||
* ``` | ||
*/ | ||
var Correios = function () { | ||
// If is not instance of Correios return a new instance | ||
if (false === (this instanceof Correios)) { | ||
return new Correios(); | ||
let soap = require('soap'); | ||
let request = require('request'); | ||
|
||
module.exports = class Correios { | ||
constructor(width) { | ||
this.calcPrecoUrl = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?wsdl'; | ||
this.cepUrl = 'http://viacep.com.br/ws/{CEP}/json'; | ||
this.calcArgs = { | ||
nCdEmpresa : '', | ||
sDsSenha : '', | ||
sCdMaoPropria : 'N', | ||
nVlValorDeclarado : 0, | ||
sCdAvisoRecebimento: 'N' | ||
}; | ||
} | ||
|
||
// Default URl's | ||
this.calcPrecoUrl = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?wsdl'; | ||
this.cepUrl = 'http://viacep.com.br/ws/{CEP}/json'; | ||
|
||
// Default args for price calculation | ||
this.calcArgs = { | ||
nCdEmpresa : '', | ||
sDsSenha : '', | ||
sCdMaoPropria : 'N', | ||
nVlValorDeclarado : 0, | ||
sCdAvisoRecebimento: 'N' | ||
}; | ||
|
||
// Default args for CEP search | ||
this.cepArgs = { | ||
cepEntrada: '', | ||
metodo : 'buscarCep' | ||
}; | ||
}; | ||
calcPreco(args) { | ||
let arg = Object.assign({}, this.calcArgs, args); | ||
|
||
return new Promise((resolve, reject) => { | ||
soap.createClient(this.calcPrecoUrl, (error, client) => { | ||
client.CalcPreco(arg, (error, result) => { | ||
if (!error | ||
&& result && result.CalcPrecoResult | ||
&& result.CalcPrecoResult.Servicos | ||
&& result.CalcPrecoResult.Servicos.cServico) { | ||
|
||
/** | ||
* Calculate price with shipping | ||
* @param {object} args | ||
* @param {function} [callback] - Callback to execute after request | ||
* @example | ||
* ``` | ||
* var args = { | ||
* nCdServico: '40010,41106,40215', | ||
* // demais parâmetros ... | ||
* }; | ||
* | ||
* correios.calcPreco(args, function (err, result) { | ||
* console.log(result); | ||
* }); | ||
* ``` | ||
*/ | ||
Correios.prototype.calcPreco = function(args, callback) { | ||
return new Promise((resolve,reject) => { | ||
var arg = extend({}, this.calcArgs, args); | ||
return resolve(result.CalcPrecoResult.Servicos.cServico); | ||
} | ||
|
||
// create the SOAP client | ||
soap.createClient(this.calcPrecoUrl, function(err, client) { | ||
client.CalcPreco(arg, function (err, result) { | ||
if (result && result.CalcPrecoResult && result.CalcPrecoResult.Servicos && result.CalcPrecoResult.Servicos.cServico) { | ||
return callback ? callback(err, result.CalcPrecoResult.Servicos.cServico) : (!err ? resolve(result.CalcPrecoResult.Servicos.cServico) : reject(err)); | ||
} else { | ||
return callback ? callback(err, null) : reject(err); | ||
} | ||
return reject(error); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}; | ||
}) | ||
} | ||
|
||
calcPrecoPrazo(args) { | ||
let arg = Object.assign({}, this.calcArgs, args); | ||
|
||
return new Promise((resolve, reject) => { | ||
soap.createClient(this.calcPrecoUrl, (error, client) => { | ||
client.CalcPrecoPrazo(arg, (error, result) => { | ||
if (!error | ||
&& result && result.CalcPrecoResult | ||
&& result.CalcPrecoPrazoResult.Servicos | ||
&& result.CalcPrecoPrazoResult.Servicos.cServico) { | ||
|
||
/** | ||
* Calculate price and estimate delivery time | ||
* @param {object} args | ||
* @param {function} [callback] - Callback to execute after request | ||
* @example | ||
* ``` | ||
* var args = { | ||
* nCdServico: '40010,41106,40215', | ||
* // demais parâmetros ... | ||
* }; | ||
* | ||
* correios.calcPrecoPrazo(args, function (err, result) { | ||
* console.log(result); | ||
* }); | ||
* ``` | ||
*/ | ||
Correios.prototype.calcPrecoPrazo = function(args, callback) { | ||
var arg = extend({}, this.calcArgs, args); | ||
return resolve(result.CalcPrecoPrazoResult.Servicos.cServico) | ||
} | ||
|
||
// create the SOAP client | ||
return new Promise((resolve, reject) => { | ||
soap.createClient(this.calcPrecoUrl, function(err, client) { | ||
client.CalcPrecoPrazo(arg, function (err, result) { | ||
callback ? callback(err, result.CalcPrecoPrazoResult.Servicos.cServico) : (!err ? resolve(result.CalcPrecoPrazoResult.Servicos.cServico) : reject(err)); | ||
return reject(error); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}; | ||
|
||
|
||
/** | ||
* Search for address by CEP | ||
* @param {object} args | ||
* @param {function} [callback] - Callback to execute after request | ||
* @example | ||
* ``` | ||
* var Correios = require('node-correios'), | ||
* correios = new Correios(); | ||
* | ||
* correios.consultaCEP({ cep: '00000000' }, function(err, result) { | ||
* console.log(result) | ||
* }); | ||
* ``` | ||
*/ | ||
Correios.prototype.consultaCEP = function(args, callback) { | ||
var arg = extend({}, args); | ||
|
||
if ('cep' in arg === false) { | ||
throw new Error('You need to inform a CEP ex: { cep: 00000000 }'); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
// make request to the cep api | ||
var url = this.cepUrl.replace('{CEP}', arg.cep.replace('-', '')); | ||
consultaCEP(args) { | ||
let arg = Object.assign({}, args); | ||
|
||
request(url, function(err, resp, body) { | ||
if (err) { | ||
return callback ? callback(err) : reject(err); | ||
} | ||
if ('cep' in arg === false) { | ||
throw new Error('You need to inform a CEP ex: { cep: 00000000 }'); | ||
} | ||
|
||
try { | ||
return callback ? callback(null, JSON.parse(body)) : resolve(JSON.parse(body)); | ||
return new Promise((resolve, reject) => { | ||
let url = this.cepUrl.replace('{CEP}', arg.cep.replace('-', '')); | ||
|
||
} catch (e) { | ||
var error = { | ||
Erro: 404, | ||
MsgErro: 'Cep não encontrado' | ||
request(url, (error, resp, body) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
return callback ? callback(error) : reject(error); | ||
} | ||
}); | ||
}) | ||
}; | ||
|
||
|
||
module.exports = Correios; | ||
try { | ||
return resolve(JSON.parse(body)) | ||
|
||
} catch (e) { | ||
return reject({ | ||
Erro: 404, | ||
MsgErro: 'Cep não encontrado' | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.