@trigger.dev/sdk 0.0.0-background-tasks-20230906212613 → 0.0.0-byo-auth-20230921145930
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 +157 -174
- package/dist/index.js +392 -254
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -107,35 +107,9 @@ var Job = class {
|
|
|
107
107
|
get version() {
|
|
108
108
|
return this.options.version;
|
|
109
109
|
}
|
|
110
|
-
get integrations() {
|
|
111
|
-
return Object.keys(this.options.integrations ?? {}).reduce((acc, key) => {
|
|
112
|
-
const integration = this.options.integrations[key];
|
|
113
|
-
acc[key] = {
|
|
114
|
-
id: integration.id,
|
|
115
|
-
metadata: integration.metadata,
|
|
116
|
-
authSource: integration.authSource
|
|
117
|
-
};
|
|
118
|
-
return acc;
|
|
119
|
-
}, {});
|
|
120
|
-
}
|
|
121
110
|
get logLevel() {
|
|
122
111
|
return this.options.logLevel;
|
|
123
112
|
}
|
|
124
|
-
toJSON() {
|
|
125
|
-
const internal = this.options.__internal;
|
|
126
|
-
return {
|
|
127
|
-
id: this.id,
|
|
128
|
-
name: this.name,
|
|
129
|
-
version: this.version,
|
|
130
|
-
event: this.trigger.event,
|
|
131
|
-
trigger: this.trigger.toJSON(),
|
|
132
|
-
integrations: this.integrations,
|
|
133
|
-
startPosition: "latest",
|
|
134
|
-
enabled: this.enabled,
|
|
135
|
-
preprocessRuns: this.trigger.preprocessRuns,
|
|
136
|
-
internal
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
113
|
};
|
|
140
114
|
__name(Job, "Job");
|
|
141
115
|
_validate = new WeakSet();
|
|
@@ -146,7 +120,7 @@ validate_fn = /* @__PURE__ */ __name(function() {
|
|
|
146
120
|
}, "#validate");
|
|
147
121
|
|
|
148
122
|
// src/triggerClient.ts
|
|
149
|
-
var
|
|
123
|
+
var import_core6 = require("@trigger.dev/core");
|
|
150
124
|
|
|
151
125
|
// src/apiClient.ts
|
|
152
126
|
var import_core = require("@trigger.dev/core");
|
|
@@ -842,7 +816,7 @@ var IO = class {
|
|
|
842
816
|
idempotencyKey,
|
|
843
817
|
task
|
|
844
818
|
});
|
|
845
|
-
throw new Error(task.error ?? "Task errored");
|
|
819
|
+
throw new Error(task.error ?? task?.output ? JSON.stringify(task.output) : "Task errored");
|
|
846
820
|
}
|
|
847
821
|
if (task.status === "WAITING") {
|
|
848
822
|
this._logger.debug("Task waiting", {
|
|
@@ -1025,6 +999,24 @@ function createIOWithIntegrations(io, auths, integrations) {
|
|
|
1025
999
|
}
|
|
1026
1000
|
__name(createIOWithIntegrations, "createIOWithIntegrations");
|
|
1027
1001
|
|
|
1002
|
+
// src/utils/typedAsyncLocalStorage.ts
|
|
1003
|
+
var import_node_async_hooks2 = require("async_hooks");
|
|
1004
|
+
var TypedAsyncLocalStorage = class {
|
|
1005
|
+
constructor() {
|
|
1006
|
+
this.storage = new import_node_async_hooks2.AsyncLocalStorage();
|
|
1007
|
+
}
|
|
1008
|
+
runWith(context, fn) {
|
|
1009
|
+
return this.storage.run(context, fn);
|
|
1010
|
+
}
|
|
1011
|
+
getStore() {
|
|
1012
|
+
return this.storage.getStore();
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
__name(TypedAsyncLocalStorage, "TypedAsyncLocalStorage");
|
|
1016
|
+
|
|
1017
|
+
// src/runLocalStorage.ts
|
|
1018
|
+
var runLocalStorage = new TypedAsyncLocalStorage();
|
|
1019
|
+
|
|
1028
1020
|
// src/triggers/eventTrigger.ts
|
|
1029
1021
|
var import_core4 = require("@trigger.dev/core");
|
|
1030
1022
|
var _options2;
|
|
@@ -1076,66 +1068,201 @@ function eventTrigger(options) {
|
|
|
1076
1068
|
}
|
|
1077
1069
|
__name(eventTrigger, "eventTrigger");
|
|
1078
1070
|
|
|
1079
|
-
// src/
|
|
1080
|
-
var
|
|
1081
|
-
var
|
|
1071
|
+
// src/triggers/scheduled.ts
|
|
1072
|
+
var import_core5 = require("@trigger.dev/core");
|
|
1073
|
+
var import_cronstrue = __toESM(require("cronstrue"));
|
|
1074
|
+
var examples = [
|
|
1075
|
+
{
|
|
1076
|
+
id: "now",
|
|
1077
|
+
name: "Now",
|
|
1078
|
+
icon: "clock",
|
|
1079
|
+
payload: {
|
|
1080
|
+
ts: import_core5.currentDate.marker,
|
|
1081
|
+
lastTimestamp: import_core5.currentDate.marker
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
];
|
|
1085
|
+
var IntervalTrigger = class {
|
|
1086
|
+
constructor(options) {
|
|
1087
|
+
this.options = options;
|
|
1088
|
+
}
|
|
1089
|
+
get event() {
|
|
1090
|
+
return {
|
|
1091
|
+
name: "trigger.scheduled",
|
|
1092
|
+
title: "Schedule",
|
|
1093
|
+
source: "trigger.dev",
|
|
1094
|
+
icon: "schedule-interval",
|
|
1095
|
+
examples,
|
|
1096
|
+
parsePayload: import_core5.ScheduledPayloadSchema.parse,
|
|
1097
|
+
properties: [
|
|
1098
|
+
{
|
|
1099
|
+
label: "Interval",
|
|
1100
|
+
text: `${this.options.seconds}s`
|
|
1101
|
+
}
|
|
1102
|
+
]
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
attachToJob(triggerClient, job) {
|
|
1106
|
+
}
|
|
1107
|
+
get preprocessRuns() {
|
|
1108
|
+
return false;
|
|
1109
|
+
}
|
|
1110
|
+
toJSON() {
|
|
1111
|
+
return {
|
|
1112
|
+
type: "scheduled",
|
|
1113
|
+
schedule: {
|
|
1114
|
+
type: "interval",
|
|
1115
|
+
options: {
|
|
1116
|
+
seconds: this.options.seconds
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
__name(IntervalTrigger, "IntervalTrigger");
|
|
1123
|
+
function intervalTrigger(options) {
|
|
1124
|
+
return new IntervalTrigger(options);
|
|
1125
|
+
}
|
|
1126
|
+
__name(intervalTrigger, "intervalTrigger");
|
|
1127
|
+
var CronTrigger = class {
|
|
1128
|
+
constructor(options) {
|
|
1129
|
+
this.options = options;
|
|
1130
|
+
}
|
|
1131
|
+
get event() {
|
|
1132
|
+
const humanReadable = import_cronstrue.default.toString(this.options.cron, {
|
|
1133
|
+
throwExceptionOnParseError: false
|
|
1134
|
+
});
|
|
1135
|
+
return {
|
|
1136
|
+
name: "trigger.scheduled",
|
|
1137
|
+
title: "Cron Schedule",
|
|
1138
|
+
source: "trigger.dev",
|
|
1139
|
+
icon: "schedule-cron",
|
|
1140
|
+
examples,
|
|
1141
|
+
parsePayload: import_core5.ScheduledPayloadSchema.parse,
|
|
1142
|
+
properties: [
|
|
1143
|
+
{
|
|
1144
|
+
label: "cron",
|
|
1145
|
+
text: this.options.cron
|
|
1146
|
+
},
|
|
1147
|
+
{
|
|
1148
|
+
label: "Schedule",
|
|
1149
|
+
text: humanReadable
|
|
1150
|
+
}
|
|
1151
|
+
]
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
attachToJob(triggerClient, job) {
|
|
1155
|
+
}
|
|
1156
|
+
get preprocessRuns() {
|
|
1157
|
+
return false;
|
|
1158
|
+
}
|
|
1159
|
+
toJSON() {
|
|
1160
|
+
return {
|
|
1161
|
+
type: "scheduled",
|
|
1162
|
+
schedule: {
|
|
1163
|
+
type: "cron",
|
|
1164
|
+
options: {
|
|
1165
|
+
cron: this.options.cron
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
__name(CronTrigger, "CronTrigger");
|
|
1172
|
+
function cronTrigger(options) {
|
|
1173
|
+
return new CronTrigger(options);
|
|
1174
|
+
}
|
|
1175
|
+
__name(cronTrigger, "cronTrigger");
|
|
1176
|
+
var DynamicSchedule = class {
|
|
1082
1177
|
constructor(client, options) {
|
|
1083
|
-
__privateAdd(this, _validate2);
|
|
1084
1178
|
this.client = client;
|
|
1085
1179
|
this.options = options;
|
|
1086
|
-
|
|
1087
|
-
client.attachBackgroundTask(this);
|
|
1180
|
+
client.attachDynamicSchedule(this.options.id);
|
|
1088
1181
|
}
|
|
1089
1182
|
get id() {
|
|
1090
|
-
return
|
|
1091
|
-
}
|
|
1092
|
-
get enabled() {
|
|
1093
|
-
return typeof this.options.enabled === "boolean" ? this.options.enabled : true;
|
|
1183
|
+
return this.options.id;
|
|
1094
1184
|
}
|
|
1095
|
-
get
|
|
1096
|
-
return
|
|
1185
|
+
get event() {
|
|
1186
|
+
return {
|
|
1187
|
+
name: "trigger.scheduled",
|
|
1188
|
+
title: "Dynamic Schedule",
|
|
1189
|
+
source: "trigger.dev",
|
|
1190
|
+
icon: "schedule-dynamic",
|
|
1191
|
+
examples,
|
|
1192
|
+
parsePayload: import_core5.ScheduledPayloadSchema.parse
|
|
1193
|
+
};
|
|
1097
1194
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
1195
|
+
async register(key, metadata) {
|
|
1196
|
+
const runStore = runLocalStorage.getStore();
|
|
1197
|
+
if (!runStore) {
|
|
1198
|
+
return this.client.registerSchedule(this.id, key, metadata);
|
|
1199
|
+
}
|
|
1200
|
+
const { io } = runStore;
|
|
1201
|
+
return await io.runTask(key, async (task) => {
|
|
1202
|
+
return this.client.registerSchedule(this.id, key, metadata);
|
|
1203
|
+
}, {
|
|
1204
|
+
name: "Register Schedule",
|
|
1205
|
+
icon: metadata.type === "cron" ? "schedule-cron" : "schedule-interval",
|
|
1206
|
+
properties: [
|
|
1207
|
+
{
|
|
1208
|
+
label: "Dynamic Schedule",
|
|
1209
|
+
text: this.id
|
|
1210
|
+
},
|
|
1211
|
+
{
|
|
1212
|
+
label: "Schedule ID",
|
|
1213
|
+
text: key
|
|
1214
|
+
}
|
|
1215
|
+
],
|
|
1216
|
+
params: metadata
|
|
1217
|
+
});
|
|
1100
1218
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1219
|
+
async unregister(key) {
|
|
1220
|
+
const runStore = runLocalStorage.getStore();
|
|
1221
|
+
if (!runStore) {
|
|
1222
|
+
return this.client.unregisterSchedule(this.id, key);
|
|
1223
|
+
}
|
|
1224
|
+
const { io } = runStore;
|
|
1225
|
+
return await io.runTask(key, async (task) => {
|
|
1226
|
+
return this.client.unregisterSchedule(this.id, key);
|
|
1227
|
+
}, {
|
|
1228
|
+
name: "Unregister Schedule",
|
|
1229
|
+
icon: "schedule",
|
|
1230
|
+
properties: [
|
|
1231
|
+
{
|
|
1232
|
+
label: "Dynamic Schedule",
|
|
1233
|
+
text: this.id
|
|
1234
|
+
},
|
|
1235
|
+
{
|
|
1236
|
+
label: "Schedule ID",
|
|
1237
|
+
text: key
|
|
1238
|
+
}
|
|
1239
|
+
]
|
|
1240
|
+
});
|
|
1103
1241
|
}
|
|
1104
|
-
|
|
1105
|
-
|
|
1242
|
+
attachToJob(triggerClient, job) {
|
|
1243
|
+
triggerClient.attachDynamicScheduleToJob(this.options.id, job);
|
|
1106
1244
|
}
|
|
1107
|
-
|
|
1245
|
+
get preprocessRuns() {
|
|
1246
|
+
return false;
|
|
1108
1247
|
}
|
|
1109
1248
|
toJSON() {
|
|
1110
1249
|
return {
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
version: this.version,
|
|
1114
|
-
enabled: this.enabled,
|
|
1115
|
-
cpu: this.options.cpu ?? 1,
|
|
1116
|
-
memory: this.options.memory ?? 256,
|
|
1117
|
-
concurrency: this.options.concurrency ?? 1,
|
|
1118
|
-
secrets: this.options.secrets ?? {}
|
|
1250
|
+
type: "dynamic",
|
|
1251
|
+
id: this.options.id
|
|
1119
1252
|
};
|
|
1120
1253
|
}
|
|
1121
1254
|
};
|
|
1122
|
-
__name(
|
|
1123
|
-
_validate2 = new WeakSet();
|
|
1124
|
-
validate_fn2 = /* @__PURE__ */ __name(function() {
|
|
1125
|
-
if (!this.version.match(/^(\d+)\.(\d+)\.(\d+)$/)) {
|
|
1126
|
-
throw new Error(`Invalid job version: "${this.version}". BackgroundTask versions must be valid semver versions.`);
|
|
1127
|
-
}
|
|
1128
|
-
}, "#validate");
|
|
1255
|
+
__name(DynamicSchedule, "DynamicSchedule");
|
|
1129
1256
|
|
|
1130
1257
|
// src/triggerClient.ts
|
|
1131
1258
|
var registerSourceEvent = {
|
|
1132
|
-
name:
|
|
1259
|
+
name: import_core6.REGISTER_SOURCE_EVENT_V2,
|
|
1133
1260
|
title: "Register Source",
|
|
1134
1261
|
source: "internal",
|
|
1135
1262
|
icon: "register-source",
|
|
1136
|
-
parsePayload:
|
|
1263
|
+
parsePayload: import_core6.RegisterSourceEventSchemaV2.parse
|
|
1137
1264
|
};
|
|
1138
|
-
var _options3, _registeredJobs, _registeredSources,
|
|
1265
|
+
var _options3, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _authResolvers, _client, _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;
|
|
1139
1266
|
var TriggerClient = class {
|
|
1140
1267
|
constructor(options) {
|
|
1141
1268
|
__privateAdd(this, _preprocessRun);
|
|
@@ -1143,20 +1270,26 @@ var TriggerClient = class {
|
|
|
1143
1270
|
__privateAdd(this, _createRunContext);
|
|
1144
1271
|
__privateAdd(this, _createPreprocessRunContext);
|
|
1145
1272
|
__privateAdd(this, _handleHttpSourceRequest);
|
|
1273
|
+
__privateAdd(this, _resolveConnections);
|
|
1274
|
+
__privateAdd(this, _resolveConnection);
|
|
1275
|
+
__privateAdd(this, _buildJobsIndex);
|
|
1276
|
+
__privateAdd(this, _buildJobIndex);
|
|
1277
|
+
__privateAdd(this, _buildJobIntegrations);
|
|
1278
|
+
__privateAdd(this, _buildJobIntegration);
|
|
1146
1279
|
__privateAdd(this, _options3, void 0);
|
|
1147
1280
|
__privateAdd(this, _registeredJobs, {});
|
|
1148
1281
|
__privateAdd(this, _registeredSources, {});
|
|
1149
|
-
__privateAdd(this, _registeredBackgroundTasks, {});
|
|
1150
1282
|
__privateAdd(this, _registeredHttpSourceHandlers, {});
|
|
1151
1283
|
__privateAdd(this, _registeredDynamicTriggers, {});
|
|
1152
1284
|
__privateAdd(this, _jobMetadataByDynamicTriggers, {});
|
|
1153
1285
|
__privateAdd(this, _registeredSchedules, {});
|
|
1286
|
+
__privateAdd(this, _authResolvers, {});
|
|
1154
1287
|
__privateAdd(this, _client, void 0);
|
|
1155
1288
|
__privateAdd(this, _internalLogger, void 0);
|
|
1156
1289
|
this.id = options.id;
|
|
1157
1290
|
__privateSet(this, _options3, options);
|
|
1158
1291
|
__privateSet(this, _client, new ApiClient(__privateGet(this, _options3)));
|
|
1159
|
-
__privateSet(this, _internalLogger, new
|
|
1292
|
+
__privateSet(this, _internalLogger, new import_core6.Logger("trigger.dev", __privateGet(this, _options3).verbose ? "debug" : "log"));
|
|
1160
1293
|
}
|
|
1161
1294
|
async handleRequest(request) {
|
|
1162
1295
|
__privateGet(this, _internalLogger).debug("handling request", {
|
|
@@ -1241,25 +1374,8 @@ var TriggerClient = class {
|
|
|
1241
1374
|
};
|
|
1242
1375
|
}
|
|
1243
1376
|
case "INDEX_ENDPOINT": {
|
|
1244
|
-
const jobId = request.headers.get("x-trigger-job-id");
|
|
1245
|
-
if (jobId) {
|
|
1246
|
-
const job = __privateGet(this, _registeredJobs)[jobId];
|
|
1247
|
-
if (!job) {
|
|
1248
|
-
return {
|
|
1249
|
-
status: 404,
|
|
1250
|
-
body: {
|
|
1251
|
-
message: "Job not found"
|
|
1252
|
-
}
|
|
1253
|
-
};
|
|
1254
|
-
}
|
|
1255
|
-
return {
|
|
1256
|
-
status: 200,
|
|
1257
|
-
body: job.toJSON()
|
|
1258
|
-
};
|
|
1259
|
-
}
|
|
1260
1377
|
const body = {
|
|
1261
|
-
jobs:
|
|
1262
|
-
backgroundTasks: Object.values(__privateGet(this, _registeredBackgroundTasks)).map((task) => task.toJSON()),
|
|
1378
|
+
jobs: __privateMethod(this, _buildJobsIndex, buildJobsIndex_fn).call(this),
|
|
1263
1379
|
sources: Object.values(__privateGet(this, _registeredSources)),
|
|
1264
1380
|
dynamicTriggers: Object.values(__privateGet(this, _registeredDynamicTriggers)).map((trigger) => ({
|
|
1265
1381
|
id: trigger.id,
|
|
@@ -1281,7 +1397,7 @@ var TriggerClient = class {
|
|
|
1281
1397
|
}
|
|
1282
1398
|
case "INITIALIZE_TRIGGER": {
|
|
1283
1399
|
const json = await request.json();
|
|
1284
|
-
const body =
|
|
1400
|
+
const body = import_core6.InitializeTriggerBodySchema.safeParse(json);
|
|
1285
1401
|
if (!body.success) {
|
|
1286
1402
|
return {
|
|
1287
1403
|
status: 400,
|
|
@@ -1306,7 +1422,7 @@ var TriggerClient = class {
|
|
|
1306
1422
|
}
|
|
1307
1423
|
case "EXECUTE_JOB": {
|
|
1308
1424
|
const json = await request.json();
|
|
1309
|
-
const execution =
|
|
1425
|
+
const execution = import_core6.RunJobBodySchema.safeParse(json);
|
|
1310
1426
|
if (!execution.success) {
|
|
1311
1427
|
return {
|
|
1312
1428
|
status: 400,
|
|
@@ -1332,7 +1448,7 @@ var TriggerClient = class {
|
|
|
1332
1448
|
}
|
|
1333
1449
|
case "PREPROCESS_RUN": {
|
|
1334
1450
|
const json = await request.json();
|
|
1335
|
-
const body =
|
|
1451
|
+
const body = import_core6.PreprocessRunBodySchema.safeParse(json);
|
|
1336
1452
|
if (!body.success) {
|
|
1337
1453
|
return {
|
|
1338
1454
|
status: 400,
|
|
@@ -1360,7 +1476,7 @@ var TriggerClient = class {
|
|
|
1360
1476
|
};
|
|
1361
1477
|
}
|
|
1362
1478
|
case "DELIVER_HTTP_SOURCE_REQUEST": {
|
|
1363
|
-
const headers =
|
|
1479
|
+
const headers = import_core6.HttpSourceRequestHeadersSchema.safeParse(Object.fromEntries(request.headers.entries()));
|
|
1364
1480
|
if (!headers.success) {
|
|
1365
1481
|
return {
|
|
1366
1482
|
status: 400,
|
|
@@ -1425,13 +1541,20 @@ var TriggerClient = class {
|
|
|
1425
1541
|
}
|
|
1426
1542
|
};
|
|
1427
1543
|
}
|
|
1544
|
+
defineJob(options) {
|
|
1545
|
+
return new Job(this, options);
|
|
1546
|
+
}
|
|
1547
|
+
defineAuthResolver(integration, resolver) {
|
|
1548
|
+
__privateGet(this, _authResolvers)[integration.id] = resolver;
|
|
1549
|
+
return this;
|
|
1550
|
+
}
|
|
1551
|
+
defineDynamicSchedule(options) {
|
|
1552
|
+
return new DynamicSchedule(this, options);
|
|
1553
|
+
}
|
|
1428
1554
|
attach(job) {
|
|
1429
1555
|
__privateGet(this, _registeredJobs)[job.id] = job;
|
|
1430
1556
|
job.trigger.attachToJob(this, job);
|
|
1431
1557
|
}
|
|
1432
|
-
attachBackgroundTask(task) {
|
|
1433
|
-
__privateGet(this, _registeredBackgroundTasks)[task.id] = task;
|
|
1434
|
-
}
|
|
1435
1558
|
attachDynamicTrigger(trigger) {
|
|
1436
1559
|
__privateGet(this, _registeredDynamicTriggers)[trigger.id] = trigger;
|
|
1437
1560
|
new Job(this, {
|
|
@@ -1530,7 +1653,11 @@ var TriggerClient = class {
|
|
|
1530
1653
|
__internal: true
|
|
1531
1654
|
});
|
|
1532
1655
|
}
|
|
1533
|
-
attachDynamicSchedule(key
|
|
1656
|
+
attachDynamicSchedule(key) {
|
|
1657
|
+
const jobs = __privateGet(this, _registeredSchedules)[key] ?? [];
|
|
1658
|
+
__privateGet(this, _registeredSchedules)[key] = jobs;
|
|
1659
|
+
}
|
|
1660
|
+
attachDynamicScheduleToJob(key, job) {
|
|
1534
1661
|
const jobs = __privateGet(this, _registeredSchedules)[key] ?? [];
|
|
1535
1662
|
jobs.push({
|
|
1536
1663
|
id: job.id,
|
|
@@ -1578,22 +1705,16 @@ var TriggerClient = class {
|
|
|
1578
1705
|
apiKey() {
|
|
1579
1706
|
return __privateGet(this, _options3).apiKey ?? process.env.TRIGGER_API_KEY;
|
|
1580
1707
|
}
|
|
1581
|
-
defineJob(options) {
|
|
1582
|
-
return new Job(this, options);
|
|
1583
|
-
}
|
|
1584
|
-
defineBackgroundTask(options) {
|
|
1585
|
-
return new BackgroundTask(this, options);
|
|
1586
|
-
}
|
|
1587
1708
|
};
|
|
1588
1709
|
__name(TriggerClient, "TriggerClient");
|
|
1589
1710
|
_options3 = new WeakMap();
|
|
1590
1711
|
_registeredJobs = new WeakMap();
|
|
1591
1712
|
_registeredSources = new WeakMap();
|
|
1592
|
-
_registeredBackgroundTasks = new WeakMap();
|
|
1593
1713
|
_registeredHttpSourceHandlers = new WeakMap();
|
|
1594
1714
|
_registeredDynamicTriggers = new WeakMap();
|
|
1595
1715
|
_jobMetadataByDynamicTriggers = new WeakMap();
|
|
1596
1716
|
_registeredSchedules = new WeakMap();
|
|
1717
|
+
_authResolvers = new WeakMap();
|
|
1597
1718
|
_client = new WeakMap();
|
|
1598
1719
|
_internalLogger = new WeakMap();
|
|
1599
1720
|
_preprocessRun = new WeakSet();
|
|
@@ -1610,7 +1731,8 @@ _executeJob = new WeakSet();
|
|
|
1610
1731
|
executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
1611
1732
|
__privateGet(this, _internalLogger).debug("executing job", {
|
|
1612
1733
|
execution: body1,
|
|
1613
|
-
job: job1.
|
|
1734
|
+
job: job1.id,
|
|
1735
|
+
version: job1.version
|
|
1614
1736
|
});
|
|
1615
1737
|
const context = __privateMethod(this, _createRunContext, createRunContext_fn).call(this, body1);
|
|
1616
1738
|
const io = new IO({
|
|
@@ -1621,11 +1743,23 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
|
1621
1743
|
client: this,
|
|
1622
1744
|
context,
|
|
1623
1745
|
jobLogLevel: job1.logLevel ?? __privateGet(this, _options3).logLevel ?? "info",
|
|
1624
|
-
jobLogger: __privateGet(this, _options3).ioLogLocalEnabled ? new
|
|
1746
|
+
jobLogger: __privateGet(this, _options3).ioLogLocalEnabled ? new import_core6.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options3).logLevel ?? "info") : void 0
|
|
1625
1747
|
});
|
|
1626
|
-
const
|
|
1748
|
+
const resolvedConnections = await __privateMethod(this, _resolveConnections, resolveConnections_fn).call(this, context, job1.options.integrations, body1.connections);
|
|
1749
|
+
if (!resolvedConnections.ok) {
|
|
1750
|
+
return {
|
|
1751
|
+
status: "UNRESOLVED_AUTH_ERROR",
|
|
1752
|
+
issues: resolvedConnections.issues
|
|
1753
|
+
};
|
|
1754
|
+
}
|
|
1755
|
+
const ioWithConnections = createIOWithIntegrations(io, resolvedConnections.data, job1.options.integrations);
|
|
1627
1756
|
try {
|
|
1628
|
-
const output = await
|
|
1757
|
+
const output = await runLocalStorage.runWith({
|
|
1758
|
+
io,
|
|
1759
|
+
ctx: context
|
|
1760
|
+
}, () => {
|
|
1761
|
+
return job1.options.run(job1.trigger.event.parsePayload(body1.event.payload ?? {}), ioWithConnections, context);
|
|
1762
|
+
});
|
|
1629
1763
|
return {
|
|
1630
1764
|
status: "SUCCESS",
|
|
1631
1765
|
output
|
|
@@ -1652,7 +1786,7 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
|
1652
1786
|
};
|
|
1653
1787
|
}
|
|
1654
1788
|
if (error instanceof RetryWithTaskError) {
|
|
1655
|
-
const errorWithStack2 =
|
|
1789
|
+
const errorWithStack2 = import_core6.ErrorWithStackSchema.safeParse(error.cause);
|
|
1656
1790
|
if (errorWithStack2.success) {
|
|
1657
1791
|
return {
|
|
1658
1792
|
status: "ERROR",
|
|
@@ -1668,7 +1802,7 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
|
1668
1802
|
task: error.task
|
|
1669
1803
|
};
|
|
1670
1804
|
}
|
|
1671
|
-
const errorWithStack =
|
|
1805
|
+
const errorWithStack = import_core6.ErrorWithStackSchema.safeParse(error);
|
|
1672
1806
|
if (errorWithStack.success) {
|
|
1673
1807
|
return {
|
|
1674
1808
|
status: "ERROR",
|
|
@@ -1800,6 +1934,154 @@ handleHttpSourceRequest_fn = /* @__PURE__ */ __name(async function(source, sourc
|
|
|
1800
1934
|
metadata: results.metadata
|
|
1801
1935
|
};
|
|
1802
1936
|
}, "#handleHttpSourceRequest");
|
|
1937
|
+
_resolveConnections = new WeakSet();
|
|
1938
|
+
resolveConnections_fn = /* @__PURE__ */ __name(async function(ctx, integrations, connections) {
|
|
1939
|
+
if (!integrations) {
|
|
1940
|
+
return {
|
|
1941
|
+
ok: true,
|
|
1942
|
+
data: {}
|
|
1943
|
+
};
|
|
1944
|
+
}
|
|
1945
|
+
const resolvedAuthResults = await Promise.all(Object.keys(integrations).map(async (key) => {
|
|
1946
|
+
const integration = integrations[key];
|
|
1947
|
+
const auth = (connections ?? {})[key];
|
|
1948
|
+
const result = await __privateMethod(this, _resolveConnection, resolveConnection_fn).call(this, ctx, integration, auth);
|
|
1949
|
+
if (result.ok) {
|
|
1950
|
+
return {
|
|
1951
|
+
ok: true,
|
|
1952
|
+
auth: result.auth,
|
|
1953
|
+
key
|
|
1954
|
+
};
|
|
1955
|
+
} else {
|
|
1956
|
+
return {
|
|
1957
|
+
ok: false,
|
|
1958
|
+
error: result.error,
|
|
1959
|
+
key
|
|
1960
|
+
};
|
|
1961
|
+
}
|
|
1962
|
+
}));
|
|
1963
|
+
const allResolved = resolvedAuthResults.every((result) => result.ok);
|
|
1964
|
+
if (allResolved) {
|
|
1965
|
+
return {
|
|
1966
|
+
ok: true,
|
|
1967
|
+
data: resolvedAuthResults.reduce((acc, result) => {
|
|
1968
|
+
acc[result.key] = result.auth;
|
|
1969
|
+
return acc;
|
|
1970
|
+
}, {})
|
|
1971
|
+
};
|
|
1972
|
+
} else {
|
|
1973
|
+
return {
|
|
1974
|
+
ok: false,
|
|
1975
|
+
issues: resolvedAuthResults.reduce((acc, result) => {
|
|
1976
|
+
if (result.ok) {
|
|
1977
|
+
return acc;
|
|
1978
|
+
}
|
|
1979
|
+
const integration = integrations[result.key];
|
|
1980
|
+
acc[result.key] = {
|
|
1981
|
+
id: integration.id,
|
|
1982
|
+
error: result.error
|
|
1983
|
+
};
|
|
1984
|
+
return acc;
|
|
1985
|
+
}, {})
|
|
1986
|
+
};
|
|
1987
|
+
}
|
|
1988
|
+
}, "#resolveConnections");
|
|
1989
|
+
_resolveConnection = new WeakSet();
|
|
1990
|
+
resolveConnection_fn = /* @__PURE__ */ __name(async function(ctx1, integration, auth) {
|
|
1991
|
+
if (auth) {
|
|
1992
|
+
return {
|
|
1993
|
+
ok: true,
|
|
1994
|
+
auth
|
|
1995
|
+
};
|
|
1996
|
+
}
|
|
1997
|
+
const authResolver = __privateGet(this, _authResolvers)[integration.id];
|
|
1998
|
+
if (!authResolver) {
|
|
1999
|
+
if (integration.authSource === "HOSTED") {
|
|
2000
|
+
return {
|
|
2001
|
+
ok: false,
|
|
2002
|
+
error: `Something went wrong: Integration ${integration.id} is missing auth credentials from Trigger.dev`
|
|
2003
|
+
};
|
|
2004
|
+
}
|
|
2005
|
+
return {
|
|
2006
|
+
ok: true,
|
|
2007
|
+
auth: void 0
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
try {
|
|
2011
|
+
const resolvedAuth = await authResolver(ctx1, integration);
|
|
2012
|
+
if (!resolvedAuth) {
|
|
2013
|
+
return {
|
|
2014
|
+
ok: false,
|
|
2015
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver returned null or undefined`
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
return {
|
|
2019
|
+
ok: true,
|
|
2020
|
+
auth: resolvedAuth.type === "apiKey" ? {
|
|
2021
|
+
type: "apiKey",
|
|
2022
|
+
accessToken: resolvedAuth.token,
|
|
2023
|
+
additionalFields: resolvedAuth.additionalFields
|
|
2024
|
+
} : {
|
|
2025
|
+
type: "oauth2",
|
|
2026
|
+
accessToken: resolvedAuth.token,
|
|
2027
|
+
additionalFields: resolvedAuth.additionalFields
|
|
2028
|
+
}
|
|
2029
|
+
};
|
|
2030
|
+
} catch (resolverError) {
|
|
2031
|
+
if (resolverError instanceof Error) {
|
|
2032
|
+
return {
|
|
2033
|
+
ok: false,
|
|
2034
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver threw. ${resolverError.name}: ${resolverError.message}`
|
|
2035
|
+
};
|
|
2036
|
+
} else if (typeof resolverError === "string") {
|
|
2037
|
+
return {
|
|
2038
|
+
ok: false,
|
|
2039
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver threw an error: ${resolverError}`
|
|
2040
|
+
};
|
|
2041
|
+
}
|
|
2042
|
+
return {
|
|
2043
|
+
ok: false,
|
|
2044
|
+
error: `Auth could not be resolved for ${integration.id}: auth resolver threw an unknown error: ${JSON.stringify(resolverError)}`
|
|
2045
|
+
};
|
|
2046
|
+
}
|
|
2047
|
+
}, "#resolveConnection");
|
|
2048
|
+
_buildJobsIndex = new WeakSet();
|
|
2049
|
+
buildJobsIndex_fn = /* @__PURE__ */ __name(function() {
|
|
2050
|
+
return Object.values(__privateGet(this, _registeredJobs)).map((job) => __privateMethod(this, _buildJobIndex, buildJobIndex_fn).call(this, job));
|
|
2051
|
+
}, "#buildJobsIndex");
|
|
2052
|
+
_buildJobIndex = new WeakSet();
|
|
2053
|
+
buildJobIndex_fn = /* @__PURE__ */ __name(function(job2) {
|
|
2054
|
+
const internal = job2.options.__internal;
|
|
2055
|
+
return {
|
|
2056
|
+
id: job2.id,
|
|
2057
|
+
name: job2.name,
|
|
2058
|
+
version: job2.version,
|
|
2059
|
+
event: job2.trigger.event,
|
|
2060
|
+
trigger: job2.trigger.toJSON(),
|
|
2061
|
+
integrations: __privateMethod(this, _buildJobIntegrations, buildJobIntegrations_fn).call(this, job2),
|
|
2062
|
+
startPosition: "latest",
|
|
2063
|
+
enabled: job2.enabled,
|
|
2064
|
+
preprocessRuns: job2.trigger.preprocessRuns,
|
|
2065
|
+
internal
|
|
2066
|
+
};
|
|
2067
|
+
}, "#buildJobIndex");
|
|
2068
|
+
_buildJobIntegrations = new WeakSet();
|
|
2069
|
+
buildJobIntegrations_fn = /* @__PURE__ */ __name(function(job3) {
|
|
2070
|
+
return Object.keys(job3.options.integrations ?? {}).reduce((acc, key) => {
|
|
2071
|
+
const integration = job3.options.integrations[key];
|
|
2072
|
+
acc[key] = __privateMethod(this, _buildJobIntegration, buildJobIntegration_fn).call(this, integration);
|
|
2073
|
+
return acc;
|
|
2074
|
+
}, {});
|
|
2075
|
+
}, "#buildJobIntegrations");
|
|
2076
|
+
_buildJobIntegration = new WeakSet();
|
|
2077
|
+
buildJobIntegration_fn = /* @__PURE__ */ __name(function(integration1) {
|
|
2078
|
+
const authSource = __privateGet(this, _authResolvers)[integration1.id] ? "RESOLVER" : integration1.authSource;
|
|
2079
|
+
return {
|
|
2080
|
+
id: integration1.id,
|
|
2081
|
+
metadata: integration1.metadata,
|
|
2082
|
+
authSource
|
|
2083
|
+
};
|
|
2084
|
+
}, "#buildJobIntegration");
|
|
1803
2085
|
function dynamicTriggerRegisterSourceJobId(id) {
|
|
1804
2086
|
return `register-dynamic-trigger-${id}`;
|
|
1805
2087
|
}
|
|
@@ -1825,7 +2107,7 @@ function deepMergeOptions(obj1, obj2) {
|
|
|
1825
2107
|
__name(deepMergeOptions, "deepMergeOptions");
|
|
1826
2108
|
|
|
1827
2109
|
// src/triggers/externalSource.ts
|
|
1828
|
-
var
|
|
2110
|
+
var import_core7 = require("@trigger.dev/core");
|
|
1829
2111
|
var ExternalSource = class {
|
|
1830
2112
|
constructor(channel, options) {
|
|
1831
2113
|
this.options = options;
|
|
@@ -1899,7 +2181,7 @@ var ExternalSourceTrigger = class {
|
|
|
1899
2181
|
title: "External Source",
|
|
1900
2182
|
rule: {
|
|
1901
2183
|
event: this.event.name,
|
|
1902
|
-
payload: (0,
|
|
2184
|
+
payload: (0, import_core7.deepMergeFilters)(this.options.source.filter(this.options.params, this.options.options), this.event.filter ?? {}, this.options.params.filter ?? {}),
|
|
1903
2185
|
source: this.event.source
|
|
1904
2186
|
},
|
|
1905
2187
|
properties: this.options.source.properties(this.options.params)
|
|
@@ -1934,7 +2216,7 @@ function omit(obj, key) {
|
|
|
1934
2216
|
__name(omit, "omit");
|
|
1935
2217
|
|
|
1936
2218
|
// src/triggers/dynamic.ts
|
|
1937
|
-
var
|
|
2219
|
+
var import_core8 = require("@trigger.dev/core");
|
|
1938
2220
|
var _client2, _options4;
|
|
1939
2221
|
var DynamicTrigger = class {
|
|
1940
2222
|
constructor(client, options) {
|
|
@@ -1963,7 +2245,7 @@ var DynamicTrigger = class {
|
|
|
1963
2245
|
rule: {
|
|
1964
2246
|
event: this.event.name,
|
|
1965
2247
|
source: this.event.source,
|
|
1966
|
-
payload: (0,
|
|
2248
|
+
payload: (0, import_core8.deepMergeFilters)(this.source.filter(params), this.event.filter ?? {})
|
|
1967
2249
|
},
|
|
1968
2250
|
source: {
|
|
1969
2251
|
version: "2",
|
|
@@ -1997,150 +2279,6 @@ __name(DynamicTrigger, "DynamicTrigger");
|
|
|
1997
2279
|
_client2 = new WeakMap();
|
|
1998
2280
|
_options4 = new WeakMap();
|
|
1999
2281
|
|
|
2000
|
-
// src/triggers/scheduled.ts
|
|
2001
|
-
var import_core8 = require("@trigger.dev/core");
|
|
2002
|
-
var import_cronstrue = __toESM(require("cronstrue"));
|
|
2003
|
-
var examples = [
|
|
2004
|
-
{
|
|
2005
|
-
id: "now",
|
|
2006
|
-
name: "Now",
|
|
2007
|
-
icon: "clock",
|
|
2008
|
-
payload: {
|
|
2009
|
-
ts: import_core8.currentDate.marker,
|
|
2010
|
-
lastTimestamp: import_core8.currentDate.marker
|
|
2011
|
-
}
|
|
2012
|
-
}
|
|
2013
|
-
];
|
|
2014
|
-
var IntervalTrigger = class {
|
|
2015
|
-
constructor(options) {
|
|
2016
|
-
this.options = options;
|
|
2017
|
-
}
|
|
2018
|
-
get event() {
|
|
2019
|
-
return {
|
|
2020
|
-
name: "trigger.scheduled",
|
|
2021
|
-
title: "Schedule",
|
|
2022
|
-
source: "trigger.dev",
|
|
2023
|
-
icon: "schedule-interval",
|
|
2024
|
-
examples,
|
|
2025
|
-
parsePayload: import_core8.ScheduledPayloadSchema.parse,
|
|
2026
|
-
properties: [
|
|
2027
|
-
{
|
|
2028
|
-
label: "Interval",
|
|
2029
|
-
text: `${this.options.seconds}s`
|
|
2030
|
-
}
|
|
2031
|
-
]
|
|
2032
|
-
};
|
|
2033
|
-
}
|
|
2034
|
-
attachToJob(triggerClient, job) {
|
|
2035
|
-
}
|
|
2036
|
-
get preprocessRuns() {
|
|
2037
|
-
return false;
|
|
2038
|
-
}
|
|
2039
|
-
toJSON() {
|
|
2040
|
-
return {
|
|
2041
|
-
type: "scheduled",
|
|
2042
|
-
schedule: {
|
|
2043
|
-
type: "interval",
|
|
2044
|
-
options: {
|
|
2045
|
-
seconds: this.options.seconds
|
|
2046
|
-
}
|
|
2047
|
-
}
|
|
2048
|
-
};
|
|
2049
|
-
}
|
|
2050
|
-
};
|
|
2051
|
-
__name(IntervalTrigger, "IntervalTrigger");
|
|
2052
|
-
function intervalTrigger(options) {
|
|
2053
|
-
return new IntervalTrigger(options);
|
|
2054
|
-
}
|
|
2055
|
-
__name(intervalTrigger, "intervalTrigger");
|
|
2056
|
-
var CronTrigger = class {
|
|
2057
|
-
constructor(options) {
|
|
2058
|
-
this.options = options;
|
|
2059
|
-
}
|
|
2060
|
-
get event() {
|
|
2061
|
-
const humanReadable = import_cronstrue.default.toString(this.options.cron, {
|
|
2062
|
-
throwExceptionOnParseError: false
|
|
2063
|
-
});
|
|
2064
|
-
return {
|
|
2065
|
-
name: "trigger.scheduled",
|
|
2066
|
-
title: "Cron Schedule",
|
|
2067
|
-
source: "trigger.dev",
|
|
2068
|
-
icon: "schedule-cron",
|
|
2069
|
-
examples,
|
|
2070
|
-
parsePayload: import_core8.ScheduledPayloadSchema.parse,
|
|
2071
|
-
properties: [
|
|
2072
|
-
{
|
|
2073
|
-
label: "cron",
|
|
2074
|
-
text: this.options.cron
|
|
2075
|
-
},
|
|
2076
|
-
{
|
|
2077
|
-
label: "Schedule",
|
|
2078
|
-
text: humanReadable
|
|
2079
|
-
}
|
|
2080
|
-
]
|
|
2081
|
-
};
|
|
2082
|
-
}
|
|
2083
|
-
attachToJob(triggerClient, job) {
|
|
2084
|
-
}
|
|
2085
|
-
get preprocessRuns() {
|
|
2086
|
-
return false;
|
|
2087
|
-
}
|
|
2088
|
-
toJSON() {
|
|
2089
|
-
return {
|
|
2090
|
-
type: "scheduled",
|
|
2091
|
-
schedule: {
|
|
2092
|
-
type: "cron",
|
|
2093
|
-
options: {
|
|
2094
|
-
cron: this.options.cron
|
|
2095
|
-
}
|
|
2096
|
-
}
|
|
2097
|
-
};
|
|
2098
|
-
}
|
|
2099
|
-
};
|
|
2100
|
-
__name(CronTrigger, "CronTrigger");
|
|
2101
|
-
function cronTrigger(options) {
|
|
2102
|
-
return new CronTrigger(options);
|
|
2103
|
-
}
|
|
2104
|
-
__name(cronTrigger, "cronTrigger");
|
|
2105
|
-
var DynamicSchedule = class {
|
|
2106
|
-
constructor(client, options) {
|
|
2107
|
-
this.client = client;
|
|
2108
|
-
this.options = options;
|
|
2109
|
-
}
|
|
2110
|
-
get id() {
|
|
2111
|
-
return this.options.id;
|
|
2112
|
-
}
|
|
2113
|
-
get event() {
|
|
2114
|
-
return {
|
|
2115
|
-
name: "trigger.scheduled",
|
|
2116
|
-
title: "Dynamic Schedule",
|
|
2117
|
-
source: "trigger.dev",
|
|
2118
|
-
icon: "schedule-dynamic",
|
|
2119
|
-
examples,
|
|
2120
|
-
parsePayload: import_core8.ScheduledPayloadSchema.parse
|
|
2121
|
-
};
|
|
2122
|
-
}
|
|
2123
|
-
async register(key, metadata) {
|
|
2124
|
-
return this.client.registerSchedule(this.id, key, metadata);
|
|
2125
|
-
}
|
|
2126
|
-
async unregister(key) {
|
|
2127
|
-
return this.client.unregisterSchedule(this.id, key);
|
|
2128
|
-
}
|
|
2129
|
-
attachToJob(triggerClient, job) {
|
|
2130
|
-
triggerClient.attachDynamicSchedule(this.options.id, job);
|
|
2131
|
-
}
|
|
2132
|
-
get preprocessRuns() {
|
|
2133
|
-
return false;
|
|
2134
|
-
}
|
|
2135
|
-
toJSON() {
|
|
2136
|
-
return {
|
|
2137
|
-
type: "dynamic",
|
|
2138
|
-
id: this.options.id
|
|
2139
|
-
};
|
|
2140
|
-
}
|
|
2141
|
-
};
|
|
2142
|
-
__name(DynamicSchedule, "DynamicSchedule");
|
|
2143
|
-
|
|
2144
2282
|
// src/triggers/notifications.ts
|
|
2145
2283
|
var import_core9 = require("@trigger.dev/core");
|
|
2146
2284
|
function missingConnectionNotification(integrations) {
|