@trigger.dev/sdk 2.0.0-next.14 → 2.0.0-next.16

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.
package/dist/index.d.ts CHANGED
@@ -15,10 +15,10 @@ declare class Logger {
15
15
  constructor(name: string, level?: LogLevel, filteredKeys?: string[], jsonReplacer?: (key: string, value: unknown) => unknown);
16
16
  filter(...keys: string[]): Logger;
17
17
  static satisfiesLogLevel(logLevel: LogLevel, setLevel: LogLevel): boolean;
18
- log(...args: any[]): void;
19
- error(...args: any[]): void;
20
- warn(...args: any[]): void;
21
- info(...args: any[]): void;
18
+ log(message: string, ...args: Array<Record<string, unknown> | undefined>): void;
19
+ error(message: string, ...args: Array<Record<string, unknown> | undefined>): void;
20
+ warn(message: string, ...args: Array<Record<string, unknown> | undefined>): void;
21
+ info(message: string, ...args: Array<Record<string, unknown> | undefined>): void;
22
22
  debug(message: string, ...args: Array<Record<string, unknown> | undefined>): void;
23
23
  }
24
24
 
@@ -48,7 +48,7 @@ declare const ServerTaskSchema: z.ZodObject<{
48
48
  startedAt: z.ZodNullable<z.ZodOptional<z.ZodDate>>;
49
49
  completedAt: z.ZodNullable<z.ZodOptional<z.ZodDate>>;
50
50
  delayUntil: z.ZodNullable<z.ZodOptional<z.ZodDate>>;
51
- status: z.ZodEnum<["PENDING", "WAITING", "RUNNING", "COMPLETED", "ERRORED"]>;
51
+ status: z.ZodEnum<["PENDING", "WAITING", "RUNNING", "COMPLETED", "ERRORED", "CANCELED"]>;
52
52
  description: z.ZodNullable<z.ZodOptional<z.ZodString>>;
53
53
  params: z.ZodNullable<z.ZodOptional<z.ZodType<DeserializedJson, z.ZodTypeDef, DeserializedJson>>>;
54
54
  properties: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodObject<{
@@ -97,7 +97,7 @@ declare const ServerTaskSchema: z.ZodObject<{
97
97
  id: string;
98
98
  name: string;
99
99
  noop: boolean;
100
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
100
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
101
101
  idempotencyKey: string;
102
102
  attempts: number;
103
103
  icon?: string | null | undefined;
@@ -128,7 +128,7 @@ declare const ServerTaskSchema: z.ZodObject<{
128
128
  id: string;
129
129
  name: string;
130
130
  noop: boolean;
131
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
131
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
132
132
  idempotencyKey: string;
133
133
  attempts: number;
134
134
  icon?: string | null | undefined;
@@ -160,20 +160,20 @@ type ServerTask = z.infer<typeof ServerTaskSchema>;
160
160
  declare const CachedTaskSchema: z.ZodObject<{
161
161
  id: z.ZodString;
162
162
  idempotencyKey: z.ZodString;
163
- status: z.ZodEnum<["PENDING", "WAITING", "RUNNING", "COMPLETED", "ERRORED"]>;
163
+ status: z.ZodEnum<["PENDING", "WAITING", "RUNNING", "COMPLETED", "ERRORED", "CANCELED"]>;
164
164
  noop: z.ZodDefault<z.ZodBoolean>;
165
165
  output: z.ZodNullable<z.ZodOptional<z.ZodType<DeserializedJson, z.ZodTypeDef, DeserializedJson>>>;
166
166
  parentId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
167
167
  }, "strip", z.ZodTypeAny, {
168
168
  id: string;
169
169
  noop: boolean;
170
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
170
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
171
171
  idempotencyKey: string;
172
172
  output?: DeserializedJson | undefined;
173
173
  parentId?: string | null | undefined;
174
174
  }, {
175
175
  id: string;
176
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
176
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
177
177
  idempotencyKey: string;
178
178
  noop?: boolean | undefined;
179
179
  output?: DeserializedJson | undefined;
@@ -3168,7 +3168,7 @@ declare class ApiClient {
3168
3168
  id: string;
3169
3169
  name: string;
3170
3170
  noop: boolean;
3171
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
3171
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
3172
3172
  idempotencyKey: string;
3173
3173
  attempts: number;
3174
3174
  icon?: string | null | undefined;
@@ -3200,7 +3200,7 @@ declare class ApiClient {
3200
3200
  id: string;
3201
3201
  name: string;
3202
3202
  noop: boolean;
3203
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
3203
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
3204
3204
  idempotencyKey: string;
3205
3205
  attempts: number;
3206
3206
  icon?: string | null | undefined;
@@ -3232,7 +3232,7 @@ declare class ApiClient {
3232
3232
  id: string;
3233
3233
  name: string;
3234
3234
  noop: boolean;
3235
- status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED";
3235
+ status: "PENDING" | "WAITING" | "RUNNING" | "COMPLETED" | "ERRORED" | "CANCELED";
3236
3236
  idempotencyKey: string;
3237
3237
  attempts: number;
3238
3238
  icon?: string | null | undefined;
@@ -4275,13 +4275,17 @@ declare class RetryWithTaskError {
4275
4275
  retryAt: Date;
4276
4276
  constructor(cause: ErrorWithStack, task: ServerTask, retryAt: Date);
4277
4277
  }
4278
+ declare class CanceledWithTaskError {
4279
+ task: ServerTask;
4280
+ constructor(task: ServerTask);
4281
+ }
4278
4282
  /** Use this function if you're using a `try/catch` block to catch errors.
4279
4283
  * It checks if a thrown error is a special internal error that you should ignore.
4280
4284
  * If this returns `true` then you must rethrow the error: `throw err;`
4281
4285
  * @param err The error to check
4282
4286
  * @returns `true` if the error is a Trigger Error, `false` otherwise.
4283
4287
  */
4284
- declare function isTriggerError(err: unknown): err is ResumeWithTaskError | RetryWithTaskError;
4288
+ declare function isTriggerError(err: unknown): err is ResumeWithTaskError | RetryWithTaskError | CanceledWithTaskError;
4285
4289
 
4286
4290
  type Task = ServerTask;
4287
4291
  declare function redactString(strings: TemplateStringsArray, ...interpolations: string[]): RedactString;
package/dist/index.js CHANGED
@@ -154,9 +154,10 @@ var logLevels = [
154
154
  "info",
155
155
  "debug"
156
156
  ];
157
- var _name, _level, _filteredKeys, _jsonReplacer;
157
+ var _name, _level, _filteredKeys, _jsonReplacer, _structuredLog, structuredLog_fn;
158
158
  var _Logger = class {
159
159
  constructor(name, level = "info", filteredKeys = [], jsonReplacer) {
160
+ __privateAdd(this, _structuredLog);
160
161
  __privateAdd(this, _name, void 0);
161
162
  __privateAdd(this, _level, void 0);
162
163
  __privateAdd(this, _filteredKeys, []);
@@ -172,36 +173,30 @@ var _Logger = class {
172
173
  static satisfiesLogLevel(logLevel, setLevel) {
173
174
  return logLevels.indexOf(logLevel) <= logLevels.indexOf(setLevel);
174
175
  }
175
- log(...args) {
176
+ log(message, ...args) {
176
177
  if (__privateGet(this, _level) < 0)
177
178
  return;
178
- console.log(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
179
+ __privateMethod(this, _structuredLog, structuredLog_fn).call(this, console.log, message, ...args);
179
180
  }
180
- error(...args) {
181
+ error(message, ...args) {
181
182
  if (__privateGet(this, _level) < 1)
182
183
  return;
183
- console.error(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
184
+ __privateMethod(this, _structuredLog, structuredLog_fn).call(this, console.error, message, ...args);
184
185
  }
185
- warn(...args) {
186
+ warn(message, ...args) {
186
187
  if (__privateGet(this, _level) < 2)
187
188
  return;
188
- console.warn(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
189
+ __privateMethod(this, _structuredLog, structuredLog_fn).call(this, console.warn, message, ...args);
189
190
  }
190
- info(...args) {
191
+ info(message, ...args) {
191
192
  if (__privateGet(this, _level) < 3)
192
193
  return;
193
- console.info(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
194
+ __privateMethod(this, _structuredLog, structuredLog_fn).call(this, console.info, message, ...args);
194
195
  }
195
196
  debug(message, ...args) {
196
197
  if (__privateGet(this, _level) < 4)
197
198
  return;
198
- const structuredLog = {
199
- timestamp: new Date(),
200
- name: __privateGet(this, _name),
201
- message,
202
- args: structureArgs(safeJsonClone(args), __privateGet(this, _filteredKeys))
203
- };
204
- console.debug(JSON.stringify(structuredLog, createReplacer(__privateGet(this, _jsonReplacer))));
199
+ __privateMethod(this, _structuredLog, structuredLog_fn).call(this, console.debug, message, ...args);
205
200
  }
206
201
  };
207
202
  var Logger = _Logger;
@@ -210,6 +205,16 @@ _name = new WeakMap();
210
205
  _level = new WeakMap();
211
206
  _filteredKeys = new WeakMap();
212
207
  _jsonReplacer = new WeakMap();
208
+ _structuredLog = new WeakSet();
209
+ structuredLog_fn = /* @__PURE__ */ __name(function(loggerFunction, message, ...args) {
210
+ const structuredLog = {
211
+ ...structureArgs(safeJsonClone(args), __privateGet(this, _filteredKeys)),
212
+ timestamp: new Date(),
213
+ name: __privateGet(this, _name),
214
+ message
215
+ };
216
+ loggerFunction(JSON.stringify(structuredLog, createReplacer(__privateGet(this, _jsonReplacer))));
217
+ }, "#structuredLog");
213
218
  function createReplacer(replacer) {
214
219
  return (key, value) => {
215
220
  if (typeof value === "bigint") {
@@ -237,19 +242,6 @@ function safeJsonClone(obj) {
237
242
  }
238
243
  }
239
244
  __name(safeJsonClone, "safeJsonClone");
240
- function formattedDateTime() {
241
- const date = new Date();
242
- const hours = date.getHours();
243
- const minutes = date.getMinutes();
244
- const seconds = date.getSeconds();
245
- const milliseconds = date.getMilliseconds();
246
- const formattedHours = hours < 10 ? `0${hours}` : hours;
247
- const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes;
248
- const formattedSeconds = seconds < 10 ? `0${seconds}` : seconds;
249
- const formattedMilliseconds = milliseconds < 10 ? `00${milliseconds}` : milliseconds < 100 ? `0${milliseconds}` : milliseconds;
250
- return `${formattedHours}:${formattedMinutes}:${formattedSeconds}.${formattedMilliseconds}`;
251
- }
252
- __name(formattedDateTime, "formattedDateTime");
253
245
  function structureArgs(args, filteredKeys = []) {
254
246
  if (args.length === 0) {
255
247
  return;
@@ -417,7 +409,8 @@ var TaskStatusSchema = import_zod7.z.enum([
417
409
  "WAITING",
418
410
  "RUNNING",
419
411
  "COMPLETED",
420
- "ERRORED"
412
+ "ERRORED",
413
+ "CANCELED"
421
414
  ]);
422
415
  var TaskSchema = import_zod7.z.object({
423
416
  id: import_zod7.z.string(),
@@ -708,6 +701,10 @@ var RunJobRetryWithTaskSchema = import_zod9.z.object({
708
701
  error: ErrorWithStackSchema,
709
702
  retryAt: import_zod9.z.coerce.date()
710
703
  });
704
+ var RunJobCanceledWithTaskSchema = import_zod9.z.object({
705
+ status: import_zod9.z.literal("CANCELED"),
706
+ task: TaskSchema
707
+ });
711
708
  var RunJobSuccessSchema = import_zod9.z.object({
712
709
  status: import_zod9.z.literal("SUCCESS"),
713
710
  output: DeserializedJsonSchema.optional()
@@ -716,6 +713,7 @@ var RunJobResponseSchema = import_zod9.z.discriminatedUnion("status", [
716
713
  RunJobErrorSchema,
717
714
  RunJobResumeWithTaskSchema,
718
715
  RunJobRetryWithTaskSchema,
716
+ RunJobCanceledWithTaskSchema,
719
717
  RunJobSuccessSchema
720
718
  ]);
721
719
  var PreprocessRunBodySchema = import_zod9.z.object({
@@ -1029,6 +1027,7 @@ var currentDate = {
1029
1027
 
1030
1028
  // src/apiClient.ts
1031
1029
  var import_zod12 = require("zod");
1030
+ var import_node_fetch = __toESM(require("node-fetch"));
1032
1031
  var _apiUrl, _options, _logger, _apiKey, apiKey_fn;
1033
1032
  var ApiClient = class {
1034
1033
  constructor(options) {
@@ -1046,7 +1045,7 @@ var ApiClient = class {
1046
1045
  url: options.url,
1047
1046
  name: options.name
1048
1047
  });
1049
- const response = await fetch(`${__privateGet(this, _apiUrl)}/api/v1/endpoints`, {
1048
+ const response = await (0, import_node_fetch.default)(`${__privateGet(this, _apiUrl)}/api/v1/endpoints`, {
1050
1049
  method: "POST",
1051
1050
  headers: {
1052
1051
  "Content-Type": "application/json",
@@ -1260,7 +1259,7 @@ function getApiKey(key) {
1260
1259
  }
1261
1260
  __name(getApiKey, "getApiKey");
1262
1261
  async function zodfetch(schema, url, requestInit, options) {
1263
- const response = await fetch(url, requestInit);
1262
+ const response = await (0, import_node_fetch.default)(url, requestInit);
1264
1263
  if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
1265
1264
  return;
1266
1265
  }
@@ -1291,8 +1290,14 @@ var RetryWithTaskError = class {
1291
1290
  }
1292
1291
  };
1293
1292
  __name(RetryWithTaskError, "RetryWithTaskError");
1293
+ var CanceledWithTaskError = class {
1294
+ constructor(task) {
1295
+ this.task = task;
1296
+ }
1297
+ };
1298
+ __name(CanceledWithTaskError, "CanceledWithTaskError");
1294
1299
  function isTriggerError(err) {
1295
- return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError;
1300
+ return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError;
1296
1301
  }
1297
1302
  __name(isTriggerError, "isTriggerError");
1298
1303
 
@@ -1672,6 +1677,13 @@ var IO = class {
1672
1677
  ...options,
1673
1678
  parentId
1674
1679
  });
1680
+ if (task.status === "CANCELED") {
1681
+ this._logger.debug("Task canceled", {
1682
+ idempotencyKey,
1683
+ task
1684
+ });
1685
+ throw new CanceledWithTaskError(task);
1686
+ }
1675
1687
  if (task.status === "COMPLETED") {
1676
1688
  this._logger.debug("Using task output", {
1677
1689
  idempotencyKey,
@@ -1708,10 +1720,13 @@ var IO = class {
1708
1720
  idempotencyKey,
1709
1721
  task
1710
1722
  });
1711
- await this._apiClient.completeTask(this._id, task.id, {
1723
+ const completedTask = await this._apiClient.completeTask(this._id, task.id, {
1712
1724
  output: result ?? void 0,
1713
1725
  properties: task.outputProperties ?? void 0
1714
1726
  });
1727
+ if (completedTask.status === "CANCELED") {
1728
+ throw new CanceledWithTaskError(completedTask);
1729
+ }
1715
1730
  return result;
1716
1731
  } catch (error) {
1717
1732
  if (isTriggerError(error)) {
@@ -2117,11 +2132,19 @@ var TriggerClient = class {
2117
2132
  }
2118
2133
  };
2119
2134
  }
2120
- const sourceRequest = new Request(headers.data["x-ts-http-url"], {
2135
+ const sourceRequestNeedsBody = headers.data["x-ts-http-method"] !== "GET";
2136
+ const sourceRequestInit = {
2121
2137
  method: headers.data["x-ts-http-method"],
2122
2138
  headers: headers.data["x-ts-http-headers"],
2123
- body: headers.data["x-ts-http-method"] !== "GET" ? request.body : void 0
2124
- });
2139
+ body: sourceRequestNeedsBody ? request.body : void 0
2140
+ };
2141
+ if (sourceRequestNeedsBody) {
2142
+ try {
2143
+ sourceRequestInit.duplex = "half";
2144
+ } catch (error) {
2145
+ }
2146
+ }
2147
+ const sourceRequest = new Request(headers.data["x-ts-http-url"], sourceRequestInit);
2125
2148
  const key = headers.data["x-ts-key"];
2126
2149
  const dynamicId = headers.data["x-ts-dynamic-id"];
2127
2150
  const secret = headers.data["x-ts-secret"];
@@ -2354,6 +2377,12 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
2354
2377
  retryAt: error.retryAt
2355
2378
  };
2356
2379
  }
2380
+ if (error instanceof CanceledWithTaskError) {
2381
+ return {
2382
+ status: "CANCELED",
2383
+ task: error.task
2384
+ };
2385
+ }
2357
2386
  if (error instanceof RetryWithTaskError) {
2358
2387
  const errorWithStack2 = ErrorWithStackSchema.safeParse(error.cause);
2359
2388
  if (errorWithStack2.success) {