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 typescript
section.
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
![image](https://user-images.githubusercontent.com/97060/206444851-da7220ae-0025-4bd9-8993-93115d406fd3.png)
Still can't figure this out what to put in that EventContext
?
![image](https://user-images.githubusercontent.com/97060/206444972-a765b466-6e54-4c73-9f6f-a378097c518d.png)
any hint?
@katopz export const onRequest: PagesFunction<Env> = async (context) => {