|
| 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