cloudflare-docs icon indicating copy to clipboard operation
cloudflare-docs copied to clipboard

Functions typescript example

Open anotherjesse opened this issue 2 years ago • 2 comments

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

anotherjesse avatar Jul 31 '22 18:07 anotherjesse

In the interim, the type you're looking for is EventContext

https://github.com/cloudflare/workers-types/blob/master/index.d.ts#L2015

KianNH avatar Aug 01 '22 00:08 KianNH

@KianNH - thanks - reading https://blog.cloudflare.com/building-full-stack-with-pages/ it seems like PagesFunction<{ NAME: KVNamespace }> is the way to go!

anotherjesse avatar Aug 01 '22 17:08 anotherjesse

@WalshyDev 's PR will address this needed change: https://github.com/cloudflare/cloudflare-docs/pull/5015

deadlypants1973 avatar Sep 29 '22 13:09 deadlypants1973

image

Still can't figure this out what to put in that EventContext?

image

any hint?

katopz avatar Dec 08 '22 12:12 katopz

@katopz export const onRequest: PagesFunction<Env> = async (context) => {

tomfuertes avatar Mar 10 '23 22:03 tomfuertes