@trigger.dev/react-hooks 3.1.2 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/commonjs/contexts.d.ts +2 -2
  2. package/dist/commonjs/contexts.js +3 -2
  3. package/dist/commonjs/contexts.js.map +1 -1
  4. package/dist/commonjs/hooks/useApiClient.d.ts +33 -2
  5. package/dist/commonjs/hooks/useApiClient.js +31 -6
  6. package/dist/commonjs/hooks/useApiClient.js.map +1 -1
  7. package/dist/commonjs/hooks/useRealtime.d.ts +100 -0
  8. package/dist/commonjs/hooks/useRealtime.js +387 -0
  9. package/dist/commonjs/hooks/useRealtime.js.map +1 -0
  10. package/dist/commonjs/hooks/useRun.js +1 -1
  11. package/dist/commonjs/hooks/useRun.js.map +1 -1
  12. package/dist/commonjs/hooks/useTaskTrigger.d.ts +101 -0
  13. package/dist/commonjs/hooks/useTaskTrigger.js +134 -0
  14. package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -0
  15. package/dist/commonjs/index.d.ts +2 -3
  16. package/dist/commonjs/index.js +2 -3
  17. package/dist/commonjs/index.js.map +1 -1
  18. package/dist/commonjs/utils/throttle.d.ts +6 -0
  19. package/dist/commonjs/utils/throttle.js +50 -0
  20. package/dist/commonjs/utils/throttle.js.map +1 -0
  21. package/dist/commonjs/utils/trigger-swr.d.ts +12 -0
  22. package/dist/commonjs/utils/trigger-swr.js.map +1 -1
  23. package/dist/esm/contexts.d.ts +2 -2
  24. package/dist/esm/contexts.js +2 -2
  25. package/dist/esm/contexts.js.map +1 -1
  26. package/dist/esm/hooks/useApiClient.d.ts +33 -2
  27. package/dist/esm/hooks/useApiClient.js +32 -7
  28. package/dist/esm/hooks/useApiClient.js.map +1 -1
  29. package/dist/esm/hooks/useRealtime.d.ts +100 -0
  30. package/dist/esm/hooks/useRealtime.js +381 -0
  31. package/dist/esm/hooks/useRealtime.js.map +1 -0
  32. package/dist/esm/hooks/useRun.js +1 -1
  33. package/dist/esm/hooks/useRun.js.map +1 -1
  34. package/dist/esm/hooks/useTaskTrigger.d.ts +101 -0
  35. package/dist/esm/hooks/useTaskTrigger.js +126 -0
  36. package/dist/esm/hooks/useTaskTrigger.js.map +1 -0
  37. package/dist/esm/index.d.ts +2 -3
  38. package/dist/esm/index.js +2 -3
  39. package/dist/esm/index.js.map +1 -1
  40. package/dist/esm/utils/throttle.d.ts +6 -0
  41. package/dist/esm/utils/throttle.js +47 -0
  42. package/dist/esm/utils/throttle.js.map +1 -0
  43. package/dist/esm/utils/trigger-swr.d.ts +12 -0
  44. package/dist/esm/utils/trigger-swr.js.map +1 -1
  45. package/package.json +2 -2
  46. package/dist/commonjs/hooks/useRealtimeBatch.d.ts +0 -19
  47. package/dist/commonjs/hooks/useRealtimeBatch.js +0 -56
  48. package/dist/commonjs/hooks/useRealtimeBatch.js.map +0 -1
  49. package/dist/commonjs/hooks/useRealtimeRun.d.ts +0 -18
  50. package/dist/commonjs/hooks/useRealtimeRun.js +0 -40
  51. package/dist/commonjs/hooks/useRealtimeRun.js.map +0 -1
  52. package/dist/commonjs/hooks/useRealtimeRunsWithTag.d.ts +0 -5
  53. package/dist/commonjs/hooks/useRealtimeRunsWithTag.js +0 -45
  54. package/dist/commonjs/hooks/useRealtimeRunsWithTag.js.map +0 -1
  55. package/dist/esm/hooks/useRealtimeBatch.d.ts +0 -19
  56. package/dist/esm/hooks/useRealtimeBatch.js +0 -53
  57. package/dist/esm/hooks/useRealtimeBatch.js.map +0 -1
  58. package/dist/esm/hooks/useRealtimeRun.d.ts +0 -18
  59. package/dist/esm/hooks/useRealtimeRun.js +0 -37
  60. package/dist/esm/hooks/useRealtimeRun.js.map +0 -1
  61. package/dist/esm/hooks/useRealtimeRunsWithTag.d.ts +0 -5
  62. package/dist/esm/hooks/useRealtimeRunsWithTag.js +0 -42
  63. package/dist/esm/hooks/useRealtimeRunsWithTag.js.map +0 -1
@@ -0,0 +1,101 @@
1
+ import { type AnyTask, type TaskIdentifier, type TaskPayload, InferRunTypes, RunHandleFromTypes } from "@trigger.dev/core/v3";
2
+ import { UseApiClientOptions } from "./useApiClient.js";
3
+ import { UseRealtimeRunInstance, UseRealtimeRunWithStreamsInstance } from "./useRealtime.js";
4
+ /**
5
+ * Base interface for task trigger instances.
6
+ *
7
+ * @template TTask - The type of the task
8
+ */
9
+ export interface TriggerInstance<TTask extends AnyTask> {
10
+ /** Function to submit the task with a payload */
11
+ submit: (payload: TaskPayload<TTask>) => void;
12
+ /** Whether the task is currently being submitted */
13
+ isLoading: boolean;
14
+ /** The handle returned after successful task submission */
15
+ handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
16
+ /** Any error that occurred during submission */
17
+ error?: Error;
18
+ }
19
+ export type UseTaskTriggerOptions = UseApiClientOptions;
20
+ /**
21
+ * Hook to trigger a task and manage its initial execution state.
22
+ *
23
+ * @template TTask - The type of the task
24
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
25
+ * @param {UseTaskTriggerOptions} [options] - Configuration options for the task trigger
26
+ * @returns {TriggerInstance<TTask>} An object containing the submit function, loading state, handle, and any errors
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import type { myTask } from './path/to/task';
31
+ * const { submit, isLoading, handle, error } = useTaskTrigger<typeof myTask>('my-task-id');
32
+ *
33
+ * // Submit the task with payload
34
+ * submit({ foo: 'bar' });
35
+ * ```
36
+ */
37
+ export declare function useTaskTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, options?: UseTaskTriggerOptions): TriggerInstance<TTask>;
38
+ /**
39
+ * Configuration options for task triggers with realtime updates.
40
+ */
41
+ export type UseRealtimeTaskTriggerOptions = UseTaskTriggerOptions & {
42
+ /** Whether the realtime subscription is enabled */
43
+ enabled?: boolean;
44
+ /** Optional throttle time in milliseconds for stream updates */
45
+ experimental_throttleInMs?: number;
46
+ };
47
+ export type RealtimeTriggerInstanceWithStreams<TTask extends AnyTask, TStreams extends Record<string, any> = Record<string, any>> = UseRealtimeRunWithStreamsInstance<TTask, TStreams> & {
48
+ submit: (payload: TaskPayload<TTask>) => void;
49
+ isLoading: boolean;
50
+ handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
51
+ };
52
+ /**
53
+ * Hook to trigger a task and subscribe to its realtime updates including stream data.
54
+ *
55
+ * @template TTask - The type of the task
56
+ * @template TStreams - The type of the streams data
57
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
58
+ * @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
59
+ * @returns {RealtimeTriggerInstanceWithStreams<TTask, TStreams>} An object containing the submit function, loading state,
60
+ * handle, run state, streams data, and error handling
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import type { myTask } from './path/to/task';
65
+ * const { submit, run, streams, error } = useRealtimeTaskTriggerWithStreams<
66
+ * typeof myTask,
67
+ * { output: string }
68
+ * >('my-task-id');
69
+ *
70
+ * // Submit and monitor the task with streams
71
+ * submit({ foo: 'bar' });
72
+ * ```
73
+ */
74
+ export declare function useRealtimeTaskTriggerWithStreams<TTask extends AnyTask, TStreams extends Record<string, any> = Record<string, any>>(id: TaskIdentifier<TTask>, options?: UseRealtimeTaskTriggerOptions): RealtimeTriggerInstanceWithStreams<TTask, TStreams>;
75
+ export type RealtimeTriggerInstance<TTask extends AnyTask> = UseRealtimeRunInstance<TTask> & {
76
+ submit: (payload: TaskPayload<TTask>) => void;
77
+ isLoading: boolean;
78
+ handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
79
+ };
80
+ /**
81
+ * Hook to trigger a task and subscribe to its realtime updates.
82
+ *
83
+ * @template TTask - The type of the task
84
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
85
+ * @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
86
+ * @returns {RealtimeTriggerInstance<TTask>} An object containing the submit function, loading state,
87
+ * handle, run state, and error handling
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * import type { myTask } from './path/to/task';
92
+ * const { submit, run, error, stop } = useRealtimeTaskTrigger<typeof myTask>('my-task-id');
93
+ *
94
+ * // Submit and monitor the task
95
+ * submit({ foo: 'bar' });
96
+ *
97
+ * // Stop monitoring when needed
98
+ * stop();
99
+ * ```
100
+ */
101
+ export declare function useRealtimeTaskTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, options?: UseRealtimeTaskTriggerOptions): RealtimeTriggerInstance<TTask>;
@@ -0,0 +1,134 @@
1
+ "use client";
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.useTaskTrigger = useTaskTrigger;
8
+ exports.useRealtimeTaskTriggerWithStreams = useRealtimeTaskTriggerWithStreams;
9
+ exports.useRealtimeTaskTrigger = useRealtimeTaskTrigger;
10
+ const v3_1 = require("@trigger.dev/core/v3");
11
+ const mutation_1 = __importDefault(require("swr/mutation"));
12
+ const useApiClient_js_1 = require("./useApiClient.js");
13
+ const useRealtime_js_1 = require("./useRealtime.js");
14
+ /**
15
+ * Hook to trigger a task and manage its initial execution state.
16
+ *
17
+ * @template TTask - The type of the task
18
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
19
+ * @param {UseTaskTriggerOptions} [options] - Configuration options for the task trigger
20
+ * @returns {TriggerInstance<TTask>} An object containing the submit function, loading state, handle, and any errors
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import type { myTask } from './path/to/task';
25
+ * const { submit, isLoading, handle, error } = useTaskTrigger<typeof myTask>('my-task-id');
26
+ *
27
+ * // Submit the task with payload
28
+ * submit({ foo: 'bar' });
29
+ * ```
30
+ */
31
+ function useTaskTrigger(id, options) {
32
+ const apiClient = (0, useApiClient_js_1.useApiClient)(options);
33
+ async function triggerTask(id, { arg: { payload, options }, }) {
34
+ const payloadPacket = await (0, v3_1.stringifyIO)(payload);
35
+ const handle = await apiClient.triggerTask(id, {
36
+ payload: payloadPacket.data,
37
+ options: {
38
+ queue: options?.queue,
39
+ concurrencyKey: options?.concurrencyKey,
40
+ payloadType: payloadPacket.dataType,
41
+ idempotencyKey: await (0, v3_1.makeIdempotencyKey)(options?.idempotencyKey),
42
+ delay: options?.delay,
43
+ ttl: options?.ttl,
44
+ tags: options?.tags,
45
+ maxAttempts: options?.maxAttempts,
46
+ metadata: options?.metadata,
47
+ maxDuration: options?.maxDuration,
48
+ },
49
+ });
50
+ return { ...handle, taskIdentifier: id };
51
+ }
52
+ const mutation = (0, mutation_1.default)(id, triggerTask);
53
+ return {
54
+ submit: (payload) => {
55
+ // trigger the task with the given payload
56
+ mutation.trigger({ payload });
57
+ },
58
+ isLoading: mutation.isMutating,
59
+ handle: mutation.data,
60
+ error: mutation.error,
61
+ };
62
+ }
63
+ /**
64
+ * Hook to trigger a task and subscribe to its realtime updates including stream data.
65
+ *
66
+ * @template TTask - The type of the task
67
+ * @template TStreams - The type of the streams data
68
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
69
+ * @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
70
+ * @returns {RealtimeTriggerInstanceWithStreams<TTask, TStreams>} An object containing the submit function, loading state,
71
+ * handle, run state, streams data, and error handling
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * import type { myTask } from './path/to/task';
76
+ * const { submit, run, streams, error } = useRealtimeTaskTriggerWithStreams<
77
+ * typeof myTask,
78
+ * { output: string }
79
+ * >('my-task-id');
80
+ *
81
+ * // Submit and monitor the task with streams
82
+ * submit({ foo: 'bar' });
83
+ * ```
84
+ */
85
+ function useRealtimeTaskTriggerWithStreams(id, options) {
86
+ const triggerInstance = useTaskTrigger(id, options);
87
+ const realtimeInstance = (0, useRealtime_js_1.useRealtimeRunWithStreams)(triggerInstance.handle?.id, {
88
+ ...options,
89
+ id: triggerInstance.handle?.id,
90
+ accessToken: triggerInstance.handle?.publicAccessToken ?? options?.accessToken,
91
+ });
92
+ return {
93
+ ...realtimeInstance,
94
+ ...triggerInstance,
95
+ };
96
+ }
97
+ /**
98
+ * Hook to trigger a task and subscribe to its realtime updates.
99
+ *
100
+ * @template TTask - The type of the task
101
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
102
+ * @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
103
+ * @returns {RealtimeTriggerInstance<TTask>} An object containing the submit function, loading state,
104
+ * handle, run state, and error handling
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * import type { myTask } from './path/to/task';
109
+ * const { submit, run, error, stop } = useRealtimeTaskTrigger<typeof myTask>('my-task-id');
110
+ *
111
+ * // Submit and monitor the task
112
+ * submit({ foo: 'bar' });
113
+ *
114
+ * // Stop monitoring when needed
115
+ * stop();
116
+ * ```
117
+ */
118
+ function useRealtimeTaskTrigger(id, options) {
119
+ const triggerInstance = useTaskTrigger(id, options);
120
+ const realtimeInstance = (0, useRealtime_js_1.useRealtimeRun)(triggerInstance.handle?.id, {
121
+ ...options,
122
+ id: triggerInstance.handle?.id,
123
+ accessToken: triggerInstance.handle?.publicAccessToken ?? options?.accessToken,
124
+ });
125
+ return {
126
+ submit: triggerInstance.submit,
127
+ isLoading: triggerInstance.isLoading,
128
+ handle: triggerInstance.handle,
129
+ run: realtimeInstance.run,
130
+ error: realtimeInstance.error ?? triggerInstance.error,
131
+ stop: realtimeInstance.stop,
132
+ };
133
+ }
134
+ //# sourceMappingURL=useTaskTrigger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTaskTrigger.js","sourceRoot":"","sources":["../../../src/hooks/useTaskTrigger.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;AAwDb,wCA4CC;AA2CD,8EAkBC;AA8BD,wDAmBC;AAhND,6CAS8B;AAC9B,4DAA0C;AAC1C,uDAAsE;AACtE,qDAK0B;AAoB1B;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,cAAc,CAC5B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3
3
  export * from "./hooks/useRun.js";
4
- export * from "./hooks/useRealtimeRun.js";
5
- export * from "./hooks/useRealtimeRunsWithTag.js";
6
- export * from "./hooks/useRealtimeBatch.js";
4
+ export * from "./hooks/useRealtime.js";
5
+ export * from "./hooks/useTaskTrigger.js";
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./contexts.js"), exports);
18
18
  __exportStar(require("./hooks/useApiClient.js"), exports);
19
19
  __exportStar(require("./hooks/useRun.js"), exports);
20
- __exportStar(require("./hooks/useRealtimeRun.js"), exports);
21
- __exportStar(require("./hooks/useRealtimeRunsWithTag.js"), exports);
22
- __exportStar(require("./hooks/useRealtimeBatch.js"), exports);
20
+ __exportStar(require("./hooks/useRealtime.js"), exports);
21
+ __exportStar(require("./hooks/useTaskTrigger.js"), exports);
23
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,0DAAwC;AACxC,oDAAkC;AAClC,4DAA0C;AAC1C,oEAAkD;AAClD,8DAA4C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,0DAAwC;AACxC,oDAAkC;AAClC,yDAAuC;AACvC,4DAA0C"}
@@ -0,0 +1,6 @@
1
+ export type ThrottledQueue<T> = {
2
+ add: (item: T) => void;
3
+ flush: () => Promise<void>;
4
+ isEmpty: () => boolean;
5
+ };
6
+ export declare function createThrottledQueue<T>(onFlush: (items: T[]) => Promise<void>, throttleInMs?: number): ThrottledQueue<T>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createThrottledQueue = createThrottledQueue;
4
+ function createThrottledQueue(onFlush, throttleInMs) {
5
+ let queue = [];
6
+ let lastFlushTime = 0;
7
+ let flushPromise = null;
8
+ const scheduleFlush = async () => {
9
+ // If no throttle specified or there's already a flush in progress, return
10
+ if (!throttleInMs) {
11
+ // Immediately flush when no throttling is specified
12
+ const itemsToFlush = [...queue];
13
+ queue = [];
14
+ await onFlush(itemsToFlush);
15
+ return;
16
+ }
17
+ if (queue.length === 0 || flushPromise)
18
+ return;
19
+ const now = Date.now();
20
+ const timeUntilNextFlush = Math.max(0, lastFlushTime + throttleInMs - now);
21
+ if (timeUntilNextFlush === 0) {
22
+ const itemsToFlush = [...queue];
23
+ queue = [];
24
+ lastFlushTime = now;
25
+ flushPromise = onFlush(itemsToFlush).finally(() => {
26
+ flushPromise = null;
27
+ // Check if more items accumulated during flush
28
+ scheduleFlush();
29
+ });
30
+ }
31
+ else {
32
+ setTimeout(scheduleFlush, timeUntilNextFlush);
33
+ }
34
+ };
35
+ return {
36
+ add: (item) => {
37
+ queue.push(item);
38
+ scheduleFlush();
39
+ },
40
+ flush: async () => {
41
+ if (queue.length === 0)
42
+ return;
43
+ const itemsToFlush = [...queue];
44
+ queue = [];
45
+ await onFlush(itemsToFlush);
46
+ },
47
+ isEmpty: () => queue.length === 0,
48
+ };
49
+ }
50
+ //# sourceMappingURL=throttle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../../src/utils/throttle.ts"],"names":[],"mappings":";;AAOA,oDAkDC;AAlDD,SAAgB,oBAAoB,CAClC,OAAsC,EACtC,YAAqB;IAErB,IAAI,KAAK,GAAQ,EAAE,CAAC;IACpB,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,IAAI,YAAY,GAAyB,IAAI,CAAC;IAE9C,MAAM,aAAa,GAAG,KAAK,IAAI,EAAE;QAC/B,0EAA0E;QAC1E,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,oDAAoD;YACpD,MAAM,YAAY,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;YAChC,KAAK,GAAG,EAAE,CAAC;YAC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
1
2
  export * from "swr";
2
3
  export { default as useSWR, SWRConfig } from "swr";
3
4
  export type CommonTriggerHookOptions = {
5
+ /**
6
+ * Poll for updates at the specified interval (in milliseconds). Polling is not recommended for most use-cases. Use the Realtime hooks instead.
7
+ */
4
8
  refreshInterval?: number;
9
+ /** Revalidate the data when the browser regains a network connection. */
5
10
  revalidateOnReconnect?: boolean;
11
+ /** Revalidate the data when the window regains focus. */
6
12
  revalidateOnFocus?: boolean;
13
+ /** Optional access token for authentication */
14
+ accessToken?: string;
15
+ /** Optional base URL for the API endpoints */
16
+ baseURL?: string;
17
+ /** Optional additional request configuration */
18
+ requestOptions?: ApiRequestOptions;
7
19
  };
@@ -1 +1 @@
1
- {"version":3,"file":"trigger-swr.js","sourceRoot":"","sources":["../../../src/utils/trigger-swr.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;AACb,yCAAyC;AACzC,sCAAoB;AACpB,yCAAyC;AACzC,2BAAmD;AAA1C,8GAAA,OAAO,OAAU;AAAE,gGAAA,SAAS,OAAA"}
1
+ {"version":3,"file":"trigger-swr.js","sourceRoot":"","sources":["../../../src/utils/trigger-swr.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;AAIb,yCAAyC;AACzC,sCAAoB;AACpB,yCAAyC;AACzC,2BAAmD;AAA1C,8GAAA,OAAO,OAAU;AAAE,gGAAA,SAAS,OAAA"}
@@ -1,4 +1,4 @@
1
1
  import React from "react";
2
2
  import type { ApiClientConfiguration } from "@trigger.dev/core/v3";
3
- declare const TriggerAuthContext: React.Context<ApiClientConfiguration | undefined>, useTriggerAuthContext: () => ApiClientConfiguration;
4
- export { TriggerAuthContext, useTriggerAuthContext };
3
+ declare const TriggerAuthContext: React.Context<ApiClientConfiguration | undefined>, useTriggerAuthContext: () => ApiClientConfiguration, useTriggerAuthContextOptional: () => ApiClientConfiguration | Partial<ApiClientConfiguration>;
4
+ export { TriggerAuthContext, useTriggerAuthContext, useTriggerAuthContextOptional };
@@ -1,5 +1,5 @@
1
1
  "use client";
2
2
  import { createContextAndHook } from "./utils/createContextAndHook.js";
3
- const [TriggerAuthContext, useTriggerAuthContext] = createContextAndHook("TriggerAuthContext");
4
- export { TriggerAuthContext, useTriggerAuthContext };
3
+ const [TriggerAuthContext, useTriggerAuthContext, useTriggerAuthContextOptional] = createContextAndHook("TriggerAuthContext");
4
+ export { TriggerAuthContext, useTriggerAuthContext, useTriggerAuthContextOptional };
5
5
  //# sourceMappingURL=contexts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"contexts.js","sourceRoot":"","sources":["../../src/contexts.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvE,MAAM,CAAC,kBAAkB,EAAE,qBAAqB,CAAC,GAC/C,oBAAoB,CAAyB,oBAAoB,CAAC,CAAC;AAErE,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"contexts.js","sourceRoot":"","sources":["../../src/contexts.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvE,MAAM,CAAC,kBAAkB,EAAE,qBAAqB,EAAE,6BAA6B,CAAC,GAC9E,oBAAoB,CAAyB,oBAAoB,CAAC,CAAC;AAErE,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,CAAC"}
@@ -1,2 +1,33 @@
1
- import { ApiClient } from "@trigger.dev/core/v3";
2
- export declare function useApiClient(): ApiClient;
1
+ import { ApiClient, ApiRequestOptions } from "@trigger.dev/core/v3";
2
+ /**
3
+ * Configuration options for creating an API client instance.
4
+ */
5
+ export type UseApiClientOptions = {
6
+ /** Optional access token for authentication */
7
+ accessToken?: string;
8
+ /** Optional base URL for the API endpoints */
9
+ baseURL?: string;
10
+ /** Optional additional request configuration */
11
+ requestOptions?: ApiRequestOptions;
12
+ };
13
+ /**
14
+ * Hook to create an API client instance using authentication context or provided options.
15
+ *
16
+ * @param {UseApiClientOptions} [options] - Configuration options for the API client
17
+ * @returns {ApiClient} An initialized API client instance
18
+ * @throws {Error} When no access token is available in either context or options
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * // Using context authentication
23
+ * const apiClient = useApiClient();
24
+ *
25
+ * // Using custom options
26
+ * const apiClient = useApiClient({
27
+ * accessToken: "your-access-token",
28
+ * baseURL: "https://api.my-trigger.com",
29
+ * requestOptions: { retry: { maxAttempts: 10 } }
30
+ * });
31
+ * ```
32
+ */
33
+ export declare function useApiClient(options?: UseApiClientOptions): ApiClient;
@@ -1,12 +1,37 @@
1
1
  "use client";
2
2
  import { ApiClient } from "@trigger.dev/core/v3";
3
- import { useTriggerAuthContext } from "../contexts.js";
4
- export function useApiClient() {
5
- const auth = useTriggerAuthContext();
6
- const baseUrl = auth.baseURL ?? "https://api.trigger.dev";
7
- if (!auth.accessToken) {
8
- throw new Error("Missing accessToken in TriggerAuthContext");
3
+ import { useTriggerAuthContextOptional } from "../contexts.js";
4
+ /**
5
+ * Hook to create an API client instance using authentication context or provided options.
6
+ *
7
+ * @param {UseApiClientOptions} [options] - Configuration options for the API client
8
+ * @returns {ApiClient} An initialized API client instance
9
+ * @throws {Error} When no access token is available in either context or options
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * // Using context authentication
14
+ * const apiClient = useApiClient();
15
+ *
16
+ * // Using custom options
17
+ * const apiClient = useApiClient({
18
+ * accessToken: "your-access-token",
19
+ * baseURL: "https://api.my-trigger.com",
20
+ * requestOptions: { retry: { maxAttempts: 10 } }
21
+ * });
22
+ * ```
23
+ */
24
+ export function useApiClient(options) {
25
+ const auth = useTriggerAuthContextOptional();
26
+ const baseUrl = options?.baseURL ?? auth?.baseURL ?? "https://api.trigger.dev";
27
+ const accessToken = options?.accessToken ?? auth?.accessToken;
28
+ if (!accessToken) {
29
+ throw new Error("Missing accessToken in TriggerAuthContext or useApiClient options");
9
30
  }
10
- return new ApiClient(baseUrl, auth.accessToken, auth.requestOptions);
31
+ const requestOptions = {
32
+ ...auth?.requestOptions,
33
+ ...options?.requestOptions,
34
+ };
35
+ return new ApiClient(baseUrl, accessToken, requestOptions);
11
36
  }
12
37
  //# sourceMappingURL=useApiClient.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useApiClient.js","sourceRoot":"","sources":["../../../src/hooks/useApiClient.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,MAAM,UAAU,YAAY;IAC1B,MAAM,IAAI,GAAG,qBAAqB,EAAE,CAAC;IAErC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,yBAAyB,CAAC;IAE1D,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAC/D,CAAC;IAED,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;AACvE,CAAC"}
1
+ {"version":3,"file":"useApiClient.js","sourceRoot":"","sources":["../../../src/hooks/useApiClient.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAqB,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAc/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,YAAY,CAAC,OAA6B;IACxD,MAAM,IAAI,GAAG,6BAA6B,EAAE,CAAC;IAE7C,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,OAAO,IAAI,yBAAyB,CAAC;IAC/E,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,IAAI,EAAE,WAAW,CAAC;IAE9D,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;IACvF,CAAC;IAED,MAAM,cAAc,GAAsB;QACxC,GAAG,IAAI,EAAE,cAAc;QACvB,GAAG,OAAO,EAAE,cAAc;KAC3B,CAAC;IAEF,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,100 @@
1
+ import { AnyTask, RealtimeRun } from "@trigger.dev/core/v3";
2
+ import { UseApiClientOptions } from "./useApiClient.js";
3
+ export type UseRealtimeRunOptions = UseApiClientOptions & {
4
+ id?: string;
5
+ enabled?: boolean;
6
+ experimental_throttleInMs?: number;
7
+ };
8
+ export type UseRealtimeRunInstance<TTask extends AnyTask = AnyTask> = {
9
+ run: RealtimeRun<TTask> | undefined;
10
+ error: Error | undefined;
11
+ /**
12
+ * Abort the current request immediately.
13
+ */
14
+ stop: () => void;
15
+ };
16
+ /**
17
+ * Hook to subscribe to realtime updates of a task run.
18
+ *
19
+ * @template TTask - The type of the task
20
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
21
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
22
+ * @returns {UseRealtimeRunInstance<TTask>} An object containing the current state of the run, error handling, and control methods
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import type { myTask } from './path/to/task';
27
+ * const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
28
+ * ```
29
+ */
30
+ export declare function useRealtimeRun<TTask extends AnyTask>(runId?: string, options?: UseRealtimeRunOptions): UseRealtimeRunInstance<TTask>;
31
+ export type StreamResults<TStreams extends Record<string, any>> = {
32
+ [K in keyof TStreams]: Array<TStreams[K]>;
33
+ };
34
+ export type UseRealtimeRunWithStreamsInstance<TTask extends AnyTask = AnyTask, TStreams extends Record<string, any> = Record<string, any>> = {
35
+ run: RealtimeRun<TTask> | undefined;
36
+ streams: StreamResults<TStreams>;
37
+ error: Error | undefined;
38
+ /**
39
+ * Abort the current request immediately, keep the generated tokens if any.
40
+ */
41
+ stop: () => void;
42
+ };
43
+ /**
44
+ * Hook to subscribe to realtime updates of a task run with associated data streams.
45
+ *
46
+ * @template TTask - The type of the task
47
+ * @template TStreams - The type of the streams data
48
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
49
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
50
+ * @returns {UseRealtimeRunWithStreamsInstance<TTask, TStreams>} An object containing the current state of the run, streams data, and error handling
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import type { myTask } from './path/to/task';
55
+ * const { run, streams, error } = useRealtimeRunWithStreams<typeof myTask, {
56
+ * output: string;
57
+ * }>('run-id-123');
58
+ * ```
59
+ */
60
+ export declare function useRealtimeRunWithStreams<TTask extends AnyTask = AnyTask, TStreams extends Record<string, any> = Record<string, any>>(runId?: string, options?: UseRealtimeRunOptions): UseRealtimeRunWithStreamsInstance<TTask, TStreams>;
61
+ export type UseRealtimeRunsInstance<TTask extends AnyTask = AnyTask> = {
62
+ runs: RealtimeRun<TTask>[];
63
+ error: Error | undefined;
64
+ /**
65
+ * Abort the current request immediately.
66
+ */
67
+ stop: () => void;
68
+ };
69
+ /**
70
+ * Hook to subscribe to realtime updates of task runs filtered by tag(s).
71
+ *
72
+ * @template TTask - The type of the task
73
+ * @param {string | string[]} tag - The tag or array of tags to filter runs by
74
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
75
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs and any error encountered
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import type { myTask } from './path/to/task';
80
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>('my-tag');
81
+ * // Or with multiple tags
82
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>(['tag1', 'tag2']);
83
+ * ```
84
+ */
85
+ export declare function useRealtimeRunsWithTag<TTask extends AnyTask>(tag: string | string[], options?: UseRealtimeRunOptions): UseRealtimeRunsInstance<TTask>;
86
+ /**
87
+ * Hook to subscribe to realtime updates of a batch of task runs.
88
+ *
89
+ * @template TTask - The type of the task
90
+ * @param {string} batchId - The unique identifier of the batch to subscribe to
91
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
92
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs, error handling, and control methods
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import type { myTask } from './path/to/task';
97
+ * const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
98
+ * ```
99
+ */
100
+ export declare function useRealtimeBatch<TTask extends AnyTask>(batchId: string, options?: UseRealtimeRunOptions): UseRealtimeRunsInstance<TTask>;