@trigger.dev/sdk 2.1.3 → 2.1.5
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 +334 -159
- package/dist/index.js +689 -319
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
package/dist/index.js
CHANGED
|
@@ -107,6 +107,9 @@ var Job = class {
|
|
|
107
107
|
get version() {
|
|
108
108
|
return this.options.version;
|
|
109
109
|
}
|
|
110
|
+
get logLevel() {
|
|
111
|
+
return this.options.logLevel;
|
|
112
|
+
}
|
|
110
113
|
get integrations() {
|
|
111
114
|
return Object.keys(this.options.integrations ?? {}).reduce((acc, key) => {
|
|
112
115
|
const integration = this.options.integrations[key];
|
|
@@ -118,9 +121,6 @@ var Job = class {
|
|
|
118
121
|
return acc;
|
|
119
122
|
}, {});
|
|
120
123
|
}
|
|
121
|
-
get logLevel() {
|
|
122
|
-
return this.options.logLevel;
|
|
123
|
-
}
|
|
124
124
|
toJSON() {
|
|
125
125
|
const internal = this.options.__internal;
|
|
126
126
|
return {
|
|
@@ -146,7 +146,7 @@ validate_fn = /* @__PURE__ */ __name(function() {
|
|
|
146
146
|
}, "#validate");
|
|
147
147
|
|
|
148
148
|
// src/triggerClient.ts
|
|
149
|
-
var
|
|
149
|
+
var import_core7 = require("@trigger.dev/core");
|
|
150
150
|
|
|
151
151
|
// src/apiClient.ts
|
|
152
152
|
var import_core = require("@trigger.dev/core");
|
|
@@ -264,6 +264,21 @@ var ApiClient = class {
|
|
|
264
264
|
}
|
|
265
265
|
});
|
|
266
266
|
}
|
|
267
|
+
async updateStatus(runId, id, status) {
|
|
268
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
269
|
+
__privateGet(this, _logger).debug("Update status", {
|
|
270
|
+
id,
|
|
271
|
+
status
|
|
272
|
+
});
|
|
273
|
+
return await zodfetch(import_core.JobRunStatusRecordSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/statuses/${id}`, {
|
|
274
|
+
method: "PUT",
|
|
275
|
+
headers: {
|
|
276
|
+
"Content-Type": "application/json",
|
|
277
|
+
Authorization: `Bearer ${apiKey}`
|
|
278
|
+
},
|
|
279
|
+
body: JSON.stringify(status)
|
|
280
|
+
});
|
|
281
|
+
}
|
|
267
282
|
async updateSource(client, key, source) {
|
|
268
283
|
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
269
284
|
__privateGet(this, _logger).debug("activating http source", {
|
|
@@ -370,6 +385,18 @@ var ApiClient = class {
|
|
|
370
385
|
}
|
|
371
386
|
});
|
|
372
387
|
}
|
|
388
|
+
async getRunStatuses(runId) {
|
|
389
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
390
|
+
__privateGet(this, _logger).debug("Getting Run statuses", {
|
|
391
|
+
runId
|
|
392
|
+
});
|
|
393
|
+
return await zodfetch(import_core.GetRunStatusesSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/statuses`, {
|
|
394
|
+
method: "GET",
|
|
395
|
+
headers: {
|
|
396
|
+
Authorization: `Bearer ${apiKey}`
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
}
|
|
373
400
|
async getRuns(jobSlug, options) {
|
|
374
401
|
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
375
402
|
__privateGet(this, _logger).debug("Getting Runs", {
|
|
@@ -455,6 +482,12 @@ var CanceledWithTaskError = class {
|
|
|
455
482
|
}
|
|
456
483
|
};
|
|
457
484
|
__name(CanceledWithTaskError, "CanceledWithTaskError");
|
|
485
|
+
var ParsedPayloadSchemaError = class {
|
|
486
|
+
constructor(schemaErrors) {
|
|
487
|
+
this.schemaErrors = schemaErrors;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
__name(ParsedPayloadSchemaError, "ParsedPayloadSchemaError");
|
|
458
491
|
function isTriggerError(err) {
|
|
459
492
|
return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError;
|
|
460
493
|
}
|
|
@@ -477,6 +510,40 @@ var retry = {
|
|
|
477
510
|
}
|
|
478
511
|
};
|
|
479
512
|
|
|
513
|
+
// src/status.ts
|
|
514
|
+
var TriggerStatus = class {
|
|
515
|
+
constructor(id, io) {
|
|
516
|
+
this.id = id;
|
|
517
|
+
this.io = io;
|
|
518
|
+
}
|
|
519
|
+
async update(key, status) {
|
|
520
|
+
const properties = [];
|
|
521
|
+
if (status.label) {
|
|
522
|
+
properties.push({
|
|
523
|
+
label: "Label",
|
|
524
|
+
text: status.label
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
if (status.state) {
|
|
528
|
+
properties.push({
|
|
529
|
+
label: "State",
|
|
530
|
+
text: status.state
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
return await this.io.runTask(key, async (task) => {
|
|
534
|
+
return await this.io.triggerClient.updateStatus(this.io.runId, this.id, status);
|
|
535
|
+
}, {
|
|
536
|
+
name: status.label ?? `Status update`,
|
|
537
|
+
icon: "bell",
|
|
538
|
+
params: {
|
|
539
|
+
...status
|
|
540
|
+
},
|
|
541
|
+
properties
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
__name(TriggerStatus, "TriggerStatus");
|
|
546
|
+
|
|
480
547
|
// src/io.ts
|
|
481
548
|
var _addToCachedTasks, addToCachedTasks_fn;
|
|
482
549
|
var IO = class {
|
|
@@ -497,6 +564,12 @@ var IO = class {
|
|
|
497
564
|
this._taskStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
498
565
|
this._context = options.context;
|
|
499
566
|
}
|
|
567
|
+
get runId() {
|
|
568
|
+
return this._id;
|
|
569
|
+
}
|
|
570
|
+
get triggerClient() {
|
|
571
|
+
return this._triggerClient;
|
|
572
|
+
}
|
|
500
573
|
get logger() {
|
|
501
574
|
return new IOLogger(async (level, message, data) => {
|
|
502
575
|
let logLevel = "info";
|
|
@@ -567,6 +640,12 @@ var IO = class {
|
|
|
567
640
|
}
|
|
568
641
|
});
|
|
569
642
|
}
|
|
643
|
+
async createStatus(key, initialStatus) {
|
|
644
|
+
const id = typeof key === "string" ? key : key.join("-");
|
|
645
|
+
const status = new TriggerStatus(id, this);
|
|
646
|
+
await status.update(key, initialStatus);
|
|
647
|
+
return status;
|
|
648
|
+
}
|
|
570
649
|
async backgroundFetch(key, url, requestInit, retry2) {
|
|
571
650
|
const urlObject = new URL(url);
|
|
572
651
|
return await this.runTask(key, async (task) => {
|
|
@@ -817,7 +896,7 @@ var IO = class {
|
|
|
817
896
|
}
|
|
818
897
|
const task = await this._apiClient.runTask(this._id, {
|
|
819
898
|
idempotencyKey,
|
|
820
|
-
displayKey: typeof key === "string" ? key :
|
|
899
|
+
displayKey: typeof key === "string" ? key : key.join("."),
|
|
821
900
|
noop: false,
|
|
822
901
|
...options ?? {},
|
|
823
902
|
parentId
|
|
@@ -878,6 +957,7 @@ var IO = class {
|
|
|
878
957
|
if (isTriggerError(error)) {
|
|
879
958
|
throw error;
|
|
880
959
|
}
|
|
960
|
+
let skipRetrying = false;
|
|
881
961
|
if (onError) {
|
|
882
962
|
try {
|
|
883
963
|
const onErrorResult = onError(error, task, this);
|
|
@@ -885,10 +965,13 @@ var IO = class {
|
|
|
885
965
|
if (onErrorResult instanceof Error) {
|
|
886
966
|
error = onErrorResult;
|
|
887
967
|
} else {
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
968
|
+
skipRetrying = !!onErrorResult.skipRetrying;
|
|
969
|
+
if (onErrorResult.retryAt && !skipRetrying) {
|
|
970
|
+
const parsedError2 = import_core3.ErrorWithStackSchema.safeParse(onErrorResult.error);
|
|
971
|
+
throw new RetryWithTaskError(parsedError2.success ? parsedError2.data : {
|
|
972
|
+
message: "Unknown error"
|
|
973
|
+
}, task, onErrorResult.retryAt);
|
|
974
|
+
}
|
|
892
975
|
}
|
|
893
976
|
}
|
|
894
977
|
} catch (innerError) {
|
|
@@ -899,7 +982,7 @@ var IO = class {
|
|
|
899
982
|
}
|
|
900
983
|
}
|
|
901
984
|
const parsedError = import_core3.ErrorWithStackSchema.safeParse(error);
|
|
902
|
-
if (options?.retry) {
|
|
985
|
+
if (options?.retry && !skipRetrying) {
|
|
903
986
|
const retryAt = (0, import_core2.calculateRetryAt)(options.retry, task.attempts - 1);
|
|
904
987
|
if (retryAt) {
|
|
905
988
|
throw new RetryWithTaskError(parsedError.success ? parsedError.data : {
|
|
@@ -1025,27 +1108,147 @@ function createIOWithIntegrations(io, auths, integrations) {
|
|
|
1025
1108
|
}
|
|
1026
1109
|
__name(createIOWithIntegrations, "createIOWithIntegrations");
|
|
1027
1110
|
|
|
1028
|
-
// src/
|
|
1111
|
+
// src/utils/typedAsyncLocalStorage.ts
|
|
1112
|
+
var import_node_async_hooks2 = require("async_hooks");
|
|
1113
|
+
var TypedAsyncLocalStorage = class {
|
|
1114
|
+
constructor() {
|
|
1115
|
+
this.storage = new import_node_async_hooks2.AsyncLocalStorage();
|
|
1116
|
+
}
|
|
1117
|
+
runWith(context, fn) {
|
|
1118
|
+
return this.storage.run(context, fn);
|
|
1119
|
+
}
|
|
1120
|
+
getStore() {
|
|
1121
|
+
return this.storage.getStore();
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
__name(TypedAsyncLocalStorage, "TypedAsyncLocalStorage");
|
|
1125
|
+
|
|
1126
|
+
// src/runLocalStorage.ts
|
|
1127
|
+
var runLocalStorage = new TypedAsyncLocalStorage();
|
|
1128
|
+
|
|
1129
|
+
// src/triggers/dynamic.ts
|
|
1029
1130
|
var import_core4 = require("@trigger.dev/core");
|
|
1030
|
-
var _options2;
|
|
1031
|
-
var
|
|
1032
|
-
constructor(options) {
|
|
1131
|
+
var _client, _options2;
|
|
1132
|
+
var DynamicTrigger = class {
|
|
1133
|
+
constructor(client, options) {
|
|
1134
|
+
__privateAdd(this, _client, void 0);
|
|
1033
1135
|
__privateAdd(this, _options2, void 0);
|
|
1136
|
+
__privateSet(this, _client, client);
|
|
1034
1137
|
__privateSet(this, _options2, options);
|
|
1138
|
+
this.source = options.source;
|
|
1139
|
+
client.attachDynamicTrigger(this);
|
|
1140
|
+
}
|
|
1141
|
+
toJSON() {
|
|
1142
|
+
return {
|
|
1143
|
+
type: "dynamic",
|
|
1144
|
+
id: __privateGet(this, _options2).id
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
get id() {
|
|
1148
|
+
return __privateGet(this, _options2).id;
|
|
1149
|
+
}
|
|
1150
|
+
get event() {
|
|
1151
|
+
return __privateGet(this, _options2).event;
|
|
1152
|
+
}
|
|
1153
|
+
registeredTriggerForParams(params, options = {}) {
|
|
1154
|
+
const key = slugifyId(this.source.key(params));
|
|
1155
|
+
return {
|
|
1156
|
+
rule: {
|
|
1157
|
+
event: this.event.name,
|
|
1158
|
+
source: this.event.source,
|
|
1159
|
+
payload: (0, import_core4.deepMergeFilters)(this.source.filter(params), this.event.filter ?? {}, options.filter ?? {})
|
|
1160
|
+
},
|
|
1161
|
+
source: {
|
|
1162
|
+
version: "2",
|
|
1163
|
+
key,
|
|
1164
|
+
channel: this.source.channel,
|
|
1165
|
+
params,
|
|
1166
|
+
options: {
|
|
1167
|
+
event: typeof this.event.name === "string" ? [
|
|
1168
|
+
this.event.name
|
|
1169
|
+
] : this.event.name
|
|
1170
|
+
},
|
|
1171
|
+
integration: {
|
|
1172
|
+
id: this.source.integration.id,
|
|
1173
|
+
metadata: this.source.integration.metadata,
|
|
1174
|
+
authSource: this.source.integration.authSource
|
|
1175
|
+
}
|
|
1176
|
+
},
|
|
1177
|
+
accountId: options.accountId
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
async register(key, params, options = {}) {
|
|
1181
|
+
const runStore = runLocalStorage.getStore();
|
|
1182
|
+
if (!runStore) {
|
|
1183
|
+
return __privateGet(this, _client).registerTrigger(this.id, key, this.registeredTriggerForParams(params, options));
|
|
1184
|
+
}
|
|
1185
|
+
const { io } = runStore;
|
|
1186
|
+
return await io.runTask([
|
|
1187
|
+
key,
|
|
1188
|
+
"register"
|
|
1189
|
+
], async (task) => {
|
|
1190
|
+
return __privateGet(this, _client).registerTrigger(this.id, key, this.registeredTriggerForParams(params, options));
|
|
1191
|
+
}, {
|
|
1192
|
+
name: "Register Dynamic Trigger",
|
|
1193
|
+
properties: [
|
|
1194
|
+
{
|
|
1195
|
+
label: "Dynamic Trigger ID",
|
|
1196
|
+
text: this.id
|
|
1197
|
+
},
|
|
1198
|
+
{
|
|
1199
|
+
label: "ID",
|
|
1200
|
+
text: key
|
|
1201
|
+
}
|
|
1202
|
+
],
|
|
1203
|
+
params
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
attachToJob(triggerClient, job) {
|
|
1207
|
+
triggerClient.attachJobToDynamicTrigger(job, this);
|
|
1208
|
+
}
|
|
1209
|
+
get preprocessRuns() {
|
|
1210
|
+
return true;
|
|
1211
|
+
}
|
|
1212
|
+
};
|
|
1213
|
+
__name(DynamicTrigger, "DynamicTrigger");
|
|
1214
|
+
_client = new WeakMap();
|
|
1215
|
+
_options2 = new WeakMap();
|
|
1216
|
+
|
|
1217
|
+
// src/triggers/eventTrigger.ts
|
|
1218
|
+
var import_core5 = require("@trigger.dev/core");
|
|
1219
|
+
|
|
1220
|
+
// src/utils/formatSchemaErrors.ts
|
|
1221
|
+
function formatSchemaErrors(errors) {
|
|
1222
|
+
return errors.map((error) => {
|
|
1223
|
+
const { path, message } = error;
|
|
1224
|
+
return {
|
|
1225
|
+
path: path.map(String),
|
|
1226
|
+
message
|
|
1227
|
+
};
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
__name(formatSchemaErrors, "formatSchemaErrors");
|
|
1231
|
+
|
|
1232
|
+
// src/triggers/eventTrigger.ts
|
|
1233
|
+
var _options3;
|
|
1234
|
+
var EventTrigger = class {
|
|
1235
|
+
constructor(options) {
|
|
1236
|
+
__privateAdd(this, _options3, void 0);
|
|
1237
|
+
__privateSet(this, _options3, options);
|
|
1035
1238
|
}
|
|
1036
1239
|
toJSON() {
|
|
1037
1240
|
return {
|
|
1038
1241
|
type: "static",
|
|
1039
|
-
title: __privateGet(this,
|
|
1242
|
+
title: __privateGet(this, _options3).name ?? __privateGet(this, _options3).event.title,
|
|
1040
1243
|
rule: {
|
|
1041
|
-
event: __privateGet(this,
|
|
1042
|
-
source: __privateGet(this,
|
|
1043
|
-
payload: (0,
|
|
1244
|
+
event: __privateGet(this, _options3).name ?? __privateGet(this, _options3).event.name,
|
|
1245
|
+
source: __privateGet(this, _options3).source ?? "trigger.dev",
|
|
1246
|
+
payload: (0, import_core5.deepMergeFilters)(__privateGet(this, _options3).filter ?? {}, __privateGet(this, _options3).event.filter ?? {})
|
|
1044
1247
|
}
|
|
1045
1248
|
};
|
|
1046
1249
|
}
|
|
1047
1250
|
get event() {
|
|
1048
|
-
return __privateGet(this,
|
|
1251
|
+
return __privateGet(this, _options3).event;
|
|
1049
1252
|
}
|
|
1050
1253
|
attachToJob(triggerClient, job) {
|
|
1051
1254
|
}
|
|
@@ -1054,7 +1257,7 @@ var EventTrigger = class {
|
|
|
1054
1257
|
}
|
|
1055
1258
|
};
|
|
1056
1259
|
__name(EventTrigger, "EventTrigger");
|
|
1057
|
-
|
|
1260
|
+
_options3 = new WeakMap();
|
|
1058
1261
|
function eventTrigger(options) {
|
|
1059
1262
|
return new EventTrigger({
|
|
1060
1263
|
name: options.name,
|
|
@@ -1067,7 +1270,11 @@ function eventTrigger(options) {
|
|
|
1067
1270
|
examples: options.examples,
|
|
1068
1271
|
parsePayload: (rawPayload) => {
|
|
1069
1272
|
if (options.schema) {
|
|
1070
|
-
|
|
1273
|
+
const results = options.schema.safeParse(rawPayload);
|
|
1274
|
+
if (!results.success) {
|
|
1275
|
+
throw new ParsedPayloadSchemaError(formatSchemaErrors(results.error.issues));
|
|
1276
|
+
}
|
|
1277
|
+
return results.data;
|
|
1071
1278
|
}
|
|
1072
1279
|
return rawPayload;
|
|
1073
1280
|
}
|
|
@@ -1076,52 +1283,251 @@ function eventTrigger(options) {
|
|
|
1076
1283
|
}
|
|
1077
1284
|
__name(eventTrigger, "eventTrigger");
|
|
1078
1285
|
|
|
1079
|
-
// src/
|
|
1080
|
-
var
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1286
|
+
// src/triggers/scheduled.ts
|
|
1287
|
+
var import_core6 = require("@trigger.dev/core");
|
|
1288
|
+
var import_cronstrue = __toESM(require("cronstrue"));
|
|
1289
|
+
var examples = [
|
|
1290
|
+
{
|
|
1291
|
+
id: "now",
|
|
1292
|
+
name: "Now",
|
|
1293
|
+
icon: "clock",
|
|
1294
|
+
payload: {
|
|
1295
|
+
ts: import_core6.currentDate.marker,
|
|
1296
|
+
lastTimestamp: import_core6.currentDate.marker
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
];
|
|
1300
|
+
var IntervalTrigger = class {
|
|
1301
|
+
constructor(options) {
|
|
1302
|
+
this.options = options;
|
|
1303
|
+
}
|
|
1304
|
+
get event() {
|
|
1305
|
+
return {
|
|
1306
|
+
name: "trigger.scheduled",
|
|
1307
|
+
title: "Schedule",
|
|
1308
|
+
source: "trigger.dev",
|
|
1309
|
+
icon: "schedule-interval",
|
|
1310
|
+
examples,
|
|
1311
|
+
parsePayload: import_core6.ScheduledPayloadSchema.parse,
|
|
1312
|
+
properties: [
|
|
1313
|
+
{
|
|
1314
|
+
label: "Interval",
|
|
1315
|
+
text: `${this.options.seconds}s`
|
|
1316
|
+
}
|
|
1317
|
+
]
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
attachToJob(triggerClient, job) {
|
|
1321
|
+
}
|
|
1322
|
+
get preprocessRuns() {
|
|
1323
|
+
return false;
|
|
1324
|
+
}
|
|
1325
|
+
toJSON() {
|
|
1326
|
+
return {
|
|
1327
|
+
type: "scheduled",
|
|
1328
|
+
schedule: {
|
|
1329
|
+
type: "interval",
|
|
1330
|
+
options: {
|
|
1331
|
+
seconds: this.options.seconds
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1086
1336
|
};
|
|
1087
|
-
|
|
1088
|
-
|
|
1337
|
+
__name(IntervalTrigger, "IntervalTrigger");
|
|
1338
|
+
function intervalTrigger(options) {
|
|
1339
|
+
return new IntervalTrigger(options);
|
|
1340
|
+
}
|
|
1341
|
+
__name(intervalTrigger, "intervalTrigger");
|
|
1342
|
+
var CronTrigger = class {
|
|
1089
1343
|
constructor(options) {
|
|
1090
|
-
|
|
1091
|
-
__privateAdd(this, _executeJob);
|
|
1092
|
-
__privateAdd(this, _createRunContext);
|
|
1093
|
-
__privateAdd(this, _createPreprocessRunContext);
|
|
1094
|
-
__privateAdd(this, _handleHttpSourceRequest);
|
|
1095
|
-
__privateAdd(this, _options3, void 0);
|
|
1096
|
-
__privateAdd(this, _registeredJobs, {});
|
|
1097
|
-
__privateAdd(this, _registeredSources, {});
|
|
1098
|
-
__privateAdd(this, _registeredHttpSourceHandlers, {});
|
|
1099
|
-
__privateAdd(this, _registeredDynamicTriggers, {});
|
|
1100
|
-
__privateAdd(this, _jobMetadataByDynamicTriggers, {});
|
|
1101
|
-
__privateAdd(this, _registeredSchedules, {});
|
|
1102
|
-
__privateAdd(this, _client, void 0);
|
|
1103
|
-
__privateAdd(this, _internalLogger, void 0);
|
|
1104
|
-
this.id = options.id;
|
|
1105
|
-
__privateSet(this, _options3, options);
|
|
1106
|
-
__privateSet(this, _client, new ApiClient(__privateGet(this, _options3)));
|
|
1107
|
-
__privateSet(this, _internalLogger, new import_core5.Logger("trigger.dev", __privateGet(this, _options3).verbose ? "debug" : "log"));
|
|
1344
|
+
this.options = options;
|
|
1108
1345
|
}
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
headers: Object.fromEntries(request.headers.entries()),
|
|
1113
|
-
method: request.method
|
|
1346
|
+
get event() {
|
|
1347
|
+
const humanReadable = import_cronstrue.default.toString(this.options.cron, {
|
|
1348
|
+
throwExceptionOnParseError: false
|
|
1114
1349
|
});
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1350
|
+
return {
|
|
1351
|
+
name: "trigger.scheduled",
|
|
1352
|
+
title: "Cron Schedule",
|
|
1353
|
+
source: "trigger.dev",
|
|
1354
|
+
icon: "schedule-cron",
|
|
1355
|
+
examples,
|
|
1356
|
+
parsePayload: import_core6.ScheduledPayloadSchema.parse,
|
|
1357
|
+
properties: [
|
|
1358
|
+
{
|
|
1359
|
+
label: "cron",
|
|
1360
|
+
text: this.options.cron
|
|
1361
|
+
},
|
|
1362
|
+
{
|
|
1363
|
+
label: "Schedule",
|
|
1364
|
+
text: humanReadable
|
|
1365
|
+
}
|
|
1366
|
+
]
|
|
1367
|
+
};
|
|
1368
|
+
}
|
|
1369
|
+
attachToJob(triggerClient, job) {
|
|
1370
|
+
}
|
|
1371
|
+
get preprocessRuns() {
|
|
1372
|
+
return false;
|
|
1373
|
+
}
|
|
1374
|
+
toJSON() {
|
|
1375
|
+
return {
|
|
1376
|
+
type: "scheduled",
|
|
1377
|
+
schedule: {
|
|
1378
|
+
type: "cron",
|
|
1379
|
+
options: {
|
|
1380
|
+
cron: this.options.cron
|
|
1381
|
+
}
|
|
1120
1382
|
}
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1383
|
+
};
|
|
1384
|
+
}
|
|
1385
|
+
};
|
|
1386
|
+
__name(CronTrigger, "CronTrigger");
|
|
1387
|
+
function cronTrigger(options) {
|
|
1388
|
+
return new CronTrigger(options);
|
|
1389
|
+
}
|
|
1390
|
+
__name(cronTrigger, "cronTrigger");
|
|
1391
|
+
var DynamicSchedule = class {
|
|
1392
|
+
constructor(client, options) {
|
|
1393
|
+
this.client = client;
|
|
1394
|
+
this.options = options;
|
|
1395
|
+
client.attachDynamicSchedule(this.options.id);
|
|
1396
|
+
}
|
|
1397
|
+
get id() {
|
|
1398
|
+
return this.options.id;
|
|
1399
|
+
}
|
|
1400
|
+
get event() {
|
|
1401
|
+
return {
|
|
1402
|
+
name: "trigger.scheduled",
|
|
1403
|
+
title: "Dynamic Schedule",
|
|
1404
|
+
source: "trigger.dev",
|
|
1405
|
+
icon: "schedule-dynamic",
|
|
1406
|
+
examples,
|
|
1407
|
+
parsePayload: import_core6.ScheduledPayloadSchema.parse
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
async register(key, metadata) {
|
|
1411
|
+
const runStore = runLocalStorage.getStore();
|
|
1412
|
+
if (!runStore) {
|
|
1413
|
+
return this.client.registerSchedule(this.id, key, metadata);
|
|
1414
|
+
}
|
|
1415
|
+
const { io } = runStore;
|
|
1416
|
+
return await io.runTask([
|
|
1417
|
+
key,
|
|
1418
|
+
"register"
|
|
1419
|
+
], async (task) => {
|
|
1420
|
+
return this.client.registerSchedule(this.id, key, metadata);
|
|
1421
|
+
}, {
|
|
1422
|
+
name: "Register Schedule",
|
|
1423
|
+
icon: metadata.type === "cron" ? "schedule-cron" : "schedule-interval",
|
|
1424
|
+
properties: [
|
|
1425
|
+
{
|
|
1426
|
+
label: "Dynamic Schedule",
|
|
1427
|
+
text: this.id
|
|
1428
|
+
},
|
|
1429
|
+
{
|
|
1430
|
+
label: "Schedule ID",
|
|
1431
|
+
text: key
|
|
1432
|
+
}
|
|
1433
|
+
],
|
|
1434
|
+
params: metadata
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
async unregister(key) {
|
|
1438
|
+
const runStore = runLocalStorage.getStore();
|
|
1439
|
+
if (!runStore) {
|
|
1440
|
+
return this.client.unregisterSchedule(this.id, key);
|
|
1441
|
+
}
|
|
1442
|
+
const { io } = runStore;
|
|
1443
|
+
return await io.runTask([
|
|
1444
|
+
key,
|
|
1445
|
+
"unregister"
|
|
1446
|
+
], async (task) => {
|
|
1447
|
+
return this.client.unregisterSchedule(this.id, key);
|
|
1448
|
+
}, {
|
|
1449
|
+
name: "Unregister Schedule",
|
|
1450
|
+
icon: "schedule",
|
|
1451
|
+
properties: [
|
|
1452
|
+
{
|
|
1453
|
+
label: "Dynamic Schedule",
|
|
1454
|
+
text: this.id
|
|
1455
|
+
},
|
|
1456
|
+
{
|
|
1457
|
+
label: "Schedule ID",
|
|
1458
|
+
text: key
|
|
1459
|
+
}
|
|
1460
|
+
]
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
attachToJob(triggerClient, job) {
|
|
1464
|
+
triggerClient.attachDynamicScheduleToJob(this.options.id, job);
|
|
1465
|
+
}
|
|
1466
|
+
get preprocessRuns() {
|
|
1467
|
+
return false;
|
|
1468
|
+
}
|
|
1469
|
+
toJSON() {
|
|
1470
|
+
return {
|
|
1471
|
+
type: "dynamic",
|
|
1472
|
+
id: this.options.id
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
};
|
|
1476
|
+
__name(DynamicSchedule, "DynamicSchedule");
|
|
1477
|
+
|
|
1478
|
+
// src/triggerClient.ts
|
|
1479
|
+
var registerSourceEvent = {
|
|
1480
|
+
name: import_core7.REGISTER_SOURCE_EVENT_V2,
|
|
1481
|
+
title: "Register Source",
|
|
1482
|
+
source: "internal",
|
|
1483
|
+
icon: "register-source",
|
|
1484
|
+
parsePayload: import_core7.RegisterSourceEventSchemaV2.parse
|
|
1485
|
+
};
|
|
1486
|
+
var _options4, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _authResolvers, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn;
|
|
1487
|
+
var TriggerClient = class {
|
|
1488
|
+
constructor(options) {
|
|
1489
|
+
__privateAdd(this, _preprocessRun);
|
|
1490
|
+
__privateAdd(this, _executeJob);
|
|
1491
|
+
__privateAdd(this, _createRunContext);
|
|
1492
|
+
__privateAdd(this, _createPreprocessRunContext);
|
|
1493
|
+
__privateAdd(this, _handleHttpSourceRequest);
|
|
1494
|
+
__privateAdd(this, _resolveConnections);
|
|
1495
|
+
__privateAdd(this, _resolveConnection);
|
|
1496
|
+
__privateAdd(this, _buildJobsIndex);
|
|
1497
|
+
__privateAdd(this, _buildJobIndex);
|
|
1498
|
+
__privateAdd(this, _buildJobIntegrations);
|
|
1499
|
+
__privateAdd(this, _buildJobIntegration);
|
|
1500
|
+
__privateAdd(this, _options4, void 0);
|
|
1501
|
+
__privateAdd(this, _registeredJobs, {});
|
|
1502
|
+
__privateAdd(this, _registeredSources, {});
|
|
1503
|
+
__privateAdd(this, _registeredHttpSourceHandlers, {});
|
|
1504
|
+
__privateAdd(this, _registeredDynamicTriggers, {});
|
|
1505
|
+
__privateAdd(this, _jobMetadataByDynamicTriggers, {});
|
|
1506
|
+
__privateAdd(this, _registeredSchedules, {});
|
|
1507
|
+
__privateAdd(this, _authResolvers, {});
|
|
1508
|
+
__privateAdd(this, _client2, void 0);
|
|
1509
|
+
__privateAdd(this, _internalLogger, void 0);
|
|
1510
|
+
this.id = options.id;
|
|
1511
|
+
__privateSet(this, _options4, options);
|
|
1512
|
+
__privateSet(this, _client2, new ApiClient(__privateGet(this, _options4)));
|
|
1513
|
+
__privateSet(this, _internalLogger, new import_core7.Logger("trigger.dev", __privateGet(this, _options4).verbose ? "debug" : "log"));
|
|
1514
|
+
}
|
|
1515
|
+
async handleRequest(request) {
|
|
1516
|
+
__privateGet(this, _internalLogger).debug("handling request", {
|
|
1517
|
+
url: request.url,
|
|
1518
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
1519
|
+
method: request.method
|
|
1520
|
+
});
|
|
1521
|
+
const apiKey = request.headers.get("x-trigger-api-key");
|
|
1522
|
+
const authorization = this.authorized(apiKey);
|
|
1523
|
+
switch (authorization) {
|
|
1524
|
+
case "authorized": {
|
|
1525
|
+
break;
|
|
1526
|
+
}
|
|
1527
|
+
case "missing-client": {
|
|
1528
|
+
return {
|
|
1529
|
+
status: 401,
|
|
1530
|
+
body: {
|
|
1125
1531
|
message: "Unauthorized: client missing apiKey"
|
|
1126
1532
|
}
|
|
1127
1533
|
};
|
|
@@ -1189,24 +1595,8 @@ var TriggerClient = class {
|
|
|
1189
1595
|
};
|
|
1190
1596
|
}
|
|
1191
1597
|
case "INDEX_ENDPOINT": {
|
|
1192
|
-
const jobId = request.headers.get("x-trigger-job-id");
|
|
1193
|
-
if (jobId) {
|
|
1194
|
-
const job = __privateGet(this, _registeredJobs)[jobId];
|
|
1195
|
-
if (!job) {
|
|
1196
|
-
return {
|
|
1197
|
-
status: 404,
|
|
1198
|
-
body: {
|
|
1199
|
-
message: "Job not found"
|
|
1200
|
-
}
|
|
1201
|
-
};
|
|
1202
|
-
}
|
|
1203
|
-
return {
|
|
1204
|
-
status: 200,
|
|
1205
|
-
body: job.toJSON()
|
|
1206
|
-
};
|
|
1207
|
-
}
|
|
1208
1598
|
const body = {
|
|
1209
|
-
jobs:
|
|
1599
|
+
jobs: __privateMethod(this, _buildJobsIndex, buildJobsIndex_fn).call(this),
|
|
1210
1600
|
sources: Object.values(__privateGet(this, _registeredSources)),
|
|
1211
1601
|
dynamicTriggers: Object.values(__privateGet(this, _registeredDynamicTriggers)).map((trigger) => ({
|
|
1212
1602
|
id: trigger.id,
|
|
@@ -1228,7 +1618,7 @@ var TriggerClient = class {
|
|
|
1228
1618
|
}
|
|
1229
1619
|
case "INITIALIZE_TRIGGER": {
|
|
1230
1620
|
const json = await request.json();
|
|
1231
|
-
const body =
|
|
1621
|
+
const body = import_core7.InitializeTriggerBodySchema.safeParse(json);
|
|
1232
1622
|
if (!body.success) {
|
|
1233
1623
|
return {
|
|
1234
1624
|
status: 400,
|
|
@@ -1253,7 +1643,7 @@ var TriggerClient = class {
|
|
|
1253
1643
|
}
|
|
1254
1644
|
case "EXECUTE_JOB": {
|
|
1255
1645
|
const json = await request.json();
|
|
1256
|
-
const execution =
|
|
1646
|
+
const execution = import_core7.RunJobBodySchema.safeParse(json);
|
|
1257
1647
|
if (!execution.success) {
|
|
1258
1648
|
return {
|
|
1259
1649
|
status: 400,
|
|
@@ -1279,7 +1669,7 @@ var TriggerClient = class {
|
|
|
1279
1669
|
}
|
|
1280
1670
|
case "PREPROCESS_RUN": {
|
|
1281
1671
|
const json = await request.json();
|
|
1282
|
-
const body =
|
|
1672
|
+
const body = import_core7.PreprocessRunBodySchema.safeParse(json);
|
|
1283
1673
|
if (!body.success) {
|
|
1284
1674
|
return {
|
|
1285
1675
|
status: 400,
|
|
@@ -1307,7 +1697,7 @@ var TriggerClient = class {
|
|
|
1307
1697
|
};
|
|
1308
1698
|
}
|
|
1309
1699
|
case "DELIVER_HTTP_SOURCE_REQUEST": {
|
|
1310
|
-
const headers =
|
|
1700
|
+
const headers = import_core7.HttpSourceRequestHeadersSchema.safeParse(Object.fromEntries(request.headers.entries()));
|
|
1311
1701
|
if (!headers.success) {
|
|
1312
1702
|
return {
|
|
1313
1703
|
status: 400,
|
|
@@ -1372,13 +1762,26 @@ var TriggerClient = class {
|
|
|
1372
1762
|
}
|
|
1373
1763
|
};
|
|
1374
1764
|
}
|
|
1765
|
+
defineJob(options) {
|
|
1766
|
+
return new Job(this, options);
|
|
1767
|
+
}
|
|
1768
|
+
defineAuthResolver(integration, resolver) {
|
|
1769
|
+
__privateGet(this, _authResolvers)[integration.id] = resolver;
|
|
1770
|
+
return this;
|
|
1771
|
+
}
|
|
1772
|
+
defineDynamicSchedule(options) {
|
|
1773
|
+
return new DynamicSchedule(this, options);
|
|
1774
|
+
}
|
|
1775
|
+
defineDynamicTrigger(options) {
|
|
1776
|
+
return new DynamicTrigger(this, options);
|
|
1777
|
+
}
|
|
1375
1778
|
attach(job) {
|
|
1376
1779
|
__privateGet(this, _registeredJobs)[job.id] = job;
|
|
1377
1780
|
job.trigger.attachToJob(this, job);
|
|
1378
1781
|
}
|
|
1379
1782
|
attachDynamicTrigger(trigger) {
|
|
1380
1783
|
__privateGet(this, _registeredDynamicTriggers)[trigger.id] = trigger;
|
|
1381
|
-
|
|
1784
|
+
this.defineJob({
|
|
1382
1785
|
id: dynamicTriggerRegisterSourceJobId(trigger.id),
|
|
1383
1786
|
name: `Register dynamic trigger ${trigger.id}`,
|
|
1384
1787
|
version: trigger.source.version,
|
|
@@ -1474,7 +1877,11 @@ var TriggerClient = class {
|
|
|
1474
1877
|
__internal: true
|
|
1475
1878
|
});
|
|
1476
1879
|
}
|
|
1477
|
-
attachDynamicSchedule(key
|
|
1880
|
+
attachDynamicSchedule(key) {
|
|
1881
|
+
const jobs = __privateGet(this, _registeredSchedules)[key] ?? [];
|
|
1882
|
+
__privateGet(this, _registeredSchedules)[key] = jobs;
|
|
1883
|
+
}
|
|
1884
|
+
attachDynamicScheduleToJob(key, job) {
|
|
1478
1885
|
const jobs = __privateGet(this, _registeredSchedules)[key] ?? [];
|
|
1479
1886
|
jobs.push({
|
|
1480
1887
|
id: job.id,
|
|
@@ -1483,58 +1890,62 @@ var TriggerClient = class {
|
|
|
1483
1890
|
__privateGet(this, _registeredSchedules)[key] = jobs;
|
|
1484
1891
|
}
|
|
1485
1892
|
async registerTrigger(id, key, options) {
|
|
1486
|
-
return __privateGet(this,
|
|
1893
|
+
return __privateGet(this, _client2).registerTrigger(this.id, id, key, options);
|
|
1487
1894
|
}
|
|
1488
1895
|
async getAuth(id) {
|
|
1489
|
-
return __privateGet(this,
|
|
1896
|
+
return __privateGet(this, _client2).getAuth(this.id, id);
|
|
1490
1897
|
}
|
|
1491
1898
|
async sendEvent(event, options) {
|
|
1492
|
-
return __privateGet(this,
|
|
1899
|
+
return __privateGet(this, _client2).sendEvent(event, options);
|
|
1493
1900
|
}
|
|
1494
1901
|
async cancelEvent(eventId) {
|
|
1495
|
-
return __privateGet(this,
|
|
1902
|
+
return __privateGet(this, _client2).cancelEvent(eventId);
|
|
1903
|
+
}
|
|
1904
|
+
async updateStatus(runId, id, status) {
|
|
1905
|
+
return __privateGet(this, _client2).updateStatus(runId, id, status);
|
|
1496
1906
|
}
|
|
1497
1907
|
async registerSchedule(id, key, schedule) {
|
|
1498
|
-
return __privateGet(this,
|
|
1908
|
+
return __privateGet(this, _client2).registerSchedule(this.id, id, key, schedule);
|
|
1499
1909
|
}
|
|
1500
1910
|
async unregisterSchedule(id, key) {
|
|
1501
|
-
return __privateGet(this,
|
|
1911
|
+
return __privateGet(this, _client2).unregisterSchedule(this.id, id, key);
|
|
1502
1912
|
}
|
|
1503
1913
|
async getEvent(eventId) {
|
|
1504
|
-
return __privateGet(this,
|
|
1914
|
+
return __privateGet(this, _client2).getEvent(eventId);
|
|
1505
1915
|
}
|
|
1506
1916
|
async getRun(runId, options) {
|
|
1507
|
-
return __privateGet(this,
|
|
1917
|
+
return __privateGet(this, _client2).getRun(runId, options);
|
|
1508
1918
|
}
|
|
1509
1919
|
async getRuns(jobSlug, options) {
|
|
1510
|
-
return __privateGet(this,
|
|
1920
|
+
return __privateGet(this, _client2).getRuns(jobSlug, options);
|
|
1921
|
+
}
|
|
1922
|
+
async getRunStatuses(runId) {
|
|
1923
|
+
return __privateGet(this, _client2).getRunStatuses(runId);
|
|
1511
1924
|
}
|
|
1512
1925
|
authorized(apiKey) {
|
|
1513
1926
|
if (typeof apiKey !== "string") {
|
|
1514
1927
|
return "missing-header";
|
|
1515
1928
|
}
|
|
1516
|
-
const localApiKey = __privateGet(this,
|
|
1929
|
+
const localApiKey = __privateGet(this, _options4).apiKey ?? process.env.TRIGGER_API_KEY;
|
|
1517
1930
|
if (!localApiKey) {
|
|
1518
1931
|
return "missing-client";
|
|
1519
1932
|
}
|
|
1520
1933
|
return apiKey === localApiKey ? "authorized" : "unauthorized";
|
|
1521
1934
|
}
|
|
1522
1935
|
apiKey() {
|
|
1523
|
-
return __privateGet(this,
|
|
1524
|
-
}
|
|
1525
|
-
defineJob(options) {
|
|
1526
|
-
return new Job(this, options);
|
|
1936
|
+
return __privateGet(this, _options4).apiKey ?? process.env.TRIGGER_API_KEY;
|
|
1527
1937
|
}
|
|
1528
1938
|
};
|
|
1529
1939
|
__name(TriggerClient, "TriggerClient");
|
|
1530
|
-
|
|
1940
|
+
_options4 = new WeakMap();
|
|
1531
1941
|
_registeredJobs = new WeakMap();
|
|
1532
1942
|
_registeredSources = new WeakMap();
|
|
1533
1943
|
_registeredHttpSourceHandlers = new WeakMap();
|
|
1534
1944
|
_registeredDynamicTriggers = new WeakMap();
|
|
1535
1945
|
_jobMetadataByDynamicTriggers = new WeakMap();
|
|
1536
1946
|
_registeredSchedules = new WeakMap();
|
|
1537
|
-
|
|
1947
|
+
_authResolvers = new WeakMap();
|
|
1948
|
+
_client2 = new WeakMap();
|
|
1538
1949
|
_internalLogger = new WeakMap();
|
|
1539
1950
|
_preprocessRun = new WeakSet();
|
|
1540
1951
|
preprocessRun_fn = /* @__PURE__ */ __name(async function(body, job) {
|
|
@@ -1550,27 +1961,46 @@ _executeJob = new WeakSet();
|
|
|
1550
1961
|
executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
1551
1962
|
__privateGet(this, _internalLogger).debug("executing job", {
|
|
1552
1963
|
execution: body1,
|
|
1553
|
-
job: job1.
|
|
1964
|
+
job: job1.id,
|
|
1965
|
+
version: job1.version
|
|
1554
1966
|
});
|
|
1555
1967
|
const context = __privateMethod(this, _createRunContext, createRunContext_fn).call(this, body1);
|
|
1556
1968
|
const io = new IO({
|
|
1557
1969
|
id: body1.run.id,
|
|
1558
1970
|
cachedTasks: body1.tasks,
|
|
1559
|
-
apiClient: __privateGet(this,
|
|
1971
|
+
apiClient: __privateGet(this, _client2),
|
|
1560
1972
|
logger: __privateGet(this, _internalLogger),
|
|
1561
1973
|
client: this,
|
|
1562
1974
|
context,
|
|
1563
|
-
jobLogLevel: job1.logLevel ?? __privateGet(this,
|
|
1564
|
-
jobLogger: __privateGet(this,
|
|
1975
|
+
jobLogLevel: job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info",
|
|
1976
|
+
jobLogger: __privateGet(this, _options4).ioLogLocalEnabled ? new import_core7.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info") : void 0
|
|
1565
1977
|
});
|
|
1566
|
-
const
|
|
1978
|
+
const resolvedConnections = await __privateMethod(this, _resolveConnections, resolveConnections_fn).call(this, context, job1.options.integrations, body1.connections);
|
|
1979
|
+
if (!resolvedConnections.ok) {
|
|
1980
|
+
return {
|
|
1981
|
+
status: "UNRESOLVED_AUTH_ERROR",
|
|
1982
|
+
issues: resolvedConnections.issues
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
const ioWithConnections = createIOWithIntegrations(io, resolvedConnections.data, job1.options.integrations);
|
|
1567
1986
|
try {
|
|
1568
|
-
const output = await
|
|
1987
|
+
const output = await runLocalStorage.runWith({
|
|
1988
|
+
io,
|
|
1989
|
+
ctx: context
|
|
1990
|
+
}, () => {
|
|
1991
|
+
return job1.options.run(job1.trigger.event.parsePayload(body1.event.payload ?? {}), ioWithConnections, context);
|
|
1992
|
+
});
|
|
1569
1993
|
return {
|
|
1570
1994
|
status: "SUCCESS",
|
|
1571
1995
|
output
|
|
1572
1996
|
};
|
|
1573
1997
|
} catch (error) {
|
|
1998
|
+
if (error instanceof ParsedPayloadSchemaError) {
|
|
1999
|
+
return {
|
|
2000
|
+
status: "INVALID_PAYLOAD",
|
|
2001
|
+
errors: error.schemaErrors
|
|
2002
|
+
};
|
|
2003
|
+
}
|
|
1574
2004
|
if (error instanceof ResumeWithTaskError) {
|
|
1575
2005
|
return {
|
|
1576
2006
|
status: "RESUME_WITH_TASK",
|
|
@@ -1592,7 +2022,7 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
|
1592
2022
|
};
|
|
1593
2023
|
}
|
|
1594
2024
|
if (error instanceof RetryWithTaskError) {
|
|
1595
|
-
const errorWithStack2 =
|
|
2025
|
+
const errorWithStack2 = import_core7.ErrorWithStackSchema.safeParse(error.cause);
|
|
1596
2026
|
if (errorWithStack2.success) {
|
|
1597
2027
|
return {
|
|
1598
2028
|
status: "ERROR",
|
|
@@ -1608,7 +2038,7 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
|
1608
2038
|
task: error.task
|
|
1609
2039
|
};
|
|
1610
2040
|
}
|
|
1611
|
-
const errorWithStack =
|
|
2041
|
+
const errorWithStack = import_core7.ErrorWithStackSchema.safeParse(error);
|
|
1612
2042
|
if (errorWithStack.success) {
|
|
1613
2043
|
return {
|
|
1614
2044
|
status: "ERROR",
|
|
@@ -1740,6 +2170,154 @@ handleHttpSourceRequest_fn = /* @__PURE__ */ __name(async function(source, sourc
|
|
|
1740
2170
|
metadata: results.metadata
|
|
1741
2171
|
};
|
|
1742
2172
|
}, "#handleHttpSourceRequest");
|
|
2173
|
+
_resolveConnections = new WeakSet();
|
|
2174
|
+
resolveConnections_fn = /* @__PURE__ */ __name(async function(ctx, integrations, connections) {
|
|
2175
|
+
if (!integrations) {
|
|
2176
|
+
return {
|
|
2177
|
+
ok: true,
|
|
2178
|
+
data: {}
|
|
2179
|
+
};
|
|
2180
|
+
}
|
|
2181
|
+
const resolvedAuthResults = await Promise.all(Object.keys(integrations).map(async (key) => {
|
|
2182
|
+
const integration = integrations[key];
|
|
2183
|
+
const auth = (connections ?? {})[key];
|
|
2184
|
+
const result = await __privateMethod(this, _resolveConnection, resolveConnection_fn).call(this, ctx, integration, auth);
|
|
2185
|
+
if (result.ok) {
|
|
2186
|
+
return {
|
|
2187
|
+
ok: true,
|
|
2188
|
+
auth: result.auth,
|
|
2189
|
+
key
|
|
2190
|
+
};
|
|
2191
|
+
} else {
|
|
2192
|
+
return {
|
|
2193
|
+
ok: false,
|
|
2194
|
+
error: result.error,
|
|
2195
|
+
key
|
|
2196
|
+
};
|
|
2197
|
+
}
|
|
2198
|
+
}));
|
|
2199
|
+
const allResolved = resolvedAuthResults.every((result) => result.ok);
|
|
2200
|
+
if (allResolved) {
|
|
2201
|
+
return {
|
|
2202
|
+
ok: true,
|
|
2203
|
+
data: resolvedAuthResults.reduce((acc, result) => {
|
|
2204
|
+
acc[result.key] = result.auth;
|
|
2205
|
+
return acc;
|
|
2206
|
+
}, {})
|
|
2207
|
+
};
|
|
2208
|
+
} else {
|
|
2209
|
+
return {
|
|
2210
|
+
ok: false,
|
|
2211
|
+
issues: resolvedAuthResults.reduce((acc, result) => {
|
|
2212
|
+
if (result.ok) {
|
|
2213
|
+
return acc;
|
|
2214
|
+
}
|
|
2215
|
+
const integration = integrations[result.key];
|
|
2216
|
+
acc[result.key] = {
|
|
2217
|
+
id: integration.id,
|
|
2218
|
+
error: result.error
|
|
2219
|
+
};
|
|
2220
|
+
return acc;
|
|
2221
|
+
}, {})
|
|
2222
|
+
};
|
|
2223
|
+
}
|
|
2224
|
+
}, "#resolveConnections");
|
|
2225
|
+
_resolveConnection = new WeakSet();
|
|
2226
|
+
resolveConnection_fn = /* @__PURE__ */ __name(async function(ctx1, integration, auth) {
|
|
2227
|
+
if (auth) {
|
|
2228
|
+
return {
|
|
2229
|
+
ok: true,
|
|
2230
|
+
auth
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
2233
|
+
const authResolver = __privateGet(this, _authResolvers)[integration.id];
|
|
2234
|
+
if (!authResolver) {
|
|
2235
|
+
if (integration.authSource === "HOSTED") {
|
|
2236
|
+
return {
|
|
2237
|
+
ok: false,
|
|
2238
|
+
error: `Something went wrong: Integration ${integration.id} is missing auth credentials from Trigger.dev`
|
|
2239
|
+
};
|
|
2240
|
+
}
|
|
2241
|
+
return {
|
|
2242
|
+
ok: true,
|
|
2243
|
+
auth: void 0
|
|
2244
|
+
};
|
|
2245
|
+
}
|
|
2246
|
+
try {
|
|
2247
|
+
const resolvedAuth = await authResolver(ctx1, integration);
|
|
2248
|
+
if (!resolvedAuth) {
|
|
2249
|
+
return {
|
|
2250
|
+
ok: false,
|
|
2251
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver returned null or undefined`
|
|
2252
|
+
};
|
|
2253
|
+
}
|
|
2254
|
+
return {
|
|
2255
|
+
ok: true,
|
|
2256
|
+
auth: resolvedAuth.type === "apiKey" ? {
|
|
2257
|
+
type: "apiKey",
|
|
2258
|
+
accessToken: resolvedAuth.token,
|
|
2259
|
+
additionalFields: resolvedAuth.additionalFields
|
|
2260
|
+
} : {
|
|
2261
|
+
type: "oauth2",
|
|
2262
|
+
accessToken: resolvedAuth.token,
|
|
2263
|
+
additionalFields: resolvedAuth.additionalFields
|
|
2264
|
+
}
|
|
2265
|
+
};
|
|
2266
|
+
} catch (resolverError) {
|
|
2267
|
+
if (resolverError instanceof Error) {
|
|
2268
|
+
return {
|
|
2269
|
+
ok: false,
|
|
2270
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver threw. ${resolverError.name}: ${resolverError.message}`
|
|
2271
|
+
};
|
|
2272
|
+
} else if (typeof resolverError === "string") {
|
|
2273
|
+
return {
|
|
2274
|
+
ok: false,
|
|
2275
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver threw an error: ${resolverError}`
|
|
2276
|
+
};
|
|
2277
|
+
}
|
|
2278
|
+
return {
|
|
2279
|
+
ok: false,
|
|
2280
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver threw an unknown error: ${JSON.stringify(resolverError)}`
|
|
2281
|
+
};
|
|
2282
|
+
}
|
|
2283
|
+
}, "#resolveConnection");
|
|
2284
|
+
_buildJobsIndex = new WeakSet();
|
|
2285
|
+
buildJobsIndex_fn = /* @__PURE__ */ __name(function() {
|
|
2286
|
+
return Object.values(__privateGet(this, _registeredJobs)).map((job) => __privateMethod(this, _buildJobIndex, buildJobIndex_fn).call(this, job));
|
|
2287
|
+
}, "#buildJobsIndex");
|
|
2288
|
+
_buildJobIndex = new WeakSet();
|
|
2289
|
+
buildJobIndex_fn = /* @__PURE__ */ __name(function(job2) {
|
|
2290
|
+
const internal = job2.options.__internal;
|
|
2291
|
+
return {
|
|
2292
|
+
id: job2.id,
|
|
2293
|
+
name: job2.name,
|
|
2294
|
+
version: job2.version,
|
|
2295
|
+
event: job2.trigger.event,
|
|
2296
|
+
trigger: job2.trigger.toJSON(),
|
|
2297
|
+
integrations: __privateMethod(this, _buildJobIntegrations, buildJobIntegrations_fn).call(this, job2),
|
|
2298
|
+
startPosition: "latest",
|
|
2299
|
+
enabled: job2.enabled,
|
|
2300
|
+
preprocessRuns: job2.trigger.preprocessRuns,
|
|
2301
|
+
internal
|
|
2302
|
+
};
|
|
2303
|
+
}, "#buildJobIndex");
|
|
2304
|
+
_buildJobIntegrations = new WeakSet();
|
|
2305
|
+
buildJobIntegrations_fn = /* @__PURE__ */ __name(function(job3) {
|
|
2306
|
+
return Object.keys(job3.options.integrations ?? {}).reduce((acc, key) => {
|
|
2307
|
+
const integration = job3.options.integrations[key];
|
|
2308
|
+
acc[key] = __privateMethod(this, _buildJobIntegration, buildJobIntegration_fn).call(this, integration);
|
|
2309
|
+
return acc;
|
|
2310
|
+
}, {});
|
|
2311
|
+
}, "#buildJobIntegrations");
|
|
2312
|
+
_buildJobIntegration = new WeakSet();
|
|
2313
|
+
buildJobIntegration_fn = /* @__PURE__ */ __name(function(integration1) {
|
|
2314
|
+
const authSource = __privateGet(this, _authResolvers)[integration1.id] ? "RESOLVER" : integration1.authSource;
|
|
2315
|
+
return {
|
|
2316
|
+
id: integration1.id,
|
|
2317
|
+
metadata: integration1.metadata,
|
|
2318
|
+
authSource
|
|
2319
|
+
};
|
|
2320
|
+
}, "#buildJobIntegration");
|
|
1743
2321
|
function dynamicTriggerRegisterSourceJobId(id) {
|
|
1744
2322
|
return `register-dynamic-trigger-${id}`;
|
|
1745
2323
|
}
|
|
@@ -1765,7 +2343,7 @@ function deepMergeOptions(obj1, obj2) {
|
|
|
1765
2343
|
__name(deepMergeOptions, "deepMergeOptions");
|
|
1766
2344
|
|
|
1767
2345
|
// src/triggers/externalSource.ts
|
|
1768
|
-
var
|
|
2346
|
+
var import_core8 = require("@trigger.dev/core");
|
|
1769
2347
|
var ExternalSource = class {
|
|
1770
2348
|
constructor(channel, options) {
|
|
1771
2349
|
this.options = options;
|
|
@@ -1839,7 +2417,7 @@ var ExternalSourceTrigger = class {
|
|
|
1839
2417
|
title: "External Source",
|
|
1840
2418
|
rule: {
|
|
1841
2419
|
event: this.event.name,
|
|
1842
|
-
payload: (0,
|
|
2420
|
+
payload: (0, import_core8.deepMergeFilters)(this.options.source.filter(this.options.params, this.options.options), this.event.filter ?? {}, this.options.params.filter ?? {}),
|
|
1843
2421
|
source: this.event.source
|
|
1844
2422
|
},
|
|
1845
2423
|
properties: this.options.source.properties(this.options.params)
|
|
@@ -1873,214 +2451,6 @@ function omit(obj, key) {
|
|
|
1873
2451
|
}
|
|
1874
2452
|
__name(omit, "omit");
|
|
1875
2453
|
|
|
1876
|
-
// src/triggers/dynamic.ts
|
|
1877
|
-
var import_core7 = require("@trigger.dev/core");
|
|
1878
|
-
var _client2, _options4;
|
|
1879
|
-
var DynamicTrigger = class {
|
|
1880
|
-
constructor(client, options) {
|
|
1881
|
-
__privateAdd(this, _client2, void 0);
|
|
1882
|
-
__privateAdd(this, _options4, void 0);
|
|
1883
|
-
__privateSet(this, _client2, client);
|
|
1884
|
-
__privateSet(this, _options4, options);
|
|
1885
|
-
this.source = options.source;
|
|
1886
|
-
client.attachDynamicTrigger(this);
|
|
1887
|
-
}
|
|
1888
|
-
toJSON() {
|
|
1889
|
-
return {
|
|
1890
|
-
type: "dynamic",
|
|
1891
|
-
id: __privateGet(this, _options4).id
|
|
1892
|
-
};
|
|
1893
|
-
}
|
|
1894
|
-
get id() {
|
|
1895
|
-
return __privateGet(this, _options4).id;
|
|
1896
|
-
}
|
|
1897
|
-
get event() {
|
|
1898
|
-
return __privateGet(this, _options4).event;
|
|
1899
|
-
}
|
|
1900
|
-
registeredTriggerForParams(params) {
|
|
1901
|
-
const key = slugifyId(this.source.key(params));
|
|
1902
|
-
return {
|
|
1903
|
-
rule: {
|
|
1904
|
-
event: this.event.name,
|
|
1905
|
-
source: this.event.source,
|
|
1906
|
-
payload: (0, import_core7.deepMergeFilters)(this.source.filter(params), this.event.filter ?? {})
|
|
1907
|
-
},
|
|
1908
|
-
source: {
|
|
1909
|
-
version: "2",
|
|
1910
|
-
key,
|
|
1911
|
-
channel: this.source.channel,
|
|
1912
|
-
params,
|
|
1913
|
-
options: {
|
|
1914
|
-
event: typeof this.event.name === "string" ? [
|
|
1915
|
-
this.event.name
|
|
1916
|
-
] : this.event.name
|
|
1917
|
-
},
|
|
1918
|
-
integration: {
|
|
1919
|
-
id: this.source.integration.id,
|
|
1920
|
-
metadata: this.source.integration.metadata,
|
|
1921
|
-
authSource: this.source.integration.authSource
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
};
|
|
1925
|
-
}
|
|
1926
|
-
async register(key, params) {
|
|
1927
|
-
return __privateGet(this, _client2).registerTrigger(this.id, key, this.registeredTriggerForParams(params));
|
|
1928
|
-
}
|
|
1929
|
-
attachToJob(triggerClient, job) {
|
|
1930
|
-
triggerClient.attachJobToDynamicTrigger(job, this);
|
|
1931
|
-
}
|
|
1932
|
-
get preprocessRuns() {
|
|
1933
|
-
return true;
|
|
1934
|
-
}
|
|
1935
|
-
};
|
|
1936
|
-
__name(DynamicTrigger, "DynamicTrigger");
|
|
1937
|
-
_client2 = new WeakMap();
|
|
1938
|
-
_options4 = new WeakMap();
|
|
1939
|
-
|
|
1940
|
-
// src/triggers/scheduled.ts
|
|
1941
|
-
var import_core8 = require("@trigger.dev/core");
|
|
1942
|
-
var import_cronstrue = __toESM(require("cronstrue"));
|
|
1943
|
-
var examples = [
|
|
1944
|
-
{
|
|
1945
|
-
id: "now",
|
|
1946
|
-
name: "Now",
|
|
1947
|
-
icon: "clock",
|
|
1948
|
-
payload: {
|
|
1949
|
-
ts: import_core8.currentDate.marker,
|
|
1950
|
-
lastTimestamp: import_core8.currentDate.marker
|
|
1951
|
-
}
|
|
1952
|
-
}
|
|
1953
|
-
];
|
|
1954
|
-
var IntervalTrigger = class {
|
|
1955
|
-
constructor(options) {
|
|
1956
|
-
this.options = options;
|
|
1957
|
-
}
|
|
1958
|
-
get event() {
|
|
1959
|
-
return {
|
|
1960
|
-
name: "trigger.scheduled",
|
|
1961
|
-
title: "Schedule",
|
|
1962
|
-
source: "trigger.dev",
|
|
1963
|
-
icon: "schedule-interval",
|
|
1964
|
-
examples,
|
|
1965
|
-
parsePayload: import_core8.ScheduledPayloadSchema.parse,
|
|
1966
|
-
properties: [
|
|
1967
|
-
{
|
|
1968
|
-
label: "Interval",
|
|
1969
|
-
text: `${this.options.seconds}s`
|
|
1970
|
-
}
|
|
1971
|
-
]
|
|
1972
|
-
};
|
|
1973
|
-
}
|
|
1974
|
-
attachToJob(triggerClient, job) {
|
|
1975
|
-
}
|
|
1976
|
-
get preprocessRuns() {
|
|
1977
|
-
return false;
|
|
1978
|
-
}
|
|
1979
|
-
toJSON() {
|
|
1980
|
-
return {
|
|
1981
|
-
type: "scheduled",
|
|
1982
|
-
schedule: {
|
|
1983
|
-
type: "interval",
|
|
1984
|
-
options: {
|
|
1985
|
-
seconds: this.options.seconds
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
|
-
};
|
|
1989
|
-
}
|
|
1990
|
-
};
|
|
1991
|
-
__name(IntervalTrigger, "IntervalTrigger");
|
|
1992
|
-
function intervalTrigger(options) {
|
|
1993
|
-
return new IntervalTrigger(options);
|
|
1994
|
-
}
|
|
1995
|
-
__name(intervalTrigger, "intervalTrigger");
|
|
1996
|
-
var CronTrigger = class {
|
|
1997
|
-
constructor(options) {
|
|
1998
|
-
this.options = options;
|
|
1999
|
-
}
|
|
2000
|
-
get event() {
|
|
2001
|
-
const humanReadable = import_cronstrue.default.toString(this.options.cron, {
|
|
2002
|
-
throwExceptionOnParseError: false
|
|
2003
|
-
});
|
|
2004
|
-
return {
|
|
2005
|
-
name: "trigger.scheduled",
|
|
2006
|
-
title: "Cron Schedule",
|
|
2007
|
-
source: "trigger.dev",
|
|
2008
|
-
icon: "schedule-cron",
|
|
2009
|
-
examples,
|
|
2010
|
-
parsePayload: import_core8.ScheduledPayloadSchema.parse,
|
|
2011
|
-
properties: [
|
|
2012
|
-
{
|
|
2013
|
-
label: "cron",
|
|
2014
|
-
text: this.options.cron
|
|
2015
|
-
},
|
|
2016
|
-
{
|
|
2017
|
-
label: "Schedule",
|
|
2018
|
-
text: humanReadable
|
|
2019
|
-
}
|
|
2020
|
-
]
|
|
2021
|
-
};
|
|
2022
|
-
}
|
|
2023
|
-
attachToJob(triggerClient, job) {
|
|
2024
|
-
}
|
|
2025
|
-
get preprocessRuns() {
|
|
2026
|
-
return false;
|
|
2027
|
-
}
|
|
2028
|
-
toJSON() {
|
|
2029
|
-
return {
|
|
2030
|
-
type: "scheduled",
|
|
2031
|
-
schedule: {
|
|
2032
|
-
type: "cron",
|
|
2033
|
-
options: {
|
|
2034
|
-
cron: this.options.cron
|
|
2035
|
-
}
|
|
2036
|
-
}
|
|
2037
|
-
};
|
|
2038
|
-
}
|
|
2039
|
-
};
|
|
2040
|
-
__name(CronTrigger, "CronTrigger");
|
|
2041
|
-
function cronTrigger(options) {
|
|
2042
|
-
return new CronTrigger(options);
|
|
2043
|
-
}
|
|
2044
|
-
__name(cronTrigger, "cronTrigger");
|
|
2045
|
-
var DynamicSchedule = class {
|
|
2046
|
-
constructor(client, options) {
|
|
2047
|
-
this.client = client;
|
|
2048
|
-
this.options = options;
|
|
2049
|
-
}
|
|
2050
|
-
get id() {
|
|
2051
|
-
return this.options.id;
|
|
2052
|
-
}
|
|
2053
|
-
get event() {
|
|
2054
|
-
return {
|
|
2055
|
-
name: "trigger.scheduled",
|
|
2056
|
-
title: "Dynamic Schedule",
|
|
2057
|
-
source: "trigger.dev",
|
|
2058
|
-
icon: "schedule-dynamic",
|
|
2059
|
-
examples,
|
|
2060
|
-
parsePayload: import_core8.ScheduledPayloadSchema.parse
|
|
2061
|
-
};
|
|
2062
|
-
}
|
|
2063
|
-
async register(key, metadata) {
|
|
2064
|
-
return this.client.registerSchedule(this.id, key, metadata);
|
|
2065
|
-
}
|
|
2066
|
-
async unregister(key) {
|
|
2067
|
-
return this.client.unregisterSchedule(this.id, key);
|
|
2068
|
-
}
|
|
2069
|
-
attachToJob(triggerClient, job) {
|
|
2070
|
-
triggerClient.attachDynamicSchedule(this.options.id, job);
|
|
2071
|
-
}
|
|
2072
|
-
get preprocessRuns() {
|
|
2073
|
-
return false;
|
|
2074
|
-
}
|
|
2075
|
-
toJSON() {
|
|
2076
|
-
return {
|
|
2077
|
-
type: "dynamic",
|
|
2078
|
-
id: this.options.id
|
|
2079
|
-
};
|
|
2080
|
-
}
|
|
2081
|
-
};
|
|
2082
|
-
__name(DynamicSchedule, "DynamicSchedule");
|
|
2083
|
-
|
|
2084
2454
|
// src/triggers/notifications.ts
|
|
2085
2455
|
var import_core9 = require("@trigger.dev/core");
|
|
2086
2456
|
function missingConnectionNotification(integrations) {
|