Skip to content

Commit

Permalink
lebab transform: arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex-stripe committed May 4, 2019
1 parent 70fd492 commit 08e4905
Show file tree
Hide file tree
Showing 88 changed files with 1,543 additions and 1,697 deletions.
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!');
});
2 changes: 1 addition & 1 deletion lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
117 changes: 55 additions & 62 deletions lib/StripeMethod.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,57 +54,55 @@ module.exports = {
var 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:
var 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: metadata,
},
auth,
{},
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response.metadata);
}
);
}
}.bind(this)
),
}
);
}
}),
cb
);
},
Expand All @@ -119,20 +117,15 @@ module.exports = {
var 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
31 changes: 14 additions & 17 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ StripeResource.prototype = {

_timeoutHandler: function(timeout, req, callback) {
var self = this;

This comment has been minimized.

Copy link
@paulasjes-stripe

paulasjes-stripe May 6, 2019

Contributor

Out of scope for this particular PR, but a followup should be removing all instances of var self = this; where arrow functions have replaced regular functions.

return function() {
return () => {
var timeoutErr = new Error('ETIMEDOUT');
timeoutErr.code = 'ETIMEDOUT';

Expand All @@ -119,14 +119,14 @@ StripeResource.prototype = {

_responseHandler: function(req, callback) {
var self = this;
return function(res) {
return (res) => {
var response = '';

res.setEncoding('utf8');
res.on('data', function(chunk) {
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', function() {
res.on('end', () => {
var headers = res.headers || {};
// NOTE: Stripe responds with lowercase header names/keys.

Expand Down Expand Up @@ -216,7 +216,7 @@ StripeResource.prototype = {

_errorHandler: function(req, requestRetries, callback) {
var self = this;
return function(error) {
return (error) => {
if (req._isAborted) {
// already handled
return;
Expand Down Expand Up @@ -349,7 +349,7 @@ StripeResource.prototype = {
requestData = data;
headers = self._defaultHeaders(auth, requestData.length, apiVersion);

self._stripe.getClientUserAgent(function(cua) {
self._stripe.getClientUserAgent((cua) => {
headers['X-Stripe-Client-User-Agent'] = cua;

if (options.headers) {
Expand Down Expand Up @@ -429,32 +429,29 @@ StripeResource.prototype = {

req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));

req.on('response', function(res) {
req.on('response', (res) => {
if (self._shouldRetry(res, requestRetries)) {
return retryRequest(makeRequest, apiVersion, headers, requestRetries);
} else {
return self._responseHandler(req, callback)(res);
}
});

req.on('error', function(error) {
req.on('error', (error) => {
if (self._shouldRetry(null, requestRetries)) {
return retryRequest(makeRequest, apiVersion, headers, requestRetries);
} else {
return self._errorHandler(req, requestRetries, callback)(error);
}
});

req.on('socket', function(socket) {
req.on('socket', (socket) => {
if (socket.connecting) {
socket.on(
isInsecureConnection ? 'connect' : 'secureConnect',
function() {
// Send payload; we're safe:
req.write(requestData);
req.end();
}
);
socket.on(isInsecureConnection ? 'connect' : 'secureConnect', () => {
// Send payload; we're safe:
req.write(requestData);
req.end();
});
} else {
// we're already connected
req.write(requestData);
Expand Down
2 changes: 1 addition & 1 deletion lib/Webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function parseHeader(header, scheme) {
}

return header.split(',').reduce(
function(accum, item) {
(accum, item) => {
var kv = item.split('=');

if (kv[0] === 't') {
Expand Down
22 changes: 11 additions & 11 deletions lib/autoPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) {
}

function asyncIteratorNext() {
return memoizedPromise(promiseCache, function(resolve, reject) {
return listPromise
return memoizedPromise(promiseCache, (resolve, reject) =>
listPromise
.then(iterate)
.then(resolve)
.catch(reject);
});
.catch(reject)
);
}

var autoPagingEach = makeAutoPagingEach(asyncIteratorNext);
Expand Down Expand Up @@ -162,7 +162,7 @@ function memoizedPromise(promiseCache, cb) {
if (promiseCache.currentPromise) {
return promiseCache.currentPromise;
}
promiseCache.currentPromise = new Promise(cb).then(function(ret) {
promiseCache.currentPromise = new Promise(cb).then((ret) => {
promiseCache.currentPromise = undefined;
return ret;
});
Expand Down Expand Up @@ -199,15 +199,15 @@ function makeAutoPagingToArray(autoPagingEach) {
'You cannot specify a limit of more than 10,000 items to fetch in `autoPagingToArray`; use `autoPagingEach` to iterate through longer lists.'
);
}
var promise = new Promise(function(resolve, reject) {
var promise = new Promise((resolve, reject) => {
var items = [];
autoPagingEach(function(item) {
autoPagingEach((item) => {
items.push(item);
if (items.length >= limit) {
return false;
}
})
.then(function() {
.then(() => {
resolve(items);
})
.catch(reject);
Expand All @@ -217,20 +217,20 @@ function makeAutoPagingToArray(autoPagingEach) {
}

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

var item = iterResult.value;
return new Promise(function(next) {
return new Promise((next) => {
// Bit confusing, perhaps; we pass a `resolve` fn
// to the user, so they can decide when and if to continue.
// They can return false, or a promise which resolves to false, to break.
onItem(item, next);
}).then(function(shouldContinue) {
}).then((shouldContinue) => {
if (shouldContinue === false) {
return handleIteration({done: true});
} else {
Expand Down
8 changes: 2 additions & 6 deletions lib/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
: utils.makeURLInterpolator(spec.path || '');
var requestMethod = (spec.method || 'GET').toUpperCase();
var urlParams = spec.urlParams || [];
var encode =
spec.encode ||
function(data) {
return data;
};
var encode = spec.encode || ((data) => data);
var host = spec.host;

// Don't mutate args externally.
Expand Down Expand Up @@ -110,7 +106,7 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
}

function makeRequest(self, requestArgs, spec, overrideData) {
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
try {
var opts = getRequestOpts(self, requestArgs, spec, overrideData);
} catch (err) {
Expand Down
8 changes: 4 additions & 4 deletions lib/resources/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ module.exports = StripeResource.extend({
function streamProcessor(fn) {
var bufferArray = [];
data.file.data
.on('data', function(line) {
.on('data', (line) => {
bufferArray.push(line);
})
.on('end', function() {
.on('end', () => {
var bufferData = Object.assign({}, data);
bufferData.file.data = Buffer.concat(bufferArray);
var buffer = fn(method, bufferData, headers);
callback(null, buffer);
})
.on('error', function(err) {
.on('error', (err) => {
var errorHandler = streamError(callback);
errorHandler(err);
});
Expand All @@ -54,7 +54,7 @@ module.exports = StripeResource.extend({
this.detail = raw.detail;
},
});
return function(error) {
return (error) => {
callback(
new StreamProcessingError({
message:
Expand Down
6 changes: 3 additions & 3 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Stripe.prototype = {

info = info || {};

var appInfo = APP_INFO_PROPERTIES.reduce(function(accum, prop) {
var appInfo = APP_INFO_PROPERTIES.reduce((accum, prop) => {
if (typeof info[prop] == 'string') {
accum = accum || {};

Expand Down Expand Up @@ -285,7 +285,7 @@ Stripe.prototype = {
if (Stripe.USER_AGENT_SERIALIZED) {
return cb(Stripe.USER_AGENT_SERIALIZED);
}
this.getClientUserAgentSeeded(Stripe.USER_AGENT, function(cua) {
this.getClientUserAgentSeeded(Stripe.USER_AGENT, (cua) => {
Stripe.USER_AGENT_SERIALIZED = cua;
cb(Stripe.USER_AGENT_SERIALIZED);
});
Expand All @@ -296,7 +296,7 @@ Stripe.prototype = {
getClientUserAgentSeeded: function(seed, cb) {
var self = this;

utils.safeExec('uname -a', function(err, uname) {
utils.safeExec('uname -a', (err, uname) => {
var userAgent = {};
for (var field in seed) {
userAgent[field] = encodeURIComponent(seed[field]);
Expand Down
Loading

0 comments on commit 08e4905

Please sign in to comment.