fix(core/js-sdk) Add the credentials field in the fetch() only if supported (#10446)

fixes https://github.com/medusajs/nextjs-starter-medusa/issues/421

The root cause of the issue is that credentials is not supported by CloudFlare workers.
This commit is contained in:
Salvador Girones Gil
2024-12-05 14:10:40 +00:00
committed by GitHub
parent 16663ec813
commit a7ad809520
+7 -1
View File
@@ -58,11 +58,17 @@ const normalizeRequest = (
body = JSON.stringify(body)
}
const isFetchCredentialsSupported = "credentials" in Request.prototype
return {
...init,
headers,
// TODO: Setting this to "include" poses some security risks, as it will send cookies to any domain. We should consider making this configurable.
credentials: config.auth?.type === "session" ? "include" : "omit",
credentials: isFetchCredentialsSupported
? config.auth?.type === "session"
? "include"
: "omit"
: undefined,
...(body ? { body: body as RequestInit["body"] } : {}),
} as RequestInit
}