Service binding type

What is the correct type for a service binding (worker to worker)? I have currently typed as Fetcher but I'm getting an error trying to provide a body to the request:
interface Platform {
env: {
API: Fetcher
}
}

const formData = new FormData()

const res = await platform.env.API.fetch("/send-otp", {
method: "POST",
body: formData,
})
interface Platform {
env: {
API: Fetcher
}
}

const formData = new FormData()

const res = await platform.env.API.fetch("/send-otp", {
method: "POST",
body: formData,
})
This gives me an error on body : Type 'FormData' is not assignable to type 'BodyInit | null | undefined' so I assume I'm typing the binding incorrectly as RequestInit should accept FormData based on the docs for Fetch. The docs suggest that Fetcher is the correct type, but as above that doesn't seem right? https://developers.cloudflare.com/workers/runtime-apis/service-bindings
Service bindings · Cloudflare Workers docs
Service bindings are an API that facilitate Worker-to-Worker communication via explicit bindings defined in your configuration. A Service binding …
3 Replies
Cyb3r-Jak3
Cyb3r-Jak317mo ago
This appears to be an issue with fetch and FormData types https://github.com/form-data/form-data/issues/513
cdslash
cdslashOP17mo ago
Ahh, thanks - I wasn't aware of that issue. Is there any workaround other than as any?
cdslash
cdslashOP16mo ago
I did find a StackOverflow answer that suggested I just need to stringify the body:
const res = await platform.env.API.fetch(
`https://${url.hostname}/api/validate-session`,
{
method: "POST",
body: JSON.stringify(body),
}
)
const res = await platform.env.API.fetch(
`https://${url.hostname}/api/validate-session`,
{
method: "POST",
body: JSON.stringify(body),
}
)
This now works. https://stackoverflow.com/questions/43997163/how-to-make-request-body-type-compatible-with-requestinit-or-bodyinit-when-using
Stack Overflow
How to make request body type compatible with RequestInit or BodyIn...
I started to use Typescript for my nodejs project. For accessing some external API, I use node-fetch to make requests. While setting up a PUT request, an error pops up, saying that the given body i...
Want results from more Discord servers?
Add your server