Skip to content

Commit 5ac6906

Browse files
Merge pull request #2314 from stripe/latest-codegen-beta
Update generated code for beta
2 parents 7d5891d + d2f66ba commit 5ac6906

29 files changed

+256
-512
lines changed

OPENAPI_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1719
1+
v1719

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@types/chai-as-promised": "^7.1.5",
3232
"@types/mocha": "^10.0.1",
3333
"@types/qs": "^6.9.7",
34+
"@types/node": ">=12.0.0",
3435
"@typescript-eslint/eslint-plugin": "^4.33.0",
3536
"@typescript-eslint/parser": "^4.33.0",
3637
"chai": "^4.3.6",
@@ -47,16 +48,24 @@
4748
"nyc": "^15.1.0",
4849
"prettier": "^1.16.4",
4950
"ts-node": "^10.9.1",
50-
"typescript": "^4.9.4"
51+
"typescript": "^4.9.4",
52+
"undici-types": "^7.8.0"
5153
},
5254
"resolutions": {
5355
"minimist": "1.2.6",
5456
"nanoid": "^3.2.0"
5557
},
5658
"dependencies": {
57-
"@types/node": ">=8.1.0",
5859
"qs": "^6.11.0"
5960
},
61+
"peerDependencies": {
62+
"@types/node": ">=12.x.x"
63+
},
64+
"peerDependenciesMeta": {
65+
"@types/node": {
66+
"optional": true
67+
}
68+
},
6069
"license": "MIT",
6170
"scripts": {
6271
"test": "tsc -p tsconfig.cjs.json && mocha",

src/Error.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable camelcase */
22
/* eslint-disable no-warning-comments */
33

4+
import {HttpClientResponseError} from './RequestSender.js';
45
import {RawErrorType, StripeRawError} from './Types.js';
56

67
export const generateV1Error = (
@@ -85,7 +86,7 @@ export class StripeError extends Error {
8586
readonly code?: string;
8687
readonly doc_url?: string;
8788
readonly param?: string;
88-
readonly detail?: string | Error;
89+
readonly detail?: string | Error | HttpClientResponseError;
8990
readonly statusCode?: number;
9091
readonly charge?: string;
9192
readonly decline_code?: string;
@@ -109,8 +110,7 @@ export class StripeError extends Error {
109110
this.headers = raw.headers;
110111
this.requestId = raw.requestId;
111112
this.statusCode = raw.statusCode;
112-
// @ts-ignore
113-
this.message = raw.message;
113+
this.message = raw.message ?? '';
114114
this.userMessage = raw.user_message;
115115
this.charge = raw.charge;
116116
this.decline_code = raw.decline_code;

src/RequestSender.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
getAPIMode,
3636
getOptionsFromArgs,
3737
getDataFromArgs,
38+
parseHttpHeaderAsString,
39+
parseHttpHeaderAsNumber,
3840
} from './utils.js';
3941

4042
export type HttpClientResponseError = {code: string};
@@ -609,11 +611,12 @@ export class RequestSender {
609611

610612
const requestStartTime = Date.now();
611613

612-
// @ts-ignore
613614
const requestEvent: RequestEvent = removeNullish({
614615
api_version: apiVersion,
615-
account: headers['Stripe-Account'],
616-
idempotency_key: headers['Idempotency-Key'],
616+
account: parseHttpHeaderAsString(headers['Stripe-Account']),
617+
idempotency_key: parseHttpHeaderAsString(
618+
headers['Idempotency-Key']
619+
),
617620
method,
618621
path,
619622
request_start_time: requestStartTime,
@@ -632,8 +635,7 @@ export class RequestSender {
632635
apiVersion,
633636
headers,
634637
requestRetries,
635-
// @ts-ignore
636-
res.getHeaders()['retry-after']
638+
parseHttpHeaderAsNumber(res.getHeaders()['retry-after'])
637639
);
638640
} else if (options.streaming && res.getStatusCode() < 400) {
639641
return this._streamingResponseHandler(
@@ -677,7 +679,6 @@ export class RequestSender {
677679
: RequestSender._generateConnectionErrorMessage(
678680
requestRetries
679681
),
680-
// @ts-ignore
681682
detail: error,
682683
})
683684
);

src/Types.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HttpClientResponseInterface,
66
} from './net/HttpClient.js';
77
import {PlatformFunctions} from './platform/PlatformFunctions.js';
8+
import {HttpClientResponseError} from './RequestSender.js';
89

910
export type AppInfo = {name?: string} & Record<string, unknown>;
1011
export type ApiMode = 'v1' | 'v2';
@@ -75,7 +76,7 @@ export type RequestEvent = {
7576
method?: string;
7677
path?: string;
7778
request_start_time: number;
78-
usage: Array<string>;
79+
usage?: Array<string>;
7980
};
8081
export type RequestHeaders = Record<string, string | number | string[]>;
8182
export type APIMode = 'preview' | 'standard';
@@ -217,7 +218,7 @@ export type StripeRawError = {
217218
doc_url?: string;
218219
decline_code?: string;
219220
param?: string;
220-
detail?: string | Error;
221+
detail?: string | Error | HttpClientResponseError;
221222
charge?: string;
222223
payment_method_type?: string;
223224
payment_intent?: any;

src/Webhooks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type WebhookSignatureObject = {
4747
};
4848
export type WebhookObject = {
4949
DEFAULT_TOLERANCE: number;
50-
signature: WebhookSignatureObject;
50+
signature: WebhookSignatureObject | null;
5151
constructEvent: (
5252
payload: WebhookPayload,
5353
header: WebhookHeader,
@@ -75,7 +75,6 @@ export function createWebhooks(
7575
): WebhookObject {
7676
const Webhook: WebhookObject = {
7777
DEFAULT_TOLERANCE: 300, // 5 minutes
78-
// @ts-ignore
7978
signature: null,
8079
constructEvent(
8180
payload: WebhookPayload,
@@ -86,6 +85,10 @@ export function createWebhooks(
8685
receivedAt: number
8786
): WebhookEvent {
8887
try {
88+
if (!this.signature) {
89+
throw new Error('ERR: missing signature helper, unable to verify');
90+
}
91+
8992
this.signature.verifyHeader(
9093
payload,
9194
header,
@@ -117,6 +120,10 @@ export function createWebhooks(
117120
cryptoProvider: CryptoProvider,
118121
receivedAt: number
119122
): Promise<WebhookEvent> {
123+
if (!this.signature) {
124+
throw new Error('ERR: missing signature helper, unable to verify');
125+
}
126+
120127
await this.signature.verifyHeaderAsync(
121128
payload,
122129
header,
@@ -394,7 +401,6 @@ export function createWebhooks(
394401
) - details.timestamp;
395402

396403
if (tolerance > 0 && timestampAge > tolerance) {
397-
// @ts-ignore
398404
throw new StripeSignatureVerificationError(header, payload, {
399405
message: 'Timestamp outside the tolerance zone',
400406
});

src/net/FetchHttpClient.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {RequestHeaders, RequestData, ResponseHeaders} from '../Types.js';
2+
import {parseHeadersForFetch} from '../utils.js';
23
import {
34
HttpClient,
45
HttpClientInterface,
@@ -139,10 +140,8 @@ export class FetchHttpClient extends HttpClient implements HttpClientInterface {
139140
url.toString(),
140141
{
141142
method,
142-
// @ts-ignore
143-
headers,
144-
// @ts-ignore
145-
body,
143+
headers: parseHeadersForFetch(headers),
144+
body: typeof body === 'object' ? JSON.stringify(body) : body,
146145
},
147146
timeout
148147
);

src/stripe.core.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ export function createStripe(
257257

258258
return accum;
259259
},
260-
// @ts-ignore
261-
undefined
260+
{}
262261
);
263262
},
264263

src/utils.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,22 @@ export const makeURLInterpolator = ((): ((s: string) => UrlInterpolator) => {
8383
return (str: string): UrlInterpolator => {
8484
const cleanString = str.replace(/["\n\r\u2028\u2029]/g, ($0) => rc[$0]);
8585
return (outputs: Record<string, unknown>): string => {
86-
return cleanString.replace(/\{([\s\S]+?)\}/g, ($0, $1) =>
87-
// @ts-ignore
88-
encodeURIComponent(outputs[$1] || '')
89-
);
86+
return cleanString.replace(/\{([\s\S]+?)\}/g, ($0, $1) => {
87+
const output = outputs[$1];
88+
if (isValidEncodeUriComponentType(output))
89+
return encodeURIComponent(output);
90+
return '';
91+
});
9092
};
9193
};
9294
})();
9395

96+
function isValidEncodeUriComponentType(
97+
value: unknown
98+
): value is number | string | boolean {
99+
return ['number', 'string', 'boolean'].includes(typeof value);
100+
}
101+
94102
export function extractUrlParams(path: string): Array<string> {
95103
const params = path.match(/\{\w+\}/g);
96104
if (!params) {
@@ -452,3 +460,27 @@ export function getAPIMode(path?: string): ApiMode {
452460
}
453461
return path.startsWith('/v2') ? 'v2' : 'v1';
454462
}
463+
464+
export function parseHttpHeaderAsString<K extends keyof RequestHeaders>(
465+
header: RequestHeaders[K]
466+
): string {
467+
if (Array.isArray(header)) {
468+
return header.join(', ');
469+
}
470+
return String(header);
471+
}
472+
473+
export function parseHttpHeaderAsNumber<K extends keyof RequestHeaders>(
474+
header: RequestHeaders[K]
475+
): number {
476+
const number = Array.isArray(header) ? header[0] : header;
477+
return Number(number);
478+
}
479+
480+
export function parseHeadersForFetch(
481+
headers: RequestHeaders
482+
): [string, string][] {
483+
return Object.entries(headers).map(([key, value]) => {
484+
return [key, parseHttpHeaderAsString(value)];
485+
});
486+
}

test/resources/generated_examples_test.spec.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,12 +4201,14 @@ describe('Generated tests', function() {
42014201
const stripe = testUtils.createMockClient([
42024202
{
42034203
method: 'GET',
4204-
path: '/v2/core/events',
4204+
path: '/v2/core/events?object_id=object_id',
42054205
response:
42064206
'{"data":[{"context":null,"created":"1970-01-12T21:42:34.472Z","id":"obj_123","object":"v2.core.event","reason":null,"type":"type","livemode":true}],"next_page_url":null,"previous_page_url":null}',
42074207
},
42084208
]);
4209-
const events = await stripe.v2.core.events.list();
4209+
const events = await stripe.v2.core.events.list({
4210+
object_id: 'object_id',
4211+
});
42104212
expect(events).not.to.be.null;
42114213
});
42124214

@@ -4423,7 +4425,7 @@ describe('Generated tests', function() {
44234425
method: 'POST',
44244426
path: '/v2/money_management/financial_addresses',
44254427
response:
4426-
'{"created":"1970-01-12T21:42:34.472Z","credentials":null,"currency":"stn","financial_account":"financial_account","id":"obj_123","object":"v2.money_management.financial_address","settlement_currency":null,"status":"failed","livemode":true}',
4428+
'{"created":"1970-01-12T21:42:34.472Z","credentials":null,"currency":"stn","financial_account":"financial_account","id":"obj_123","object":"v2.money_management.financial_address","status":"failed","livemode":true}',
44274429
},
44284430
]);
44294431
const financialAddress = await stripe.v2.moneyManagement.financialAddresses.create(
@@ -4441,7 +4443,7 @@ describe('Generated tests', function() {
44414443
method: 'GET',
44424444
path: '/v2/money_management/financial_addresses',
44434445
response:
4444-
'{"data":[{"created":"1970-01-12T21:42:34.472Z","credentials":null,"currency":"stn","financial_account":"financial_account","id":"obj_123","object":"v2.money_management.financial_address","settlement_currency":null,"status":"failed","livemode":true}],"next_page_url":null,"previous_page_url":null}',
4446+
'{"data":[{"created":"1970-01-12T21:42:34.472Z","credentials":null,"currency":"stn","financial_account":"financial_account","id":"obj_123","object":"v2.money_management.financial_address","status":"failed","livemode":true}],"next_page_url":null,"previous_page_url":null}',
44454447
},
44464448
]);
44474449
const financialAddresses = await stripe.v2.moneyManagement.financialAddresses.list();
@@ -4454,7 +4456,7 @@ describe('Generated tests', function() {
44544456
method: 'GET',
44554457
path: '/v2/money_management/financial_addresses/id_123',
44564458
response:
4457-
'{"created":"1970-01-12T21:42:34.472Z","credentials":null,"currency":"stn","financial_account":"financial_account","id":"obj_123","object":"v2.money_management.financial_address","settlement_currency":null,"status":"failed","livemode":true}',
4459+
'{"created":"1970-01-12T21:42:34.472Z","credentials":null,"currency":"stn","financial_account":"financial_account","id":"obj_123","object":"v2.money_management.financial_address","status":"failed","livemode":true}',
44584460
},
44594461
]);
44604462
const financialAddress = await stripe.v2.moneyManagement.financialAddresses.retrieve(
@@ -4854,7 +4856,7 @@ describe('Generated tests', function() {
48544856
method: 'GET',
48554857
path: '/v2/money_management/received_credits',
48564858
response:
4857-
'{"data":[{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_credit","receipt_url":null,"status":"returned","status_details":null,"status_transitions":null,"type":"crypto_wallet_transfer","livemode":true,"balance_transfer":null,"bank_transfer":null,"crypto_wallet_transfer":null}],"next_page_url":null,"previous_page_url":null}',
4859+
'{"data":[{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_credit","receipt_url":null,"status":"returned","status_details":null,"status_transitions":null,"type":"card_spend","livemode":true,"balance_transfer":null,"bank_transfer":null,"card_spend":null}],"next_page_url":null,"previous_page_url":null}',
48584860
},
48594861
]);
48604862
const receivedCredits = await stripe.v2.moneyManagement.receivedCredits.list();
@@ -4867,7 +4869,7 @@ describe('Generated tests', function() {
48674869
method: 'GET',
48684870
path: '/v2/money_management/received_credits/id_123',
48694871
response:
4870-
'{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_credit","receipt_url":null,"status":"returned","status_details":null,"status_transitions":null,"type":"crypto_wallet_transfer","livemode":true,"balance_transfer":null,"bank_transfer":null,"crypto_wallet_transfer":null}',
4872+
'{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_credit","receipt_url":null,"status":"returned","status_details":null,"status_transitions":null,"type":"card_spend","livemode":true,"balance_transfer":null,"bank_transfer":null,"card_spend":null}',
48714873
},
48724874
]);
48734875
const receivedCredit = await stripe.v2.moneyManagement.receivedCredits.retrieve(
@@ -4882,7 +4884,7 @@ describe('Generated tests', function() {
48824884
method: 'GET',
48834885
path: '/v2/money_management/received_debits',
48844886
response:
4885-
'{"data":[{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_debit","receipt_url":null,"status":"canceled","status_details":null,"status_transitions":null,"type":"bank_transfer","livemode":true,"bank_transfer":null}],"next_page_url":null,"previous_page_url":null}',
4887+
'{"data":[{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_debit","receipt_url":null,"status":"canceled","status_details":null,"status_transitions":null,"type":"bank_transfer","livemode":true,"bank_transfer":null,"card_spend":null}],"next_page_url":null,"previous_page_url":null}',
48864888
},
48874889
]);
48884890
const receivedDebits = await stripe.v2.moneyManagement.receivedDebits.list();
@@ -4895,7 +4897,7 @@ describe('Generated tests', function() {
48954897
method: 'GET',
48964898
path: '/v2/money_management/received_debits/id_123',
48974899
response:
4898-
'{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_debit","receipt_url":null,"status":"canceled","status_details":null,"status_transitions":null,"type":"bank_transfer","livemode":true,"bank_transfer":null}',
4900+
'{"amount":{"currency":"USD","value":96},"created":"1970-01-12T21:42:34.472Z","description":null,"financial_account":"financial_account","id":"obj_123","object":"v2.money_management.received_debit","receipt_url":null,"status":"canceled","status_details":null,"status_transitions":null,"type":"bank_transfer","livemode":true,"bank_transfer":null,"card_spend":null}',
48994901
},
49004902
]);
49014903
const receivedDebit = await stripe.v2.moneyManagement.receivedDebits.retrieve(

test/utils.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe('utils', () => {
1111

1212
expect(template({foo: '', baz: ''})).to.equal('/some/url//?ok=1');
1313

14+
expect(template({foo: 0, baz: 't'})).to.equal('/some/url/0/t?ok=1');
15+
1416
expect(
1517
// Test encoding:
1618
template({foo: 'FOO', baz: '__::baz::__'})

tsconfig.cjs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"outDir": "./cjs",
44
"module": "commonjs",
5+
"moduleResolution": "node",
56
"target": "es2017",
67
"checkJs": false,
78
"alwaysStrict": true,

types/V2/Core/AccountLinks.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare module 'stripe' {
55
namespace V2 {
66
namespace Core {
77
/**
8-
* AccountLinks are the means by which a Merchant grants an Account permission to access Stripe-hosted application, such as Recipient Onboarding.
8+
* AccountLinks are the means by which a Merchant grants an Account permission to access Stripe-hosted applications, such as Recipient Onboarding. This API is only available for users enrolled in the public preview for Global Payouts.
99
*/
1010
interface AccountLink {
1111
/**

0 commit comments

Comments
 (0)