cloudflare-docs
cloudflare-docs copied to clipboard
Functions typescript example
Which Cloudflare product does this pertain to?
Pages
Existing documentation URL(s)
- https://developers.cloudflare.com/pages/platform/functions/#using-typescript
- https://developers.cloudflare.com/pages/platform/functions/#functions-routing
Section that requires update
- https://developers.cloudflare.com/pages/platform/functions/#writing-your-first-function
or showing an example in the
using typescriptsection.
What needs to change?
While routing and "using typescript" specify how to use typescript files / ensure the types are installed, there are no examples with the actual type annotations for an onRequest function.
VSCode typescript language server doesn't automatically determine the type for
export async function onRequestPost(context) {
It thinks that context is (parameter) context: any which limits typescript's ability to provide documentation / discovery of capabilities.
How should it change?
Providing at least one example with type annotations
Additional information
No response
In the interim, the type you're looking for is EventContext
https://github.com/cloudflare/workers-types/blob/master/index.d.ts#L2015
@KianNH - thanks - reading https://blog.cloudflare.com/building-full-stack-with-pages/ it seems like PagesFunction<{ NAME: KVNamespace }> is the way to go!
@WalshyDev 's PR will address this needed change: https://github.com/cloudflare/cloudflare-docs/pull/5015
Still can't figure this out what to put in that EventContext?
any hint?
@katopz export const onRequest: PagesFunction<Env> = async (context) => {