Skip to content

Commit 3c99464

Browse files
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 d88a3e7. * 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 e5eccb8.
1 parent 4af9522 commit 3c99464

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+46
-620
lines changed

lib/StripeMethod.basic.js

-3
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ module.exports = {
1515
retrieve: stripeMethod({
1616
method: 'GET',
1717
path: '/{id}',
18-
urlParams: ['id'],
1918
}),
2019

2120
update: stripeMethod({
2221
method: 'POST',
2322
path: '{id}',
24-
urlParams: ['id'],
2523
}),
2624

2725
// Avoid 'delete' keyword in JS
2826
del: stripeMethod({
2927
method: 'DELETE',
3028
path: '{id}',
31-
urlParams: ['id'],
3229
}),
3330
};

lib/StripeMethod.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function stripeMethod(spec) {
2626

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

29+
spec.urlParams = utils.extractUrlParams(
30+
self.createResourcePathWithSymbols(spec.path || '')
31+
);
32+
2933
const requestPromise = utils.callbackifyPromiseWithTimeout(
3034
makeRequest(self, args, spec, {}),
3135
callback

lib/StripeResource.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const uuid = require('uuid/v4');
88
const utils = require('./utils');
99
const Error = require('./Error');
1010

11-
const hasOwn = {}.hasOwnProperty;
12-
1311
const defaultHttpAgent = new http.Agent({keepAlive: true});
1412
const defaultHttpsAgent = new https.Agent({keepAlive: true});
1513

@@ -25,9 +23,13 @@ StripeResource.MAX_BUFFERED_REQUEST_METRICS = 100;
2523
/**
2624
* Encapsulates request logic for a Stripe Resource
2725
*/
28-
function StripeResource(stripe, urlData) {
26+
function StripeResource(stripe, deprecatedUrlData) {
2927
this._stripe = stripe;
30-
this._urlData = urlData || {};
28+
if (deprecatedUrlData) {
29+
throw new Error(
30+
'Support for curried url params was dropped in stripe-node v7.0.0. Instead, pass two ids.'
31+
);
32+
}
3133

3234
this.basePath = utils.makeURLInterpolator(
3335
this.basePath || stripe.getApiField('basePath')
@@ -81,17 +83,6 @@ StripeResource.prototype = {
8183
.replace(/\\/g, '/')}`; // ugly workaround for Windows
8284
},
8385

84-
createUrlData() {
85-
const urlData = {};
86-
// Merge in baseData
87-
for (const i in this._urlData) {
88-
if (hasOwn.call(this._urlData, i)) {
89-
urlData[i] = this._urlData[i];
90-
}
91-
}
92-
return urlData;
93-
},
94-
9586
// DEPRECATED: Here for backcompat in case users relied on this.
9687
wrapTimeout: utils.callbackifyPromiseWithTimeout,
9788

lib/makeRequest.js

+9-38
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,31 @@
11
'use strict';
22

33
const utils = require('./utils');
4-
const OPTIONAL_REGEX = /^optional!/;
54

65
function getRequestOpts(self, requestArgs, spec, overrideData) {
76
// Extract spec values with defaults.
8-
const commandPath =
9-
typeof spec.path == 'function'
10-
? spec.path
11-
: utils.makeURLInterpolator(spec.path || '');
7+
const commandPath = utils.makeURLInterpolator(spec.path || '');
128
const requestMethod = (spec.method || 'GET').toUpperCase();
139
const urlParams = spec.urlParams || [];
1410
const encode = spec.encode || ((data) => data);
1511
const host = spec.host;
12+
const path = self.createResourcePathWithSymbols(spec.path);
1613

1714
// Don't mutate args externally.
1815
const args = [].slice.call(requestArgs);
1916

2017
// Generate and validate url params.
21-
const urlData = self.createUrlData();
22-
for (let i = 0, l = urlParams.length; i < l; ++i) {
23-
var path;
24-
25-
// Note that we shift the args array after every iteration so this just
26-
// grabs the "next" argument for use as a URL parameter.
27-
const arg = args[0];
28-
29-
let param = urlParams[i];
30-
31-
const isOptional = OPTIONAL_REGEX.test(param);
32-
param = param.replace(OPTIONAL_REGEX, '');
33-
34-
if (param == 'id' && typeof arg !== 'string') {
35-
path = self.createResourcePathWithSymbols(spec.path);
18+
const urlData = urlParams.reduce((urlData, param) => {
19+
const arg = args.shift();
20+
if (typeof arg !== 'string') {
3621
throw new Error(
37-
`Stripe: "id" must be a string, but got: ${typeof arg} (on API request to \`${requestMethod} ${path}\`)`
22+
`Stripe: Argument "${param}" must be a string, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)`
3823
);
3924
}
4025

41-
if (!arg) {
42-
if (isOptional) {
43-
urlData[param] = '';
44-
continue;
45-
}
46-
47-
path = self.createResourcePathWithSymbols(spec.path);
48-
throw new Error(
49-
`Stripe: Argument "${
50-
urlParams[i]
51-
}" required, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)`
52-
);
53-
}
54-
55-
urlData[param] = args.shift();
56-
}
26+
urlData[param] = arg;
27+
return urlData;
28+
}, {});
5729

5830
// Pull request data and options (headers, auth) from args.
5931
const dataFromArgs = utils.getDataFromArgs(args);
@@ -62,7 +34,6 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
6234

6335
// Validate that there are no more args.
6436
if (args.length) {
65-
path = self.createResourcePathWithSymbols(spec.path);
6637
throw new Error(
6738
`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}\`)`
6839
);

lib/resources/Accounts.js

-18
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ module.exports = StripeResource.extend({
2020
update: stripeMethod({
2121
method: 'POST',
2222
path: 'accounts/{id}',
23-
urlParams: ['id'],
2423
}),
2524

2625
// Avoid 'delete' keyword in JS
2726
del: stripeMethod({
2827
method: 'DELETE',
2928
path: 'accounts/{id}',
30-
urlParams: ['id'],
3129
}),
3230

3331
reject: stripeMethod({
3432
method: 'POST',
3533
path: 'accounts/{id}/reject',
36-
urlParams: ['id'],
3734
}),
3835

3936
retrieve(id) {
@@ -43,7 +40,6 @@ module.exports = StripeResource.extend({
4340
return stripeMethod({
4441
method: 'GET',
4542
path: 'accounts/{id}',
46-
urlParams: ['id'],
4743
}).apply(this, arguments);
4844
} else {
4945
if (id === null || id === undefined) {
@@ -64,20 +60,17 @@ module.exports = StripeResource.extend({
6460
listCapabilities: stripeMethod({
6561
method: 'GET',
6662
path: 'accounts/{accountId}/capabilities',
67-
urlParams: ['accountId'],
6863
methodType: 'list',
6964
}),
7065

7166
retrieveCapability: stripeMethod({
7267
method: 'GET',
7368
path: 'accounts/{accountId}/capabilities/{capabilityId}',
74-
urlParams: ['accountId', 'capabilityId'],
7569
}),
7670

7771
updateCapability: stripeMethod({
7872
method: 'POST',
7973
path: 'accounts/{accountId}/capabilities/{capabilityId}',
80-
urlParams: ['accountId', 'capabilityId'],
8174
}),
8275

8376
/**
@@ -87,32 +80,27 @@ module.exports = StripeResource.extend({
8780
createExternalAccount: stripeMethod({
8881
method: 'POST',
8982
path: 'accounts/{accountId}/external_accounts',
90-
urlParams: ['accountId'],
9183
}),
9284

9385
listExternalAccounts: stripeMethod({
9486
method: 'GET',
9587
path: 'accounts/{accountId}/external_accounts',
96-
urlParams: ['accountId'],
9788
methodType: 'list',
9889
}),
9990

10091
retrieveExternalAccount: stripeMethod({
10192
method: 'GET',
10293
path: 'accounts/{accountId}/external_accounts/{externalAccountId}',
103-
urlParams: ['accountId', 'externalAccountId'],
10494
}),
10595

10696
updateExternalAccount: stripeMethod({
10797
method: 'POST',
10898
path: 'accounts/{accountId}/external_accounts/{externalAccountId}',
109-
urlParams: ['accountId', 'externalAccountId'],
11099
}),
111100

112101
deleteExternalAccount: stripeMethod({
113102
method: 'DELETE',
114103
path: 'accounts/{accountId}/external_accounts/{externalAccountId}',
115-
urlParams: ['accountId', 'externalAccountId'],
116104
}),
117105

118106
/**
@@ -122,7 +110,6 @@ module.exports = StripeResource.extend({
122110
createLoginLink: stripeMethod({
123111
method: 'POST',
124112
path: 'accounts/{accountId}/login_links',
125-
urlParams: ['accountId'],
126113
}),
127114

128115
/**
@@ -132,31 +119,26 @@ module.exports = StripeResource.extend({
132119
createPerson: stripeMethod({
133120
method: 'POST',
134121
path: 'accounts/{accountId}/persons',
135-
urlParams: ['accountId'],
136122
}),
137123

138124
listPersons: stripeMethod({
139125
method: 'GET',
140126
path: 'accounts/{accountId}/persons',
141-
urlParams: ['accountId'],
142127
methodType: 'list',
143128
}),
144129

145130
retrievePerson: stripeMethod({
146131
method: 'GET',
147132
path: 'accounts/{accountId}/persons/{personId}',
148-
urlParams: ['accountId', 'personId'],
149133
}),
150134

151135
updatePerson: stripeMethod({
152136
method: 'POST',
153137
path: 'accounts/{accountId}/persons/{personId}',
154-
urlParams: ['accountId', 'personId'],
155138
}),
156139

157140
deletePerson: stripeMethod({
158141
method: 'DELETE',
159142
path: 'accounts/{accountId}/persons/{personId}',
160-
urlParams: ['accountId', 'personId'],
161143
}),
162144
});

lib/resources/ApplicationFeeRefunds.js

-21
This file was deleted.

lib/resources/ApplicationFees.js

-4
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@ module.exports = StripeResource.extend({
1111
createRefund: stripeMethod({
1212
method: 'POST',
1313
path: '/{feeId}/refunds',
14-
urlParams: ['feeId'],
1514
}),
1615

1716
listRefunds: stripeMethod({
1817
method: 'GET',
1918
path: '/{feeId}/refunds',
20-
urlParams: ['feeId'],
2119
methodType: 'list',
2220
}),
2321

2422
retrieveRefund: stripeMethod({
2523
method: 'GET',
2624
path: '/{feeId}/refunds/{refundId}',
27-
urlParams: ['feeId', 'refundId'],
2825
}),
2926

3027
updateRefund: stripeMethod({
3128
method: 'POST',
3229
path: '/{feeId}/refunds/{refundId}',
33-
urlParams: ['feeId', 'refundId'],
3430
}),
3531
});

lib/resources/BitcoinReceivers.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = StripeResource.extend({
1111
listTransactions: stripeMethod({
1212
method: 'GET',
1313
path: '/{id}/transactions',
14-
urlParams: ['id'],
1514
methodType: 'list',
1615
}),
1716
});

lib/resources/Capabilities.js

-9
This file was deleted.

lib/resources/Charges.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ module.exports = StripeResource.extend({
1111
capture: stripeMethod({
1212
method: 'POST',
1313
path: '/{id}/capture',
14-
urlParams: ['id'],
1514
}),
1615
});

lib/resources/CreditNotes.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ module.exports = StripeResource.extend({
1111
voidCreditNote: stripeMethod({
1212
method: 'POST',
1313
path: '/{id}/void',
14-
urlParams: ['id'],
1514
}),
1615
});

0 commit comments

Comments
 (0)