-
Notifications
You must be signed in to change notification settings - Fork 331
feat: add Props type parameter to ExportedHandler for typed ctx.props #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agents": minor | ||
| --- | ||
|
|
||
| Add Props type parameter to ExportedHandler for typed ctx.props |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * Type tests for ExportedHandler with Props support | ||
| * @see https://github.com/cloudflare/agents/issues/501 | ||
| */ | ||
| import type { ExportedHandler } from "../types"; | ||
|
|
||
| type TestEnv = { | ||
| MY_VAR: string; | ||
| }; | ||
|
|
||
| type TestProps = { | ||
| userId: string; | ||
| baseUrl: string; | ||
| }; | ||
|
|
||
| // Props flows to fetch handler | ||
| { | ||
| const handler: ExportedHandler<TestEnv, TestProps> = { | ||
| async fetch(request, env, ctx) { | ||
| const _userId: string = ctx.props.userId; | ||
| const _myVar: string = env.MY_VAR; | ||
| return new Response("ok"); | ||
| } | ||
| }; | ||
| handler; | ||
| } | ||
|
|
||
| // Props flows to scheduled handler | ||
| { | ||
| const handler: ExportedHandler<TestEnv, TestProps> = { | ||
| async scheduled(controller, env, ctx) { | ||
| const _userId: string = ctx.props.userId; | ||
| } | ||
| }; | ||
| handler; | ||
| } | ||
|
|
||
| // Props flows to queue handler | ||
| { | ||
| type QueueMessage = { data: string }; | ||
| const handler: ExportedHandler<TestEnv, TestProps, QueueMessage> = { | ||
| async queue(batch, env, ctx) { | ||
| const _userId: string = ctx.props.userId; | ||
| const _data: string = batch.messages[0].body.data; | ||
| } | ||
| }; | ||
| handler; | ||
| } | ||
|
|
||
| // satisfies pattern works | ||
| { | ||
| const handler = { | ||
| async fetch(request, env, ctx) { | ||
| const userId: string = ctx.props.userId; | ||
| return new Response(userId); | ||
| } | ||
| } satisfies ExportedHandler<TestEnv, TestProps>; | ||
| handler; | ||
| } | ||
|
|
||
| // Without Props, ctx.props is unknown (default) | ||
| { | ||
| const handler: ExportedHandler<TestEnv> = { | ||
| async fetch(request, env, ctx) { | ||
| const _props: unknown = ctx.props; | ||
| return new Response("ok"); | ||
| } | ||
| }; | ||
| handler; | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should be typing these here. These are already defined in the output of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the quick reply! I want to make sure I understand the concern. Are you saying wrangler types already generates ExportedHandler with a Props parameter? I added Props support to enable typed ctx.props which I don't believe exists in the current wrangler types output. Should these types live elsewhere, or should I be extending the wrangler generated types differently? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like a good improvement independently of the types below.