cdslash
cdslash
CDCloudflare Developers
Created by cdslash on 8/11/2023 in #workers-help
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
4 replies
CDCloudflare Developers
Created by cdslash on 3/1/2023 in #workers-help
Including secrets in types generated by wrangler
I am trying to use npx wrangler types to allow my wrangler.toml to be the source of truth for my worker types. I previously used a bindings.ts file that had something like:
export interface Bindings {
PUBLIC_CACHE: KVNamespace
SECRET_API_KEY: string
}
export interface Bindings {
PUBLIC_CACHE: KVNamespace
SECRET_API_KEY: string
}
where the SECRET_API_KEY is added with echo 'abcdef12345' | npx wrangler secret put SECRET_API_KEY. I noted in a comment in my wrangler.toml that this exists, but otherwise it's not included. When generating types, the secret is not generated in the Env within worker-configuration.d.ts presumably because it's not in wrangler.toml. Is there a way to include secrets that wrangler has created but that are not included in wrangler.toml when generating types?
2 replies