(input: BuildBodyInput)
| 40 | // json wins over body when both are provided; tests assert single-source-of-truth via type system, |
| 41 | // but at runtime we still prefer json explicitly. |
| 42 | export function buildBody(input: BuildBodyInput): BuildBodyResult { |
| 43 | const { json, body, method } = input |
| 44 | const isPayloadMethod = method !== 'GET' && method !== 'HEAD' |
| 45 | |
| 46 | if (json !== undefined) { |
| 47 | if (!isPayloadMethod) |
| 48 | return { body: undefined, contentType: undefined } |
| 49 | if (isJSONSerializable(json)) |
| 50 | return { body: JSON.stringify(json), contentType: 'application/json' } |
| 51 | return { body: json as BodyInit, contentType: undefined } |
| 52 | } |
| 53 | |
| 54 | // A raw `body` is passed through untouched. This is replay-safe only for buffered |
| 55 | // payloads (string, Blob, FormData, typed arrays) — a single-shot ReadableStream |
| 56 | // would be consumed on the first attempt and replay empty on retry. Safe today |
| 57 | // because the only stream/multipart caller (file-upload) uses POST, which is not |
| 58 | // in RETRY_METHODS; revisit if a ReadableStream body is ever sent over GET/PUT/DELETE. |
| 59 | if (body !== undefined && isPayloadMethod) |
| 60 | return { body, contentType: undefined } |
| 61 | |
| 62 | return { body: undefined, contentType: undefined } |
| 63 | } |
no test coverage detected