Repository navigation
Conversation
The body was only prepared when truthy, so 0 and false skipped the JSON serialization and the content-type header, and fetch sent them as text/plain. Only undefined and null mean "no body" now; an empty string is still left to fetch, as there is nothing to serialize.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
agammann
left a comment
There was a problem hiding this comment.
I traced the new guard through the JSON serialization path: 0 and false now reach isJSONSerializable and get the JSON content type, while undefined, null, and the deliberately excluded empty string keep their existing empty-body behavior. The echo tests check both the bytes sent and the header, so this covers the regression at the public $fetch boundary. I did not run tests locally.
BryanOnKeys
left a comment
There was a problem hiding this comment.
The runtime guard now lets 0 and false reach JSON serialization, but TypeScript callers still cannot pass those values. On 6d422b9, TypeScript 5.9.3 reports TS2322 at test/index.test.ts:165 for the new body: value assertion. I ran tsc --noEmit --isolatedDeclarations false --composite false --incremental false against the PR base and head with the same dependencies. The base passed and the head produced that error.
Could we extend FetchOptions.body to accept number and boolean alongside the existing types so the new test exercises the supported public API without a cast?
resolves #645
A request body of
0orfalsewas sent astext/plain, while1andtruewere sent as JSON: the body was only prepared when truthy (if (context.options.body && isPayloadMethod(...))), so0andfalseskipped the serialization and thecontent-typeheader and reachedfetch()as they are.The guard now checks for "no body" instead of truthiness: only
undefinedandnullmean no body, as forfetch()itself. An empty string is deliberately left tofetch()as before, since there is nothing to serialize and an emptyapplication/jsonbody would not be a JSON document.isJSONSerializable()already accepted numbers and booleans, so nothing else changes.Tests:
0andfalsearrive asapplication/jsonwith the body0/false;undefinedandnullsend no body and no content type. The first test fails without the fix.Reproduction: https://github.com/rhelling/ofetch-request-body-repo —
0 of 8 casesfail against a build of this branch,2 of 8against 1.5.1 and 2.0.0-alpha.3.Summary by CodeRabbit
0andfalsevalues as JSON bodies.undefinedandnullcontinue to produce empty bodies without a content-type header.