-
Notifications
You must be signed in to change notification settings - Fork 95
fix(router): allow required flags in handlers #2283
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: refactor
Are you sure you want to change the base?
Changes from all commits
46b2e50
7db23cf
20aa718
cadf60f
b674279
7666050
d11a5cd
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import type z from "zod"; | ||
| import type { Context, ContextKey } from "./context"; | ||
| import type { Middleware } from "./middleware"; | ||
|
|
||
| // Flag is generic over its literal name `N` and its inferred value type `T`, so a | ||
| // tuple of flags can be mapped to a typed object at the authoring boundary (see | ||
|
|
@@ -110,6 +111,7 @@ type CreateHandlerInput< | |
| arguments?: A; | ||
| handle?: HandleFn<F, A>; | ||
| children?: Handler[]; | ||
| middlewares?: Middleware[]; | ||
|
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. Only the router should know about the middleware, not all the handlers.
Contributor
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. yeah this is only needed because we have handlers that want middleware that isn't on its parent router (ex. some project commands support tui, some don't). However, if we move the tui middleware to the root handler this should be able to go away. |
||
| }; | ||
|
|
||
| const noOpHandler = async (_ctx: Context, _flags: any, _args: any): Promise<void> => {}; | ||
|
|
@@ -121,6 +123,7 @@ class BaseHandler implements Handler { | |
| _arguments: Argument[]; | ||
| _handle: HandleFn<any, any>; | ||
| _children: Handler[]; | ||
| _middlewares: Middleware[]; | ||
|
|
||
| constructor( | ||
| input: CreateHandlerInput<readonly Flag<string, any>[], readonly Argument<string, any>[]>, | ||
|
|
@@ -131,6 +134,7 @@ class BaseHandler implements Handler { | |
| this._arguments = (input.arguments ?? []) as Argument[]; | ||
| this._handle = (input.handle ?? noOpHandler) as HandleFn<any, any>; | ||
| this._children = input.children ?? []; | ||
| this._middlewares = input.middlewares ?? []; | ||
| } | ||
|
|
||
| name(): string { | ||
|
|
@@ -160,6 +164,10 @@ class BaseHandler implements Handler { | |
| children(): Handler[] { | ||
| return this._children; | ||
| } | ||
|
|
||
| middlewares(): Middleware[] { | ||
| return this._middlewares; | ||
| } | ||
| } | ||
|
|
||
| // createHandler infers the flags tuple from `flags` (the `const` type parameter | ||
|
|
||
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.
what do we think about using commander's
helpGroup? ref: https://github.com/tj/commander.js/blob/master/examples/help-groups.jsoutput would look like:
this would render required flags in a dedicated section while keeping presentation separate from validation. also, we should avoid makeOptionMandatory(), since it rejects missing flags before the TUI middleware can run
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.
I like that idea! I think @jariy17 is working on something similar about grouping flags, so I think we can revisit this once that lands.