Comparing version 2.1.0 to 2.2.0
@@ -7,2 +7,6 @@ 'use strict'; | ||
Stripe.DEFAULT_API_VERSION = null; | ||
// Use node's default timeout: | ||
Stripe.DEFAULT_TIMEOUT = require('http').createServer().timeout; | ||
Stripe.PACKAGE_VERSION = require('fs').readFileSync( | ||
@@ -60,2 +64,3 @@ require('path').resolve(__dirname, '../VERSION'), 'utf-8' | ||
version: Stripe.DEFAULT_API_VERSION, | ||
timeout: Stripe.DEFAULT_TIMEOUT, | ||
dev: false | ||
@@ -98,2 +103,9 @@ }; | ||
setTimeout: function(timeout) { | ||
this._setApiField( | ||
'timeout', | ||
timeout == null ? Stripe.DEFAULT_TIMEOUT : timeout | ||
); | ||
}, | ||
_setApiField: function(key, value) { | ||
@@ -100,0 +112,0 @@ this._api[key] = value; |
@@ -83,2 +83,72 @@ 'use strict'; | ||
_timeoutHandler: function(timeout, req, callback) { | ||
var self = this; | ||
return function() { | ||
var timeoutErr = new Error('ETIMEDOUT'); | ||
timeoutErr.code = 'ETIMEDOUT'; | ||
req._isAborted = true; | ||
req.abort(); | ||
callback.call( | ||
self, | ||
new Error.StripeConnectionError({ | ||
message: 'Request aborted due to timeout being reached (' + timeout + 'ms)', | ||
detail: timeoutErr | ||
}), | ||
null | ||
); | ||
} | ||
}, | ||
_responseHandler: function(req, callback) { | ||
var self = this; | ||
return function(res) { | ||
var response = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk) { | ||
response += chunk; | ||
}); | ||
res.on('end', function() { | ||
try { | ||
response = JSON.parse(response); | ||
if (response.error) { | ||
var err; | ||
if (res.statusCode === 401) { | ||
err = new Error.StripeAuthenticationError(response.error); | ||
} else { | ||
err = Error.StripeError.generate(response.error); | ||
} | ||
return callback.call(self, err, null); | ||
} | ||
} catch (e) { | ||
return callback.call( | ||
self, | ||
new Error.StripeAPIError({ | ||
message: 'Invalid JSON received from the Stripe API' | ||
}), | ||
null | ||
); | ||
} | ||
callback.call(self, null, response); | ||
}); | ||
}; | ||
}, | ||
_errorHandler: function(req, callback) { | ||
var self = this; | ||
return function(error) { | ||
if (req._isAborted) return; // already handled | ||
callback.call( | ||
self, | ||
new Error.StripeConnectionError({ | ||
message: 'An error occurred with our connection to Stripe', | ||
detail: error | ||
}), | ||
null | ||
); | ||
} | ||
}, | ||
_request: function(method, path, data, auth, callback) { | ||
@@ -113,2 +183,4 @@ | ||
var timeout = self._stripe.getApiField('timeout'); | ||
var req = ( | ||
@@ -124,45 +196,6 @@ self._stripe.getApiField('protocol') == 'http' ? http : https | ||
req.on('response', function(res) { | ||
var response = ''; | ||
req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback)); | ||
req.on('response', self._responseHandler(req, callback)); | ||
req.on('error', self._errorHandler(req, callback)); | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk) { | ||
response += chunk; | ||
}); | ||
res.on('end', function() { | ||
try { | ||
response = JSON.parse(response); | ||
if (response.error) { | ||
var err; | ||
if (res.statusCode === 401) { | ||
err = new Error.StripeAuthenticationError(response.error); | ||
} else { | ||
err = Error.StripeError.generate(response.error); | ||
} | ||
return callback.call(self, err, null); | ||
} | ||
} catch (e) { | ||
return callback.call( | ||
self, | ||
new Error.StripeAPIError({ | ||
message: 'Invalid JSON received from the Stripe API' | ||
}), | ||
null | ||
); | ||
} | ||
callback.call(self, null, response); | ||
}); | ||
}); | ||
req.on('error', function(error) { | ||
callback.call( | ||
self, | ||
new Error.StripeConnectionError({ | ||
message: 'An error occurred with our connection to Stripe', | ||
detail: error | ||
}), | ||
null | ||
); | ||
}); | ||
req.write(requestData); | ||
@@ -169,0 +202,0 @@ req.end(); |
{ | ||
"name": "stripe", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Stripe API wrapper", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/stripe/stripe-node", |
@@ -82,6 +82,12 @@ 'use strict'; | ||
plan: 'someNonExistentPlan' + +new Date | ||
}).then(null, function(err) { | ||
// Resolve with the error so we can inspect it below | ||
return err; | ||
}); | ||
}) | ||
).to.be.eventually.rejected; | ||
).to.eventually.satisfy(function(err) { | ||
return err.type === 'StripeInvalidRequest' && | ||
err.rawType === 'invalid_request_error'; | ||
}); | ||
@@ -264,2 +270,65 @@ }); | ||
describe('Invoice Items', function() { | ||
it('Can get invoice-items created after a certain time', function() { | ||
var invoiceItemId; | ||
return expect( | ||
stripe.customers.create(CUSTOMER_DETAILS) | ||
.then(function(cust) { | ||
cleanup.deleteCustomer(cust.id); | ||
return stripe.invoiceItems.create({ | ||
customer: cust.id, | ||
amount: 1700, | ||
currency: CURRENCY, | ||
description: 'InvoiceItemFlowSpec987654321' | ||
}); | ||
}) | ||
.then(function(ii) { | ||
invoiceItemId = ii.id | ||
cleanup.deleteInvoiceItem(ii.id); | ||
var deferred = when.defer(); | ||
// Return found invoiceItem description for two searches: | ||
return when.join( | ||
// This search should give us our invoice-item: | ||
stripe.invoiceItems.list({ | ||
created: { | ||
gt: ii.date - 1 | ||
} | ||
}).then(function(items) { | ||
// Just get the invoiceItem we created: (to be sure) | ||
return items.data.filter(function(ii) { | ||
return ii.id === invoiceItemId; | ||
})[0].description; | ||
}), | ||
// This search should also give us ours: | ||
stripe.invoiceItems.list({ | ||
created: { | ||
gte: ii.date | ||
} | ||
}).then(function(items) { | ||
// Just get the invoiceItem we created: | ||
return items.data.filter(function(ii) { | ||
return ii.id === invoiceItemId; | ||
})[0].description; | ||
}), | ||
// This search should give us nothing: | ||
stripe.invoiceItems.list({ | ||
created: { | ||
gt: ii.date | ||
} | ||
}).then(function(items) { | ||
return items.data.length === 0 ? null : items; | ||
}) | ||
); | ||
}) | ||
).to.eventually.deep.equal([ | ||
'InvoiceItemFlowSpec987654321', | ||
'InvoiceItemFlowSpec987654321', | ||
null | ||
]); | ||
}); | ||
}); | ||
}); |
@@ -36,2 +36,16 @@ 'use strict'; | ||
describe('setTimeout', function() { | ||
it('Should define a default equal to the node default', function() { | ||
expect(stripe.getApiField('timeout')).to.equal(require('http').createServer().timeout); | ||
}); | ||
it('Should allow me to set a custom timeout', function() { | ||
stripe.setTimeout(900); | ||
expect(stripe.getApiField('timeout')).to.equal(900); | ||
}); | ||
it('Should allow me to set null, to reset to the default', function() { | ||
stripe.setTimeout(null); | ||
expect(stripe.getApiField('timeout')).to.equal(require('http').createServer().timeout); | ||
}); | ||
}); | ||
describe('Callback support', function() { | ||
@@ -38,0 +52,0 @@ |
@@ -122,2 +122,7 @@ 'use strict'; | ||
}); | ||
}, | ||
deleteInvoiceItem: function(iiId) { | ||
this.add(function() { | ||
return this._stripe.invoiceItems.del(iiId); | ||
}); | ||
} | ||
@@ -124,0 +129,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
84640
2262
7