Skip to content

Commit abd1a87

Browse files
authored
Merge pull request #2622 from stripe/jar/merge-node-beta
Merge to beta
2 parents 065fe5d + 3a7b0f6 commit abd1a87

File tree

75 files changed

+12985
-183
lines changed

Some content is hidden

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

75 files changed

+12985
-183
lines changed

src/Decimal.ts

Lines changed: 1158 additions & 0 deletions
Large diffs are not rendered by default.

src/StripeResource.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
getAPIMode,
32
getDataFromArgs,
43
getOptionsFromArgs,
54
makeURLInterpolator,
@@ -17,7 +16,7 @@ import {
1716
UrlInterpolator,
1817
} from './Types.js';
1918
import {HttpClientResponseInterface} from './net/HttpClient.js';
20-
import {coerceV2RequestData, coerceV2ResponseData} from './V2Int64.js';
19+
import {coerceV2RequestData, coerceV2ResponseData} from './V2Coercion.js';
2120

2221
// Provide extension mechanism for Stripe Resource Sub-Classes
2322
StripeResource.extend = protoExtend;
@@ -211,9 +210,8 @@ StripeResource.prototype = {
211210
return;
212211
}
213212

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) {
213+
// Coerce int64_string/decimal_string fields in request body: number/Decimal → string
214+
if (spec.requestSchema && opts.bodyData) {
217215
opts.bodyData = coerceV2RequestData(
218216
opts.bodyData,
219217
spec.requestSchema
@@ -227,9 +225,9 @@ StripeResource.prototype = {
227225
if (err) {
228226
reject(err);
229227
} else {
230-
// Coerce int64_string fields in response: string → bigint
228+
// Coerce int64_string/decimal_string fields in response: string → bigint/Decimal
231229
try {
232-
if (apiMode === 'v2' && spec.responseSchema) {
230+
if (spec.responseSchema) {
233231
coerceV2ResponseData(response, spec.responseSchema);
234232
}
235233
resolve(

src/Types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type BufferedFile = {
1818
};
1919
export type V2RuntimeSchema =
2020
| {kind: 'int64_string'}
21+
| {kind: 'decimal_string'}
2122
| {kind: 'object'; fields: Record<string, V2RuntimeSchema>}
2223
| {kind: 'array'; element: V2RuntimeSchema}
2324
| {kind: 'nullable'; inner: V2RuntimeSchema};

src/V2Int64.ts renamed to src/V2Coercion.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {Decimal} from './Decimal.js';
12
import {V2RuntimeSchema} from './Types.js';
23

34
/**
@@ -21,6 +22,13 @@ export const coerceV2RequestData = (
2122
? String(data)
2223
: data;
2324

25+
case 'decimal_string':
26+
// Duck-type check: Decimal instances have toFixed() and isZero() methods.
27+
return typeof (data as any).toFixed === 'function' &&
28+
typeof (data as any).isZero === 'function'
29+
? (data as Decimal).toString()
30+
: data;
31+
2432
case 'object': {
2533
if (typeof data !== 'object' || Array.isArray(data)) {
2634
return data;
@@ -78,6 +86,18 @@ export const coerceV2ResponseData = (
7886
}
7987
return data;
8088

89+
case 'decimal_string':
90+
if (typeof data === 'string') {
91+
try {
92+
return Decimal.from(data);
93+
} catch {
94+
throw new Error(
95+
`Failed to coerce decimal_string value: expected a decimal string, got '${data}'`
96+
);
97+
}
98+
}
99+
return data;
100+
81101
case 'object': {
82102
if (typeof data !== 'object' || Array.isArray(data)) {
83103
return data;

0 commit comments

Comments
 (0)