Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to handle timeout error #9

Merged
merged 3 commits into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ parseSoapResponse = (soapMessage) ->

return ret

module.exports = exports = (countryCode, vatNumber, callback) ->
module.exports = exports = (countryCode, vatNumber, timeout, callback) ->
if typeof timeout is 'function'
callback = timeout
timeout = null

if countryCode not in EU_COUNTRIES_CODES or !vatNumber?.length
return process.nextTick -> callback new Error ERROR_MSG['INVALID_INPUT']

Expand Down Expand Up @@ -110,7 +114,9 @@ module.exports = exports = (countryCode, vatNumber, callback) ->

return callback null, data

if timeout then req.setTimeout timeout, ->
req.abort()

req.on 'error', callback
req.write xml
req.end()

7 changes: 6 additions & 1 deletion test/validate-vat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ validate = require '../src/index'

describe 'validate()', ->

# Netflix 2016 VAT Number
it 'should return true if it is a valid VAT number', (done) ->
validate 'GB', '802311782', (err, validationInfo) ->
validate 'NL', '853746333B01', (err, validationInfo) ->
if err then return done err
expect(validationInfo.valid).to.be true
done()

it 'should abort request if timeout is specified', (done) ->
validate 'NL', '853746333B01', 10, (err) ->
expect(err.code).to.be 'ECONNRESET'
done()

it 'should return false if it is an invalid VAT number', (done) ->
validate 'GB', '802311783', (err, validationInfo) ->
Expand Down