Repository: stripe/stripe-node
Pull request #959 (2020-07-16 15:33:51)

Refactor remaining 'var' to 'let/const' usages
Cleans up the remaining `var` usages and converts them to `let/const` usages. 

Let me know if you can find any other `var`s and I will update the pull request. Thanks!

richardm-stripe: Looks good to me! Thanks for the cleanups.

Patch:
diff --git a/lib/StripeResource.js b/lib/StripeResource.js
index b2d73fde..036812dc 100644
--- a/lib/StripeResource.js
+++ b/lib/StripeResource.js
@@ -124,10 +124,25 @@ StripeResource.prototype = {
         const headers = res.headers || {};
         // NOTE: Stripe responds with lowercase header names/keys.
 
-        // For convenience, make Request-Id easily accessible on
+        // For convenience, make some headers easily accessible on
         // lastResponse.
         res.requestId = headers['request-id'];
 
+        const stripeAccount = headers['stripe-account'];
+        if (stripeAccount) {
+          res.stripeAccount = stripeAccount;
+        }
+
+        const apiVersion = headers['stripe-version'];
+        if (apiVersion) {
+          res.apiVersion = apiVersion;
+        }
+
+        const idempotencyKey = headers['idempotency-key'];
+        if (idempotencyKey) {
+          res.idempotencyKey = idempotencyKey;
+        }
+
         const requestEndTime = Date.now();
         const requestDurationMs = requestEndTime - req._requestStart;
 
@@ -408,7 +423,7 @@ StripeResource.prototype = {
           : this._stripe.getApiField('timeout');
 
       const isInsecureConnection =
-        this._stripe.getApiField('protocol') == 'http';
+        this._stripe.getApiField('protocol') === 'http';
       let agent = this._stripe.getApiField('agent');
       if (agent == null) {
         agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent;
diff --git a/lib/Webhooks.js b/lib/Webhooks.js
index aad76df2..ca362e59 100644
--- a/lib/Webhooks.js
+++ b/lib/Webhooks.js
@@ -48,7 +48,7 @@ const Webhook = {
         opts.secret
       );
 
-    var generatedHeader = [
+    const generatedHeader = [
       't=' + opts.timestamp,
       opts.scheme + '=' + opts.signature,
     ].join(',');
diff --git a/lib/makeRequest.js b/lib/makeRequest.js
index 14740f23..0da41f5b 100644
--- a/lib/makeRequest.js
+++ b/lib/makeRequest.js
@@ -64,8 +64,9 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
 
 function makeRequest(self, requestArgs, spec, overrideData) {
   return new Promise((resolve, reject) => {
+    let opts;
     try {
-      var opts = getRequestOpts(self, requestArgs, spec, overrideData);
+      opts = getRequestOpts(self, requestArgs, spec, overrideData);
     } catch (err) {
       reject(err);
       return;
diff --git a/lib/utils.js b/lib/utils.js
index 551a0620..16c9f95b 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -67,7 +67,7 @@ const utils = (module.exports = {
   /**
    * Outputs a new function with interpolated object property values.
    * Use like so:
-   *   var fn = makeURLInterpolator('some/url/{param1}/{param2}');
+   *   const fn = makeURLInterpolator('some/url/{param1}/{param2}');
    *   fn({ param1: 123, param2: 456 }); // => 'some/url/123/456'
    */
   makeURLInterpolator: (() => {


Test patch:
diff --git a/test/resources/BalanceTransactions.spec.js b/test/resources/BalanceTransactions.spec.js
index c79c3054..c4081a3e 100644
--- a/test/resources/BalanceTransactions.spec.js
+++ b/test/resources/BalanceTransactions.spec.js
@@ -1,7 +1,7 @@
 'use strict';
 
-var stripe = require('../../testUtils').getSpyableStripe();
-var expect = require('chai').expect;
+const stripe = require('../../testUtils').getSpyableStripe();
+const expect = require('chai').expect;
 
 describe('BalanceTransactions Resource', function() {
   describe('retrieve', function() {
diff --git a/test/stripe.spec.js b/test/stripe.spec.js
index 08e27ffd..2de04746 100644
--- a/test/stripe.spec.js
+++ b/test/stripe.spec.js
@@ -6,6 +6,7 @@ const testUtils = require('../testUtils');
 const utils = require('../lib/utils');
 const Stripe = require('../lib/stripe');
 const stripe = require('../lib/stripe')(testUtils.getUserStripeKey(), 'latest');
+const crypto = require('crypto');
 
 const expect = require('chai').expect;
 
@@ -390,22 +391,58 @@ describe('Stripe Module', function() {
           })
         ).to.eventually.equal('Called!'));
 
-      it('Will expose HTTP response object', () =>
-        expect(
-          new Promise((resolve, reject) => {
-            stripe.customers.create(CUSTOMER_DETAILS, (err, customer) => {
-              cleanup.deleteCustomer(customer.id);
+      describe('lastResponse', () => {
+        it('Will expose HTTP response object', () =>
+          expect(
+            new Promise((resolve, reject) => {
+              stripe.customers.create(CUSTOMER_DETAILS, (err, customer) => {
+                cleanup.deleteCustomer(customer.id);
 
-              const headers = customer.lastResponse.headers;
-              expect(headers).to.contain.keys('request-id');
+                const headers = customer.lastResponse.headers;
+                expect(headers).to.contain.keys('request-id');
 
-              expect(customer.lastResponse.requestId).to.match(/^req_/);
-              expect(customer.lastResponse.statusCode).to.equal(200);
+                resolve('Called!');
+              });
+            })
+          ).to.eventually.equal('Called!'));
 
-              resolve('Called!');
-            });
-          })
-        ).to.eventually.equal('Called!'));
+        it('Will have request id, status code and version', () =>
+          expect(
+            new Promise((resolve, reject) => {
+              stripe.customers.create(CUSTOMER_DETAILS, (_err, customer) => {
+                cleanup.deleteCustomer(customer.id);
+
+                expect(customer.lastResponse.requestId).to.match(/^req_/);
+                expect(customer.lastResponse.statusCode).to.equal(200);
+                expect(customer.lastResponse.apiVersion).to.be.a('string').that
+                  .is.not.empty;
+
+                resolve('Called!');
+              });
+            })
+          ).to.eventually.equal('Called!'));
+
+        it('Will have the idempotency key', () =>
+          expect(
+            new Promise((resolve, reject) => {
+              const key = crypto.randomBytes(16).toString('hex');
+
+              stripe.customers.create(
+                CUSTOMER_DETAILS,
+                {
+                  idempotencyKey: key,
+                },
+                (err, customer) => {
+                  cleanup.deleteCustomer(customer.id);
+
+                  expect(customer.lastResponse.idempotencyKey).to.equal(key);
+
+                  resolve('Called!');
+                }
+              );
+            })
+          ).to.eventually.equal('Called!'));
+      });
 
       it('Given an error the callback will receive it', () =>
         expect(
diff --git a/test/utils.spec.js b/test/utils.spec.js
index 5ca1443d..c20f709c 100644
--- a/test/utils.spec.js
+++ b/test/utils.spec.js
@@ -510,8 +510,8 @@ describe('utils', () => {
     it('handles being unable to require `child_process`', () => {
       utils._exec = null;
 
-      var actualErr = null;
-      var actualRes = null;
+      let actualErr = null;
+      let actualRes = null;
       function myCb(err, res) {
         actualErr = err;
         actualRes = res;