From 6a90c61435996410287a9b94e412c22e0f99ff1f Mon Sep 17 00:00:00 2001 From: "Alex Rattray (Stripe)" Date: Fri, 10 May 2019 16:03:26 -0700 Subject: [PATCH] Remove "curried" nested resources and manually specified urlParams (#625) * Drop support for optional url params * Delete nested resource files * Remove urlData * Extract urlParams from path instead of manual definition Verified this is no different with: ```js const urlParams = utils.extractUrlParams(spec.path || ''); if ( !(spec.urlParams || []).every((x, i) => urlParams[i] === x) || (spec.urlParams || []).length !== urlParams.length ) { throw Error( 'mismatch' + JSON.stringify(urlParams) + JSON.stringify(spec.urlParams) ); } ``` inside StripeMethod * Remove manually specified urlParams * Add a deprecation error message * Revert "Delete nested resource files" This reverts commit d88a3e715b20cbe9478b05140e825c381bc5f817. * Fix nested resources for non-curried urlParams and update tests to demonstrate their use * Refactor makeRequest * Revert "Revert "Delete nested resource files"" This reverts commit e5eccb85dc87dd4406766c97131445629ea1e6c2. --- lib/StripeMethod.basic.js | 3 - lib/StripeMethod.js | 4 + lib/StripeResource.js | 21 ++--- lib/makeRequest.js | 47 +++-------- lib/resources/Accounts.js | 15 ---- lib/resources/ApplicationFeeRefunds.js | 21 ----- lib/resources/ApplicationFees.js | 4 - lib/resources/BitcoinReceivers.js | 1 - lib/resources/Charges.js | 1 - lib/resources/CreditNotes.js | 1 - lib/resources/Customers.js | 11 --- lib/resources/Disputes.js | 5 +- lib/resources/Invoices.js | 6 -- lib/resources/Issuing/Authorizations.js | 2 - lib/resources/Issuing/Cards.js | 1 - lib/resources/LoginLinks.js | 9 --- lib/resources/Orders.js | 6 +- lib/resources/PaymentIntents.js | 3 - lib/resources/PaymentMethods.js | 2 - lib/resources/Payouts.js | 1 - lib/resources/Persons.js | 9 --- lib/resources/Recipients.js | 5 -- lib/resources/Reviews.js | 1 - lib/resources/Sources.js | 2 - .../SubscriptionScheduleRevisions.js | 9 --- lib/resources/SubscriptionSchedules.js | 4 - lib/resources/Subscriptions.js | 1 - lib/resources/TaxIds.js | 9 --- lib/resources/Topups.js | 1 - lib/resources/TransferReversals.js | 9 --- lib/resources/Transfers.js | 4 - lib/resources/UsageRecordSummaries.js | 1 - lib/resources/UsageRecords.js | 1 - lib/stripe.js | 8 -- lib/utils.js | 9 +++ test/resources/ApplicationFeeRefunds.spec.js | 56 ------------- test/resources/LoginLinks.spec.js | 27 ------- test/resources/Persons.spec.js | 80 ------------------- .../SubscriptionScheduleRevision.spec.js | 42 ---------- test/resources/TaxIds.spec.js | 67 ---------------- test/resources/TransferReversals.spec.js | 70 ---------------- test/utils.spec.js | 10 +++ 42 files changed, 46 insertions(+), 543 deletions(-) delete mode 100644 lib/resources/ApplicationFeeRefunds.js delete mode 100644 lib/resources/LoginLinks.js delete mode 100644 lib/resources/Persons.js delete mode 100644 lib/resources/SubscriptionScheduleRevisions.js delete mode 100644 lib/resources/TaxIds.js delete mode 100644 lib/resources/TransferReversals.js delete mode 100644 test/resources/ApplicationFeeRefunds.spec.js delete mode 100644 test/resources/LoginLinks.spec.js delete mode 100644 test/resources/Persons.spec.js delete mode 100644 test/resources/SubscriptionScheduleRevision.spec.js delete mode 100644 test/resources/TaxIds.spec.js delete mode 100644 test/resources/TransferReversals.spec.js diff --git a/lib/StripeMethod.basic.js b/lib/StripeMethod.basic.js index efbd6a48a0..c807ea1849 100644 --- a/lib/StripeMethod.basic.js +++ b/lib/StripeMethod.basic.js @@ -15,19 +15,16 @@ module.exports = { retrieve: stripeMethod({ method: 'GET', path: '/{id}', - urlParams: ['id'], }), update: stripeMethod({ method: 'POST', path: '{id}', - urlParams: ['id'], }), // Avoid 'delete' keyword in JS del: stripeMethod({ method: 'DELETE', path: '{id}', - urlParams: ['id'], }), }; diff --git a/lib/StripeMethod.js b/lib/StripeMethod.js index 99f18426a7..dd8c6b846e 100644 --- a/lib/StripeMethod.js +++ b/lib/StripeMethod.js @@ -26,6 +26,10 @@ function stripeMethod(spec) { const callback = typeof args[args.length - 1] == 'function' && args.pop(); + spec.urlParams = utils.extractUrlParams( + self.createResourcePathWithSymbols(spec.path || '') + ); + const requestPromise = utils.callbackifyPromiseWithTimeout( makeRequest(self, args, spec, {}), callback diff --git a/lib/StripeResource.js b/lib/StripeResource.js index a252123451..cfe509156a 100644 --- a/lib/StripeResource.js +++ b/lib/StripeResource.js @@ -8,8 +8,6 @@ const uuid = require('uuid/v4'); const utils = require('./utils'); const Error = require('./Error'); -const hasOwn = {}.hasOwnProperty; - const defaultHttpAgent = new http.Agent({keepAlive: true}); const defaultHttpsAgent = new https.Agent({keepAlive: true}); @@ -25,9 +23,13 @@ StripeResource.MAX_BUFFERED_REQUEST_METRICS = 100; /** * Encapsulates request logic for a Stripe Resource */ -function StripeResource(stripe, urlData) { +function StripeResource(stripe, deprecatedUrlData) { this._stripe = stripe; - this._urlData = urlData || {}; + if (deprecatedUrlData) { + throw new Error( + 'Support for curried url params was dropped in stripe-node v7.0.0. Instead, pass two ids.' + ); + } this.basePath = utils.makeURLInterpolator( this.basePath || stripe.getApiField('basePath') @@ -81,17 +83,6 @@ StripeResource.prototype = { .replace(/\\/g, '/')}`; // ugly workaround for Windows }, - createUrlData() { - const urlData = {}; - // Merge in baseData - for (const i in this._urlData) { - if (hasOwn.call(this._urlData, i)) { - urlData[i] = this._urlData[i]; - } - } - return urlData; - }, - // DEPRECATED: Here for backcompat in case users relied on this. wrapTimeout: utils.callbackifyPromiseWithTimeout, diff --git a/lib/makeRequest.js b/lib/makeRequest.js index cb49d4c2ed..c6b105b980 100644 --- a/lib/makeRequest.js +++ b/lib/makeRequest.js @@ -1,59 +1,31 @@ 'use strict'; const utils = require('./utils'); -const OPTIONAL_REGEX = /^optional!/; function getRequestOpts(self, requestArgs, spec, overrideData) { // Extract spec values with defaults. - const commandPath = - typeof spec.path == 'function' - ? spec.path - : utils.makeURLInterpolator(spec.path || ''); + const commandPath = utils.makeURLInterpolator(spec.path || ''); const requestMethod = (spec.method || 'GET').toUpperCase(); const urlParams = spec.urlParams || []; const encode = spec.encode || ((data) => data); const host = spec.host; + const path = self.createResourcePathWithSymbols(spec.path); // Don't mutate args externally. const args = [].slice.call(requestArgs); // Generate and validate url params. - const urlData = self.createUrlData(); - for (let i = 0, l = urlParams.length; i < l; ++i) { - var path; - - // Note that we shift the args array after every iteration so this just - // grabs the "next" argument for use as a URL parameter. - const arg = args[0]; - - let param = urlParams[i]; - - const isOptional = OPTIONAL_REGEX.test(param); - param = param.replace(OPTIONAL_REGEX, ''); - - if (param == 'id' && typeof arg !== 'string') { - path = self.createResourcePathWithSymbols(spec.path); + const urlData = urlParams.reduce((urlData, param) => { + const arg = args.shift(); + if (typeof arg !== 'string') { throw new Error( - `Stripe: "id" must be a string, but got: ${typeof arg} (on API request to \`${requestMethod} ${path}\`)` + `Stripe: Argument "${param}" must be a string, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)` ); } - if (!arg) { - if (isOptional) { - urlData[param] = ''; - continue; - } - - path = self.createResourcePathWithSymbols(spec.path); - throw new Error( - `Stripe: Argument "${ - urlParams[i] - }" required, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)` - ); - } - - urlData[param] = args.shift(); - } + urlData[param] = arg; + return urlData; + }, {}); // Pull request data and options (headers, auth) from args. const dataFromArgs = utils.getDataFromArgs(args); @@ -62,7 +34,6 @@ function getRequestOpts(self, requestArgs, spec, overrideData) { // Validate that there are no more args. if (args.length) { - path = self.createResourcePathWithSymbols(spec.path); throw new Error( `Stripe: Unknown arguments (${args}). Did you mean to pass an options object? See https://github.com/stripe/stripe-node/wiki/Passing-Options. (on API request to ${requestMethod} \`${path}\`)` ); diff --git a/lib/resources/Accounts.js b/lib/resources/Accounts.js index 75e25acf1d..a1a318804e 100644 --- a/lib/resources/Accounts.js +++ b/lib/resources/Accounts.js @@ -20,20 +20,17 @@ module.exports = StripeResource.extend({ update: stripeMethod({ method: 'POST', path: 'accounts/{id}', - urlParams: ['id'], }), // Avoid 'delete' keyword in JS del: stripeMethod({ method: 'DELETE', path: 'accounts/{id}', - urlParams: ['id'], }), reject: stripeMethod({ method: 'POST', path: 'accounts/{id}/reject', - urlParams: ['id'], }), retrieve(id) { @@ -43,7 +40,6 @@ module.exports = StripeResource.extend({ return stripeMethod({ method: 'GET', path: 'accounts/{id}', - urlParams: ['id'], }).apply(this, arguments); } else { if (id === null || id === undefined) { @@ -64,32 +60,27 @@ module.exports = StripeResource.extend({ createExternalAccount: stripeMethod({ method: 'POST', path: 'accounts/{accountId}/external_accounts', - urlParams: ['accountId'], }), listExternalAccounts: stripeMethod({ method: 'GET', path: 'accounts/{accountId}/external_accounts', - urlParams: ['accountId'], methodType: 'list', }), retrieveExternalAccount: stripeMethod({ method: 'GET', path: 'accounts/{accountId}/external_accounts/{externalAccountId}', - urlParams: ['accountId', 'externalAccountId'], }), updateExternalAccount: stripeMethod({ method: 'POST', path: 'accounts/{accountId}/external_accounts/{externalAccountId}', - urlParams: ['accountId', 'externalAccountId'], }), deleteExternalAccount: stripeMethod({ method: 'DELETE', path: 'accounts/{accountId}/external_accounts/{externalAccountId}', - urlParams: ['accountId', 'externalAccountId'], }), /** @@ -99,7 +90,6 @@ module.exports = StripeResource.extend({ createLoginLink: stripeMethod({ method: 'POST', path: 'accounts/{accountId}/login_links', - urlParams: ['accountId'], }), /** @@ -109,31 +99,26 @@ module.exports = StripeResource.extend({ createPerson: stripeMethod({ method: 'POST', path: 'accounts/{accountId}/persons', - urlParams: ['accountId'], }), listPersons: stripeMethod({ method: 'GET', path: 'accounts/{accountId}/persons', - urlParams: ['accountId'], methodType: 'list', }), retrievePerson: stripeMethod({ method: 'GET', path: 'accounts/{accountId}/persons/{personId}', - urlParams: ['accountId', 'personId'], }), updatePerson: stripeMethod({ method: 'POST', path: 'accounts/{accountId}/persons/{personId}', - urlParams: ['accountId', 'personId'], }), deletePerson: stripeMethod({ method: 'DELETE', path: 'accounts/{accountId}/persons/{personId}', - urlParams: ['accountId', 'personId'], }), }); diff --git a/lib/resources/ApplicationFeeRefunds.js b/lib/resources/ApplicationFeeRefunds.js deleted file mode 100644 index 68d92045be..0000000000 --- a/lib/resources/ApplicationFeeRefunds.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -const StripeResource = require('../StripeResource'); - -/** - * ApplicationFeeRefunds is a unique resource in that, upon instantiation, - * requires an application fee id , and therefore each of its methods only - * require the refundId argument. - * - * This streamlines the API specifically for the case of accessing refunds - * on a returned application fee object. - * - * E.g. applicationFeeObject.refunds.retrieve(refundId) - * (As opposed to the also-supported stripe.applicationFees.retrieveRefund(chargeId, - * refundId)) - */ -module.exports = StripeResource.extend({ - path: 'application_fees/{feeId}/refunds', - - includeBasic: ['create', 'list', 'retrieve', 'update'], -}); diff --git a/lib/resources/ApplicationFees.js b/lib/resources/ApplicationFees.js index 3c676057f3..0a439cc1d0 100644 --- a/lib/resources/ApplicationFees.js +++ b/lib/resources/ApplicationFees.js @@ -11,25 +11,21 @@ module.exports = StripeResource.extend({ createRefund: stripeMethod({ method: 'POST', path: '/{feeId}/refunds', - urlParams: ['feeId'], }), listRefunds: stripeMethod({ method: 'GET', path: '/{feeId}/refunds', - urlParams: ['feeId'], methodType: 'list', }), retrieveRefund: stripeMethod({ method: 'GET', path: '/{feeId}/refunds/{refundId}', - urlParams: ['feeId', 'refundId'], }), updateRefund: stripeMethod({ method: 'POST', path: '/{feeId}/refunds/{refundId}', - urlParams: ['feeId', 'refundId'], }), }); diff --git a/lib/resources/BitcoinReceivers.js b/lib/resources/BitcoinReceivers.js index 7e4fe033c6..1ee13383e0 100644 --- a/lib/resources/BitcoinReceivers.js +++ b/lib/resources/BitcoinReceivers.js @@ -11,7 +11,6 @@ module.exports = StripeResource.extend({ listTransactions: stripeMethod({ method: 'GET', path: '/{id}/transactions', - urlParams: ['id'], methodType: 'list', }), }); diff --git a/lib/resources/Charges.js b/lib/resources/Charges.js index c29a54e1fd..1c5dd469eb 100644 --- a/lib/resources/Charges.js +++ b/lib/resources/Charges.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ capture: stripeMethod({ method: 'POST', path: '/{id}/capture', - urlParams: ['id'], }), }); diff --git a/lib/resources/CreditNotes.js b/lib/resources/CreditNotes.js index 5bf04705c5..e38dd295ca 100644 --- a/lib/resources/CreditNotes.js +++ b/lib/resources/CreditNotes.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ voidCreditNote: stripeMethod({ method: 'POST', path: '/{id}/void', - urlParams: ['id'], }), }); diff --git a/lib/resources/Customers.js b/lib/resources/Customers.js index cc57d9c108..1255184b93 100644 --- a/lib/resources/Customers.js +++ b/lib/resources/Customers.js @@ -15,7 +15,6 @@ module.exports = StripeResource.extend({ deleteDiscount: stripeMethod({ method: 'DELETE', path: '/{customerId}/discount', - urlParams: ['customerId'], }), /** @@ -25,38 +24,32 @@ module.exports = StripeResource.extend({ createSource: stripeMethod({ method: 'POST', path: '/{customerId}/sources', - urlParams: ['customerId'], }), listSources: stripeMethod({ method: 'GET', path: '/{customerId}/sources', - urlParams: ['customerId'], methodType: 'list', }), retrieveSource: stripeMethod({ method: 'GET', path: '/{customerId}/sources/{sourceId}', - urlParams: ['customerId', 'sourceId'], }), updateSource: stripeMethod({ method: 'POST', path: '/{customerId}/sources/{sourceId}', - urlParams: ['customerId', 'sourceId'], }), deleteSource: stripeMethod({ method: 'DELETE', path: '/{customerId}/sources/{sourceId}', - urlParams: ['customerId', 'sourceId'], }), verifySource: stripeMethod({ method: 'POST', path: '/{customerId}/sources/{sourceId}/verify', - urlParams: ['customerId', 'sourceId'], }), /** @@ -66,25 +59,21 @@ module.exports = StripeResource.extend({ createTaxId: stripeMethod({ method: 'POST', path: '/{customerId}/tax_ids', - urlParams: ['customerId'], }), deleteTaxId: stripeMethod({ method: 'DELETE', path: '/{customerId}/tax_ids/{taxIdId}', - urlParams: ['customerId', 'taxIdId'], }), listTaxIds: stripeMethod({ method: 'GET', path: '/{customerId}/tax_ids', - urlParams: ['customerId'], methodType: 'list', }), retrieveTaxId: stripeMethod({ method: 'GET', path: '/{customerId}/tax_ids/{taxIdId}', - urlParams: ['customerId', 'taxIdId'], }), }); diff --git a/lib/resources/Disputes.js b/lib/resources/Disputes.js index 836bf663f7..1d476249a0 100644 --- a/lib/resources/Disputes.js +++ b/lib/resources/Disputes.js @@ -8,5 +8,8 @@ module.exports = StripeResource.extend({ includeBasic: ['list', 'retrieve', 'update'], - close: stripeMethod({method: 'POST', path: '/{id}/close', urlParams: ['id']}), + close: stripeMethod({ + method: 'POST', + path: '/{id}/close', + }), }); diff --git a/lib/resources/Invoices.js b/lib/resources/Invoices.js index 6de26da88c..5faca3ff0c 100644 --- a/lib/resources/Invoices.js +++ b/lib/resources/Invoices.js @@ -11,13 +11,11 @@ module.exports = StripeResource.extend({ finalizeInvoice: stripeMethod({ method: 'POST', path: '{id}/finalize', - urlParams: ['id'], }), listLineItems: stripeMethod({ method: 'GET', path: '{id}/lines', - urlParams: ['id'], }), listUpcomingLineItems: stripeMethod({ @@ -28,13 +26,11 @@ module.exports = StripeResource.extend({ markUncollectible: stripeMethod({ method: 'POST', path: '{id}/mark_uncollectible', - urlParams: ['id'], }), pay: stripeMethod({ method: 'POST', path: '{id}/pay', - urlParams: ['id'], }), retrieveUpcoming: stripeMethod({ @@ -45,12 +41,10 @@ module.exports = StripeResource.extend({ sendInvoice: stripeMethod({ method: 'POST', path: '{id}/send', - urlParams: ['id'], }), voidInvoice: stripeMethod({ method: 'POST', path: '{id}/void', - urlParams: ['id'], }), }); diff --git a/lib/resources/Issuing/Authorizations.js b/lib/resources/Issuing/Authorizations.js index af8a3f55f2..1be1546969 100644 --- a/lib/resources/Issuing/Authorizations.js +++ b/lib/resources/Issuing/Authorizations.js @@ -11,12 +11,10 @@ module.exports = StripeResource.extend({ approve: stripeMethod({ method: 'POST', path: '/{id}/approve', - urlParams: ['id'], }), decline: stripeMethod({ method: 'POST', path: '/{id}/decline', - urlParams: ['id'], }), }); diff --git a/lib/resources/Issuing/Cards.js b/lib/resources/Issuing/Cards.js index 9f5e41b876..81475c9524 100644 --- a/lib/resources/Issuing/Cards.js +++ b/lib/resources/Issuing/Cards.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ retrieveDetails: stripeMethod({ method: 'GET', path: '/{id}/details', - urlParams: ['id'], }), }); diff --git a/lib/resources/LoginLinks.js b/lib/resources/LoginLinks.js deleted file mode 100644 index 842a447ddb..0000000000 --- a/lib/resources/LoginLinks.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const StripeResource = require('../StripeResource'); - -module.exports = StripeResource.extend({ - path: 'accounts/{accountId}/login_links', - - includeBasic: ['create'], -}); diff --git a/lib/resources/Orders.js b/lib/resources/Orders.js index e368bae4bd..b242e3cf30 100644 --- a/lib/resources/Orders.js +++ b/lib/resources/Orders.js @@ -8,11 +8,13 @@ module.exports = StripeResource.extend({ includeBasic: ['create', 'list', 'retrieve', 'update'], - pay: stripeMethod({method: 'POST', path: '/{id}/pay', urlParams: ['id']}), + pay: stripeMethod({ + method: 'POST', + path: '/{id}/pay', + }), returnOrder: stripeMethod({ method: 'POST', path: '/{id}/returns', - urlParams: ['id'], }), }); diff --git a/lib/resources/PaymentIntents.js b/lib/resources/PaymentIntents.js index df99d2a36b..54da65e251 100644 --- a/lib/resources/PaymentIntents.js +++ b/lib/resources/PaymentIntents.js @@ -11,18 +11,15 @@ module.exports = StripeResource.extend({ cancel: stripeMethod({ method: 'POST', path: '/{id}/cancel', - urlParams: ['id'], }), capture: stripeMethod({ method: 'POST', path: '/{id}/capture', - urlParams: ['id'], }), confirm: stripeMethod({ method: 'POST', path: '/{id}/confirm', - urlParams: ['id'], }), }); diff --git a/lib/resources/PaymentMethods.js b/lib/resources/PaymentMethods.js index be727a6bbd..38e3bd893a 100644 --- a/lib/resources/PaymentMethods.js +++ b/lib/resources/PaymentMethods.js @@ -11,12 +11,10 @@ module.exports = StripeResource.extend({ attach: stripeMethod({ method: 'POST', path: '/{id}/attach', - urlParams: ['id'], }), detach: stripeMethod({ method: 'POST', path: '/{id}/detach', - urlParams: ['id'], }), }); diff --git a/lib/resources/Payouts.js b/lib/resources/Payouts.js index 384688e803..31785ef458 100644 --- a/lib/resources/Payouts.js +++ b/lib/resources/Payouts.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ cancel: stripeMethod({ method: 'POST', path: '/{id}/cancel', - urlParams: ['id'], }), }); diff --git a/lib/resources/Persons.js b/lib/resources/Persons.js deleted file mode 100644 index 921561ea64..0000000000 --- a/lib/resources/Persons.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const StripeResource = require('../StripeResource'); - -module.exports = StripeResource.extend({ - path: 'accounts/{accountId}/persons', - - includeBasic: ['create', 'del', 'list', 'retrieve', 'update'], -}); diff --git a/lib/resources/Recipients.js b/lib/resources/Recipients.js index 2cff998537..84e594e701 100644 --- a/lib/resources/Recipients.js +++ b/lib/resources/Recipients.js @@ -15,31 +15,26 @@ module.exports = StripeResource.extend({ createCard: stripeMethod({ method: 'POST', path: '/{id}/cards', - urlParams: ['id'], }), listCards: stripeMethod({ method: 'GET', path: '/{id}/cards', - urlParams: ['id'], methodType: 'list', }), retrieveCard: stripeMethod({ method: 'GET', path: '/{recipientId}/cards/{cardId}', - urlParams: ['recipientId', 'cardId'], }), updateCard: stripeMethod({ method: 'POST', path: '/{recipientId}/cards/{cardId}', - urlParams: ['recipientId', 'cardId'], }), deleteCard: stripeMethod({ method: 'DELETE', path: '/{recipientId}/cards/{cardId}', - urlParams: ['recipientId', 'cardId'], }), }); diff --git a/lib/resources/Reviews.js b/lib/resources/Reviews.js index 40a150512d..8bbd670cff 100644 --- a/lib/resources/Reviews.js +++ b/lib/resources/Reviews.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ approve: stripeMethod({ method: 'POST', path: '/{id}/approve', - urlParams: ['id'], }), }); diff --git a/lib/resources/Sources.js b/lib/resources/Sources.js index ed213880d7..6afc7bbfe0 100644 --- a/lib/resources/Sources.js +++ b/lib/resources/Sources.js @@ -11,13 +11,11 @@ module.exports = StripeResource.extend({ listSourceTransactions: stripeMethod({ method: 'GET', path: '/{id}/source_transactions', - urlParams: ['id'], methodType: 'list', }), verify: stripeMethod({ method: 'POST', path: '/{id}/verify', - urlParams: ['id'], }), }); diff --git a/lib/resources/SubscriptionScheduleRevisions.js b/lib/resources/SubscriptionScheduleRevisions.js deleted file mode 100644 index 7748b9a721..0000000000 --- a/lib/resources/SubscriptionScheduleRevisions.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const StripeResource = require('../StripeResource'); - -module.exports = StripeResource.extend({ - path: 'subscription_schedules/{scheduleId}/revisions', - - includeBasic: ['list', 'retrieve'], -}); diff --git a/lib/resources/SubscriptionSchedules.js b/lib/resources/SubscriptionSchedules.js index e78ed46ef1..e803533c2c 100644 --- a/lib/resources/SubscriptionSchedules.js +++ b/lib/resources/SubscriptionSchedules.js @@ -11,25 +11,21 @@ module.exports = StripeResource.extend({ cancel: stripeMethod({ method: 'POST', path: '/{id}/cancel', - urlParams: ['id'], }), release: stripeMethod({ method: 'POST', path: '/{id}/release', - urlParams: ['id'], }), listRevisions: stripeMethod({ method: 'GET', path: '/{scheduleId}/revisions', - urlParams: ['scheduleId'], methodType: 'list', }), retrieveRevision: stripeMethod({ method: 'GET', path: '/{scheduleId}/revisions/{revisionId}', - urlParams: ['scheduleId', 'revisionId'], }), }); diff --git a/lib/resources/Subscriptions.js b/lib/resources/Subscriptions.js index 72290a31a5..3b404c2eb8 100644 --- a/lib/resources/Subscriptions.js +++ b/lib/resources/Subscriptions.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ deleteDiscount: stripeMethod({ method: 'DELETE', path: '/{id}/discount', - urlParams: ['id'], }), }); diff --git a/lib/resources/TaxIds.js b/lib/resources/TaxIds.js deleted file mode 100644 index 7cc865bd87..0000000000 --- a/lib/resources/TaxIds.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const StripeResource = require('../StripeResource'); - -module.exports = StripeResource.extend({ - path: 'customers/{customerId}/tax_ids', - - includeBasic: ['create', 'del', 'list', 'retrieve'], -}); diff --git a/lib/resources/Topups.js b/lib/resources/Topups.js index d2f487a8d7..eecb88207d 100644 --- a/lib/resources/Topups.js +++ b/lib/resources/Topups.js @@ -11,6 +11,5 @@ module.exports = StripeResource.extend({ cancel: stripeMethod({ method: 'POST', path: '/{id}/cancel', - urlParams: ['id'], }), }); diff --git a/lib/resources/TransferReversals.js b/lib/resources/TransferReversals.js deleted file mode 100644 index 207b653491..0000000000 --- a/lib/resources/TransferReversals.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const StripeResource = require('../StripeResource'); - -module.exports = StripeResource.extend({ - path: 'transfers/{transferId}/reversals', - - includeBasic: ['create', 'list', 'retrieve', 'update'], -}); diff --git a/lib/resources/Transfers.js b/lib/resources/Transfers.js index 3bbe6f8d9e..75f0007ac7 100644 --- a/lib/resources/Transfers.js +++ b/lib/resources/Transfers.js @@ -14,25 +14,21 @@ module.exports = StripeResource.extend({ createReversal: stripeMethod({ method: 'POST', path: '/{id}/reversals', - urlParams: ['id'], }), listReversals: stripeMethod({ method: 'GET', path: '/{id}/reversals', - urlParams: ['id'], methodType: 'list', }), retrieveReversal: stripeMethod({ method: 'GET', path: '/{transferId}/reversals/{reversalId}', - urlParams: ['transferId', 'reversalId'], }), updateReversal: stripeMethod({ method: 'POST', path: '/{transferId}/reversals/{reversalId}', - urlParams: ['transferId', 'reversalId'], }), }); diff --git a/lib/resources/UsageRecordSummaries.js b/lib/resources/UsageRecordSummaries.js index 8a59a43ef6..db4722188a 100644 --- a/lib/resources/UsageRecordSummaries.js +++ b/lib/resources/UsageRecordSummaries.js @@ -9,7 +9,6 @@ module.exports = StripeResource.extend({ list: stripeMethod({ method: 'GET', path: '{subscriptionItem}/usage_record_summaries', - urlParams: ['subscriptionItem'], methodType: 'list', }), }); diff --git a/lib/resources/UsageRecords.js b/lib/resources/UsageRecords.js index c45d448a65..ad8960b8b2 100644 --- a/lib/resources/UsageRecords.js +++ b/lib/resources/UsageRecords.js @@ -9,6 +9,5 @@ module.exports = StripeResource.extend({ create: stripeMethod({ method: 'POST', path: '/{id}/usage_records', - urlParams: ['id'], }), }); diff --git a/lib/stripe.js b/lib/stripe.js index 4250054163..99777370ef 100644 --- a/lib/stripe.js +++ b/lib/stripe.js @@ -55,7 +55,6 @@ const resources = { InvoiceItems: require('./resources/InvoiceItems'), Invoices: require('./resources/Invoices'), IssuerFraudRecords: require('./resources/IssuerFraudRecords'), - LoginLinks: require('./resources/LoginLinks'), OAuth: require('./resources/OAuth'), OrderReturns: require('./resources/OrderReturns'), Orders: require('./resources/Orders'), @@ -81,13 +80,6 @@ const resources = { UsageRecordSummaries: require('./resources/UsageRecordSummaries'), WebhookEndpoints: require('./resources/WebhookEndpoints'), - // The following rely on pre-filled IDs: - ApplicationFeeRefunds: require('./resources/ApplicationFeeRefunds'), - Persons: require('./resources/Persons'), - SubscriptionScheduleRevisions: require('./resources/SubscriptionScheduleRevisions'), - TaxIds: require('./resources/TaxIds'), - TransferReversals: require('./resources/TransferReversals'), - // Namespaced resources Checkout: resourceNamespace('checkout', { Sessions: require('./resources/Checkout/Sessions'), diff --git a/lib/utils.js b/lib/utils.js index ae8ddfd778..d2aeb370e1 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -66,6 +66,15 @@ const utils = (module.exports = { }; })(), + extractUrlParams: (path) => { + const params = path.match(/\{\w+\}/g); + if (!params) { + return []; + } + + return params.map((param) => param.replace(/[{}]/g, '')); + }, + /** * Return the data argument from a list of arguments */ diff --git a/test/resources/ApplicationFeeRefunds.spec.js b/test/resources/ApplicationFeeRefunds.spec.js deleted file mode 100644 index 75be78aa2e..0000000000 --- a/test/resources/ApplicationFeeRefunds.spec.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict'; - -const resources = require('../../lib/stripe').resources; -const stripe = require('../../testUtils').getSpyableStripe(); -const expect = require('chai').expect; - -const APPFEE_TEST_ID = 'appFeeIdTest999'; -const REFUND_TEST_ID = 'refundIdTest999'; - -// Create new CustomerCard instance with pre-filled customerId: -const appFeeRefund = new resources.ApplicationFeeRefunds(stripe, { - feeId: APPFEE_TEST_ID, -}); - -// Use spy from existing resource: -appFeeRefund._request = stripe.customers._request; - -describe('ApplicationFeeRefund Resource', () => { - describe('retrieve', () => { - it('Sends the correct request', () => { - appFeeRefund.retrieve(REFUND_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/application_fees/${APPFEE_TEST_ID}/refunds/${REFUND_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); - - describe('update', () => { - it('Sends the correct request', () => { - appFeeRefund.update(REFUND_TEST_ID, { - metadata: {key: 'value'}, - }); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/application_fees/${APPFEE_TEST_ID}/refunds/${REFUND_TEST_ID}`, - data: {metadata: {key: 'value'}}, - headers: {}, - }); - }); - }); - - describe('list', () => { - it('Sends the correct request', () => { - appFeeRefund.list(); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/application_fees/${APPFEE_TEST_ID}/refunds`, - data: {}, - headers: {}, - }); - }); - }); -}); diff --git a/test/resources/LoginLinks.spec.js b/test/resources/LoginLinks.spec.js deleted file mode 100644 index f8c6c36183..0000000000 --- a/test/resources/LoginLinks.spec.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const resources = require('../../lib/stripe').resources; -const stripe = require('../../testUtils').getSpyableStripe(); -const expect = require('chai').expect; - -const ACCOUNT_ID = 'acct_EXPRESS'; - -// Create new LoginLink instance with pre-filled accountId: -const loginLink = new resources.LoginLinks(stripe, {accountId: ACCOUNT_ID}); - -// Use spy from existing resource: -loginLink._request = stripe.customers._request; - -describe('LoginLink Resource', () => { - describe('create', () => { - it('Sends the correct request', () => { - loginLink.create(); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/accounts/${ACCOUNT_ID}/login_links`, - headers: {}, - data: {}, - }); - }); - }); -}); diff --git a/test/resources/Persons.spec.js b/test/resources/Persons.spec.js deleted file mode 100644 index 0b199f6715..0000000000 --- a/test/resources/Persons.spec.js +++ /dev/null @@ -1,80 +0,0 @@ -'use strict'; - -const resources = require('../../lib/stripe').resources; -const stripe = require('../../testUtils').getSpyableStripe(); -const expect = require('chai').expect; - -const ACCOUNT_TEST_ID = 'acct_123'; -const PERSON_TEST_ID = 'person_123'; - -// Create new Person instance with pre-filled accountId: -const person = new resources.Persons(stripe, {accountId: ACCOUNT_TEST_ID}); - -// Use spy from existing resource: -person._request = stripe.customers._request; - -describe('Person Resource', () => { - describe('create', () => { - it('Sends the correct request', () => { - person.create({ - first_name: 'John', - }); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons`, - data: {first_name: 'John'}, - headers: {}, - }); - }); - }); - - describe('delete', () => { - it('Sends the correct request', () => { - person.del(PERSON_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'DELETE', - url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons/${PERSON_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); - - describe('list', () => { - it('Sends the correct request', () => { - person.list(); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons`, - data: {}, - headers: {}, - }); - }); - }); - - describe('retrieve', () => { - it('Sends the correct request', () => { - person.retrieve(PERSON_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons/${PERSON_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); - - describe('update', () => { - it('Sends the correct request', () => { - person.update(PERSON_TEST_ID, { - first_name: 'John', - }); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons/${PERSON_TEST_ID}`, - data: {first_name: 'John'}, - headers: {}, - }); - }); - }); -}); diff --git a/test/resources/SubscriptionScheduleRevision.spec.js b/test/resources/SubscriptionScheduleRevision.spec.js deleted file mode 100644 index 4c15320d7a..0000000000 --- a/test/resources/SubscriptionScheduleRevision.spec.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict'; - -const resources = require('../../lib/stripe').resources; -const stripe = require('../../testUtils').getSpyableStripe(); -const expect = require('chai').expect; - -const SCHEDULE_TEST_ID = 'sub_sched_123'; -const REVISION_TEST_ID = 'sub_sched_rev_123'; - -// Create new SubscriptionScheduleRevision instance with pre-filled scheduleId: -const revision = new resources.SubscriptionScheduleRevisions(stripe, { - scheduleId: SCHEDULE_TEST_ID, -}); - -// Use spy from existing resource: -revision._request = stripe.customers._request; - -describe('SubscriptionScheduleRevision Resource', () => { - describe('list', () => { - it('Sends the correct request', () => { - revision.list(); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/subscription_schedules/${SCHEDULE_TEST_ID}/revisions`, - data: {}, - headers: {}, - }); - }); - }); - - describe('retrieve', () => { - it('Sends the correct request', () => { - revision.retrieve(REVISION_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/subscription_schedules/${SCHEDULE_TEST_ID}/revisions/${REVISION_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); -}); diff --git a/test/resources/TaxIds.spec.js b/test/resources/TaxIds.spec.js deleted file mode 100644 index fda30f9faa..0000000000 --- a/test/resources/TaxIds.spec.js +++ /dev/null @@ -1,67 +0,0 @@ -'use strict'; - -const resources = require('../../lib/stripe').resources; -const stripe = require('../../testUtils').getSpyableStripe(); -const expect = require('chai').expect; - -const CUSTOMER_TEST_ID = 'cus_123'; -const TAX_ID_TEST_ID = 'txi_123'; - -const taxId = new resources.TaxIds(stripe, {customerId: CUSTOMER_TEST_ID}); - -// Use spy from existing resource: -taxId._request = stripe.customers._request; - -describe('TaxId Resource', () => { - describe('create', () => { - it('Sends the correct request', () => { - const data = { - type: 'eu_vat', - value: '11111', - }; - taxId.create(data); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids`, - data, - headers: {}, - }); - }); - }); - - describe('delete', () => { - it('Sends the correct request', () => { - taxId.del(TAX_ID_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'DELETE', - url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids/${TAX_ID_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); - - describe('list', () => { - it('Sends the correct request', () => { - taxId.list(); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids`, - data: {}, - headers: {}, - }); - }); - }); - - describe('retrieve', () => { - it('Sends the correct request', () => { - taxId.retrieve(TAX_ID_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids/${TAX_ID_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); -}); diff --git a/test/resources/TransferReversals.spec.js b/test/resources/TransferReversals.spec.js deleted file mode 100644 index 3b0c9e67fb..0000000000 --- a/test/resources/TransferReversals.spec.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict'; - -const resources = require('../../lib/stripe').resources; -const stripe = require('../../testUtils').getSpyableStripe(); -const expect = require('chai').expect; - -const TRANSFER_TEST_ID = 'transferIdTest999'; -const REVERSAL_TEST_ID = 'reversalIdTest999'; - -// Create new CustomerCard instance with pre-filled customerId: -const transferReversal = new resources.TransferReversals(stripe, { - transferId: TRANSFER_TEST_ID, -}); - -// Use spy from existing resource: -transferReversal._request = stripe.customers._request; - -describe('TransferReversal Resource', () => { - describe('retrieve', () => { - it('Sends the correct request', () => { - transferReversal.retrieve(REVERSAL_TEST_ID); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/transfers/${TRANSFER_TEST_ID}/reversals/${REVERSAL_TEST_ID}`, - data: {}, - headers: {}, - }); - }); - }); - - describe('create', () => { - it('Sends the correct request', () => { - transferReversal.create({ - amount: 100, - }); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/transfers/${TRANSFER_TEST_ID}/reversals`, - data: {amount: 100}, - headers: {}, - }); - }); - }); - - describe('update', () => { - it('Sends the correct request', () => { - transferReversal.update(REVERSAL_TEST_ID, { - metadata: {key: 'value'}, - }); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'POST', - url: `/v1/transfers/${TRANSFER_TEST_ID}/reversals/${REVERSAL_TEST_ID}`, - data: {metadata: {key: 'value'}}, - headers: {}, - }); - }); - }); - - describe('list', () => { - it('Sends the correct request', () => { - transferReversal.list(); - expect(stripe.LAST_REQUEST).to.deep.equal({ - method: 'GET', - url: `/v1/transfers/${TRANSFER_TEST_ID}/reversals`, - data: {}, - headers: {}, - }); - }); - }); -}); diff --git a/test/utils.spec.js b/test/utils.spec.js index 202da2f67c..861998fc76 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -22,6 +22,16 @@ describe('utils', () => { }); }); + describe('extractUrlParams', () => { + it('works with multiple params', () => { + expect( + utils.extractUrlParams( + 'accounts/{accountId}/external_accounts/{externalAccountId}' + ) + ).to.deep.equal(['accountId', 'externalAccountId']); + }); + }); + describe('stringifyRequestData', () => { it('Handles basic types', () => { expect(