Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetch): support stringifying arrays #814

Merged
merged 5 commits into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/fetch/src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export async function fetch(url: URL | string, options?: RequestOptions | FetchR
* @param value - The value to check.
* @returns A boolean indicating whether the value should be stringified as JSON.
*/
function shouldJsonStringify(value: unknown) {
function shouldJsonStringify(value: unknown): boolean {
// If the value is not an object, it should not be stringified
if (typeof value !== 'object') return false;
// Buffers should not be stringified
Expand All @@ -202,6 +202,8 @@ function shouldJsonStringify(value: unknown) {
if (value.constructor === Object) return true;
// Has toJSON method
if ('toJSON' in value && typeof value.toJSON === 'function') return true;
// Array of items, check if every item in the array is serializable
if (Array.isArray(value)) return value.every(shouldJsonStringify);
favna marked this conversation as resolved.
Show resolved Hide resolved

// Anything else (such as streams or unserializables)
return false;
Expand Down