Skip to content

Commit 0887e71

Browse files
committed
additional react-hooks jsdocs
1 parent 3433b12 commit 0887e71

File tree

3 files changed

+146
-9
lines changed

3 files changed

+146
-9
lines changed

packages/react-hooks/src/hooks/useApiClient.ts

+26
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,38 @@
33
import { ApiClient, ApiRequestOptions } from "@trigger.dev/core/v3";
44
import { useTriggerAuthContextOptional } from "../contexts.js";
55

6+
/**
7+
* Configuration options for creating an API client instance.
8+
*/
69
export type UseApiClientOptions = {
10+
/** Optional access token for authentication */
711
accessToken?: string;
12+
/** Optional base URL for the API endpoints */
813
baseURL?: string;
14+
/** Optional additional request configuration */
915
requestOptions?: ApiRequestOptions;
1016
};
1117

18+
/**
19+
* Hook to create an API client instance using authentication context or provided options.
20+
*
21+
* @param {UseApiClientOptions} [options] - Configuration options for the API client
22+
* @returns {ApiClient} An initialized API client instance
23+
* @throws {Error} When no access token is available in either context or options
24+
*
25+
* @example
26+
* ```ts
27+
* // Using context authentication
28+
* const apiClient = useApiClient();
29+
*
30+
* // Using custom options
31+
* const apiClient = useApiClient({
32+
* accessToken: "your-access-token",
33+
* baseURL: "https://api.my-trigger.com",
34+
* requestOptions: { retry: { maxAttempts: 10 } }
35+
* });
36+
* ```
37+
*/
1238
export function useApiClient(options?: UseApiClientOptions): ApiClient {
1339
const auth = useTriggerAuthContextOptional();
1440

packages/react-hooks/src/hooks/useRealtime.ts

+45-9
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ export type UseRealtimeRunInstance<TTask extends AnyTask = AnyTask> = {
2424
};
2525

2626
/**
27-
* hook to subscribe to realtime updates of a task run.
27+
* Hook to subscribe to realtime updates of a task run.
2828
*
29-
* @template TTask - The type of the task.
30-
* @param {string} runId - The unique identifier of the run to subscribe to.
31-
* @returns {{ run: RealtimeRun<TTask> | undefined, error: Error | null }} An object containing the current state of the run and any error encountered.
29+
* @template TTask - The type of the task
30+
* @param {string} [runId] - The unique identifier of the run to subscribe to
31+
* @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
32+
* @returns {UseRealtimeRunInstance<TTask>} An object containing the current state of the run, error handling, and control methods
3233
*
3334
* @example
3435
* ```ts
3536
* import type { myTask } from './path/to/task';
3637
* const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
3738
* ```
3839
*/
40+
3941
export function useRealtimeRun<TTask extends AnyTask>(
4042
runId?: string,
4143
options?: UseRealtimeRunOptions
@@ -133,6 +135,23 @@ export type UseRealtimeRunWithStreamsInstance<
133135
stop: () => void;
134136
};
135137

138+
/**
139+
* Hook to subscribe to realtime updates of a task run with associated data streams.
140+
*
141+
* @template TTask - The type of the task
142+
* @template TStreams - The type of the streams data
143+
* @param {string} [runId] - The unique identifier of the run to subscribe to
144+
* @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
145+
* @returns {UseRealtimeRunWithStreamsInstance<TTask, TStreams>} An object containing the current state of the run, streams data, and error handling
146+
*
147+
* @example
148+
* ```ts
149+
* import type { myTask } from './path/to/task';
150+
* const { run, streams, error } = useRealtimeRunWithStreams<typeof myTask, {
151+
* output: string;
152+
* }>('run-id-123');
153+
* ```
154+
*/
136155
export function useRealtimeRunWithStreams<
137156
TTask extends AnyTask = AnyTask,
138157
TStreams extends Record<string, any> = Record<string, any>,
@@ -249,6 +268,22 @@ export type UseRealtimeRunsInstance<TTask extends AnyTask = AnyTask> = {
249268
stop: () => void;
250269
};
251270

271+
/**
272+
* Hook to subscribe to realtime updates of task runs filtered by tag(s).
273+
*
274+
* @template TTask - The type of the task
275+
* @param {string | string[]} tag - The tag or array of tags to filter runs by
276+
* @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
277+
* @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs and any error encountered
278+
*
279+
* @example
280+
* ```ts
281+
* import type { myTask } from './path/to/task';
282+
* const { runs, error } = useRealtimeRunsWithTag<typeof myTask>('my-tag');
283+
* // Or with multiple tags
284+
* const { runs, error } = useRealtimeRunsWithTag<typeof myTask>(['tag1', 'tag2']);
285+
* ```
286+
*/
252287
export function useRealtimeRunsWithTag<TTask extends AnyTask>(
253288
tag: string | string[],
254289
options?: UseRealtimeRunOptions
@@ -321,19 +356,20 @@ export function useRealtimeRunsWithTag<TTask extends AnyTask>(
321356
}
322357

323358
/**
324-
* hook to subscribe to realtime updates of a batch of task runs.
359+
* Hook to subscribe to realtime updates of a batch of task runs.
325360
*
326-
* @template TTask - The type of the task.
327-
* @param {string} batchId - The unique identifier of the batch to subscribe to.
328-
* @returns {{ runs: RealtimeRun<TTask>[], error: Error | null }} An object containing the current state of the runs and any error encountered.
361+
* @template TTask - The type of the task
362+
* @param {string} batchId - The unique identifier of the batch to subscribe to
363+
* @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
364+
* @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs, error handling, and control methods
329365
*
330366
* @example
331-
*
332367
* ```ts
333368
* import type { myTask } from './path/to/task';
334369
* const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
335370
* ```
336371
*/
372+
337373
export function useRealtimeBatch<TTask extends AnyTask>(
338374
batchId: string,
339375
options?: UseRealtimeRunOptions

packages/react-hooks/src/hooks/useTaskTrigger.ts

+75
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,41 @@ import {
1919
UseRealtimeRunWithStreamsInstance,
2020
} from "./useRealtime.js";
2121

22+
/**
23+
* Base interface for task trigger instances.
24+
*
25+
* @template TTask - The type of the task
26+
*/
2227
export interface TriggerInstance<TTask extends AnyTask> {
28+
/** Function to submit the task with a payload */
2329
submit: (payload: TaskPayload<TTask>) => void;
30+
/** Whether the task is currently being submitted */
2431
isLoading: boolean;
32+
/** The handle returned after successful task submission */
2533
handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
34+
/** Any error that occurred during submission */
2635
error?: Error;
2736
}
2837

2938
export type UseTaskTriggerOptions = UseApiClientOptions;
3039

40+
/**
41+
* Hook to trigger a task and manage its initial execution state.
42+
*
43+
* @template TTask - The type of the task
44+
* @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
45+
* @param {UseTaskTriggerOptions} [options] - Configuration options for the task trigger
46+
* @returns {TriggerInstance<TTask>} An object containing the submit function, loading state, handle, and any errors
47+
*
48+
* @example
49+
* ```ts
50+
* import type { myTask } from './path/to/task';
51+
* const { submit, isLoading, handle, error } = useTaskTrigger<typeof myTask>('my-task-id');
52+
*
53+
* // Submit the task with payload
54+
* submit({ foo: 'bar' });
55+
* ```
56+
*/
3157
export function useTaskTrigger<TTask extends AnyTask>(
3258
id: TaskIdentifier<TTask>,
3359
options?: UseTaskTriggerOptions
@@ -74,8 +100,13 @@ export function useTaskTrigger<TTask extends AnyTask>(
74100
};
75101
}
76102

103+
/**
104+
* Configuration options for task triggers with realtime updates.
105+
*/
77106
export type UseRealtimeTaskTriggerOptions = UseTaskTriggerOptions & {
107+
/** Whether the realtime subscription is enabled */
78108
enabled?: boolean;
109+
/** Optional throttle time in milliseconds for stream updates */
79110
experimental_throttleInMs?: number;
80111
};
81112

@@ -88,6 +119,28 @@ export type RealtimeTriggerInstanceWithStreams<
88119
handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
89120
};
90121

122+
/**
123+
* Hook to trigger a task and subscribe to its realtime updates including stream data.
124+
*
125+
* @template TTask - The type of the task
126+
* @template TStreams - The type of the streams data
127+
* @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
128+
* @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
129+
* @returns {RealtimeTriggerInstanceWithStreams<TTask, TStreams>} An object containing the submit function, loading state,
130+
* handle, run state, streams data, and error handling
131+
*
132+
* @example
133+
* ```ts
134+
* import type { myTask } from './path/to/task';
135+
* const { submit, run, streams, error } = useRealtimeTaskTriggerWithStreams<
136+
* typeof myTask,
137+
* { output: string }
138+
* >('my-task-id');
139+
*
140+
* // Submit and monitor the task with streams
141+
* submit({ foo: 'bar' });
142+
* ```
143+
*/
91144
export function useRealtimeTaskTriggerWithStreams<
92145
TTask extends AnyTask,
93146
TStreams extends Record<string, any> = Record<string, any>,
@@ -114,6 +167,28 @@ export type RealtimeTriggerInstance<TTask extends AnyTask> = UseRealtimeRunInsta
114167
handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
115168
};
116169

170+
/**
171+
* Hook to trigger a task and subscribe to its realtime updates.
172+
*
173+
* @template TTask - The type of the task
174+
* @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
175+
* @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
176+
* @returns {RealtimeTriggerInstance<TTask>} An object containing the submit function, loading state,
177+
* handle, run state, and error handling
178+
*
179+
* @example
180+
* ```ts
181+
* import type { myTask } from './path/to/task';
182+
* const { submit, run, error, stop } = useRealtimeTaskTrigger<typeof myTask>('my-task-id');
183+
*
184+
* // Submit and monitor the task
185+
* submit({ foo: 'bar' });
186+
*
187+
* // Stop monitoring when needed
188+
* stop();
189+
* ```
190+
*/
191+
117192
export function useRealtimeTaskTrigger<TTask extends AnyTask>(
118193
id: TaskIdentifier<TTask>,
119194
options?: UseRealtimeTaskTriggerOptions

0 commit comments

Comments
 (0)