Skip to content

Commit f4a34ac

Browse files
Merge upstream and update generated code for v2204 and
2 parents 5dc3955 + c2387c1 commit f4a34ac

9 files changed

Lines changed: 883 additions & 6 deletions

File tree

CODEGEN_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
273184f052dd3c191b1993098365d0c2437d2cb4
1+
19704d8af6f1949f2d8f646ca37978ad12f936a4

src/StripeResource.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
getAPIMode,
23
getDataFromArgs,
34
getOptionsFromArgs,
45
makeURLInterpolator,
@@ -16,6 +17,7 @@ import {
1617
UrlInterpolator,
1718
} from './Types.js';
1819
import {HttpClientResponseInterface} from './net/HttpClient.js';
20+
import {coerceV2RequestData, coerceV2ResponseData} from './V2Int64.js';
1921

2022
// Provide extension mechanism for Stripe Resource Sub-Classes
2123
StripeResource.extend = protoExtend;
@@ -209,18 +211,35 @@ StripeResource.prototype = {
209211
return;
210212
}
211213

214+
// Coerce int64_string fields in request body: number → string
215+
const apiMode = getAPIMode(spec.fullPath || spec.path);
216+
if (apiMode === 'v2' && spec.requestSchema && opts.bodyData) {
217+
opts.bodyData = coerceV2RequestData(
218+
opts.bodyData,
219+
spec.requestSchema
220+
) as RequestData;
221+
}
222+
212223
function requestCallback(
213224
err: any,
214225
response: HttpClientResponseInterface
215226
): void {
216227
if (err) {
217228
reject(err);
218229
} else {
219-
resolve(
220-
spec.transformResponseData
221-
? spec.transformResponseData(response)
222-
: response
223-
);
230+
// Coerce int64_string fields in response: string → bigint
231+
try {
232+
if (apiMode === 'v2' && spec.responseSchema) {
233+
coerceV2ResponseData(response, spec.responseSchema);
234+
}
235+
resolve(
236+
spec.transformResponseData
237+
? spec.transformResponseData(response)
238+
: response
239+
);
240+
} catch (e) {
241+
reject(e);
242+
}
224243
}
225244
}
226245

src/Types.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export type BufferedFile = {
1515
type: string;
1616
file: {data: Uint8Array};
1717
};
18+
export type V2RuntimeSchema =
19+
| {kind: 'int64_string'}
20+
| {kind: 'object'; fields: Record<string, V2RuntimeSchema>}
21+
| {kind: 'array'; element: V2RuntimeSchema}
22+
| {kind: 'nullable'; inner: V2RuntimeSchema};
1823
export type MethodSpec = {
1924
method: string;
2025
methodType?: string;
@@ -28,6 +33,8 @@ export type MethodSpec = {
2833
host?: string;
2934
transformResponseData?: (response: HttpClientResponseInterface) => any;
3035
usage?: Array<string>;
36+
requestSchema?: V2RuntimeSchema;
37+
responseSchema?: V2RuntimeSchema;
3138
};
3239
export type MultipartRequestData = RequestData | StreamingFile | BufferedFile;
3340
// rawErrorTypeEnum: The beginning of the section generated from our OpenAPI spec
@@ -39,6 +46,11 @@ export type RawErrorType =
3946
| 'rate_limit_error'
4047
| 'authentication_error'
4148
| 'invalid_grant'
49+
| 'invalid_client'
50+
| 'invalid_request'
51+
| 'invalid_scope'
52+
| 'unsupported_grant_type'
53+
| 'unsupported_response_type'
4254
| 'already_canceled'
4355
| 'already_exists'
4456
| 'blocked_by_stripe'

src/V2Int64.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {V2RuntimeSchema} from './Types.js';
2+
3+
/**
4+
* Coerces outbound V2 request data by converting bigint (or number)
5+
* int64_string fields to strings, matching the wire format expected by the API.
6+
*
7+
* Walks the schema tree and only touches fields that are marked as
8+
* int64_string. All other values are left unchanged.
9+
*/
10+
export const coerceV2RequestData = (
11+
data: unknown,
12+
schema: V2RuntimeSchema
13+
): unknown => {
14+
if (data == null) {
15+
return data;
16+
}
17+
18+
switch (schema.kind) {
19+
case 'int64_string':
20+
return typeof data === 'bigint' || typeof data === 'number'
21+
? String(data)
22+
: data;
23+
24+
case 'object': {
25+
if (typeof data !== 'object' || Array.isArray(data)) {
26+
return data;
27+
}
28+
const obj = data as Record<string, unknown>;
29+
const result: Record<string, unknown> = {};
30+
for (const key of Object.keys(obj)) {
31+
const fieldSchema = schema.fields[key];
32+
result[key] = fieldSchema
33+
? coerceV2RequestData(obj[key], fieldSchema)
34+
: obj[key];
35+
}
36+
return result;
37+
}
38+
39+
case 'array': {
40+
if (!Array.isArray(data)) {
41+
return data;
42+
}
43+
return data.map((element) =>
44+
coerceV2RequestData(element, schema.element)
45+
);
46+
}
47+
48+
case 'nullable':
49+
return coerceV2RequestData(data, schema.inner);
50+
}
51+
};
52+
53+
/**
54+
* Coerces inbound V2 response data by converting string int64_string fields
55+
* to bigints, matching the SDK's public type contract.
56+
*
57+
* Walks the schema tree and only touches fields that are marked as
58+
* int64_string. All other values are left unchanged.
59+
*/
60+
export const coerceV2ResponseData = (
61+
data: unknown,
62+
schema: V2RuntimeSchema
63+
): unknown => {
64+
if (data == null) {
65+
return data;
66+
}
67+
68+
switch (schema.kind) {
69+
case 'int64_string':
70+
if (typeof data === 'string') {
71+
try {
72+
return BigInt(data);
73+
} catch {
74+
throw new Error(
75+
`Failed to coerce int64_string value: expected an integer string, got '${data}'`
76+
);
77+
}
78+
}
79+
return data;
80+
81+
case 'object': {
82+
if (typeof data !== 'object' || Array.isArray(data)) {
83+
return data;
84+
}
85+
const obj = data as Record<string, unknown>;
86+
for (const key of Object.keys(schema.fields)) {
87+
if (key in obj) {
88+
obj[key] = coerceV2ResponseData(obj[key], schema.fields[key]);
89+
}
90+
}
91+
return obj;
92+
}
93+
94+
case 'array': {
95+
if (!Array.isArray(data)) {
96+
return data;
97+
}
98+
for (let i = 0; i < data.length; i++) {
99+
data[i] = coerceV2ResponseData(data[i], schema.element);
100+
}
101+
return data;
102+
}
103+
104+
case 'nullable':
105+
return coerceV2ResponseData(data, schema.inner);
106+
}
107+
};

0 commit comments

Comments
 (0)