@trigger.dev/sdk 2.0.0-next.5 → 2.0.0-next.7

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.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
9
  var __export = (target, all) => {
@@ -16,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
16
18
  }
17
19
  return to;
18
20
  };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
19
25
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
26
  var __accessCheck = (obj, member, msg) => {
21
27
  if (!member.has(obj))
@@ -112,6 +118,9 @@ var Job = class {
112
118
  return acc;
113
119
  }, {});
114
120
  }
121
+ get logLevel() {
122
+ return this.options.logLevel;
123
+ }
115
124
  toJSON() {
116
125
  const internal = this.options.__internal;
117
126
  return {
@@ -160,6 +169,9 @@ var _Logger = class {
160
169
  filter(...keys) {
161
170
  return new _Logger(__privateGet(this, _name), logLevels[__privateGet(this, _level)], keys, __privateGet(this, _jsonReplacer));
162
171
  }
172
+ static satisfiesLogLevel(logLevel, setLevel) {
173
+ return logLevels.indexOf(logLevel) <= logLevels.indexOf(setLevel);
174
+ }
163
175
  log(...args) {
164
176
  if (__privateGet(this, _level) < 0)
165
177
  return;
@@ -605,12 +617,12 @@ var IndexEndpointResponseSchema = import_zod9.z.object({
605
617
  dynamicSchedules: import_zod9.z.array(RegisterDynamicSchedulePayloadSchema)
606
618
  });
607
619
  var RawEventSchema = import_zod9.z.object({
608
- id: import_zod9.z.string().default(() => (0, import_ulid.ulid)()),
609
620
  name: import_zod9.z.string(),
610
- source: import_zod9.z.string().optional(),
611
621
  payload: import_zod9.z.any(),
612
622
  context: import_zod9.z.any().optional(),
613
- timestamp: import_zod9.z.string().datetime().optional()
623
+ id: import_zod9.z.string().default(() => (0, import_ulid.ulid)()),
624
+ timestamp: import_zod9.z.coerce.date().optional(),
625
+ source: import_zod9.z.string().optional()
614
626
  });
615
627
  var ApiEventLogSchema = import_zod9.z.object({
616
628
  id: import_zod9.z.string(),
@@ -622,7 +634,7 @@ var ApiEventLogSchema = import_zod9.z.object({
622
634
  deliveredAt: import_zod9.z.coerce.date().optional().nullable()
623
635
  });
624
636
  var SendEventOptionsSchema = import_zod9.z.object({
625
- deliverAt: import_zod9.z.string().datetime().optional(),
637
+ deliverAt: import_zod9.z.coerce.date().optional(),
626
638
  deliverAfter: import_zod9.z.number().int().optional(),
627
639
  accountId: import_zod9.z.string().optional()
628
640
  });
@@ -1335,6 +1347,8 @@ var IO = class {
1335
1347
  this._triggerClient = options.client;
1336
1348
  this._logger = options.logger ?? new Logger("trigger.dev", options.logLevel);
1337
1349
  this._cachedTasks = /* @__PURE__ */ new Map();
1350
+ this._jobLogger = options.jobLogger;
1351
+ this._jobLogLevel = options.jobLogLevel;
1338
1352
  if (options.cachedTasks) {
1339
1353
  options.cachedTasks.forEach((task) => {
1340
1354
  this._cachedTasks.set(task.id, task);
@@ -1345,45 +1359,57 @@ var IO = class {
1345
1359
  }
1346
1360
  get logger() {
1347
1361
  return new IOLogger(async (level, message, data) => {
1362
+ let logLevel = "info";
1348
1363
  switch (level) {
1364
+ case "LOG": {
1365
+ this._jobLogger?.log(message, data);
1366
+ logLevel = "log";
1367
+ break;
1368
+ }
1349
1369
  case "DEBUG": {
1350
- this._logger.debug(message, data);
1370
+ this._jobLogger?.debug(message, data);
1371
+ logLevel = "debug";
1351
1372
  break;
1352
1373
  }
1353
1374
  case "INFO": {
1354
- this._logger.info(message, data);
1375
+ this._jobLogger?.info(message, data);
1376
+ logLevel = "info";
1355
1377
  break;
1356
1378
  }
1357
1379
  case "WARN": {
1358
- this._logger.warn(message, data);
1380
+ this._jobLogger?.warn(message, data);
1381
+ logLevel = "warn";
1359
1382
  break;
1360
1383
  }
1361
1384
  case "ERROR": {
1362
- this._logger.error(message, data);
1385
+ this._jobLogger?.error(message, data);
1386
+ logLevel = "error";
1363
1387
  break;
1364
1388
  }
1365
1389
  }
1366
- await this.runTask([
1367
- message,
1368
- level
1369
- ], {
1370
- name: "log",
1371
- icon: "log",
1372
- description: message,
1373
- params: data,
1374
- properties: [
1375
- {
1376
- label: "Level",
1377
- text: level
1378
- }
1379
- ],
1380
- style: {
1381
- style: "minimal",
1382
- variant: level.toLowerCase()
1383
- },
1384
- noop: true
1385
- }, async (task) => {
1386
- });
1390
+ if (Logger.satisfiesLogLevel(logLevel, this._jobLogLevel)) {
1391
+ await this.runTask([
1392
+ message,
1393
+ level
1394
+ ], {
1395
+ name: "log",
1396
+ icon: "log",
1397
+ description: message,
1398
+ params: data,
1399
+ properties: [
1400
+ {
1401
+ label: "Level",
1402
+ text: level
1403
+ }
1404
+ ],
1405
+ style: {
1406
+ style: "minimal",
1407
+ variant: level.toLowerCase()
1408
+ },
1409
+ noop: true
1410
+ }, async (task) => {
1411
+ });
1412
+ }
1387
1413
  });
1388
1414
  }
1389
1415
  async wait(key, seconds) {
@@ -1438,7 +1464,19 @@ var IO = class {
1438
1464
  params: {
1439
1465
  event,
1440
1466
  options
1441
- }
1467
+ },
1468
+ properties: [
1469
+ {
1470
+ label: "name",
1471
+ text: event.name
1472
+ },
1473
+ ...event?.id ? [
1474
+ {
1475
+ label: "ID",
1476
+ text: event.id
1477
+ }
1478
+ ] : []
1479
+ ]
1442
1480
  }, async (task) => {
1443
1481
  return await this._triggerClient.sendEvent(event, options);
1444
1482
  });
@@ -1758,6 +1796,9 @@ var IOLogger = class {
1758
1796
  constructor(callback) {
1759
1797
  this.callback = callback;
1760
1798
  }
1799
+ log(message, properties) {
1800
+ return this.callback("LOG", message, properties);
1801
+ }
1761
1802
  debug(message, properties) {
1762
1803
  return this.callback("DEBUG", message, properties);
1763
1804
  }
@@ -1830,7 +1871,7 @@ var registerSourceEvent = {
1830
1871
  icon: "register-source",
1831
1872
  parsePayload: RegisterSourceEventSchema.parse
1832
1873
  };
1833
- var _options3, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _client, _logger2, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn;
1874
+ var _options3, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _client, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn;
1834
1875
  var TriggerClient = class {
1835
1876
  constructor(options) {
1836
1877
  __privateAdd(this, _preprocessRun);
@@ -1846,14 +1887,14 @@ var TriggerClient = class {
1846
1887
  __privateAdd(this, _jobMetadataByDynamicTriggers, {});
1847
1888
  __privateAdd(this, _registeredSchedules, {});
1848
1889
  __privateAdd(this, _client, void 0);
1849
- __privateAdd(this, _logger2, void 0);
1890
+ __privateAdd(this, _internalLogger, void 0);
1850
1891
  this.id = options.id;
1851
1892
  __privateSet(this, _options3, options);
1852
1893
  __privateSet(this, _client, new ApiClient(__privateGet(this, _options3)));
1853
- __privateSet(this, _logger2, new Logger("trigger.dev", __privateGet(this, _options3).logLevel));
1894
+ __privateSet(this, _internalLogger, new Logger("trigger.dev", __privateGet(this, _options3).verbose ? "debug" : "log"));
1854
1895
  }
1855
1896
  async handleRequest(request) {
1856
- __privateGet(this, _logger2).debug("handling request", {
1897
+ __privateGet(this, _internalLogger).debug("handling request", {
1857
1898
  url: request.url,
1858
1899
  headers: Object.fromEntries(request.headers.entries()),
1859
1900
  method: request.method
@@ -1914,7 +1955,7 @@ var TriggerClient = class {
1914
1955
  status: 200,
1915
1956
  body: {
1916
1957
  ok: false,
1917
- message: "Missing endpoint ID"
1958
+ error: "Missing endpoint ID"
1918
1959
  }
1919
1960
  };
1920
1961
  }
@@ -1923,7 +1964,7 @@ var TriggerClient = class {
1923
1964
  status: 200,
1924
1965
  body: {
1925
1966
  ok: false,
1926
- message: `Endpoint ID mismatch error. Expected ${this.id}, got ${endpointId}`
1967
+ error: `Endpoint ID mismatch error. Expected ${this.id}, got ${endpointId}`
1927
1968
  }
1928
1969
  };
1929
1970
  }
@@ -2139,7 +2180,7 @@ var TriggerClient = class {
2139
2180
  }
2140
2181
  attachSource(options) {
2141
2182
  __privateGet(this, _registeredHttpSourceHandlers)[options.key] = async (s, r) => {
2142
- return await options.source.handle(s, r, __privateGet(this, _logger2));
2183
+ return await options.source.handle(s, r, __privateGet(this, _internalLogger));
2143
2184
  };
2144
2185
  let registeredSource = __privateGet(this, _registeredSources)[options.key];
2145
2186
  if (!registeredSource) {
@@ -2241,7 +2282,7 @@ _registeredDynamicTriggers = new WeakMap();
2241
2282
  _jobMetadataByDynamicTriggers = new WeakMap();
2242
2283
  _registeredSchedules = new WeakMap();
2243
2284
  _client = new WeakMap();
2244
- _logger2 = new WeakMap();
2285
+ _internalLogger = new WeakMap();
2245
2286
  _preprocessRun = new WeakSet();
2246
2287
  preprocessRun_fn = /* @__PURE__ */ __name(async function(body, job) {
2247
2288
  const context = __privateMethod(this, _createPreprocessRunContext, createPreprocessRunContext_fn).call(this, body);
@@ -2254,7 +2295,7 @@ preprocessRun_fn = /* @__PURE__ */ __name(async function(body, job) {
2254
2295
  }, "#preprocessRun");
2255
2296
  _executeJob = new WeakSet();
2256
2297
  executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
2257
- __privateGet(this, _logger2).debug("executing job", {
2298
+ __privateGet(this, _internalLogger).debug("executing job", {
2258
2299
  execution: body1,
2259
2300
  job: job1.toJSON()
2260
2301
  });
@@ -2263,9 +2304,11 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
2263
2304
  id: body1.run.id,
2264
2305
  cachedTasks: body1.tasks,
2265
2306
  apiClient: __privateGet(this, _client),
2266
- logger: __privateGet(this, _logger2),
2307
+ logger: __privateGet(this, _internalLogger),
2267
2308
  client: this,
2268
- context
2309
+ context,
2310
+ jobLogLevel: job1.logLevel ?? __privateGet(this, _options3).logLevel ?? "info",
2311
+ jobLogger: __privateGet(this, _options3).ioLogLocalEnabled ? new Logger(job1.id, job1.logLevel ?? __privateGet(this, _options3).logLevel ?? "info") : void 0
2269
2312
  });
2270
2313
  const ioWithConnections = createIOWithIntegrations(io, body1.connections, job1.options.integrations);
2271
2314
  try {
@@ -2358,13 +2401,13 @@ createPreprocessRunContext_fn = /* @__PURE__ */ __name(function(body2) {
2358
2401
  }, "#createPreprocessRunContext");
2359
2402
  _handleHttpSourceRequest = new WeakSet();
2360
2403
  handleHttpSourceRequest_fn = /* @__PURE__ */ __name(async function(source, sourceRequest) {
2361
- __privateGet(this, _logger2).debug("Handling HTTP source request", {
2404
+ __privateGet(this, _internalLogger).debug("Handling HTTP source request", {
2362
2405
  source
2363
2406
  });
2364
2407
  if (source.dynamicId) {
2365
2408
  const dynamicTrigger = __privateGet(this, _registeredDynamicTriggers)[source.dynamicId];
2366
2409
  if (!dynamicTrigger) {
2367
- __privateGet(this, _logger2).debug("No dynamic trigger registered for HTTP source", {
2410
+ __privateGet(this, _internalLogger).debug("No dynamic trigger registered for HTTP source", {
2368
2411
  source
2369
2412
  });
2370
2413
  return {
@@ -2377,7 +2420,7 @@ handleHttpSourceRequest_fn = /* @__PURE__ */ __name(async function(source, sourc
2377
2420
  events: []
2378
2421
  };
2379
2422
  }
2380
- const results2 = await dynamicTrigger.source.handle(source, sourceRequest, __privateGet(this, _logger2));
2423
+ const results2 = await dynamicTrigger.source.handle(source, sourceRequest, __privateGet(this, _internalLogger));
2381
2424
  if (!results2) {
2382
2425
  return {
2383
2426
  events: [],
@@ -2401,7 +2444,7 @@ handleHttpSourceRequest_fn = /* @__PURE__ */ __name(async function(source, sourc
2401
2444
  }
2402
2445
  const handler = __privateGet(this, _registeredHttpSourceHandlers)[source.key];
2403
2446
  if (!handler) {
2404
- __privateGet(this, _logger2).debug("No handler registered for HTTP source", {
2447
+ __privateGet(this, _internalLogger).debug("No handler registered for HTTP source", {
2405
2448
  source
2406
2449
  });
2407
2450
  return {
@@ -2610,6 +2653,7 @@ _client2 = new WeakMap();
2610
2653
  _options4 = new WeakMap();
2611
2654
 
2612
2655
  // src/triggers/scheduled.ts
2656
+ var import_cronstrue = __toESM(require("cronstrue"));
2613
2657
  var examples = [
2614
2658
  {
2615
2659
  id: "now",
@@ -2668,6 +2712,9 @@ var CronTrigger = class {
2668
2712
  this.options = options;
2669
2713
  }
2670
2714
  get event() {
2715
+ const humanReadable = import_cronstrue.default.toString(this.options.cron, {
2716
+ throwExceptionOnParseError: false
2717
+ });
2671
2718
  return {
2672
2719
  name: "trigger.scheduled",
2673
2720
  title: "Cron Schedule",
@@ -2677,8 +2724,12 @@ var CronTrigger = class {
2677
2724
  parsePayload: ScheduledPayloadSchema.parse,
2678
2725
  properties: [
2679
2726
  {
2680
- label: "Expression",
2727
+ label: "cron",
2681
2728
  text: this.options.cron
2729
+ },
2730
+ {
2731
+ label: "Schedule",
2732
+ text: humanReadable
2682
2733
  }
2683
2734
  ]
2684
2735
  };