Skip to content

Commit

Permalink
lebab transform: destruct-param
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex-stripe committed May 4, 2019
1 parent 7eba992 commit b56f52d
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 139 deletions.
6 changes: 3 additions & 3 deletions examples/webhook-signing/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const app = express();
app.post(
'/webhooks',
bodyParser.raw({type: 'application/json'}),
(req, res) => {
const sig = req.headers['stripe-signature'];
({headers, body}, res) => {
const sig = headers['stripe-signature'];

let event;

try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
event = stripe.webhooks.constructEvent(body, sig, webhookSecret);
} catch (err) {
// On error, return the error message
return res.status(400).send(`Webhook Error: ${err.message}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/StripeMethod.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ module.exports = {

return utils.callbackifyPromiseWithTimeout(
new Promise((resolve, reject) => {
this._request('GET', null, path, {}, auth, {}, (err, response) => {
this._request('GET', null, path, {}, auth, {}, (err, {metadata}) => {
if (err) {
reject(err);
} else {
resolve(response.metadata);
resolve(metadata);
}
});
}),
Expand Down
12 changes: 6 additions & 6 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ StripeResource.prototype = {
};
},

_responseHandler(req, callback) {
_responseHandler({_requestStart, _requestEvent}, callback) {
const self = this;
return (res) => {
let response = '';
Expand All @@ -132,14 +132,14 @@ StripeResource.prototype = {
// lastResponse.
res.requestId = headers['request-id'];

const requestDurationMs = Date.now() - req._requestStart;
const requestDurationMs = Date.now() - _requestStart;

const responseEvent = utils.removeEmpty({
api_version: headers['stripe-version'],
account: headers['stripe-account'],
idempotency_key: headers['idempotency-key'],
method: req._requestEvent.method,
path: req._requestEvent.path,
method: _requestEvent.method,
path: _requestEvent.path,
status: res.statusCode,
request_id: res.requestId,
elapsed: requestDurationMs,
Expand Down Expand Up @@ -209,10 +209,10 @@ StripeResource.prototype = {
}`;
},

_errorHandler(req, requestRetries, callback) {
_errorHandler({_isAborted}, requestRetries, callback) {
const self = this;
return (error) => {
if (req._isAborted) {
if (_isAborted) {
// already handled
return;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/autoPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ function getItemCallback(args) {
};
}

function getLastId(listResult) {
const lastIdx = listResult.data.length - 1;
const lastItem = listResult.data[lastIdx];
function getLastId({data}) {
const lastIdx = data.length - 1;
const lastItem = data[lastIdx];
const lastId = lastItem && lastItem.id;
if (!lastId) {
throw Error(
Expand Down Expand Up @@ -215,13 +215,13 @@ function makeAutoPagingToArray(autoPagingEach) {

function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) {
return new Promise((resolve, reject) => {
function handleIteration(iterResult) {
if (iterResult.done) {
function handleIteration({done, value}) {
if (done) {
resolve();
return;
}

const item = iterResult.value;
const item = value;
return new Promise((next) => {
// Bit confusing, perhaps; we pass a `resolve` fn
// to the user, so they can decide when and if to continue.
Expand Down
4 changes: 2 additions & 2 deletions lib/resources/EphemeralKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const stripeMethod = StripeResource.method;
module.exports = StripeResource.extend({
create: stripeMethod({
method: 'POST',
validator(data, options) {
if (!options.headers || !options.headers['Stripe-Version']) {
validator(data, {headers}) {
if (!headers || !headers['Stripe-Version']) {
throw new Error(
'stripe_version must be specified to create an ephemeral key'
);
Expand Down
6 changes: 3 additions & 3 deletions lib/resources/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ module.exports = StripeResource.extend({
function streamError(callback) {
const StreamProcessingError = Error.extend({
type: 'StreamProcessingError',
populate(raw) {
populate({message, detail}) {
this.type = this.type;
this.message = raw.message;
this.detail = raw.detail;
this.message = message;
this.detail = detail;
},
});
return (error) => {
Expand Down
31 changes: 14 additions & 17 deletions lib/resources/Invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,42 @@ module.exports = StripeResource.extend({

retrieveUpcoming: stripeMethod({
method: 'GET',
path(urlData) {
path({invoiceOptionsOrCustomerId, invoiceOptionsOrSubscriptionId}) {
let url = 'upcoming?';
let hasParam = false;

// If you pass just a hash with the relevant parameters, including customer id inside.
if (
urlData.invoiceOptionsOrCustomerId &&
typeof urlData.invoiceOptionsOrCustomerId === 'object'
invoiceOptionsOrCustomerId &&
typeof invoiceOptionsOrCustomerId === 'object'
) {
return (
url + utils.stringifyRequestData(urlData.invoiceOptionsOrCustomerId)
);
return url + utils.stringifyRequestData(invoiceOptionsOrCustomerId);
}

// Legacy implementation where the first parameter is a customer id as a string
if (
urlData.invoiceOptionsOrCustomerId &&
typeof urlData.invoiceOptionsOrCustomerId === 'string'
invoiceOptionsOrCustomerId &&
typeof invoiceOptionsOrCustomerId === 'string'
) {
url = `${url}customer=${urlData.invoiceOptionsOrCustomerId}`;
url = `${url}customer=${invoiceOptionsOrCustomerId}`;
hasParam = true;
}

// Legacy support where second argument is the subscription id
if (
urlData.invoiceOptionsOrSubscriptionId &&
typeof urlData.invoiceOptionsOrSubscriptionId === 'string'
invoiceOptionsOrSubscriptionId &&
typeof invoiceOptionsOrSubscriptionId === 'string'
) {
return `${url + (hasParam ? '&' : '')}subscription=${
urlData.invoiceOptionsOrSubscriptionId
}`;
return `${url +
(hasParam ? '&' : '')}subscription=${invoiceOptionsOrSubscriptionId}`;
} else if (
urlData.invoiceOptionsOrSubscriptionId &&
typeof urlData.invoiceOptionsOrSubscriptionId === 'object'
invoiceOptionsOrSubscriptionId &&
typeof invoiceOptionsOrSubscriptionId === 'object'
) {
return (
url +
(hasParam ? '&' : '') +
utils.stringifyRequestData(urlData.invoiceOptionsOrSubscriptionId)
utils.stringifyRequestData(invoiceOptionsOrSubscriptionId)
);
}
return url;
Expand Down
6 changes: 3 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ const utils = (module.exports = {
* Determine if file data is a derivative of EventEmitter class.
* https://nodejs.org/api/events.html#events_events
*/
checkForStream(obj) {
if (obj.file && obj.file.data) {
return obj.file.data instanceof EventEmitter;
checkForStream({file}) {
if (file && file.data) {
return file.data instanceof EventEmitter;
}
return false;
},
Expand Down
38 changes: 19 additions & 19 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ describe('StripeResource', () => {
.post(options.path, options.params)
.replyWithError('bad stuff');

realStripe.charges.create(options.data, (err) => {
expect(err.detail.message).to.deep.equal('bad stuff');
realStripe.charges.create(options.data, ({detail}) => {
expect(detail.message).to.deep.equal('bad stuff');
done();
});
});
Expand All @@ -154,12 +154,12 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.charges.create(options.data, (err) => {
realStripe.charges.create(options.data, ({message, detail}) => {
const errorMessage = realStripe.invoices._generateConnectionErrorMessage(
1
);
expect(err.message).to.equal(errorMessage);
expect(err.detail.message).to.deep.equal('worse stuff');
expect(message).to.equal(errorMessage);
expect(detail.message).to.deep.equal('worse stuff');
done();
});
});
Expand All @@ -177,8 +177,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(2);

realStripe.charges.create(options.data, (err, charge) => {
expect(charge.id).to.equal('ch_123');
realStripe.charges.create(options.data, (err, {id}) => {
expect(id).to.equal('ch_123');
done();
});
});
Expand All @@ -200,8 +200,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.charges.create(options.data, (err, charge) => {
expect(charge.id).to.equal('ch_123');
realStripe.charges.create(options.data, (err, {id}) => {
expect(id).to.equal('ch_123');
done();
});
});
Expand All @@ -217,8 +217,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.charges.create(options.data, (err) => {
expect(err.type).to.equal('StripeCardError');
realStripe.charges.create(options.data, ({type}) => {
expect(type).to.equal('StripeCardError');
done();
});
});
Expand All @@ -234,8 +234,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.charges.create(options.data, (err) => {
expect(err.type).to.equal('StripeAPIError');
realStripe.charges.create(options.data, ({type}) => {
expect(type).to.equal('StripeAPIError');
done();
});
});
Expand All @@ -251,8 +251,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.oauth.token(options.data, (err) => {
expect(err.type).to.equal('StripeInvalidGrantError');
realStripe.oauth.token(options.data, ({type}) => {
expect(type).to.equal('StripeInvalidGrantError');
done();
});
});
Expand All @@ -274,8 +274,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.charges.create(options.data, (err, charge) => {
expect(charge.id).to.equal('ch_123');
realStripe.charges.create(options.data, (err, {id}) => {
expect(id).to.equal('ch_123');
done();
});
});
Expand All @@ -297,8 +297,8 @@ describe('StripeResource', () => {

realStripe.setMaxNetworkRetries(1);

realStripe.charges.retrieve('ch_123', (err, charge) => {
expect(charge.id).to.equal('ch_123');
realStripe.charges.retrieve('ch_123', (err, {id}) => {
expect(id).to.equal('ch_123');
done();
});
});
Expand Down
Loading

0 comments on commit b56f52d

Please sign in to comment.