@@ -6,11 +6,11 @@ import { resumeHook } from './runtime/resume-hook.js';
66/**
77 * Defines a typed hook for type-safe hook creation and resumption.
88 *
9- * This helper provides type safety by allowing you to define the payload type once
10- * and reuse it when creating hooks and resuming them .
9+ * This helper provides type safety by allowing you to define the input and output types
10+ * for the hook's payload, with optional validation and transformation via a schema .
1111 *
12- * @param schema - Schema used to validate and transform the payload before resuming
13- * @returns An object with `create` and `resume` functions pre-typed with the payload type
12+ * @param schema - Schema used to validate and transform the input payload before resuming
13+ * @returns An object with `create` and `resume` functions pre-typed with the input and output types
1414 *
1515 * @example
1616 *
@@ -23,49 +23,48 @@ import { resumeHook } from './runtime/resume-hook.js';
2323 * "use workflow";
2424 *
2525 * const hook = approvalHook.create();
26- * const result = await hook; // Fully typed as { approved: boolean; comment: string }
26+ * const result = await hook; // Fully typed as { approved: boolean; comment: string; }
2727 * }
2828 *
2929 * // In an API route
3030 * export async function POST(request: Request) {
3131 * const { token, approved, comment } = await request.json();
32- * await approvalHook.resume(token, { approved, comment });
32+ * await approvalHook.resume(token, { approved, comment }); // Input type
3333 * return Response.json({ success: true });
3434 * }
3535 * ```
3636 */
37- export function defineHook < T > ( {
37+ export function defineHook < TInput , TOutput = TInput > ( {
3838 schema,
3939} : {
40- schema ?: StandardSchemaV1 < T , T > ;
40+ schema ?: StandardSchemaV1 < TInput , TOutput > ;
4141} = { } ) {
4242 return {
4343 /**
44- * Creates a new hook with the defined payload type.
44+ * Creates a new hook with the defined output type.
4545 *
4646 * Note: This method is not available in runtime bundles. Use it from workflow contexts only.
4747 *
4848 * @param _options - Optional hook configuration
49- * @returns A Hook that resolves to the defined payload type
49+ * @returns A Hook that resolves to the defined output type
5050 */
51- // @ts -expect-error `options` is here for types/docs
52- create ( options ?: HookOptions ) : Hook < T > {
51+ create ( _options ?: HookOptions ) : Hook < TOutput > {
5352 throw new Error (
5453 '`defineHook().create()` can only be called inside a workflow function.'
5554 ) ;
5655 } ,
5756
5857 /**
59- * Resumes a hook by sending a payload with the defined type.
58+ * Resumes a hook by sending a payload with the defined input type.
6059 * This is a type-safe wrapper around the `resumeHook` runtime function.
6160 *
6261 * @param token - The unique token identifying the hook
6362 * @param payload - The payload to send; if a `schema` is configured it is validated/transformed before resuming
6463 * @returns Promise resolving to the hook entity, or null if the hook doesn't exist
6564 */
66- async resume ( token : string , payload : T ) : Promise < HookEntity | null > {
65+ async resume ( token : string , payload : TInput ) : Promise < HookEntity | null > {
6766 if ( ! schema ?. [ '~standard' ] ) {
68- return await resumeHook < T > ( token , payload ) ;
67+ return await resumeHook ( token , payload ) ;
6968 }
7069
7170 let result = schema [ '~standard' ] . validate ( payload ) ;
@@ -78,7 +77,7 @@ export function defineHook<T>({
7877 throw new Error ( JSON . stringify ( result . issues , null , 2 ) ) ;
7978 }
8079
81- return await resumeHook < T > ( token , result . value ) ;
80+ return await resumeHook < TOutput > ( token , result . value ) ;
8281 } ,
8382 } ;
8483}
0 commit comments