-
Notifications
You must be signed in to change notification settings - Fork 38
Reduce duplication in Tasks API layer #805
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
Open
EhabY
wants to merge
2
commits into
refactor-tasks-webview
Choose a base branch
from
reduce-duplication-tasks-api
base: refactor-tasks-webview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,20 +9,23 @@ | |
|
|
||
| /** Request definition: params P, response R */ | ||
| export interface RequestDef<P = void, R = void> { | ||
| readonly kind: "request"; | ||
| readonly method: string; | ||
| /** @internal Phantom types for inference - not present at runtime */ | ||
| readonly _types?: { params: P; response: R }; | ||
| } | ||
|
|
||
| /** Command definition: params P, no response */ | ||
| export interface CommandDef<P = void> { | ||
| readonly kind: "command"; | ||
| readonly method: string; | ||
| /** @internal Phantom type for inference - not present at runtime */ | ||
| readonly _types?: { params: P }; | ||
| } | ||
|
|
||
| /** Notification definition: data D (extension to webview) */ | ||
| export interface NotificationDef<D = void> { | ||
| readonly kind: "notification"; | ||
| readonly method: string; | ||
| /** @internal Phantom type for inference - not present at runtime */ | ||
| readonly _types?: { data: D }; | ||
|
|
@@ -34,19 +37,19 @@ export interface NotificationDef<D = void> { | |
| export function defineRequest<P = void, R = void>( | ||
| method: string, | ||
| ): RequestDef<P, R> { | ||
| return { method } as RequestDef<P, R>; | ||
| return { kind: "request", method } as RequestDef<P, R>; | ||
| } | ||
|
|
||
| /** Define a fire-and-forget command */ | ||
| export function defineCommand<P = void>(method: string): CommandDef<P> { | ||
| return { method } as CommandDef<P>; | ||
| return { kind: "command", method } as CommandDef<P>; | ||
| } | ||
|
|
||
| /** Define a push notification (extension to webview) */ | ||
| export function defineNotification<D = void>( | ||
| method: string, | ||
| ): NotificationDef<D> { | ||
| return { method } as NotificationDef<D>; | ||
| return { kind: "notification", method } as NotificationDef<D>; | ||
| } | ||
|
|
||
| // --- Wire format --- | ||
|
|
@@ -73,28 +76,135 @@ export interface IpcNotification<D = unknown> { | |
| readonly data?: D; | ||
| } | ||
|
|
||
| // --- Handler utilities --- | ||
|
|
||
| /** Extract params type from a request/command definition */ | ||
| export type ParamsOf<T> = T extends { _types?: { params: infer P } } ? P : void; | ||
|
|
||
| /** Extract response type from a request definition */ | ||
| export type ResponseOf<T> = T extends { _types?: { response: infer R } } | ||
| ? R | ||
| : void; | ||
| // --- Mapped types for handler completeness --- | ||
|
|
||
| /** Requires a handler for every RequestDef in Api. Compile error if one is missing. */ | ||
| export type RequestHandlerMap<Api> = { | ||
| [K in keyof Api as Api[K] extends { kind: "request" } | ||
| ? K | ||
| : never]: Api[K] extends RequestDef<infer P, infer R> | ||
| ? (params: P) => Promise<R> | ||
| : never; | ||
| }; | ||
|
|
||
| /** Requires a handler for every CommandDef in Api. Compile error if one is missing. */ | ||
| export type CommandHandlerMap<Api> = { | ||
| [K in keyof Api as Api[K] extends { kind: "command" } | ||
| ? K | ||
| : never]: Api[K] extends CommandDef<infer P> | ||
| ? (params: P) => void | Promise<void> | ||
| : never; | ||
| }; | ||
|
|
||
| // --- API hook type --- | ||
|
|
||
| /** Derives a fully typed hook interface from an API definition object. */ | ||
| export type ApiHook<Api> = { | ||
| [K in keyof Api as Api[K] extends { kind: "request" } | ||
| ? K | ||
| : never]: Api[K] extends RequestDef<infer P, infer R> | ||
| ? (...args: P extends void ? [] : [params: P]) => Promise<R> | ||
| : never; | ||
| } & { | ||
| [K in keyof Api as Api[K] extends { kind: "command" } | ||
| ? K | ||
| : never]: Api[K] extends CommandDef<infer P> | ||
| ? (...args: P extends void ? [] : [params: P]) => void | ||
| : never; | ||
| } & { | ||
| [K in keyof Api as Api[K] extends { kind: "notification" } | ||
| ? `on${Capitalize<K & string>}` | ||
| : never]: Api[K] extends NotificationDef<infer D> | ||
| ? D extends void | ||
| ? (cb: () => void) => () => void | ||
| : (cb: (data: D) => void) => () => void | ||
| : never; | ||
| }; | ||
|
|
||
| // --- Builder functions --- | ||
|
|
||
| /** Build a method-indexed map of request handlers with compile-time completeness. */ | ||
| export function buildRequestHandlers< | ||
| Api extends Record<string, { method: string }>, | ||
| >( | ||
| api: Api, | ||
| handlers: RequestHandlerMap<Api>, | ||
| ): Record<string, (params: unknown) => Promise<unknown>>; | ||
| export function buildRequestHandlers( | ||
| api: Record<string, { method: string }>, | ||
| handlers: Record<string, (params: unknown) => Promise<unknown>>, | ||
| ) { | ||
| const result: Record<string, (params: unknown) => Promise<unknown>> = {}; | ||
| for (const key of Object.keys(handlers)) { | ||
| result[api[key].method] = handlers[key]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** Type-safe request handler - infers params and return type from definition */ | ||
| export function requestHandler<P, R>( | ||
| _def: RequestDef<P, R>, | ||
| fn: (params: P) => Promise<R>, | ||
| ): (params: unknown) => Promise<unknown> { | ||
| return fn as (params: unknown) => Promise<unknown>; | ||
| /** Build a method-indexed map of command handlers with compile-time completeness. */ | ||
| export function buildCommandHandlers< | ||
| Api extends Record<string, { method: string }>, | ||
| >( | ||
| api: Api, | ||
| handlers: CommandHandlerMap<Api>, | ||
| ): Record<string, (params: unknown) => void | Promise<void>>; | ||
| export function buildCommandHandlers( | ||
| api: Record<string, { method: string }>, | ||
| handlers: Record<string, (params: unknown) => void | Promise<void>>, | ||
| ) { | ||
| const result: Record<string, (params: unknown) => void | Promise<void>> = {}; | ||
| for (const key of Object.keys(handlers)) { | ||
| result[api[key].method] = handlers[key]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** Type-safe command handler - infers params type from definition */ | ||
| export function commandHandler<P>( | ||
| _def: CommandDef<P>, | ||
| fn: (params: P) => void | Promise<void>, | ||
| ): (params: unknown) => void | Promise<void> { | ||
| return fn as (params: unknown) => void | Promise<void>; | ||
| /** Build a typed API hook from an API definition and IPC primitives. */ | ||
| export function buildApiHook< | ||
| Api extends Record<string, { kind: string; method: string }>, | ||
| >( | ||
| api: Api, | ||
| ipc: { | ||
| request: <P, R>( | ||
| def: { method: string; _types?: { params: P; response: R } }, | ||
| ...args: P extends void ? [] : [params: P] | ||
| ) => Promise<R>; | ||
| command: <P>( | ||
| def: { method: string; _types?: { params: P } }, | ||
| ...args: P extends void ? [] : [params: P] | ||
| ) => void; | ||
| onNotification: <D>( | ||
| def: { method: string; _types?: { data: D } }, | ||
| cb: (data: D) => void, | ||
| ) => () => void; | ||
| }, | ||
| ): ApiHook<Api>; | ||
| export function buildApiHook( | ||
|
Collaborator
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. non-blocking: thoughts on a unit test for this function? If it is sufficiently tested indirectly then no worries. |
||
| api: Record<string, { kind: string; method: string }>, | ||
| ipc: { | ||
| request: (def: { method: string }, params: unknown) => Promise<unknown>; | ||
|
Member
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. shouldn't be |
||
| command: (def: { method: string }, params: unknown) => void; | ||
| onNotification: ( | ||
| def: { method: string }, | ||
| cb: (data: unknown) => void, | ||
| ) => () => void; | ||
| }, | ||
| ) { | ||
| const result: Record<string, unknown> = {}; | ||
| for (const [key, def] of Object.entries(api)) { | ||
| switch (def.kind) { | ||
| case "request": | ||
| result[key] = (params: unknown) => ipc.request(def, params); | ||
| break; | ||
| case "command": | ||
| result[key] = (params: unknown) => ipc.command(def, params); | ||
| break; | ||
| case "notification": | ||
| result[`on${key[0].toUpperCase()}${key.slice(1)}`] = ( | ||
| cb: (data: unknown) => void, | ||
| ) => ipc.onNotification(def, cb); | ||
| break; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,6 @@ | ||
| /** | ||
| * Tasks API hook - provides type-safe access to all Tasks operations. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const api = useTasksApi(); | ||
| * const tasks = await api.getTasks(); | ||
| * api.viewInCoder("task-id"); | ||
| * ``` | ||
| */ | ||
|
|
||
| import { | ||
| TasksApi, | ||
| type CreateTaskParams, | ||
| type Task, | ||
| type TaskActionParams, | ||
| } from "@repo/shared"; | ||
| import { TasksApi, buildApiHook } from "@repo/shared"; | ||
| import { useIpc } from "@repo/webview-shared/react"; | ||
|
|
||
| export function useTasksApi() { | ||
| const { request, command, onNotification } = useIpc(); | ||
|
|
||
| return { | ||
| // Requests | ||
| getTasks: () => request(TasksApi.getTasks), | ||
| getTemplates: () => request(TasksApi.getTemplates), | ||
| getTask: (taskId: string) => request(TasksApi.getTask, { taskId }), | ||
| getTaskDetails: (taskId: string) => | ||
| request(TasksApi.getTaskDetails, { taskId }), | ||
| createTask: (params: CreateTaskParams) => | ||
| request(TasksApi.createTask, params), | ||
| deleteTask: (params: TaskActionParams) => | ||
| request(TasksApi.deleteTask, params), | ||
| pauseTask: (params: TaskActionParams) => | ||
| request(TasksApi.pauseTask, params), | ||
| resumeTask: (params: TaskActionParams) => | ||
| request(TasksApi.resumeTask, params), | ||
| downloadLogs: (taskId: string) => | ||
| request(TasksApi.downloadLogs, { taskId }), | ||
| sendTaskMessage: (taskId: string, message: string) => | ||
| request(TasksApi.sendTaskMessage, { taskId, message }), | ||
|
|
||
| // Commands | ||
| viewInCoder: (taskId: string) => command(TasksApi.viewInCoder, { taskId }), | ||
| viewLogs: (taskId: string) => command(TasksApi.viewLogs, { taskId }), | ||
| stopStreamingWorkspaceLogs: () => | ||
| command(TasksApi.stopStreamingWorkspaceLogs), | ||
|
|
||
| // Notifications | ||
| onTaskUpdated: (cb: (task: Task) => void) => | ||
| onNotification(TasksApi.taskUpdated, cb), | ||
| onTasksUpdated: (cb: (tasks: Task[]) => void) => | ||
| onNotification(TasksApi.tasksUpdated, cb), | ||
| onWorkspaceLogsAppend: (cb: (lines: string[]) => void) => | ||
| onNotification(TasksApi.workspaceLogsAppend, cb), | ||
| onRefresh: (cb: () => void) => onNotification(TasksApi.refresh, cb), | ||
| onShowCreateForm: (cb: () => void) => | ||
| onNotification(TasksApi.showCreateForm, cb), | ||
| }; | ||
| return buildApiHook(TasksApi, useIpc()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
with never?
readonly _types?: never & { params: P; response: R };