Fetch not calling JSON.stringify for body objects.

Hey, i'm currently using @sapphire/fetch and trying to send a POST request for a API with a object on the body. The thing is: the object is not being stringified and the API is receiving [object Object]. I went through the @sapphire/fetch source code and, apparently, the function that determines if a body should be stringified or not (shouldJsonStringify) is returning false for my object. (https://github.com/sapphiredev/utilities/blob/main/packages/fetch/src/lib/fetch.ts#L191-L208) And just to make it clear, the object that i'm trying to send is in fact...a object, because using typeof theObject returns object lol, but its doesn't have a toJSON function or a Object constructor, in fact it has a [Function: Object] constructor, which is not included in the shouldJsonStringify function. Hope that this is enough information, happy to create a issue or maybe create a pull request with a fix. Downgrading the lib to 3.0.2 is my workaround for now.
GitHub
utilities/packages/fetch/src/lib/fetch.ts at main · sapphiredev/uti...
Common JavaScript utilities for Sapphire Projects. Contribute to sapphiredev/utilities development by creating an account on GitHub.
7 Replies
Favna
Favna3mo ago
What's your object? We made that change because I use the lib at work as well and we needed to be able to upload Blobs and we have this running in DTA for some weeks now without problems. That includes regular objects.
sazz
sazzOP3mo ago
it's pretty much this { email: "[email protected]", password: "abc123" }. it comes from a zod z.infer of a schema can provide a code snippet too, i am just not on my computer rn I think this just another case of JavaScript is being weird, and having some plain objects with different constructors than Object for some reason
Favna
Favna3mo ago
please provide a repro sample yes My best guess is that this is some weird quirk of zod so we'll need a repro to ensure we can support it
kyra
kyra3mo ago
Yep, they probably do Object.create so I’ll have to check
sazz
sazzOP3mo ago
made a repro sample, it's pretty much like this: lmk if u guys need more info, happy to provide
import { fetch } from "@sapphire/fetch";
import { z } from "zod";

const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
});

async function login(values: z.infer<typeof loginSchema>) {
try {
const response = await fetch(
`<api_url>/auth/login`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: values,
}
);

console.log(response)

return {
success: true,
};
} catch (error) {
console.error(error)
}
}

login({
password: "abc123"
})
import { fetch } from "@sapphire/fetch";
import { z } from "zod";

const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
});

async function login(values: z.infer<typeof loginSchema>) {
try {
const response = await fetch(
`<api_url>/auth/login`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: values,
}
);

console.log(response)

return {
success: true,
};
} catch (error) {
console.error(error)
}
}

login({
password: "abc123"
})
kyra
kyra3mo ago
I can't repro
No description
sazz
sazzOP3mo ago
hmmm wonder if it's being caused by something else then i will take a look later today
Want results from more Discord servers?
Add your server