Skip to content

Commit

Permalink
Modernize ES5 to ES6 with lebab (#607)
Browse files Browse the repository at this point in the history
* Add lebab and a script to run it

* lebab transform: arrow

* lebab transform: arg-rest

* lebab transform: arg-spread

* lebab transform: obj-method

* lebab transform: obj-shorthand

* lebab transform: let

* lebab transform: template

* lebab transform: default-param

* lebab transform: destruct-param

* lebab transform: includes

* Revert "Add lebab and a script to run it"

This reverts commit 70fd492.

* Revert "lebab transform: destruct-param" because its changes didn't seem good.

This  reverts commit b56f52d.

* Revert "lebab transform: default-param" because it seems dangerous / backwards-incompatible.

This reverts commit 7eba992.

* Unrelated: mark 8.1 as minimum 8-series version

* Add mocha-only script

* Use arrows in more places

* Loosen some eslint rules I don't love
  • Loading branch information
rattrayalex-stripe authored and ob-stripe committed May 8, 2019
1 parent 060f4a8 commit 6b5f39d
Show file tree
Hide file tree
Showing 152 changed files with 2,519 additions and 2,741 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
'accessor-pairs': 'error',
'array-bracket-spacing': ['error', 'never'],
'array-callback-return': 'off',
'arrow-body-style': 'error',
'arrow-parens': 'error',
'arrow-spacing': 'error',
'block-scoped-var': 'off',
Expand Down Expand Up @@ -43,7 +42,7 @@ module.exports = {
'func-call-spacing': 'error',
'func-name-matching': 'error',
'func-names': 'off',
'func-style': ['error', 'declaration'],
'func-style': ['error', 'declaration', {allowArrowFunctions: true}],
'generator-star-spacing': 'error',
'global-require': 'off',
'guard-for-in': 'off',
Expand Down
13 changes: 8 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
language: node_js
cache:
yarn: true
directories:
- node_modules
node_js:
- "6"
- "8"
- "9"
- "10"
- '6'
- '8'
- '10'
sudo: false
after_success: npm run report && npm run coveralls
after_success: yarn report && yarn coveralls
2 changes: 1 addition & 1 deletion examples/webhook-signing/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ app.post(
}
);

app.listen(3000, function() {
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
10 changes: 5 additions & 5 deletions lib/Error.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

var utils = require('./utils');
const utils = require('./utils');

module.exports = _Error;

/**
* Generic Error klass to wrap any errors returned by stripe-node
*/
function _Error(raw) {
this.populate.apply(this, arguments);
this.populate(...arguments);
this.stack = new Error(this.message).stack;
}

Expand All @@ -27,9 +27,9 @@ _Error.extend = utils.protoExtend;
* Create subclass of internal Error klass
* (Specifically for errors returned from Stripe's REST API)
*/
var StripeError = (_Error.StripeError = _Error.extend({
const StripeError = (_Error.StripeError = _Error.extend({
type: 'StripeError',
populate: function(raw) {
populate(raw) {
// Move from prototype def (so it appears in stringified obj)
this.type = this.type;

Expand All @@ -49,7 +49,7 @@ var StripeError = (_Error.StripeError = _Error.extend({
/**
* Helper factory which takes raw stripe errors and outputs wrapping instances
*/
StripeError.generate = function(rawStripeError) {
StripeError.generate = (rawStripeError) => {
switch (rawStripeError.type) {
case 'card_error':
return new _Error.StripeCardError(rawStripeError);
Expand Down
35 changes: 17 additions & 18 deletions lib/MultipartDataGenerator.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
'use strict';

var Buffer = require('safe-buffer').Buffer;
var utils = require('./utils');
const Buffer = require('safe-buffer').Buffer;
const utils = require('./utils');

// Method for formatting HTTP body for the multipart/form-data specification
// Mostly taken from Fermata.js
// https://github.com/natevw/fermata/blob/5d9732a33d776ce925013a265935facd1626cc88/fermata.js#L315-L343
function multipartDataGenerator(method, data, headers) {
var segno = (
const segno = (
Math.round(Math.random() * 1e16) + Math.round(Math.random() * 1e16)
).toString();
headers['Content-Type'] = 'multipart/form-data; boundary=' + segno;
var buffer = Buffer.alloc(0);
headers['Content-Type'] = `multipart/form-data; boundary=${segno}`;
let buffer = Buffer.alloc(0);

function push(l) {
var prevBuffer = buffer;
var newBuffer = l instanceof Buffer ? l : Buffer.from(l);
const prevBuffer = buffer;
const newBuffer = l instanceof Buffer ? l : Buffer.from(l);
buffer = Buffer.alloc(prevBuffer.length + newBuffer.length + 2);
prevBuffer.copy(buffer);
newBuffer.copy(buffer, prevBuffer.length);
buffer.write('\r\n', buffer.length - 2);
}

function q(s) {
return '"' + s.replace(/"|"/g, '%22').replace(/\r\n|\r|\n/g, ' ') + '"';
return `"${s.replace(/"|"/g, '%22').replace(/\r\n|\r|\n/g, ' ')}"`;
}

for (var k in utils.flattenAndStringify(data)) {
var v = data[k];
push('--' + segno);
for (const k in utils.flattenAndStringify(data)) {
const v = data[k];
push(`--${segno}`);
if (v.hasOwnProperty('data')) {
push(
'Content-Disposition: form-data; name=' +
q(k) +
'; filename=' +
q(v.name || 'blob')
`Content-Disposition: form-data; name=${q(k)}; filename=${q(
v.name || 'blob'
)}`
);
push('Content-Type: ' + (v.type || 'application/octet-stream'));
push(`Content-Type: ${v.type || 'application/octet-stream'}`);
push('');
push(v.data);
} else {
push('Content-Disposition: form-data; name=' + q(k));
push(`Content-Disposition: form-data; name=${q(k)}`);
push('');
push(v);
}
}
push('--' + segno + '--');
push(`--${segno}--`);

return buffer;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ResourceNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// It also works recursively, so you could do i.e. `stripe.billing.invoicing.pay`.

function ResourceNamespace(stripe, resources) {
for (var name in resources) {
var camelCaseName = name[0].toLowerCase() + name.substring(1);
for (const name in resources) {
const camelCaseName = name[0].toLowerCase() + name.substring(1);

var resource = new resources[name](stripe);
const resource = new resources[name](stripe);

this[camelCaseName] = resource;
}
Expand Down
135 changes: 64 additions & 71 deletions lib/StripeMethod.basic.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var isPlainObject = require('lodash.isplainobject');
var stripeMethod = require('./StripeMethod');
var utils = require('./utils');
const isPlainObject = require('lodash.isplainobject');
const stripeMethod = require('./StripeMethod');
const utils = require('./utils');

module.exports = {
create: stripeMethod({
Expand Down Expand Up @@ -33,12 +33,12 @@ module.exports = {
urlParams: ['id'],
}),

setMetadata: function(id, key, value, auth, cb) {
var self = this;
var data = key;
var isObject = isPlainObject(key);
setMetadata(id, key, value, auth, cb) {
const self = this;
const data = key;
const isObject = isPlainObject(key);
// We assume null for an empty object
var isNull = data === null || (isObject && !Object.keys(data).length);
const isNull = data === null || (isObject && !Object.keys(data).length);

// Allow optional passing of auth & cb:
if ((isNull || isObject) && typeof value == 'string') {
Expand All @@ -50,85 +50,78 @@ module.exports = {
auth = null;
}

var urlData = this.createUrlData();
var path = this.createFullPath('/' + id, urlData);
const urlData = this.createUrlData();
const path = this.createFullPath(`/${id}`, urlData);

return utils.callbackifyPromiseWithTimeout(
new Promise(
function(resolve, reject) {
if (isNull) {
// Reset metadata:
sendMetadata(null, auth);
} else if (!isObject) {
// Set individual metadata property:
var metadata = {};
metadata[key] = value;
sendMetadata(metadata, auth);
} else {
// Set entire metadata object after resetting it:
this._request(
'POST',
null,
path,
{metadata: null},
auth,
{},
function(err, response) {
if (err) {
return reject(err);
}
sendMetadata(data, auth);
new Promise((resolve, reject) => {
if (isNull) {
// Reset metadata:
sendMetadata(null, auth);
} else if (!isObject) {
// Set individual metadata property:
const metadata = {};
metadata[key] = value;
sendMetadata(metadata, auth);
} else {
// Set entire metadata object after resetting it:
this._request(
'POST',
null,
path,
{metadata: null},
auth,
{},
(err, response) => {
if (err) {
return reject(err);
}
);
}
sendMetadata(data, auth);
}
);
}

function sendMetadata(metadata, auth) {
self._request(
'POST',
null,
path,
{metadata: metadata},
auth,
{},
function(err, response) {
if (err) {
reject(err);
} else {
resolve(response.metadata);
}
function sendMetadata(metadata, auth) {
self._request(
'POST',
null,
path,
{metadata},
auth,
{},
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response.metadata);
}
);
}
}.bind(this)
),
}
);
}
}),
cb
);
},

getMetadata: function(id, auth, cb) {
getMetadata(id, auth, cb) {
if (!cb && typeof auth == 'function') {
cb = auth;
auth = null;
}

var urlData = this.createUrlData();
var path = this.createFullPath('/' + id, urlData);
const urlData = this.createUrlData();
const path = this.createFullPath(`/${id}`, urlData);

return utils.callbackifyPromiseWithTimeout(
new Promise(
function(resolve, reject) {
this._request('GET', null, path, {}, auth, {}, function(
err,
response
) {
if (err) {
reject(err);
} else {
resolve(response.metadata);
}
});
}.bind(this)
),
new Promise((resolve, reject) => {
this._request('GET', null, path, {}, auth, {}, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response.metadata);
}
});
}),
cb
);
},
Expand Down
16 changes: 8 additions & 8 deletions lib/StripeMethod.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var utils = require('./utils');
var makeRequest = require('./makeRequest');
var makeAutoPaginationMethods = require('./autoPagination')
const utils = require('./utils');
const makeRequest = require('./makeRequest');
const makeAutoPaginationMethods = require('./autoPagination')
.makeAutoPaginationMethods;

/**
Expand All @@ -21,18 +21,18 @@ var makeAutoPaginationMethods = require('./autoPagination')
*/
function stripeMethod(spec) {
return function() {
var self = this;
var args = [].slice.call(arguments);
const self = this;
const args = [].slice.call(arguments);

var callback = typeof args[args.length - 1] == 'function' && args.pop();
const callback = typeof args[args.length - 1] == 'function' && args.pop();

var requestPromise = utils.callbackifyPromiseWithTimeout(
const requestPromise = utils.callbackifyPromiseWithTimeout(
makeRequest(self, args, spec, {}),
callback
);

if (spec.methodType === 'list') {
var autoPaginationMethods = makeAutoPaginationMethods(
const autoPaginationMethods = makeAutoPaginationMethods(
self,
args,
spec,
Expand Down
Loading

0 comments on commit 6b5f39d

Please sign in to comment.