@trigger.dev/sdk 3.0.0-beta.40 → 3.0.0-beta.41

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.
@@ -1,4 +1,4 @@
1
- import { RetryOptions, FetchRetryOptions, TaskRunContext, QueueOptions, InitOutput, MachineCpu, MachineMemory, RunFnParams, InitFnParams, HandleErrorFnParams, HandleErrorResult, MiddlewareFnParams, StartFnParams, SuccessFnParams, FailureFnParams, ListProjectRunsQueryParams, CursorPagePromise, ListRunResponseItem, ListRunsQueryParams, ApiPromise, ReplayRunResponse, CanceledRunResponse, RetrieveRunResponse, ScheduledTaskPayload, CreateScheduleOptions, ScheduleObject, UpdateScheduleOptions, DeletedScheduleObject, ListScheduleOptions, OffsetLimitPagePromise, ImportEnvironmentVariablesParams, EnvironmentVariableResponseBody, EnvironmentVariables, CreateEnvironmentVariableParams, EnvironmentVariableValue, UpdateEnvironmentVariableParams, ApiClientConfiguration } from '@trigger.dev/core/v3';
1
+ import { RetryOptions, FetchRetryOptions, ListProjectRunsQueryParams, CursorPagePromise, ListRunResponseItem, ListRunsQueryParams, ApiPromise, ReplayRunResponse, CanceledRunResponse, RetrieveRunResponse, TaskRunContext, QueueOptions, InitOutput, MachineCpu, MachineMemory, RunFnParams, InitFnParams, HandleErrorFnParams, HandleErrorResult, MiddlewareFnParams, StartFnParams, SuccessFnParams, FailureFnParams, ScheduledTaskPayload, CreateScheduleOptions, ScheduleObject, UpdateScheduleOptions, DeletedScheduleObject, ListScheduleOptions, OffsetLimitPagePromise, ImportEnvironmentVariablesParams, EnvironmentVariableResponseBody, EnvironmentVariables, CreateEnvironmentVariableParams, EnvironmentVariableValue, UpdateEnvironmentVariableParams, ApiClientConfiguration } from '@trigger.dev/core/v3';
2
2
  export { ApiClientConfiguration, ApiError, AuthenticationError, BadRequestError, ConflictError, HandleErrorArgs, HandleErrorFunction, ImportEnvironmentVariablesParams, InternalServerError, LogLevel, NotFoundError, PermissionDeniedError, RateLimitError, ResolveEnvironmentVariablesFunction, ResolveEnvironmentVariablesParams, ResolveEnvironmentVariablesResult, RetryOptions, ProjectConfig as TriggerConfig, UnprocessableEntityError, logger } from '@trigger.dev/core/v3';
3
3
  import { HttpHandler } from 'msw';
4
4
 
@@ -48,6 +48,206 @@ declare const retry: {
48
48
  };
49
49
  };
50
50
 
51
+ type RetrieveRunResult<TOutput> = Prettify<TOutput extends RunHandle<infer THandleOutput> ? Omit<RetrieveRunResponse, "output"> & {
52
+ output?: THandleOutput;
53
+ } : Omit<RetrieveRunResponse, "output"> & {
54
+ output?: TOutput;
55
+ }>;
56
+ declare const runs: {
57
+ replay: typeof replayRun;
58
+ cancel: typeof cancelRun;
59
+ retrieve: typeof retrieveRun;
60
+ list: typeof listRuns;
61
+ poll: typeof poll;
62
+ };
63
+ declare function listRuns(projectRef: string, params?: ListProjectRunsQueryParams): CursorPagePromise<typeof ListRunResponseItem>;
64
+ declare function listRuns(params?: ListRunsQueryParams): CursorPagePromise<typeof ListRunResponseItem>;
65
+ declare function retrieveRun<TRunId extends RunHandle<any> | string>(runId: TRunId): ApiPromise<RetrieveRunResult<TRunId>>;
66
+ declare function replayRun(runId: string): ApiPromise<ReplayRunResponse>;
67
+ declare function cancelRun(runId: string): ApiPromise<CanceledRunResponse>;
68
+ type PollOptions = {
69
+ pollIntervalMs?: number;
70
+ };
71
+ declare function poll<TRunHandle extends RunHandle<any> | string>(handle: TRunHandle, options?: {
72
+ pollIntervalMs?: number;
73
+ }): Promise<(TRunHandle extends RunHandle<infer THandleOutput> ? Omit<{
74
+ status: "CANCELED" | "COMPLETED" | "FAILED" | "WAITING_FOR_DEPLOY" | "QUEUED" | "EXECUTING" | "REATTEMPTING" | "FROZEN" | "CRASHED" | "INTERRUPTED" | "SYSTEM_FAILURE";
75
+ id: string;
76
+ isTest: boolean;
77
+ createdAt: Date;
78
+ attempts: ({
79
+ status: "PENDING" | "CANCELED" | "COMPLETED" | "FAILED" | "EXECUTING" | "PAUSED";
80
+ id: string;
81
+ createdAt: Date;
82
+ updatedAt: Date;
83
+ startedAt?: Date | undefined;
84
+ completedAt?: Date | undefined;
85
+ error?: {
86
+ message: string;
87
+ name?: string | undefined;
88
+ stackTrace?: string | undefined;
89
+ } | undefined;
90
+ } | undefined)[];
91
+ updatedAt: Date;
92
+ taskIdentifier: string;
93
+ isQueued: boolean;
94
+ isExecuting: boolean;
95
+ isCompleted: boolean;
96
+ isSuccess: boolean;
97
+ isFailed: boolean;
98
+ isCancelled: boolean;
99
+ payload?: any;
100
+ output?: any;
101
+ schedule?: {
102
+ id: string;
103
+ generator: {
104
+ type: "CRON";
105
+ expression: string;
106
+ description: string;
107
+ };
108
+ externalId?: string | undefined;
109
+ deduplicationKey?: string | undefined;
110
+ } | undefined;
111
+ idempotencyKey?: string | undefined;
112
+ version?: string | undefined;
113
+ startedAt?: Date | undefined;
114
+ finishedAt?: Date | undefined;
115
+ }, "output"> & {
116
+ output?: THandleOutput | undefined;
117
+ } : Omit<{
118
+ status: "CANCELED" | "COMPLETED" | "FAILED" | "WAITING_FOR_DEPLOY" | "QUEUED" | "EXECUTING" | "REATTEMPTING" | "FROZEN" | "CRASHED" | "INTERRUPTED" | "SYSTEM_FAILURE";
119
+ id: string;
120
+ isTest: boolean;
121
+ createdAt: Date;
122
+ attempts: ({
123
+ status: "PENDING" | "CANCELED" | "COMPLETED" | "FAILED" | "EXECUTING" | "PAUSED";
124
+ id: string;
125
+ createdAt: Date;
126
+ updatedAt: Date;
127
+ startedAt?: Date | undefined;
128
+ completedAt?: Date | undefined;
129
+ error?: {
130
+ message: string;
131
+ name?: string | undefined;
132
+ stackTrace?: string | undefined;
133
+ } | undefined;
134
+ } | undefined)[];
135
+ updatedAt: Date;
136
+ taskIdentifier: string;
137
+ isQueued: boolean;
138
+ isExecuting: boolean;
139
+ isCompleted: boolean;
140
+ isSuccess: boolean;
141
+ isFailed: boolean;
142
+ isCancelled: boolean;
143
+ payload?: any;
144
+ output?: any;
145
+ schedule?: {
146
+ id: string;
147
+ generator: {
148
+ type: "CRON";
149
+ expression: string;
150
+ description: string;
151
+ };
152
+ externalId?: string | undefined;
153
+ deduplicationKey?: string | undefined;
154
+ } | undefined;
155
+ idempotencyKey?: string | undefined;
156
+ version?: string | undefined;
157
+ startedAt?: Date | undefined;
158
+ finishedAt?: Date | undefined;
159
+ }, "output"> & {
160
+ output?: TRunHandle | undefined;
161
+ }) extends infer T ? { [K in keyof T]: (TRunHandle extends RunHandle<infer THandleOutput> ? Omit<{
162
+ status: "CANCELED" | "COMPLETED" | "FAILED" | "WAITING_FOR_DEPLOY" | "QUEUED" | "EXECUTING" | "REATTEMPTING" | "FROZEN" | "CRASHED" | "INTERRUPTED" | "SYSTEM_FAILURE";
163
+ id: string;
164
+ isTest: boolean;
165
+ createdAt: Date;
166
+ attempts: ({
167
+ status: "PENDING" | "CANCELED" | "COMPLETED" | "FAILED" | "EXECUTING" | "PAUSED";
168
+ id: string;
169
+ createdAt: Date;
170
+ updatedAt: Date;
171
+ startedAt?: Date | undefined;
172
+ completedAt?: Date | undefined;
173
+ error?: {
174
+ message: string;
175
+ name?: string | undefined;
176
+ stackTrace?: string | undefined;
177
+ } | undefined;
178
+ } | undefined)[];
179
+ updatedAt: Date;
180
+ taskIdentifier: string;
181
+ isQueued: boolean;
182
+ isExecuting: boolean;
183
+ isCompleted: boolean;
184
+ isSuccess: boolean;
185
+ isFailed: boolean;
186
+ isCancelled: boolean;
187
+ payload?: any;
188
+ output?: any;
189
+ schedule?: {
190
+ id: string;
191
+ generator: {
192
+ type: "CRON";
193
+ expression: string;
194
+ description: string;
195
+ };
196
+ externalId?: string | undefined;
197
+ deduplicationKey?: string | undefined;
198
+ } | undefined;
199
+ idempotencyKey?: string | undefined;
200
+ version?: string | undefined;
201
+ startedAt?: Date | undefined;
202
+ finishedAt?: Date | undefined;
203
+ }, "output"> & {
204
+ output?: THandleOutput | undefined;
205
+ } : Omit<{
206
+ status: "CANCELED" | "COMPLETED" | "FAILED" | "WAITING_FOR_DEPLOY" | "QUEUED" | "EXECUTING" | "REATTEMPTING" | "FROZEN" | "CRASHED" | "INTERRUPTED" | "SYSTEM_FAILURE";
207
+ id: string;
208
+ isTest: boolean;
209
+ createdAt: Date;
210
+ attempts: ({
211
+ status: "PENDING" | "CANCELED" | "COMPLETED" | "FAILED" | "EXECUTING" | "PAUSED";
212
+ id: string;
213
+ createdAt: Date;
214
+ updatedAt: Date;
215
+ startedAt?: Date | undefined;
216
+ completedAt?: Date | undefined;
217
+ error?: {
218
+ message: string;
219
+ name?: string | undefined;
220
+ stackTrace?: string | undefined;
221
+ } | undefined;
222
+ } | undefined)[];
223
+ updatedAt: Date;
224
+ taskIdentifier: string;
225
+ isQueued: boolean;
226
+ isExecuting: boolean;
227
+ isCompleted: boolean;
228
+ isSuccess: boolean;
229
+ isFailed: boolean;
230
+ isCancelled: boolean;
231
+ payload?: any;
232
+ output?: any;
233
+ schedule?: {
234
+ id: string;
235
+ generator: {
236
+ type: "CRON";
237
+ expression: string;
238
+ description: string;
239
+ };
240
+ externalId?: string | undefined;
241
+ deduplicationKey?: string | undefined;
242
+ } | undefined;
243
+ idempotencyKey?: string | undefined;
244
+ version?: string | undefined;
245
+ startedAt?: Date | undefined;
246
+ finishedAt?: Date | undefined;
247
+ }, "output"> & {
248
+ output?: TRunHandle | undefined;
249
+ })[K]; } : never>;
250
+
51
251
  type Context = TaskRunContext;
52
252
  type RequireOne<T, K extends keyof T> = {
53
253
  [X in Exclude<keyof T, K>]?: T[X];
@@ -58,9 +258,9 @@ type Queue = RequireOne<QueueOptions, "name">;
58
258
  declare function queue(options: {
59
259
  name: string;
60
260
  } & QueueOptions): Queue;
61
- type TaskOptions<TPayload = void, TOutput = unknown, TInitOutput extends InitOutput = any> = {
261
+ type TaskOptions<TIdentifier extends string, TPayload = void, TOutput = unknown, TInitOutput extends InitOutput = any> = {
62
262
  /** An id for your task. This must be unique inside your project and not change between versions. */
63
- id: string;
263
+ id: TIdentifier;
64
264
  /** The retry settings when an uncaught error is thrown.
65
265
  *
66
266
  * If omitted it will use the values in your `trigger.config.ts` file.
@@ -195,13 +395,21 @@ type TaskOptions<TPayload = void, TOutput = unknown, TInitOutput extends InitOut
195
395
  */
196
396
  onFailure?: (payload: TPayload, error: unknown, params: FailureFnParams<TInitOutput>) => Promise<void>;
197
397
  };
198
- type InvokeHandle = {
199
- id: string;
398
+ declare const __output: unique symbol;
399
+ type BrandOutput<B> = {
400
+ [__output]: B;
200
401
  };
201
- type InvokeBatchHandle = {
402
+ type BrandedOutput<T, B> = T & BrandOutput<B>;
403
+ type RunHandle<TOutput> = BrandedOutput<{
404
+ id: string;
405
+ }, TOutput>;
406
+ /**
407
+ * A BatchRunHandle can be used to retrieve the runs of a batch trigger in a typesafe manner.
408
+ */
409
+ type BatchRunHandle<TOutput> = BrandedOutput<{
202
410
  batchId: string;
203
- runs: string[];
204
- };
411
+ runs: Array<RunHandle<TOutput>>;
412
+ }, TOutput>;
205
413
  type TaskRunResult<TOutput = any> = {
206
414
  ok: true;
207
415
  id: string;
@@ -222,19 +430,19 @@ type BatchItem<TInput> = TInput extends void ? {
222
430
  payload: TInput;
223
431
  options?: TaskRunOptions;
224
432
  };
225
- interface Task<TInput = void, TOutput = any> {
433
+ interface Task<TIdentifier extends string, TInput = void, TOutput = any> {
226
434
  /**
227
435
  * The id of the task.
228
436
  */
229
- id: string;
437
+ id: TIdentifier;
230
438
  /**
231
439
  * Trigger a task with the given payload, and continue without waiting for the result. If you want to wait for the result, use `triggerAndWait`. Returns the id of the triggered task run.
232
440
  * @param payload
233
441
  * @param options
234
- * @returns InvokeHandle
442
+ * @returns RunHandle
235
443
  * - `id` - The id of the triggered task run.
236
444
  */
237
- trigger: (payload: TInput, options?: TaskRunOptions) => Promise<InvokeHandle>;
445
+ trigger: (payload: TInput, options?: TaskRunOptions) => Promise<RunHandle<TOutput>>;
238
446
  /**
239
447
  * Batch trigger multiple task runs with the given payloads, and continue without waiting for the results. If you want to wait for the results, use `batchTriggerAndWait`. Returns the id of the triggered batch.
240
448
  * @param items
@@ -242,7 +450,7 @@ interface Task<TInput = void, TOutput = any> {
242
450
  * - `batchId` - The id of the triggered batch.
243
451
  * - `runs` - The ids of the triggered task runs.
244
452
  */
245
- batchTrigger: (items: Array<BatchItem<TInput>>) => Promise<InvokeBatchHandle>;
453
+ batchTrigger: (items: Array<BatchItem<TInput>>) => Promise<BatchRunHandle<TOutput>>;
246
454
  /**
247
455
  * Trigger a task with the given payload, and wait for the result. Returns the result of the task run
248
456
  * @param payload
@@ -282,6 +490,12 @@ interface Task<TInput = void, TOutput = any> {
282
490
  */
283
491
  batchTriggerAndWait: (items: Array<BatchItem<TInput>>) => Promise<BatchResult<TOutput>>;
284
492
  }
493
+ type AnyTask = Task<string, any, any>;
494
+ type TaskPayload<TTask extends AnyTask> = TTask extends Task<string, infer TInput, any> ? TInput : never;
495
+ type TaskOutput<TTask extends AnyTask> = TTask extends Task<string, any, infer TOutput> ? TOutput : never;
496
+ type TaskOutputHandle<TTask extends AnyTask> = TTask extends Task<string, any, infer TOutput> ? RunHandle<TOutput> : never;
497
+ type TaskBatchOutputHandle<TTask extends AnyTask> = TTask extends Task<string, any, infer TOutput> ? BatchRunHandle<TOutput> : never;
498
+ type TaskIdentifier<TTask extends AnyTask> = TTask extends Task<infer TIdentifier, any, any> ? TIdentifier : never;
285
499
  type TaskRunOptions = {
286
500
  idempotencyKey?: string;
287
501
  maxAttempts?: number;
@@ -291,6 +505,43 @@ type TaskRunOptions = {
291
505
  concurrencyKey?: string;
292
506
  };
293
507
  type TaskRunConcurrencyOptions = Queue;
508
+ type Prettify<T> = {
509
+ [K in keyof T]: T[K];
510
+ } & {};
511
+ /**
512
+ * Trigger a task by its identifier with the given payload. Returns a typesafe `RunHandle`.
513
+ *
514
+ * @example
515
+ *
516
+ * ```ts
517
+ * import { tasks, runs } from "@trigger.dev/sdk/v3";
518
+ * import type { myTask } from "./myTasks"; // Import just the type of the task
519
+ *
520
+ * const handle = await tasks.trigger<typeof myTask>("my-task", { foo: "bar" }); // The id and payload are fully typesafe
521
+ * const run = await runs.retrieve(handle);
522
+ * console.log(run.output) // The output is also fully typed
523
+ * ```
524
+ *
525
+ * @returns {RunHandle} An object with the `id` of the run. Can be used to retrieve the completed run output in a typesafe manner.
526
+ */
527
+ declare function trigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TaskRunOptions): Promise<TaskOutputHandle<TTask>>;
528
+ /**
529
+ * Trigger a task by its identifier with the given payload and poll until the run is completed.
530
+ *
531
+ * @example
532
+ *
533
+ * ```ts
534
+ * import { tasks, runs } from "@trigger.dev/sdk/v3";
535
+ * import type { myTask } from "./myTasks"; // Import just the type of the task
536
+ *
537
+ * const run = await tasks.triggerAndPoll<typeof myTask>("my-task", { foo: "bar" }); // The id and payload are fully typesafe
538
+ * console.log(run.output) // The output is also fully typed
539
+ * ```
540
+ *
541
+ * @returns {Run} The completed run, either successful or failed.
542
+ */
543
+ declare function triggerAndPoll<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TaskRunOptions & PollOptions): Promise<RetrieveRunResult<TaskOutputHandle<TTask>>>;
544
+ declare function batchTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, items: Array<BatchItem<TaskPayload<TTask>>>): Promise<TaskBatchOutputHandle<TTask>>;
294
545
 
295
546
  /** Creates a task that can be triggered
296
547
  * @param options - Task options
@@ -310,7 +561,12 @@ type TaskRunConcurrencyOptions = Queue;
310
561
  *
311
562
  * @returns A task that can be triggered
312
563
  */
313
- declare function task$1<TInput = void, TOutput = unknown, TInitOutput extends InitOutput = any>(options: TaskOptions<TInput, TOutput, TInitOutput>): Task<TInput, TOutput>;
564
+ declare function task$1<TIdentifier extends string, TInput = void, TOutput = unknown, TInitOutput extends InitOutput = any>(options: TaskOptions<TIdentifier, TInput, TOutput, TInitOutput>): Task<TIdentifier, TInput, TOutput>;
565
+ declare const tasks: {
566
+ trigger: typeof trigger;
567
+ triggerAndPoll: typeof triggerAndPoll;
568
+ batchTrigger: typeof batchTrigger;
569
+ };
314
570
 
315
571
  type WaitOptions = {
316
572
  seconds: number;
@@ -413,20 +669,7 @@ declare const usage: {
413
669
  }>;
414
670
  };
415
671
 
416
- type RetrieveRunResult = RetrieveRunResponse;
417
- declare const runs: {
418
- replay: typeof replayRun;
419
- cancel: typeof cancelRun;
420
- retrieve: typeof retrieveRun;
421
- list: typeof listRuns;
422
- };
423
- declare function listRuns(projectRef: string, params?: ListProjectRunsQueryParams): CursorPagePromise<typeof ListRunResponseItem>;
424
- declare function listRuns(params?: ListRunsQueryParams): CursorPagePromise<typeof ListRunResponseItem>;
425
- declare function retrieveRun(runId: string): ApiPromise<RetrieveRunResult>;
426
- declare function replayRun(runId: string): ApiPromise<ReplayRunResponse>;
427
- declare function cancelRun(runId: string): ApiPromise<CanceledRunResponse>;
428
-
429
- declare function task<TOutput, TInitOutput extends InitOutput>(params: TaskOptions<ScheduledTaskPayload, TOutput, TInitOutput>): Task<ScheduledTaskPayload, TOutput>;
672
+ declare function task<TIdentifier extends string, TOutput, TInitOutput extends InitOutput>(params: TaskOptions<TIdentifier, ScheduledTaskPayload, TOutput, TInitOutput>): Task<TIdentifier, ScheduledTaskPayload, TOutput>;
430
673
  /**
431
674
  * Creates a new schedule
432
675
  * @param options
@@ -540,4 +783,4 @@ declare namespace envvars {
540
783
  */
541
784
  declare function configure(options: ApiClientConfiguration): void;
542
785
 
543
- export { type CacheEntry, type CacheFunction, type CacheMetadata, type CacheStore, type ComputeUsage, type Context, type CurrentUsage, type Eventually, InMemoryCache, type Task, type TaskOptions, type WaitOptions, configure, createCache, envvars, queue, retry, runs, index as schedules, task$1 as task, usage, wait };
786
+ export { type BatchItem, type BatchResult, type BatchRunHandle, type CacheEntry, type CacheFunction, type CacheMetadata, type CacheStore, type ComputeUsage, type Context, type CurrentUsage, type Eventually, InMemoryCache, type Queue, type RunHandle, type Task, type TaskIdentifier, type TaskOptions, type TaskOutput, type TaskPayload, type TaskRunOptions, type TaskRunResult, type WaitOptions, configure, createCache, envvars, queue, retry, runs, index as schedules, task$1 as task, tasks, usage, wait };