@trigger.dev/sdk 0.0.0-background-tasks-20230906212613
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.ts +1325 -0
- package/dist/index.js +2275 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2275 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
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
|
+
));
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
var __accessCheck = (obj, member, msg) => {
|
|
27
|
+
if (!member.has(obj))
|
|
28
|
+
throw TypeError("Cannot " + msg);
|
|
29
|
+
};
|
|
30
|
+
var __privateGet = (obj, member, getter) => {
|
|
31
|
+
__accessCheck(obj, member, "read from private field");
|
|
32
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
33
|
+
};
|
|
34
|
+
var __privateAdd = (obj, member, value) => {
|
|
35
|
+
if (member.has(obj))
|
|
36
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
37
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
38
|
+
};
|
|
39
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
40
|
+
__accessCheck(obj, member, "write to private field");
|
|
41
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
var __privateMethod = (obj, member, method) => {
|
|
45
|
+
__accessCheck(obj, member, "access private method");
|
|
46
|
+
return method;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/index.ts
|
|
50
|
+
var src_exports = {};
|
|
51
|
+
__export(src_exports, {
|
|
52
|
+
CronTrigger: () => CronTrigger,
|
|
53
|
+
DynamicSchedule: () => DynamicSchedule,
|
|
54
|
+
DynamicTrigger: () => DynamicTrigger,
|
|
55
|
+
EventTrigger: () => EventTrigger,
|
|
56
|
+
ExternalSource: () => ExternalSource,
|
|
57
|
+
ExternalSourceTrigger: () => ExternalSourceTrigger,
|
|
58
|
+
IO: () => IO,
|
|
59
|
+
IOLogger: () => IOLogger,
|
|
60
|
+
IntervalTrigger: () => IntervalTrigger,
|
|
61
|
+
Job: () => Job,
|
|
62
|
+
MissingConnectionNotification: () => MissingConnectionNotification,
|
|
63
|
+
MissingConnectionResolvedNotification: () => MissingConnectionResolvedNotification,
|
|
64
|
+
TriggerClient: () => TriggerClient,
|
|
65
|
+
cronTrigger: () => cronTrigger,
|
|
66
|
+
eventTrigger: () => eventTrigger,
|
|
67
|
+
intervalTrigger: () => intervalTrigger,
|
|
68
|
+
isTriggerError: () => isTriggerError,
|
|
69
|
+
missingConnectionNotification: () => missingConnectionNotification,
|
|
70
|
+
missingConnectionResolvedNotification: () => missingConnectionResolvedNotification,
|
|
71
|
+
omit: () => omit,
|
|
72
|
+
redactString: () => redactString,
|
|
73
|
+
retry: () => retry
|
|
74
|
+
});
|
|
75
|
+
module.exports = __toCommonJS(src_exports);
|
|
76
|
+
|
|
77
|
+
// src/utils.ts
|
|
78
|
+
function slugifyId(input) {
|
|
79
|
+
const replaceSpacesWithDash = input.toLowerCase().replace(/\s+/g, "-");
|
|
80
|
+
const removeNonUrlSafeChars = replaceSpacesWithDash.replace(/[^a-zA-Z0-9-._~]/g, "");
|
|
81
|
+
return removeNonUrlSafeChars;
|
|
82
|
+
}
|
|
83
|
+
__name(slugifyId, "slugifyId");
|
|
84
|
+
|
|
85
|
+
// src/job.ts
|
|
86
|
+
var _validate, validate_fn;
|
|
87
|
+
var Job = class {
|
|
88
|
+
constructor(client, options) {
|
|
89
|
+
__privateAdd(this, _validate);
|
|
90
|
+
this.client = client;
|
|
91
|
+
this.options = options;
|
|
92
|
+
__privateMethod(this, _validate, validate_fn).call(this);
|
|
93
|
+
client.attach(this);
|
|
94
|
+
}
|
|
95
|
+
get id() {
|
|
96
|
+
return slugifyId(this.options.id);
|
|
97
|
+
}
|
|
98
|
+
get enabled() {
|
|
99
|
+
return typeof this.options.enabled === "boolean" ? this.options.enabled : true;
|
|
100
|
+
}
|
|
101
|
+
get name() {
|
|
102
|
+
return this.options.name;
|
|
103
|
+
}
|
|
104
|
+
get trigger() {
|
|
105
|
+
return this.options.trigger;
|
|
106
|
+
}
|
|
107
|
+
get version() {
|
|
108
|
+
return this.options.version;
|
|
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
|
+
get logLevel() {
|
|
122
|
+
return this.options.logLevel;
|
|
123
|
+
}
|
|
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
|
+
};
|
|
140
|
+
__name(Job, "Job");
|
|
141
|
+
_validate = new WeakSet();
|
|
142
|
+
validate_fn = /* @__PURE__ */ __name(function() {
|
|
143
|
+
if (!this.version.match(/^(\d+)\.(\d+)\.(\d+)$/)) {
|
|
144
|
+
throw new Error(`Invalid job version: "${this.version}". Job versions must be valid semver versions.`);
|
|
145
|
+
}
|
|
146
|
+
}, "#validate");
|
|
147
|
+
|
|
148
|
+
// src/triggerClient.ts
|
|
149
|
+
var import_core5 = require("@trigger.dev/core");
|
|
150
|
+
|
|
151
|
+
// src/apiClient.ts
|
|
152
|
+
var import_core = require("@trigger.dev/core");
|
|
153
|
+
var import_node_fetch = __toESM(require("node-fetch"));
|
|
154
|
+
var import_zod = require("zod");
|
|
155
|
+
var _apiUrl, _options, _logger, _apiKey, apiKey_fn;
|
|
156
|
+
var ApiClient = class {
|
|
157
|
+
constructor(options) {
|
|
158
|
+
__privateAdd(this, _apiKey);
|
|
159
|
+
__privateAdd(this, _apiUrl, void 0);
|
|
160
|
+
__privateAdd(this, _options, void 0);
|
|
161
|
+
__privateAdd(this, _logger, void 0);
|
|
162
|
+
__privateSet(this, _options, options);
|
|
163
|
+
__privateSet(this, _apiUrl, __privateGet(this, _options).apiUrl ?? process.env.TRIGGER_API_URL ?? "https://api.trigger.dev");
|
|
164
|
+
__privateSet(this, _logger, new import_core.Logger("trigger.dev", __privateGet(this, _options).logLevel));
|
|
165
|
+
}
|
|
166
|
+
async registerEndpoint(options) {
|
|
167
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
168
|
+
__privateGet(this, _logger).debug("Registering endpoint", {
|
|
169
|
+
url: options.url,
|
|
170
|
+
name: options.name
|
|
171
|
+
});
|
|
172
|
+
const response = await (0, import_node_fetch.default)(`${__privateGet(this, _apiUrl)}/api/v1/endpoints`, {
|
|
173
|
+
method: "POST",
|
|
174
|
+
headers: {
|
|
175
|
+
"Content-Type": "application/json",
|
|
176
|
+
Authorization: `Bearer ${apiKey}`
|
|
177
|
+
},
|
|
178
|
+
body: JSON.stringify({
|
|
179
|
+
url: options.url,
|
|
180
|
+
name: options.name
|
|
181
|
+
})
|
|
182
|
+
});
|
|
183
|
+
if (response.status >= 400 && response.status < 500) {
|
|
184
|
+
const body = await response.json();
|
|
185
|
+
throw new Error(body.error);
|
|
186
|
+
}
|
|
187
|
+
if (response.status !== 200) {
|
|
188
|
+
throw new Error(`Failed to register entry point, got status code ${response.status}`);
|
|
189
|
+
}
|
|
190
|
+
return await response.json();
|
|
191
|
+
}
|
|
192
|
+
async runTask(runId, task) {
|
|
193
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
194
|
+
__privateGet(this, _logger).debug("Running Task", {
|
|
195
|
+
task
|
|
196
|
+
});
|
|
197
|
+
return await zodfetch(import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks`, {
|
|
198
|
+
method: "POST",
|
|
199
|
+
headers: {
|
|
200
|
+
"Content-Type": "application/json",
|
|
201
|
+
Authorization: `Bearer ${apiKey}`,
|
|
202
|
+
"Idempotency-Key": task.idempotencyKey
|
|
203
|
+
},
|
|
204
|
+
body: JSON.stringify(task)
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async completeTask(runId, id, task) {
|
|
208
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
209
|
+
__privateGet(this, _logger).debug("Complete Task", {
|
|
210
|
+
task
|
|
211
|
+
});
|
|
212
|
+
return await zodfetch(import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks/${id}/complete`, {
|
|
213
|
+
method: "POST",
|
|
214
|
+
headers: {
|
|
215
|
+
"Content-Type": "application/json",
|
|
216
|
+
Authorization: `Bearer ${apiKey}`
|
|
217
|
+
},
|
|
218
|
+
body: JSON.stringify(task)
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
async failTask(runId, id, body) {
|
|
222
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
223
|
+
__privateGet(this, _logger).debug("Fail Task", {
|
|
224
|
+
id,
|
|
225
|
+
runId,
|
|
226
|
+
body
|
|
227
|
+
});
|
|
228
|
+
return await zodfetch(import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks/${id}/fail`, {
|
|
229
|
+
method: "POST",
|
|
230
|
+
headers: {
|
|
231
|
+
"Content-Type": "application/json",
|
|
232
|
+
Authorization: `Bearer ${apiKey}`
|
|
233
|
+
},
|
|
234
|
+
body: JSON.stringify(body)
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
async sendEvent(event, options = {}) {
|
|
238
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
239
|
+
__privateGet(this, _logger).debug("Sending event", {
|
|
240
|
+
event
|
|
241
|
+
});
|
|
242
|
+
return await zodfetch(import_core.ApiEventLogSchema, `${__privateGet(this, _apiUrl)}/api/v1/events`, {
|
|
243
|
+
method: "POST",
|
|
244
|
+
headers: {
|
|
245
|
+
"Content-Type": "application/json",
|
|
246
|
+
Authorization: `Bearer ${apiKey}`
|
|
247
|
+
},
|
|
248
|
+
body: JSON.stringify({
|
|
249
|
+
event,
|
|
250
|
+
options
|
|
251
|
+
})
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async cancelEvent(eventId) {
|
|
255
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
256
|
+
__privateGet(this, _logger).debug("Cancelling event", {
|
|
257
|
+
eventId
|
|
258
|
+
});
|
|
259
|
+
return await zodfetch(import_core.ApiEventLogSchema, `${__privateGet(this, _apiUrl)}/api/v1/events/${eventId}/cancel`, {
|
|
260
|
+
method: "POST",
|
|
261
|
+
headers: {
|
|
262
|
+
"Content-Type": "application/json",
|
|
263
|
+
Authorization: `Bearer ${apiKey}`
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
async updateSource(client, key, source) {
|
|
268
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
269
|
+
__privateGet(this, _logger).debug("activating http source", {
|
|
270
|
+
source
|
|
271
|
+
});
|
|
272
|
+
const response = await zodfetch(import_core.TriggerSourceSchema, `${__privateGet(this, _apiUrl)}/api/v2/${client}/sources/${key}`, {
|
|
273
|
+
method: "PUT",
|
|
274
|
+
headers: {
|
|
275
|
+
"Content-Type": "application/json",
|
|
276
|
+
Authorization: `Bearer ${apiKey}`
|
|
277
|
+
},
|
|
278
|
+
body: JSON.stringify(source)
|
|
279
|
+
});
|
|
280
|
+
return response;
|
|
281
|
+
}
|
|
282
|
+
async registerTrigger(client, id, key, payload) {
|
|
283
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
284
|
+
__privateGet(this, _logger).debug("registering trigger", {
|
|
285
|
+
id,
|
|
286
|
+
payload
|
|
287
|
+
});
|
|
288
|
+
const response = await zodfetch(import_core.RegisterSourceEventSchemaV2, `${__privateGet(this, _apiUrl)}/api/v2/${client}/triggers/${id}/registrations/${key}`, {
|
|
289
|
+
method: "PUT",
|
|
290
|
+
headers: {
|
|
291
|
+
"Content-Type": "application/json",
|
|
292
|
+
Authorization: `Bearer ${apiKey}`
|
|
293
|
+
},
|
|
294
|
+
body: JSON.stringify(payload)
|
|
295
|
+
});
|
|
296
|
+
return response;
|
|
297
|
+
}
|
|
298
|
+
async registerSchedule(client, id, key, payload) {
|
|
299
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
300
|
+
__privateGet(this, _logger).debug("registering schedule", {
|
|
301
|
+
id,
|
|
302
|
+
payload
|
|
303
|
+
});
|
|
304
|
+
const response = await zodfetch(import_core.RegisterScheduleResponseBodySchema, `${__privateGet(this, _apiUrl)}/api/v1/${client}/schedules/${id}/registrations`, {
|
|
305
|
+
method: "POST",
|
|
306
|
+
headers: {
|
|
307
|
+
"Content-Type": "application/json",
|
|
308
|
+
Authorization: `Bearer ${apiKey}`
|
|
309
|
+
},
|
|
310
|
+
body: JSON.stringify({
|
|
311
|
+
id: key,
|
|
312
|
+
...payload
|
|
313
|
+
})
|
|
314
|
+
});
|
|
315
|
+
return response;
|
|
316
|
+
}
|
|
317
|
+
async unregisterSchedule(client, id, key) {
|
|
318
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
319
|
+
__privateGet(this, _logger).debug("unregistering schedule", {
|
|
320
|
+
id
|
|
321
|
+
});
|
|
322
|
+
const response = await zodfetch(import_zod.z.object({
|
|
323
|
+
ok: import_zod.z.boolean()
|
|
324
|
+
}), `${__privateGet(this, _apiUrl)}/api/v1/${client}/schedules/${id}/registrations/${encodeURIComponent(key)}`, {
|
|
325
|
+
method: "DELETE",
|
|
326
|
+
headers: {
|
|
327
|
+
"Content-Type": "application/json",
|
|
328
|
+
Authorization: `Bearer ${apiKey}`
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
return response;
|
|
332
|
+
}
|
|
333
|
+
async getAuth(client, id) {
|
|
334
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
335
|
+
__privateGet(this, _logger).debug("getting auth", {
|
|
336
|
+
id
|
|
337
|
+
});
|
|
338
|
+
const response = await zodfetch(import_core.ConnectionAuthSchema, `${__privateGet(this, _apiUrl)}/api/v1/${client}/auth/${id}`, {
|
|
339
|
+
method: "GET",
|
|
340
|
+
headers: {
|
|
341
|
+
Accept: "application/json",
|
|
342
|
+
Authorization: `Bearer ${apiKey}`
|
|
343
|
+
}
|
|
344
|
+
}, {
|
|
345
|
+
optional: true
|
|
346
|
+
});
|
|
347
|
+
return response;
|
|
348
|
+
}
|
|
349
|
+
async getEvent(eventId) {
|
|
350
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
351
|
+
__privateGet(this, _logger).debug("Getting Event", {
|
|
352
|
+
eventId
|
|
353
|
+
});
|
|
354
|
+
return await zodfetch(import_core.GetEventSchema, `${__privateGet(this, _apiUrl)}/api/v1/events/${eventId}`, {
|
|
355
|
+
method: "GET",
|
|
356
|
+
headers: {
|
|
357
|
+
Authorization: `Bearer ${apiKey}`
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
async getRun(runId, options) {
|
|
362
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
363
|
+
__privateGet(this, _logger).debug("Getting Run", {
|
|
364
|
+
runId
|
|
365
|
+
});
|
|
366
|
+
return await zodfetch(import_core.GetRunSchema, (0, import_core.urlWithSearchParams)(`${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}`, options), {
|
|
367
|
+
method: "GET",
|
|
368
|
+
headers: {
|
|
369
|
+
Authorization: `Bearer ${apiKey}`
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async getRuns(jobSlug, options) {
|
|
374
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
375
|
+
__privateGet(this, _logger).debug("Getting Runs", {
|
|
376
|
+
jobSlug
|
|
377
|
+
});
|
|
378
|
+
return await zodfetch(import_core.GetRunsSchema, (0, import_core.urlWithSearchParams)(`${__privateGet(this, _apiUrl)}/api/v1/jobs/${jobSlug}/runs`, options), {
|
|
379
|
+
method: "GET",
|
|
380
|
+
headers: {
|
|
381
|
+
Authorization: `Bearer ${apiKey}`
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
__name(ApiClient, "ApiClient");
|
|
387
|
+
_apiUrl = new WeakMap();
|
|
388
|
+
_options = new WeakMap();
|
|
389
|
+
_logger = new WeakMap();
|
|
390
|
+
_apiKey = new WeakSet();
|
|
391
|
+
apiKey_fn = /* @__PURE__ */ __name(async function() {
|
|
392
|
+
const apiKey = getApiKey(__privateGet(this, _options).apiKey);
|
|
393
|
+
if (apiKey.status === "invalid") {
|
|
394
|
+
throw new Error("Invalid API key");
|
|
395
|
+
} else if (apiKey.status === "missing") {
|
|
396
|
+
throw new Error("Missing API key");
|
|
397
|
+
}
|
|
398
|
+
return apiKey.apiKey;
|
|
399
|
+
}, "#apiKey");
|
|
400
|
+
function getApiKey(key) {
|
|
401
|
+
const apiKey = key ?? process.env.TRIGGER_API_KEY;
|
|
402
|
+
if (!apiKey) {
|
|
403
|
+
return {
|
|
404
|
+
status: "missing"
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
const isValid = apiKey.match(/^tr_[a-z]+_[a-zA-Z0-9]+$/);
|
|
408
|
+
if (!isValid) {
|
|
409
|
+
return {
|
|
410
|
+
status: "invalid",
|
|
411
|
+
apiKey
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
return {
|
|
415
|
+
status: "valid",
|
|
416
|
+
apiKey
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
__name(getApiKey, "getApiKey");
|
|
420
|
+
async function zodfetch(schema, url, requestInit, options) {
|
|
421
|
+
const response = await (0, import_node_fetch.default)(url, requestInit);
|
|
422
|
+
if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (response.status >= 400 && response.status < 500) {
|
|
426
|
+
const body = await response.json();
|
|
427
|
+
throw new Error(body.error);
|
|
428
|
+
}
|
|
429
|
+
if (response.status !== 200) {
|
|
430
|
+
throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
|
|
431
|
+
}
|
|
432
|
+
const jsonBody = await response.json();
|
|
433
|
+
return schema.parse(jsonBody);
|
|
434
|
+
}
|
|
435
|
+
__name(zodfetch, "zodfetch");
|
|
436
|
+
|
|
437
|
+
// src/errors.ts
|
|
438
|
+
var ResumeWithTaskError = class {
|
|
439
|
+
constructor(task) {
|
|
440
|
+
this.task = task;
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
__name(ResumeWithTaskError, "ResumeWithTaskError");
|
|
444
|
+
var RetryWithTaskError = class {
|
|
445
|
+
constructor(cause, task, retryAt) {
|
|
446
|
+
this.cause = cause;
|
|
447
|
+
this.task = task;
|
|
448
|
+
this.retryAt = retryAt;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
__name(RetryWithTaskError, "RetryWithTaskError");
|
|
452
|
+
var CanceledWithTaskError = class {
|
|
453
|
+
constructor(task) {
|
|
454
|
+
this.task = task;
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
__name(CanceledWithTaskError, "CanceledWithTaskError");
|
|
458
|
+
function isTriggerError(err) {
|
|
459
|
+
return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError;
|
|
460
|
+
}
|
|
461
|
+
__name(isTriggerError, "isTriggerError");
|
|
462
|
+
|
|
463
|
+
// src/io.ts
|
|
464
|
+
var import_core3 = require("@trigger.dev/core");
|
|
465
|
+
var import_node_async_hooks = require("async_hooks");
|
|
466
|
+
var import_node_crypto = require("crypto");
|
|
467
|
+
|
|
468
|
+
// src/retry.ts
|
|
469
|
+
var import_core2 = require("@trigger.dev/core");
|
|
470
|
+
var retry = {
|
|
471
|
+
standardBackoff: {
|
|
472
|
+
limit: 8,
|
|
473
|
+
factor: 1.8,
|
|
474
|
+
minTimeoutInMs: 500,
|
|
475
|
+
maxTimeoutInMs: 3e4,
|
|
476
|
+
randomize: true
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
// src/io.ts
|
|
481
|
+
var _addToCachedTasks, addToCachedTasks_fn;
|
|
482
|
+
var IO = class {
|
|
483
|
+
constructor(options) {
|
|
484
|
+
__privateAdd(this, _addToCachedTasks);
|
|
485
|
+
this._id = options.id;
|
|
486
|
+
this._apiClient = options.apiClient;
|
|
487
|
+
this._triggerClient = options.client;
|
|
488
|
+
this._logger = options.logger ?? new import_core3.Logger("trigger.dev", options.logLevel);
|
|
489
|
+
this._cachedTasks = /* @__PURE__ */ new Map();
|
|
490
|
+
this._jobLogger = options.jobLogger;
|
|
491
|
+
this._jobLogLevel = options.jobLogLevel;
|
|
492
|
+
if (options.cachedTasks) {
|
|
493
|
+
options.cachedTasks.forEach((task) => {
|
|
494
|
+
this._cachedTasks.set(task.idempotencyKey, task);
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
this._taskStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
498
|
+
this._context = options.context;
|
|
499
|
+
}
|
|
500
|
+
get logger() {
|
|
501
|
+
return new IOLogger(async (level, message, data) => {
|
|
502
|
+
let logLevel = "info";
|
|
503
|
+
switch (level) {
|
|
504
|
+
case "LOG": {
|
|
505
|
+
this._jobLogger?.log(message, data);
|
|
506
|
+
logLevel = "log";
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
case "DEBUG": {
|
|
510
|
+
this._jobLogger?.debug(message, data);
|
|
511
|
+
logLevel = "debug";
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
case "INFO": {
|
|
515
|
+
this._jobLogger?.info(message, data);
|
|
516
|
+
logLevel = "info";
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
case "WARN": {
|
|
520
|
+
this._jobLogger?.warn(message, data);
|
|
521
|
+
logLevel = "warn";
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
case "ERROR": {
|
|
525
|
+
this._jobLogger?.error(message, data);
|
|
526
|
+
logLevel = "error";
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (import_core3.Logger.satisfiesLogLevel(logLevel, this._jobLogLevel)) {
|
|
531
|
+
await this.runTask([
|
|
532
|
+
message,
|
|
533
|
+
level
|
|
534
|
+
], async (task) => {
|
|
535
|
+
}, {
|
|
536
|
+
name: "log",
|
|
537
|
+
icon: "log",
|
|
538
|
+
description: message,
|
|
539
|
+
params: data,
|
|
540
|
+
properties: [
|
|
541
|
+
{
|
|
542
|
+
label: "Level",
|
|
543
|
+
text: level
|
|
544
|
+
}
|
|
545
|
+
],
|
|
546
|
+
style: {
|
|
547
|
+
style: "minimal",
|
|
548
|
+
variant: level.toLowerCase()
|
|
549
|
+
},
|
|
550
|
+
noop: true
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
async wait(key, seconds) {
|
|
556
|
+
return await this.runTask(key, async (task) => {
|
|
557
|
+
}, {
|
|
558
|
+
name: "wait",
|
|
559
|
+
icon: "clock",
|
|
560
|
+
params: {
|
|
561
|
+
seconds
|
|
562
|
+
},
|
|
563
|
+
noop: true,
|
|
564
|
+
delayUntil: new Date(Date.now() + seconds * 1e3),
|
|
565
|
+
style: {
|
|
566
|
+
style: "minimal"
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
async backgroundFetch(key, url, requestInit, retry2) {
|
|
571
|
+
const urlObject = new URL(url);
|
|
572
|
+
return await this.runTask(key, async (task) => {
|
|
573
|
+
return task.output;
|
|
574
|
+
}, {
|
|
575
|
+
name: `fetch ${urlObject.hostname}${urlObject.pathname}`,
|
|
576
|
+
params: {
|
|
577
|
+
url,
|
|
578
|
+
requestInit,
|
|
579
|
+
retry: retry2
|
|
580
|
+
},
|
|
581
|
+
operation: "fetch",
|
|
582
|
+
icon: "background",
|
|
583
|
+
noop: false,
|
|
584
|
+
properties: [
|
|
585
|
+
{
|
|
586
|
+
label: "url",
|
|
587
|
+
text: url,
|
|
588
|
+
url
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
label: "method",
|
|
592
|
+
text: requestInit?.method ?? "GET"
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
label: "background",
|
|
596
|
+
text: "true"
|
|
597
|
+
}
|
|
598
|
+
]
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
async sendEvent(key, event, options) {
|
|
602
|
+
return await this.runTask(key, async (task) => {
|
|
603
|
+
return await this._triggerClient.sendEvent(event, options);
|
|
604
|
+
}, {
|
|
605
|
+
name: "sendEvent",
|
|
606
|
+
params: {
|
|
607
|
+
event,
|
|
608
|
+
options
|
|
609
|
+
},
|
|
610
|
+
properties: [
|
|
611
|
+
{
|
|
612
|
+
label: "name",
|
|
613
|
+
text: event.name
|
|
614
|
+
},
|
|
615
|
+
...event?.id ? [
|
|
616
|
+
{
|
|
617
|
+
label: "ID",
|
|
618
|
+
text: event.id
|
|
619
|
+
}
|
|
620
|
+
] : []
|
|
621
|
+
]
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
async getEvent(key, id) {
|
|
625
|
+
return await this.runTask(key, async (task) => {
|
|
626
|
+
return await this._triggerClient.getEvent(id);
|
|
627
|
+
}, {
|
|
628
|
+
name: "getEvent",
|
|
629
|
+
params: {
|
|
630
|
+
id
|
|
631
|
+
},
|
|
632
|
+
properties: [
|
|
633
|
+
{
|
|
634
|
+
label: "id",
|
|
635
|
+
text: id
|
|
636
|
+
}
|
|
637
|
+
]
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
async cancelEvent(key, eventId) {
|
|
641
|
+
return await this.runTask(key, async (task) => {
|
|
642
|
+
return await this._triggerClient.cancelEvent(eventId);
|
|
643
|
+
}, {
|
|
644
|
+
name: "cancelEvent",
|
|
645
|
+
params: {
|
|
646
|
+
eventId
|
|
647
|
+
},
|
|
648
|
+
properties: [
|
|
649
|
+
{
|
|
650
|
+
label: "id",
|
|
651
|
+
text: eventId
|
|
652
|
+
}
|
|
653
|
+
]
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
async updateSource(key, options) {
|
|
657
|
+
return this.runTask(key, async (task) => {
|
|
658
|
+
return await this._apiClient.updateSource(this._triggerClient.id, options.key, options);
|
|
659
|
+
}, {
|
|
660
|
+
name: "Update Source",
|
|
661
|
+
description: "Update Source",
|
|
662
|
+
properties: [
|
|
663
|
+
{
|
|
664
|
+
label: "key",
|
|
665
|
+
text: options.key
|
|
666
|
+
}
|
|
667
|
+
],
|
|
668
|
+
params: options,
|
|
669
|
+
redact: {
|
|
670
|
+
paths: [
|
|
671
|
+
"secret"
|
|
672
|
+
]
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
async registerInterval(key, dynamicSchedule, id, options) {
|
|
677
|
+
return await this.runTask(key, async (task) => {
|
|
678
|
+
return dynamicSchedule.register(id, {
|
|
679
|
+
type: "interval",
|
|
680
|
+
options
|
|
681
|
+
});
|
|
682
|
+
}, {
|
|
683
|
+
name: "register-interval",
|
|
684
|
+
properties: [
|
|
685
|
+
{
|
|
686
|
+
label: "schedule",
|
|
687
|
+
text: dynamicSchedule.id
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
label: "id",
|
|
691
|
+
text: id
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
label: "seconds",
|
|
695
|
+
text: options.seconds.toString()
|
|
696
|
+
}
|
|
697
|
+
],
|
|
698
|
+
params: options
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
async unregisterInterval(key, dynamicSchedule, id) {
|
|
702
|
+
return await this.runTask(key, async (task) => {
|
|
703
|
+
return dynamicSchedule.unregister(id);
|
|
704
|
+
}, {
|
|
705
|
+
name: "unregister-interval",
|
|
706
|
+
properties: [
|
|
707
|
+
{
|
|
708
|
+
label: "schedule",
|
|
709
|
+
text: dynamicSchedule.id
|
|
710
|
+
},
|
|
711
|
+
{
|
|
712
|
+
label: "id",
|
|
713
|
+
text: id
|
|
714
|
+
}
|
|
715
|
+
]
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
async registerCron(key, dynamicSchedule, id, options) {
|
|
719
|
+
return await this.runTask(key, async (task) => {
|
|
720
|
+
return dynamicSchedule.register(id, {
|
|
721
|
+
type: "cron",
|
|
722
|
+
options
|
|
723
|
+
});
|
|
724
|
+
}, {
|
|
725
|
+
name: "register-cron",
|
|
726
|
+
properties: [
|
|
727
|
+
{
|
|
728
|
+
label: "schedule",
|
|
729
|
+
text: dynamicSchedule.id
|
|
730
|
+
},
|
|
731
|
+
{
|
|
732
|
+
label: "id",
|
|
733
|
+
text: id
|
|
734
|
+
},
|
|
735
|
+
{
|
|
736
|
+
label: "cron",
|
|
737
|
+
text: options.cron
|
|
738
|
+
}
|
|
739
|
+
],
|
|
740
|
+
params: options
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
async unregisterCron(key, dynamicSchedule, id) {
|
|
744
|
+
return await this.runTask(key, async (task) => {
|
|
745
|
+
return dynamicSchedule.unregister(id);
|
|
746
|
+
}, {
|
|
747
|
+
name: "unregister-cron",
|
|
748
|
+
properties: [
|
|
749
|
+
{
|
|
750
|
+
label: "schedule",
|
|
751
|
+
text: dynamicSchedule.id
|
|
752
|
+
},
|
|
753
|
+
{
|
|
754
|
+
label: "id",
|
|
755
|
+
text: id
|
|
756
|
+
}
|
|
757
|
+
]
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
async registerTrigger(key, trigger, id, params) {
|
|
761
|
+
return await this.runTask(key, async (task) => {
|
|
762
|
+
const registration = await this.runTask("register-source", async (subtask1) => {
|
|
763
|
+
return trigger.register(id, params);
|
|
764
|
+
}, {
|
|
765
|
+
name: "register-source"
|
|
766
|
+
});
|
|
767
|
+
return {
|
|
768
|
+
id: registration.id,
|
|
769
|
+
key: registration.source.key
|
|
770
|
+
};
|
|
771
|
+
}, {
|
|
772
|
+
name: "register-trigger",
|
|
773
|
+
properties: [
|
|
774
|
+
{
|
|
775
|
+
label: "trigger",
|
|
776
|
+
text: trigger.id
|
|
777
|
+
},
|
|
778
|
+
{
|
|
779
|
+
label: "id",
|
|
780
|
+
text: id
|
|
781
|
+
}
|
|
782
|
+
],
|
|
783
|
+
params
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
async getAuth(key, clientId) {
|
|
787
|
+
if (!clientId) {
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
return this.runTask(key, async (task) => {
|
|
791
|
+
return await this._triggerClient.getAuth(clientId);
|
|
792
|
+
}, {
|
|
793
|
+
name: "get-auth"
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
async runTask(key, callback, options, onError) {
|
|
797
|
+
const parentId = this._taskStorage.getStore()?.taskId;
|
|
798
|
+
if (parentId) {
|
|
799
|
+
this._logger.debug("Using parent task", {
|
|
800
|
+
parentId,
|
|
801
|
+
key,
|
|
802
|
+
options
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
const idempotencyKey = await generateIdempotencyKey([
|
|
806
|
+
this._id,
|
|
807
|
+
parentId ?? "",
|
|
808
|
+
key
|
|
809
|
+
].flat());
|
|
810
|
+
const cachedTask = this._cachedTasks.get(idempotencyKey);
|
|
811
|
+
if (cachedTask && cachedTask.status === "COMPLETED") {
|
|
812
|
+
this._logger.debug("Using completed cached task", {
|
|
813
|
+
idempotencyKey,
|
|
814
|
+
cachedTask
|
|
815
|
+
});
|
|
816
|
+
return cachedTask.output;
|
|
817
|
+
}
|
|
818
|
+
const task = await this._apiClient.runTask(this._id, {
|
|
819
|
+
idempotencyKey,
|
|
820
|
+
displayKey: typeof key === "string" ? key : void 0,
|
|
821
|
+
noop: false,
|
|
822
|
+
...options ?? {},
|
|
823
|
+
parentId
|
|
824
|
+
});
|
|
825
|
+
if (task.status === "CANCELED") {
|
|
826
|
+
this._logger.debug("Task canceled", {
|
|
827
|
+
idempotencyKey,
|
|
828
|
+
task
|
|
829
|
+
});
|
|
830
|
+
throw new CanceledWithTaskError(task);
|
|
831
|
+
}
|
|
832
|
+
if (task.status === "COMPLETED") {
|
|
833
|
+
this._logger.debug("Using task output", {
|
|
834
|
+
idempotencyKey,
|
|
835
|
+
task
|
|
836
|
+
});
|
|
837
|
+
__privateMethod(this, _addToCachedTasks, addToCachedTasks_fn).call(this, task);
|
|
838
|
+
return task.output;
|
|
839
|
+
}
|
|
840
|
+
if (task.status === "ERRORED") {
|
|
841
|
+
this._logger.debug("Task errored", {
|
|
842
|
+
idempotencyKey,
|
|
843
|
+
task
|
|
844
|
+
});
|
|
845
|
+
throw new Error(task.error ?? "Task errored");
|
|
846
|
+
}
|
|
847
|
+
if (task.status === "WAITING") {
|
|
848
|
+
this._logger.debug("Task waiting", {
|
|
849
|
+
idempotencyKey,
|
|
850
|
+
task
|
|
851
|
+
});
|
|
852
|
+
throw new ResumeWithTaskError(task);
|
|
853
|
+
}
|
|
854
|
+
if (task.status === "RUNNING" && typeof task.operation === "string") {
|
|
855
|
+
this._logger.debug("Task running operation", {
|
|
856
|
+
idempotencyKey,
|
|
857
|
+
task
|
|
858
|
+
});
|
|
859
|
+
throw new ResumeWithTaskError(task);
|
|
860
|
+
}
|
|
861
|
+
const executeTask = /* @__PURE__ */ __name(async () => {
|
|
862
|
+
try {
|
|
863
|
+
const result = await callback(task, this);
|
|
864
|
+
const output = import_core3.SerializableJsonSchema.parse(result);
|
|
865
|
+
this._logger.debug("Completing using output", {
|
|
866
|
+
idempotencyKey,
|
|
867
|
+
task
|
|
868
|
+
});
|
|
869
|
+
const completedTask = await this._apiClient.completeTask(this._id, task.id, {
|
|
870
|
+
output: output ?? void 0,
|
|
871
|
+
properties: task.outputProperties ?? void 0
|
|
872
|
+
});
|
|
873
|
+
if (completedTask.status === "CANCELED") {
|
|
874
|
+
throw new CanceledWithTaskError(completedTask);
|
|
875
|
+
}
|
|
876
|
+
return output;
|
|
877
|
+
} catch (error) {
|
|
878
|
+
if (isTriggerError(error)) {
|
|
879
|
+
throw error;
|
|
880
|
+
}
|
|
881
|
+
if (onError) {
|
|
882
|
+
try {
|
|
883
|
+
const onErrorResult = onError(error, task, this);
|
|
884
|
+
if (onErrorResult) {
|
|
885
|
+
if (onErrorResult instanceof Error) {
|
|
886
|
+
error = onErrorResult;
|
|
887
|
+
} else {
|
|
888
|
+
const parsedError2 = import_core3.ErrorWithStackSchema.safeParse(onErrorResult.error);
|
|
889
|
+
throw new RetryWithTaskError(parsedError2.success ? parsedError2.data : {
|
|
890
|
+
message: "Unknown error"
|
|
891
|
+
}, task, onErrorResult.retryAt);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
} catch (innerError) {
|
|
895
|
+
if (isTriggerError(innerError)) {
|
|
896
|
+
throw innerError;
|
|
897
|
+
}
|
|
898
|
+
error = innerError;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
const parsedError = import_core3.ErrorWithStackSchema.safeParse(error);
|
|
902
|
+
if (options?.retry) {
|
|
903
|
+
const retryAt = (0, import_core2.calculateRetryAt)(options.retry, task.attempts - 1);
|
|
904
|
+
if (retryAt) {
|
|
905
|
+
throw new RetryWithTaskError(parsedError.success ? parsedError.data : {
|
|
906
|
+
message: "Unknown error"
|
|
907
|
+
}, task, retryAt);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
if (parsedError.success) {
|
|
911
|
+
await this._apiClient.failTask(this._id, task.id, {
|
|
912
|
+
error: parsedError.data
|
|
913
|
+
});
|
|
914
|
+
} else {
|
|
915
|
+
await this._apiClient.failTask(this._id, task.id, {
|
|
916
|
+
error: {
|
|
917
|
+
message: JSON.stringify(error),
|
|
918
|
+
name: "Unknown Error"
|
|
919
|
+
}
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
throw error;
|
|
923
|
+
}
|
|
924
|
+
}, "executeTask");
|
|
925
|
+
return this._taskStorage.run({
|
|
926
|
+
taskId: task.id
|
|
927
|
+
}, executeTask);
|
|
928
|
+
}
|
|
929
|
+
async try(tryCallback, catchCallback) {
|
|
930
|
+
try {
|
|
931
|
+
return await tryCallback();
|
|
932
|
+
} catch (error) {
|
|
933
|
+
if (isTriggerError(error)) {
|
|
934
|
+
throw error;
|
|
935
|
+
}
|
|
936
|
+
return await catchCallback(error);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
__name(IO, "IO");
|
|
941
|
+
_addToCachedTasks = new WeakSet();
|
|
942
|
+
addToCachedTasks_fn = /* @__PURE__ */ __name(function(task) {
|
|
943
|
+
this._cachedTasks.set(task.idempotencyKey, task);
|
|
944
|
+
}, "#addToCachedTasks");
|
|
945
|
+
async function generateIdempotencyKey(keyMaterial) {
|
|
946
|
+
const keys = keyMaterial.map((key2) => {
|
|
947
|
+
if (typeof key2 === "string") {
|
|
948
|
+
return key2;
|
|
949
|
+
}
|
|
950
|
+
return stableStringify(key2);
|
|
951
|
+
});
|
|
952
|
+
const key = keys.join(":");
|
|
953
|
+
const hash = await import_node_crypto.webcrypto.subtle.digest("SHA-256", Buffer.from(key));
|
|
954
|
+
return Buffer.from(hash).toString("hex");
|
|
955
|
+
}
|
|
956
|
+
__name(generateIdempotencyKey, "generateIdempotencyKey");
|
|
957
|
+
function stableStringify(obj) {
|
|
958
|
+
function sortKeys(obj2) {
|
|
959
|
+
if (typeof obj2 !== "object" || obj2 === null) {
|
|
960
|
+
return obj2;
|
|
961
|
+
}
|
|
962
|
+
if (Array.isArray(obj2)) {
|
|
963
|
+
return obj2.map(sortKeys);
|
|
964
|
+
}
|
|
965
|
+
const sortedKeys = Object.keys(obj2).sort();
|
|
966
|
+
const sortedObj2 = {};
|
|
967
|
+
for (const key of sortedKeys) {
|
|
968
|
+
sortedObj2[key] = sortKeys(obj2[key]);
|
|
969
|
+
}
|
|
970
|
+
return sortedObj2;
|
|
971
|
+
}
|
|
972
|
+
__name(sortKeys, "sortKeys");
|
|
973
|
+
const sortedObj = sortKeys(obj);
|
|
974
|
+
return JSON.stringify(sortedObj);
|
|
975
|
+
}
|
|
976
|
+
__name(stableStringify, "stableStringify");
|
|
977
|
+
var IOLogger = class {
|
|
978
|
+
constructor(callback) {
|
|
979
|
+
this.callback = callback;
|
|
980
|
+
}
|
|
981
|
+
log(message, properties) {
|
|
982
|
+
return this.callback("LOG", message, properties);
|
|
983
|
+
}
|
|
984
|
+
debug(message, properties) {
|
|
985
|
+
return this.callback("DEBUG", message, properties);
|
|
986
|
+
}
|
|
987
|
+
info(message, properties) {
|
|
988
|
+
return this.callback("INFO", message, properties);
|
|
989
|
+
}
|
|
990
|
+
warn(message, properties) {
|
|
991
|
+
return this.callback("WARN", message, properties);
|
|
992
|
+
}
|
|
993
|
+
error(message, properties) {
|
|
994
|
+
return this.callback("ERROR", message, properties);
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
__name(IOLogger, "IOLogger");
|
|
998
|
+
|
|
999
|
+
// src/ioWithIntegrations.ts
|
|
1000
|
+
function createIOWithIntegrations(io, auths, integrations) {
|
|
1001
|
+
if (!integrations) {
|
|
1002
|
+
return io;
|
|
1003
|
+
}
|
|
1004
|
+
const connections = Object.entries(integrations).reduce((acc, [connectionKey, integration]) => {
|
|
1005
|
+
let auth = auths?.[connectionKey];
|
|
1006
|
+
acc[connectionKey] = {
|
|
1007
|
+
integration,
|
|
1008
|
+
auth
|
|
1009
|
+
};
|
|
1010
|
+
return acc;
|
|
1011
|
+
}, {});
|
|
1012
|
+
return new Proxy(io, {
|
|
1013
|
+
get(target, prop, receiver) {
|
|
1014
|
+
if (prop === "__io") {
|
|
1015
|
+
return io;
|
|
1016
|
+
}
|
|
1017
|
+
if (typeof prop === "string" && prop in connections) {
|
|
1018
|
+
const { integration, auth } = connections[prop];
|
|
1019
|
+
return integration.cloneForRun(io, prop, auth);
|
|
1020
|
+
}
|
|
1021
|
+
const value = Reflect.get(target, prop, receiver);
|
|
1022
|
+
return typeof value == "function" ? value.bind(target) : value;
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
__name(createIOWithIntegrations, "createIOWithIntegrations");
|
|
1027
|
+
|
|
1028
|
+
// src/triggers/eventTrigger.ts
|
|
1029
|
+
var import_core4 = require("@trigger.dev/core");
|
|
1030
|
+
var _options2;
|
|
1031
|
+
var EventTrigger = class {
|
|
1032
|
+
constructor(options) {
|
|
1033
|
+
__privateAdd(this, _options2, void 0);
|
|
1034
|
+
__privateSet(this, _options2, options);
|
|
1035
|
+
}
|
|
1036
|
+
toJSON() {
|
|
1037
|
+
return {
|
|
1038
|
+
type: "static",
|
|
1039
|
+
title: __privateGet(this, _options2).name ?? __privateGet(this, _options2).event.title,
|
|
1040
|
+
rule: {
|
|
1041
|
+
event: __privateGet(this, _options2).name ?? __privateGet(this, _options2).event.name,
|
|
1042
|
+
source: __privateGet(this, _options2).source ?? "trigger.dev",
|
|
1043
|
+
payload: (0, import_core4.deepMergeFilters)(__privateGet(this, _options2).filter ?? {}, __privateGet(this, _options2).event.filter ?? {})
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
get event() {
|
|
1048
|
+
return __privateGet(this, _options2).event;
|
|
1049
|
+
}
|
|
1050
|
+
attachToJob(triggerClient, job) {
|
|
1051
|
+
}
|
|
1052
|
+
get preprocessRuns() {
|
|
1053
|
+
return false;
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
__name(EventTrigger, "EventTrigger");
|
|
1057
|
+
_options2 = new WeakMap();
|
|
1058
|
+
function eventTrigger(options) {
|
|
1059
|
+
return new EventTrigger({
|
|
1060
|
+
name: options.name,
|
|
1061
|
+
filter: options.filter,
|
|
1062
|
+
event: {
|
|
1063
|
+
name: options.name,
|
|
1064
|
+
title: "Event",
|
|
1065
|
+
source: options.source ?? "trigger.dev",
|
|
1066
|
+
icon: "custom-event",
|
|
1067
|
+
examples: options.examples,
|
|
1068
|
+
parsePayload: (rawPayload) => {
|
|
1069
|
+
if (options.schema) {
|
|
1070
|
+
return options.schema.parse(rawPayload);
|
|
1071
|
+
}
|
|
1072
|
+
return rawPayload;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
__name(eventTrigger, "eventTrigger");
|
|
1078
|
+
|
|
1079
|
+
// src/backgroundTask.ts
|
|
1080
|
+
var _validate2, validate_fn2;
|
|
1081
|
+
var BackgroundTask = class {
|
|
1082
|
+
constructor(client, options) {
|
|
1083
|
+
__privateAdd(this, _validate2);
|
|
1084
|
+
this.client = client;
|
|
1085
|
+
this.options = options;
|
|
1086
|
+
__privateMethod(this, _validate2, validate_fn2).call(this);
|
|
1087
|
+
client.attachBackgroundTask(this);
|
|
1088
|
+
}
|
|
1089
|
+
get id() {
|
|
1090
|
+
return slugifyId(this.options.id);
|
|
1091
|
+
}
|
|
1092
|
+
get enabled() {
|
|
1093
|
+
return typeof this.options.enabled === "boolean" ? this.options.enabled : true;
|
|
1094
|
+
}
|
|
1095
|
+
get name() {
|
|
1096
|
+
return this.options.name;
|
|
1097
|
+
}
|
|
1098
|
+
get schema() {
|
|
1099
|
+
return this.options.schema;
|
|
1100
|
+
}
|
|
1101
|
+
get version() {
|
|
1102
|
+
return this.options.version;
|
|
1103
|
+
}
|
|
1104
|
+
get logLevel() {
|
|
1105
|
+
return this.options.logLevel;
|
|
1106
|
+
}
|
|
1107
|
+
async invoke(key, payload) {
|
|
1108
|
+
}
|
|
1109
|
+
toJSON() {
|
|
1110
|
+
return {
|
|
1111
|
+
id: this.id,
|
|
1112
|
+
name: this.name,
|
|
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 ?? {}
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
__name(BackgroundTask, "BackgroundTask");
|
|
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");
|
|
1129
|
+
|
|
1130
|
+
// src/triggerClient.ts
|
|
1131
|
+
var registerSourceEvent = {
|
|
1132
|
+
name: import_core5.REGISTER_SOURCE_EVENT_V2,
|
|
1133
|
+
title: "Register Source",
|
|
1134
|
+
source: "internal",
|
|
1135
|
+
icon: "register-source",
|
|
1136
|
+
parsePayload: import_core5.RegisterSourceEventSchemaV2.parse
|
|
1137
|
+
};
|
|
1138
|
+
var _options3, _registeredJobs, _registeredSources, _registeredBackgroundTasks, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _client, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn;
|
|
1139
|
+
var TriggerClient = class {
|
|
1140
|
+
constructor(options) {
|
|
1141
|
+
__privateAdd(this, _preprocessRun);
|
|
1142
|
+
__privateAdd(this, _executeJob);
|
|
1143
|
+
__privateAdd(this, _createRunContext);
|
|
1144
|
+
__privateAdd(this, _createPreprocessRunContext);
|
|
1145
|
+
__privateAdd(this, _handleHttpSourceRequest);
|
|
1146
|
+
__privateAdd(this, _options3, void 0);
|
|
1147
|
+
__privateAdd(this, _registeredJobs, {});
|
|
1148
|
+
__privateAdd(this, _registeredSources, {});
|
|
1149
|
+
__privateAdd(this, _registeredBackgroundTasks, {});
|
|
1150
|
+
__privateAdd(this, _registeredHttpSourceHandlers, {});
|
|
1151
|
+
__privateAdd(this, _registeredDynamicTriggers, {});
|
|
1152
|
+
__privateAdd(this, _jobMetadataByDynamicTriggers, {});
|
|
1153
|
+
__privateAdd(this, _registeredSchedules, {});
|
|
1154
|
+
__privateAdd(this, _client, void 0);
|
|
1155
|
+
__privateAdd(this, _internalLogger, void 0);
|
|
1156
|
+
this.id = options.id;
|
|
1157
|
+
__privateSet(this, _options3, options);
|
|
1158
|
+
__privateSet(this, _client, new ApiClient(__privateGet(this, _options3)));
|
|
1159
|
+
__privateSet(this, _internalLogger, new import_core5.Logger("trigger.dev", __privateGet(this, _options3).verbose ? "debug" : "log"));
|
|
1160
|
+
}
|
|
1161
|
+
async handleRequest(request) {
|
|
1162
|
+
__privateGet(this, _internalLogger).debug("handling request", {
|
|
1163
|
+
url: request.url,
|
|
1164
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
1165
|
+
method: request.method
|
|
1166
|
+
});
|
|
1167
|
+
const apiKey = request.headers.get("x-trigger-api-key");
|
|
1168
|
+
const authorization = this.authorized(apiKey);
|
|
1169
|
+
switch (authorization) {
|
|
1170
|
+
case "authorized": {
|
|
1171
|
+
break;
|
|
1172
|
+
}
|
|
1173
|
+
case "missing-client": {
|
|
1174
|
+
return {
|
|
1175
|
+
status: 401,
|
|
1176
|
+
body: {
|
|
1177
|
+
message: "Unauthorized: client missing apiKey"
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
case "missing-header": {
|
|
1182
|
+
return {
|
|
1183
|
+
status: 401,
|
|
1184
|
+
body: {
|
|
1185
|
+
message: "Unauthorized: missing x-trigger-api-key header"
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
case "unauthorized": {
|
|
1190
|
+
return {
|
|
1191
|
+
status: 401,
|
|
1192
|
+
body: {
|
|
1193
|
+
message: `Forbidden: client apiKey mismatch: Make sure you are using the correct API Key for your environment`
|
|
1194
|
+
}
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
if (request.method !== "POST") {
|
|
1199
|
+
return {
|
|
1200
|
+
status: 405,
|
|
1201
|
+
body: {
|
|
1202
|
+
message: "Method not allowed (only POST is allowed)"
|
|
1203
|
+
}
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
const action = request.headers.get("x-trigger-action");
|
|
1207
|
+
if (!action) {
|
|
1208
|
+
return {
|
|
1209
|
+
status: 400,
|
|
1210
|
+
body: {
|
|
1211
|
+
message: "Missing x-trigger-action header"
|
|
1212
|
+
}
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
switch (action) {
|
|
1216
|
+
case "PING": {
|
|
1217
|
+
const endpointId = request.headers.get("x-trigger-endpoint-id");
|
|
1218
|
+
if (!endpointId) {
|
|
1219
|
+
return {
|
|
1220
|
+
status: 200,
|
|
1221
|
+
body: {
|
|
1222
|
+
ok: false,
|
|
1223
|
+
error: "Missing endpoint ID"
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
if (this.id !== endpointId) {
|
|
1228
|
+
return {
|
|
1229
|
+
status: 200,
|
|
1230
|
+
body: {
|
|
1231
|
+
ok: false,
|
|
1232
|
+
error: `Endpoint ID mismatch error. Expected ${this.id}, got ${endpointId}`
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
return {
|
|
1237
|
+
status: 200,
|
|
1238
|
+
body: {
|
|
1239
|
+
ok: true
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
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
|
+
const body = {
|
|
1261
|
+
jobs: Object.values(__privateGet(this, _registeredJobs)).map((job) => job.toJSON()),
|
|
1262
|
+
backgroundTasks: Object.values(__privateGet(this, _registeredBackgroundTasks)).map((task) => task.toJSON()),
|
|
1263
|
+
sources: Object.values(__privateGet(this, _registeredSources)),
|
|
1264
|
+
dynamicTriggers: Object.values(__privateGet(this, _registeredDynamicTriggers)).map((trigger) => ({
|
|
1265
|
+
id: trigger.id,
|
|
1266
|
+
jobs: __privateGet(this, _jobMetadataByDynamicTriggers)[trigger.id] ?? [],
|
|
1267
|
+
registerSourceJob: {
|
|
1268
|
+
id: dynamicTriggerRegisterSourceJobId(trigger.id),
|
|
1269
|
+
version: trigger.source.version
|
|
1270
|
+
}
|
|
1271
|
+
})),
|
|
1272
|
+
dynamicSchedules: Object.entries(__privateGet(this, _registeredSchedules)).map(([id, jobs]) => ({
|
|
1273
|
+
id,
|
|
1274
|
+
jobs
|
|
1275
|
+
}))
|
|
1276
|
+
};
|
|
1277
|
+
return {
|
|
1278
|
+
status: 200,
|
|
1279
|
+
body
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
case "INITIALIZE_TRIGGER": {
|
|
1283
|
+
const json = await request.json();
|
|
1284
|
+
const body = import_core5.InitializeTriggerBodySchema.safeParse(json);
|
|
1285
|
+
if (!body.success) {
|
|
1286
|
+
return {
|
|
1287
|
+
status: 400,
|
|
1288
|
+
body: {
|
|
1289
|
+
message: "Invalid trigger body"
|
|
1290
|
+
}
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
const dynamicTrigger = __privateGet(this, _registeredDynamicTriggers)[body.data.id];
|
|
1294
|
+
if (!dynamicTrigger) {
|
|
1295
|
+
return {
|
|
1296
|
+
status: 404,
|
|
1297
|
+
body: {
|
|
1298
|
+
message: "Dynamic trigger not found"
|
|
1299
|
+
}
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
return {
|
|
1303
|
+
status: 200,
|
|
1304
|
+
body: dynamicTrigger.registeredTriggerForParams(body.data.params)
|
|
1305
|
+
};
|
|
1306
|
+
}
|
|
1307
|
+
case "EXECUTE_JOB": {
|
|
1308
|
+
const json = await request.json();
|
|
1309
|
+
const execution = import_core5.RunJobBodySchema.safeParse(json);
|
|
1310
|
+
if (!execution.success) {
|
|
1311
|
+
return {
|
|
1312
|
+
status: 400,
|
|
1313
|
+
body: {
|
|
1314
|
+
message: "Invalid execution"
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
}
|
|
1318
|
+
const job = __privateGet(this, _registeredJobs)[execution.data.job.id];
|
|
1319
|
+
if (!job) {
|
|
1320
|
+
return {
|
|
1321
|
+
status: 404,
|
|
1322
|
+
body: {
|
|
1323
|
+
message: "Job not found"
|
|
1324
|
+
}
|
|
1325
|
+
};
|
|
1326
|
+
}
|
|
1327
|
+
const results = await __privateMethod(this, _executeJob, executeJob_fn).call(this, execution.data, job);
|
|
1328
|
+
return {
|
|
1329
|
+
status: 200,
|
|
1330
|
+
body: results
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
case "PREPROCESS_RUN": {
|
|
1334
|
+
const json = await request.json();
|
|
1335
|
+
const body = import_core5.PreprocessRunBodySchema.safeParse(json);
|
|
1336
|
+
if (!body.success) {
|
|
1337
|
+
return {
|
|
1338
|
+
status: 400,
|
|
1339
|
+
body: {
|
|
1340
|
+
message: "Invalid body"
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
const job = __privateGet(this, _registeredJobs)[body.data.job.id];
|
|
1345
|
+
if (!job) {
|
|
1346
|
+
return {
|
|
1347
|
+
status: 404,
|
|
1348
|
+
body: {
|
|
1349
|
+
message: "Job not found"
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
const results = await __privateMethod(this, _preprocessRun, preprocessRun_fn).call(this, body.data, job);
|
|
1354
|
+
return {
|
|
1355
|
+
status: 200,
|
|
1356
|
+
body: {
|
|
1357
|
+
abort: results.abort,
|
|
1358
|
+
properties: results.properties
|
|
1359
|
+
}
|
|
1360
|
+
};
|
|
1361
|
+
}
|
|
1362
|
+
case "DELIVER_HTTP_SOURCE_REQUEST": {
|
|
1363
|
+
const headers = import_core5.HttpSourceRequestHeadersSchema.safeParse(Object.fromEntries(request.headers.entries()));
|
|
1364
|
+
if (!headers.success) {
|
|
1365
|
+
return {
|
|
1366
|
+
status: 400,
|
|
1367
|
+
body: {
|
|
1368
|
+
message: "Invalid headers"
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
const sourceRequestNeedsBody = headers.data["x-ts-http-method"] !== "GET";
|
|
1373
|
+
const sourceRequestInit = {
|
|
1374
|
+
method: headers.data["x-ts-http-method"],
|
|
1375
|
+
headers: headers.data["x-ts-http-headers"],
|
|
1376
|
+
body: sourceRequestNeedsBody ? request.body : void 0
|
|
1377
|
+
};
|
|
1378
|
+
if (sourceRequestNeedsBody) {
|
|
1379
|
+
try {
|
|
1380
|
+
sourceRequestInit.duplex = "half";
|
|
1381
|
+
} catch (error) {
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
const sourceRequest = new Request(headers.data["x-ts-http-url"], sourceRequestInit);
|
|
1385
|
+
const key = headers.data["x-ts-key"];
|
|
1386
|
+
const dynamicId = headers.data["x-ts-dynamic-id"];
|
|
1387
|
+
const secret = headers.data["x-ts-secret"];
|
|
1388
|
+
const params = headers.data["x-ts-params"];
|
|
1389
|
+
const data = headers.data["x-ts-data"];
|
|
1390
|
+
const auth = headers.data["x-ts-auth"];
|
|
1391
|
+
const inputMetadata = headers.data["x-ts-metadata"];
|
|
1392
|
+
const source = {
|
|
1393
|
+
key,
|
|
1394
|
+
dynamicId,
|
|
1395
|
+
secret,
|
|
1396
|
+
params,
|
|
1397
|
+
data,
|
|
1398
|
+
auth,
|
|
1399
|
+
metadata: inputMetadata
|
|
1400
|
+
};
|
|
1401
|
+
const { response, events, metadata } = await __privateMethod(this, _handleHttpSourceRequest, handleHttpSourceRequest_fn).call(this, source, sourceRequest);
|
|
1402
|
+
return {
|
|
1403
|
+
status: 200,
|
|
1404
|
+
body: {
|
|
1405
|
+
events,
|
|
1406
|
+
response,
|
|
1407
|
+
metadata
|
|
1408
|
+
}
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
case "VALIDATE": {
|
|
1412
|
+
return {
|
|
1413
|
+
status: 200,
|
|
1414
|
+
body: {
|
|
1415
|
+
ok: true,
|
|
1416
|
+
endpointId: this.id
|
|
1417
|
+
}
|
|
1418
|
+
};
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return {
|
|
1422
|
+
status: 405,
|
|
1423
|
+
body: {
|
|
1424
|
+
message: "Method not allowed"
|
|
1425
|
+
}
|
|
1426
|
+
};
|
|
1427
|
+
}
|
|
1428
|
+
attach(job) {
|
|
1429
|
+
__privateGet(this, _registeredJobs)[job.id] = job;
|
|
1430
|
+
job.trigger.attachToJob(this, job);
|
|
1431
|
+
}
|
|
1432
|
+
attachBackgroundTask(task) {
|
|
1433
|
+
__privateGet(this, _registeredBackgroundTasks)[task.id] = task;
|
|
1434
|
+
}
|
|
1435
|
+
attachDynamicTrigger(trigger) {
|
|
1436
|
+
__privateGet(this, _registeredDynamicTriggers)[trigger.id] = trigger;
|
|
1437
|
+
new Job(this, {
|
|
1438
|
+
id: dynamicTriggerRegisterSourceJobId(trigger.id),
|
|
1439
|
+
name: `Register dynamic trigger ${trigger.id}`,
|
|
1440
|
+
version: trigger.source.version,
|
|
1441
|
+
trigger: new EventTrigger({
|
|
1442
|
+
event: registerSourceEvent,
|
|
1443
|
+
filter: {
|
|
1444
|
+
dynamicTriggerId: [
|
|
1445
|
+
trigger.id
|
|
1446
|
+
]
|
|
1447
|
+
}
|
|
1448
|
+
}),
|
|
1449
|
+
integrations: {
|
|
1450
|
+
integration: trigger.source.integration
|
|
1451
|
+
},
|
|
1452
|
+
run: async (event, io, ctx) => {
|
|
1453
|
+
const updates = await trigger.source.register(event.source.params, event, io, ctx);
|
|
1454
|
+
if (!updates) {
|
|
1455
|
+
return;
|
|
1456
|
+
}
|
|
1457
|
+
return await io.updateSource("update-source", {
|
|
1458
|
+
key: event.source.key,
|
|
1459
|
+
...updates
|
|
1460
|
+
});
|
|
1461
|
+
},
|
|
1462
|
+
__internal: true
|
|
1463
|
+
});
|
|
1464
|
+
}
|
|
1465
|
+
attachJobToDynamicTrigger(job, trigger) {
|
|
1466
|
+
const jobs = __privateGet(this, _jobMetadataByDynamicTriggers)[trigger.id] ?? [];
|
|
1467
|
+
jobs.push({
|
|
1468
|
+
id: job.id,
|
|
1469
|
+
version: job.version
|
|
1470
|
+
});
|
|
1471
|
+
__privateGet(this, _jobMetadataByDynamicTriggers)[trigger.id] = jobs;
|
|
1472
|
+
}
|
|
1473
|
+
attachSource(options) {
|
|
1474
|
+
__privateGet(this, _registeredHttpSourceHandlers)[options.key] = async (s, r) => {
|
|
1475
|
+
return await options.source.handle(s, r, __privateGet(this, _internalLogger));
|
|
1476
|
+
};
|
|
1477
|
+
let registeredSource = __privateGet(this, _registeredSources)[options.key];
|
|
1478
|
+
if (!registeredSource) {
|
|
1479
|
+
registeredSource = {
|
|
1480
|
+
version: "2",
|
|
1481
|
+
channel: options.source.channel,
|
|
1482
|
+
key: options.key,
|
|
1483
|
+
params: options.params,
|
|
1484
|
+
options: {},
|
|
1485
|
+
integration: {
|
|
1486
|
+
id: options.source.integration.id,
|
|
1487
|
+
metadata: options.source.integration.metadata,
|
|
1488
|
+
authSource: options.source.integration.authSource
|
|
1489
|
+
},
|
|
1490
|
+
registerSourceJob: {
|
|
1491
|
+
id: options.key,
|
|
1492
|
+
version: options.source.version
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
const newOptions = deepMergeOptions({
|
|
1497
|
+
event: typeof options.event.name === "string" ? [
|
|
1498
|
+
options.event.name
|
|
1499
|
+
] : options.event.name
|
|
1500
|
+
}, options.options ?? {});
|
|
1501
|
+
registeredSource.options = deepMergeOptions(registeredSource.options, newOptions);
|
|
1502
|
+
__privateGet(this, _registeredSources)[options.key] = registeredSource;
|
|
1503
|
+
new Job(this, {
|
|
1504
|
+
id: options.key,
|
|
1505
|
+
name: options.key,
|
|
1506
|
+
version: options.source.version,
|
|
1507
|
+
trigger: new EventTrigger({
|
|
1508
|
+
event: registerSourceEvent,
|
|
1509
|
+
filter: {
|
|
1510
|
+
source: {
|
|
1511
|
+
key: [
|
|
1512
|
+
options.key
|
|
1513
|
+
]
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
}),
|
|
1517
|
+
integrations: {
|
|
1518
|
+
integration: options.source.integration
|
|
1519
|
+
},
|
|
1520
|
+
run: async (event, io, ctx) => {
|
|
1521
|
+
const updates = await options.source.register(options.params, event, io, ctx);
|
|
1522
|
+
if (!updates) {
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
return await io.updateSource("update-source", {
|
|
1526
|
+
key: options.key,
|
|
1527
|
+
...updates
|
|
1528
|
+
});
|
|
1529
|
+
},
|
|
1530
|
+
__internal: true
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
attachDynamicSchedule(key, job) {
|
|
1534
|
+
const jobs = __privateGet(this, _registeredSchedules)[key] ?? [];
|
|
1535
|
+
jobs.push({
|
|
1536
|
+
id: job.id,
|
|
1537
|
+
version: job.version
|
|
1538
|
+
});
|
|
1539
|
+
__privateGet(this, _registeredSchedules)[key] = jobs;
|
|
1540
|
+
}
|
|
1541
|
+
async registerTrigger(id, key, options) {
|
|
1542
|
+
return __privateGet(this, _client).registerTrigger(this.id, id, key, options);
|
|
1543
|
+
}
|
|
1544
|
+
async getAuth(id) {
|
|
1545
|
+
return __privateGet(this, _client).getAuth(this.id, id);
|
|
1546
|
+
}
|
|
1547
|
+
async sendEvent(event, options) {
|
|
1548
|
+
return __privateGet(this, _client).sendEvent(event, options);
|
|
1549
|
+
}
|
|
1550
|
+
async cancelEvent(eventId) {
|
|
1551
|
+
return __privateGet(this, _client).cancelEvent(eventId);
|
|
1552
|
+
}
|
|
1553
|
+
async registerSchedule(id, key, schedule) {
|
|
1554
|
+
return __privateGet(this, _client).registerSchedule(this.id, id, key, schedule);
|
|
1555
|
+
}
|
|
1556
|
+
async unregisterSchedule(id, key) {
|
|
1557
|
+
return __privateGet(this, _client).unregisterSchedule(this.id, id, key);
|
|
1558
|
+
}
|
|
1559
|
+
async getEvent(eventId) {
|
|
1560
|
+
return __privateGet(this, _client).getEvent(eventId);
|
|
1561
|
+
}
|
|
1562
|
+
async getRun(runId, options) {
|
|
1563
|
+
return __privateGet(this, _client).getRun(runId, options);
|
|
1564
|
+
}
|
|
1565
|
+
async getRuns(jobSlug, options) {
|
|
1566
|
+
return __privateGet(this, _client).getRuns(jobSlug, options);
|
|
1567
|
+
}
|
|
1568
|
+
authorized(apiKey) {
|
|
1569
|
+
if (typeof apiKey !== "string") {
|
|
1570
|
+
return "missing-header";
|
|
1571
|
+
}
|
|
1572
|
+
const localApiKey = __privateGet(this, _options3).apiKey ?? process.env.TRIGGER_API_KEY;
|
|
1573
|
+
if (!localApiKey) {
|
|
1574
|
+
return "missing-client";
|
|
1575
|
+
}
|
|
1576
|
+
return apiKey === localApiKey ? "authorized" : "unauthorized";
|
|
1577
|
+
}
|
|
1578
|
+
apiKey() {
|
|
1579
|
+
return __privateGet(this, _options3).apiKey ?? process.env.TRIGGER_API_KEY;
|
|
1580
|
+
}
|
|
1581
|
+
defineJob(options) {
|
|
1582
|
+
return new Job(this, options);
|
|
1583
|
+
}
|
|
1584
|
+
defineBackgroundTask(options) {
|
|
1585
|
+
return new BackgroundTask(this, options);
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
__name(TriggerClient, "TriggerClient");
|
|
1589
|
+
_options3 = new WeakMap();
|
|
1590
|
+
_registeredJobs = new WeakMap();
|
|
1591
|
+
_registeredSources = new WeakMap();
|
|
1592
|
+
_registeredBackgroundTasks = new WeakMap();
|
|
1593
|
+
_registeredHttpSourceHandlers = new WeakMap();
|
|
1594
|
+
_registeredDynamicTriggers = new WeakMap();
|
|
1595
|
+
_jobMetadataByDynamicTriggers = new WeakMap();
|
|
1596
|
+
_registeredSchedules = new WeakMap();
|
|
1597
|
+
_client = new WeakMap();
|
|
1598
|
+
_internalLogger = new WeakMap();
|
|
1599
|
+
_preprocessRun = new WeakSet();
|
|
1600
|
+
preprocessRun_fn = /* @__PURE__ */ __name(async function(body, job) {
|
|
1601
|
+
const context = __privateMethod(this, _createPreprocessRunContext, createPreprocessRunContext_fn).call(this, body);
|
|
1602
|
+
const parsedPayload = job.trigger.event.parsePayload(body.event.payload ?? {});
|
|
1603
|
+
const properties = job.trigger.event.runProperties?.(parsedPayload) ?? [];
|
|
1604
|
+
return {
|
|
1605
|
+
abort: false,
|
|
1606
|
+
properties
|
|
1607
|
+
};
|
|
1608
|
+
}, "#preprocessRun");
|
|
1609
|
+
_executeJob = new WeakSet();
|
|
1610
|
+
executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
1611
|
+
__privateGet(this, _internalLogger).debug("executing job", {
|
|
1612
|
+
execution: body1,
|
|
1613
|
+
job: job1.toJSON()
|
|
1614
|
+
});
|
|
1615
|
+
const context = __privateMethod(this, _createRunContext, createRunContext_fn).call(this, body1);
|
|
1616
|
+
const io = new IO({
|
|
1617
|
+
id: body1.run.id,
|
|
1618
|
+
cachedTasks: body1.tasks,
|
|
1619
|
+
apiClient: __privateGet(this, _client),
|
|
1620
|
+
logger: __privateGet(this, _internalLogger),
|
|
1621
|
+
client: this,
|
|
1622
|
+
context,
|
|
1623
|
+
jobLogLevel: job1.logLevel ?? __privateGet(this, _options3).logLevel ?? "info",
|
|
1624
|
+
jobLogger: __privateGet(this, _options3).ioLogLocalEnabled ? new import_core5.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options3).logLevel ?? "info") : void 0
|
|
1625
|
+
});
|
|
1626
|
+
const ioWithConnections = createIOWithIntegrations(io, body1.connections, job1.options.integrations);
|
|
1627
|
+
try {
|
|
1628
|
+
const output = await job1.options.run(job1.trigger.event.parsePayload(body1.event.payload ?? {}), ioWithConnections, context);
|
|
1629
|
+
return {
|
|
1630
|
+
status: "SUCCESS",
|
|
1631
|
+
output
|
|
1632
|
+
};
|
|
1633
|
+
} catch (error) {
|
|
1634
|
+
if (error instanceof ResumeWithTaskError) {
|
|
1635
|
+
return {
|
|
1636
|
+
status: "RESUME_WITH_TASK",
|
|
1637
|
+
task: error.task
|
|
1638
|
+
};
|
|
1639
|
+
}
|
|
1640
|
+
if (error instanceof RetryWithTaskError) {
|
|
1641
|
+
return {
|
|
1642
|
+
status: "RETRY_WITH_TASK",
|
|
1643
|
+
task: error.task,
|
|
1644
|
+
error: error.cause,
|
|
1645
|
+
retryAt: error.retryAt
|
|
1646
|
+
};
|
|
1647
|
+
}
|
|
1648
|
+
if (error instanceof CanceledWithTaskError) {
|
|
1649
|
+
return {
|
|
1650
|
+
status: "CANCELED",
|
|
1651
|
+
task: error.task
|
|
1652
|
+
};
|
|
1653
|
+
}
|
|
1654
|
+
if (error instanceof RetryWithTaskError) {
|
|
1655
|
+
const errorWithStack2 = import_core5.ErrorWithStackSchema.safeParse(error.cause);
|
|
1656
|
+
if (errorWithStack2.success) {
|
|
1657
|
+
return {
|
|
1658
|
+
status: "ERROR",
|
|
1659
|
+
error: errorWithStack2.data,
|
|
1660
|
+
task: error.task
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
return {
|
|
1664
|
+
status: "ERROR",
|
|
1665
|
+
error: {
|
|
1666
|
+
message: "Unknown error"
|
|
1667
|
+
},
|
|
1668
|
+
task: error.task
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
const errorWithStack = import_core5.ErrorWithStackSchema.safeParse(error);
|
|
1672
|
+
if (errorWithStack.success) {
|
|
1673
|
+
return {
|
|
1674
|
+
status: "ERROR",
|
|
1675
|
+
error: errorWithStack.data
|
|
1676
|
+
};
|
|
1677
|
+
}
|
|
1678
|
+
return {
|
|
1679
|
+
status: "ERROR",
|
|
1680
|
+
error: {
|
|
1681
|
+
message: "Unknown error"
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
}
|
|
1685
|
+
}, "#executeJob");
|
|
1686
|
+
_createRunContext = new WeakSet();
|
|
1687
|
+
createRunContext_fn = /* @__PURE__ */ __name(function(execution) {
|
|
1688
|
+
const { event, organization, environment, job, run, source } = execution;
|
|
1689
|
+
return {
|
|
1690
|
+
event: {
|
|
1691
|
+
id: event.id,
|
|
1692
|
+
name: event.name,
|
|
1693
|
+
context: event.context,
|
|
1694
|
+
timestamp: event.timestamp
|
|
1695
|
+
},
|
|
1696
|
+
organization,
|
|
1697
|
+
environment,
|
|
1698
|
+
job,
|
|
1699
|
+
run,
|
|
1700
|
+
account: execution.account,
|
|
1701
|
+
source
|
|
1702
|
+
};
|
|
1703
|
+
}, "#createRunContext");
|
|
1704
|
+
_createPreprocessRunContext = new WeakSet();
|
|
1705
|
+
createPreprocessRunContext_fn = /* @__PURE__ */ __name(function(body2) {
|
|
1706
|
+
const { event, organization, environment, job, run, account } = body2;
|
|
1707
|
+
return {
|
|
1708
|
+
event: {
|
|
1709
|
+
id: event.id,
|
|
1710
|
+
name: event.name,
|
|
1711
|
+
context: event.context,
|
|
1712
|
+
timestamp: event.timestamp
|
|
1713
|
+
},
|
|
1714
|
+
organization,
|
|
1715
|
+
environment,
|
|
1716
|
+
job,
|
|
1717
|
+
run,
|
|
1718
|
+
account
|
|
1719
|
+
};
|
|
1720
|
+
}, "#createPreprocessRunContext");
|
|
1721
|
+
_handleHttpSourceRequest = new WeakSet();
|
|
1722
|
+
handleHttpSourceRequest_fn = /* @__PURE__ */ __name(async function(source, sourceRequest) {
|
|
1723
|
+
__privateGet(this, _internalLogger).debug("Handling HTTP source request", {
|
|
1724
|
+
source
|
|
1725
|
+
});
|
|
1726
|
+
if (source.dynamicId) {
|
|
1727
|
+
const dynamicTrigger = __privateGet(this, _registeredDynamicTriggers)[source.dynamicId];
|
|
1728
|
+
if (!dynamicTrigger) {
|
|
1729
|
+
__privateGet(this, _internalLogger).debug("No dynamic trigger registered for HTTP source", {
|
|
1730
|
+
source
|
|
1731
|
+
});
|
|
1732
|
+
return {
|
|
1733
|
+
response: {
|
|
1734
|
+
status: 200,
|
|
1735
|
+
body: {
|
|
1736
|
+
ok: true
|
|
1737
|
+
}
|
|
1738
|
+
},
|
|
1739
|
+
events: []
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
const results2 = await dynamicTrigger.source.handle(source, sourceRequest, __privateGet(this, _internalLogger));
|
|
1743
|
+
if (!results2) {
|
|
1744
|
+
return {
|
|
1745
|
+
events: [],
|
|
1746
|
+
response: {
|
|
1747
|
+
status: 200,
|
|
1748
|
+
body: {
|
|
1749
|
+
ok: true
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
}
|
|
1754
|
+
return {
|
|
1755
|
+
events: results2.events,
|
|
1756
|
+
response: results2.response ?? {
|
|
1757
|
+
status: 200,
|
|
1758
|
+
body: {
|
|
1759
|
+
ok: true
|
|
1760
|
+
}
|
|
1761
|
+
},
|
|
1762
|
+
metadata: results2.metadata
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1765
|
+
const handler = __privateGet(this, _registeredHttpSourceHandlers)[source.key];
|
|
1766
|
+
if (!handler) {
|
|
1767
|
+
__privateGet(this, _internalLogger).debug("No handler registered for HTTP source", {
|
|
1768
|
+
source
|
|
1769
|
+
});
|
|
1770
|
+
return {
|
|
1771
|
+
response: {
|
|
1772
|
+
status: 200,
|
|
1773
|
+
body: {
|
|
1774
|
+
ok: true
|
|
1775
|
+
}
|
|
1776
|
+
},
|
|
1777
|
+
events: []
|
|
1778
|
+
};
|
|
1779
|
+
}
|
|
1780
|
+
const results = await handler(source, sourceRequest);
|
|
1781
|
+
if (!results) {
|
|
1782
|
+
return {
|
|
1783
|
+
events: [],
|
|
1784
|
+
response: {
|
|
1785
|
+
status: 200,
|
|
1786
|
+
body: {
|
|
1787
|
+
ok: true
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
};
|
|
1791
|
+
}
|
|
1792
|
+
return {
|
|
1793
|
+
events: results.events,
|
|
1794
|
+
response: results.response ?? {
|
|
1795
|
+
status: 200,
|
|
1796
|
+
body: {
|
|
1797
|
+
ok: true
|
|
1798
|
+
}
|
|
1799
|
+
},
|
|
1800
|
+
metadata: results.metadata
|
|
1801
|
+
};
|
|
1802
|
+
}, "#handleHttpSourceRequest");
|
|
1803
|
+
function dynamicTriggerRegisterSourceJobId(id) {
|
|
1804
|
+
return `register-dynamic-trigger-${id}`;
|
|
1805
|
+
}
|
|
1806
|
+
__name(dynamicTriggerRegisterSourceJobId, "dynamicTriggerRegisterSourceJobId");
|
|
1807
|
+
function deepMergeOptions(obj1, obj2) {
|
|
1808
|
+
const mergedOptions = {
|
|
1809
|
+
...obj1
|
|
1810
|
+
};
|
|
1811
|
+
for (const key in obj2) {
|
|
1812
|
+
if (obj2.hasOwnProperty(key)) {
|
|
1813
|
+
if (key in mergedOptions) {
|
|
1814
|
+
mergedOptions[key] = [
|
|
1815
|
+
...mergedOptions[key],
|
|
1816
|
+
...obj2[key]
|
|
1817
|
+
];
|
|
1818
|
+
} else {
|
|
1819
|
+
mergedOptions[key] = obj2[key];
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
return mergedOptions;
|
|
1824
|
+
}
|
|
1825
|
+
__name(deepMergeOptions, "deepMergeOptions");
|
|
1826
|
+
|
|
1827
|
+
// src/triggers/externalSource.ts
|
|
1828
|
+
var import_core6 = require("@trigger.dev/core");
|
|
1829
|
+
var ExternalSource = class {
|
|
1830
|
+
constructor(channel, options) {
|
|
1831
|
+
this.options = options;
|
|
1832
|
+
this.channel = channel;
|
|
1833
|
+
}
|
|
1834
|
+
async handle(source, rawEvent, logger) {
|
|
1835
|
+
return this.options.handler({
|
|
1836
|
+
source: {
|
|
1837
|
+
...source,
|
|
1838
|
+
params: source.params
|
|
1839
|
+
},
|
|
1840
|
+
rawEvent
|
|
1841
|
+
}, logger, this.options.integration);
|
|
1842
|
+
}
|
|
1843
|
+
filter(params, options) {
|
|
1844
|
+
return this.options.filter?.(params, options) ?? {};
|
|
1845
|
+
}
|
|
1846
|
+
properties(params) {
|
|
1847
|
+
return this.options.properties?.(params) ?? [];
|
|
1848
|
+
}
|
|
1849
|
+
async register(params, registerEvent, io, ctx) {
|
|
1850
|
+
const { result: event, ommited: source } = omit(registerEvent, "source");
|
|
1851
|
+
const { result: sourceWithoutChannel, ommited: channel } = omit(source, "channel");
|
|
1852
|
+
const { result: channelWithoutType } = omit(channel, "type");
|
|
1853
|
+
const updates = await this.options.register({
|
|
1854
|
+
...event,
|
|
1855
|
+
source: {
|
|
1856
|
+
...sourceWithoutChannel,
|
|
1857
|
+
...channelWithoutType
|
|
1858
|
+
},
|
|
1859
|
+
params
|
|
1860
|
+
}, io, ctx);
|
|
1861
|
+
return updates;
|
|
1862
|
+
}
|
|
1863
|
+
key(params) {
|
|
1864
|
+
const parts = [
|
|
1865
|
+
this.options.id,
|
|
1866
|
+
this.channel
|
|
1867
|
+
];
|
|
1868
|
+
parts.push(this.options.key(params));
|
|
1869
|
+
parts.push(this.integration.id);
|
|
1870
|
+
return parts.join("-");
|
|
1871
|
+
}
|
|
1872
|
+
get integration() {
|
|
1873
|
+
return this.options.integration;
|
|
1874
|
+
}
|
|
1875
|
+
get integrationConfig() {
|
|
1876
|
+
return {
|
|
1877
|
+
id: this.integration.id,
|
|
1878
|
+
metadata: this.integration.metadata
|
|
1879
|
+
};
|
|
1880
|
+
}
|
|
1881
|
+
get id() {
|
|
1882
|
+
return this.options.id;
|
|
1883
|
+
}
|
|
1884
|
+
get version() {
|
|
1885
|
+
return this.options.version;
|
|
1886
|
+
}
|
|
1887
|
+
};
|
|
1888
|
+
__name(ExternalSource, "ExternalSource");
|
|
1889
|
+
var ExternalSourceTrigger = class {
|
|
1890
|
+
constructor(options) {
|
|
1891
|
+
this.options = options;
|
|
1892
|
+
}
|
|
1893
|
+
get event() {
|
|
1894
|
+
return this.options.event;
|
|
1895
|
+
}
|
|
1896
|
+
toJSON() {
|
|
1897
|
+
return {
|
|
1898
|
+
type: "static",
|
|
1899
|
+
title: "External Source",
|
|
1900
|
+
rule: {
|
|
1901
|
+
event: this.event.name,
|
|
1902
|
+
payload: (0, import_core6.deepMergeFilters)(this.options.source.filter(this.options.params, this.options.options), this.event.filter ?? {}, this.options.params.filter ?? {}),
|
|
1903
|
+
source: this.event.source
|
|
1904
|
+
},
|
|
1905
|
+
properties: this.options.source.properties(this.options.params)
|
|
1906
|
+
};
|
|
1907
|
+
}
|
|
1908
|
+
attachToJob(triggerClient, job) {
|
|
1909
|
+
triggerClient.attachSource({
|
|
1910
|
+
key: slugifyId(this.options.source.key(this.options.params)),
|
|
1911
|
+
source: this.options.source,
|
|
1912
|
+
event: this.options.event,
|
|
1913
|
+
params: this.options.params,
|
|
1914
|
+
options: this.options.options
|
|
1915
|
+
});
|
|
1916
|
+
}
|
|
1917
|
+
get preprocessRuns() {
|
|
1918
|
+
return true;
|
|
1919
|
+
}
|
|
1920
|
+
};
|
|
1921
|
+
__name(ExternalSourceTrigger, "ExternalSourceTrigger");
|
|
1922
|
+
function omit(obj, key) {
|
|
1923
|
+
const result = {};
|
|
1924
|
+
for (const k of Object.keys(obj)) {
|
|
1925
|
+
if (k === key)
|
|
1926
|
+
continue;
|
|
1927
|
+
result[k] = obj[k];
|
|
1928
|
+
}
|
|
1929
|
+
return {
|
|
1930
|
+
result,
|
|
1931
|
+
ommited: obj[key]
|
|
1932
|
+
};
|
|
1933
|
+
}
|
|
1934
|
+
__name(omit, "omit");
|
|
1935
|
+
|
|
1936
|
+
// src/triggers/dynamic.ts
|
|
1937
|
+
var import_core7 = require("@trigger.dev/core");
|
|
1938
|
+
var _client2, _options4;
|
|
1939
|
+
var DynamicTrigger = class {
|
|
1940
|
+
constructor(client, options) {
|
|
1941
|
+
__privateAdd(this, _client2, void 0);
|
|
1942
|
+
__privateAdd(this, _options4, void 0);
|
|
1943
|
+
__privateSet(this, _client2, client);
|
|
1944
|
+
__privateSet(this, _options4, options);
|
|
1945
|
+
this.source = options.source;
|
|
1946
|
+
client.attachDynamicTrigger(this);
|
|
1947
|
+
}
|
|
1948
|
+
toJSON() {
|
|
1949
|
+
return {
|
|
1950
|
+
type: "dynamic",
|
|
1951
|
+
id: __privateGet(this, _options4).id
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
get id() {
|
|
1955
|
+
return __privateGet(this, _options4).id;
|
|
1956
|
+
}
|
|
1957
|
+
get event() {
|
|
1958
|
+
return __privateGet(this, _options4).event;
|
|
1959
|
+
}
|
|
1960
|
+
registeredTriggerForParams(params) {
|
|
1961
|
+
const key = slugifyId(this.source.key(params));
|
|
1962
|
+
return {
|
|
1963
|
+
rule: {
|
|
1964
|
+
event: this.event.name,
|
|
1965
|
+
source: this.event.source,
|
|
1966
|
+
payload: (0, import_core7.deepMergeFilters)(this.source.filter(params), this.event.filter ?? {})
|
|
1967
|
+
},
|
|
1968
|
+
source: {
|
|
1969
|
+
version: "2",
|
|
1970
|
+
key,
|
|
1971
|
+
channel: this.source.channel,
|
|
1972
|
+
params,
|
|
1973
|
+
options: {
|
|
1974
|
+
event: typeof this.event.name === "string" ? [
|
|
1975
|
+
this.event.name
|
|
1976
|
+
] : this.event.name
|
|
1977
|
+
},
|
|
1978
|
+
integration: {
|
|
1979
|
+
id: this.source.integration.id,
|
|
1980
|
+
metadata: this.source.integration.metadata,
|
|
1981
|
+
authSource: this.source.integration.authSource
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
async register(key, params) {
|
|
1987
|
+
return __privateGet(this, _client2).registerTrigger(this.id, key, this.registeredTriggerForParams(params));
|
|
1988
|
+
}
|
|
1989
|
+
attachToJob(triggerClient, job) {
|
|
1990
|
+
triggerClient.attachJobToDynamicTrigger(job, this);
|
|
1991
|
+
}
|
|
1992
|
+
get preprocessRuns() {
|
|
1993
|
+
return true;
|
|
1994
|
+
}
|
|
1995
|
+
};
|
|
1996
|
+
__name(DynamicTrigger, "DynamicTrigger");
|
|
1997
|
+
_client2 = new WeakMap();
|
|
1998
|
+
_options4 = new WeakMap();
|
|
1999
|
+
|
|
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
|
+
// src/triggers/notifications.ts
|
|
2145
|
+
var import_core9 = require("@trigger.dev/core");
|
|
2146
|
+
function missingConnectionNotification(integrations) {
|
|
2147
|
+
return new MissingConnectionNotification({
|
|
2148
|
+
integrations
|
|
2149
|
+
});
|
|
2150
|
+
}
|
|
2151
|
+
__name(missingConnectionNotification, "missingConnectionNotification");
|
|
2152
|
+
function missingConnectionResolvedNotification(integrations) {
|
|
2153
|
+
return new MissingConnectionResolvedNotification({
|
|
2154
|
+
integrations
|
|
2155
|
+
});
|
|
2156
|
+
}
|
|
2157
|
+
__name(missingConnectionResolvedNotification, "missingConnectionResolvedNotification");
|
|
2158
|
+
var MissingConnectionNotification = class {
|
|
2159
|
+
constructor(options) {
|
|
2160
|
+
this.options = options;
|
|
2161
|
+
}
|
|
2162
|
+
get event() {
|
|
2163
|
+
return {
|
|
2164
|
+
name: import_core9.MISSING_CONNECTION_NOTIFICATION,
|
|
2165
|
+
title: "Missing Connection Notification",
|
|
2166
|
+
source: "trigger.dev",
|
|
2167
|
+
icon: "connection-alert",
|
|
2168
|
+
parsePayload: import_core9.MissingConnectionNotificationPayloadSchema.parse,
|
|
2169
|
+
properties: [
|
|
2170
|
+
{
|
|
2171
|
+
label: "Integrations",
|
|
2172
|
+
text: this.options.integrations.map((i) => i.id).join(", ")
|
|
2173
|
+
}
|
|
2174
|
+
]
|
|
2175
|
+
};
|
|
2176
|
+
}
|
|
2177
|
+
attachToJob(triggerClient, job) {
|
|
2178
|
+
}
|
|
2179
|
+
get preprocessRuns() {
|
|
2180
|
+
return false;
|
|
2181
|
+
}
|
|
2182
|
+
toJSON() {
|
|
2183
|
+
return {
|
|
2184
|
+
type: "static",
|
|
2185
|
+
title: this.event.title,
|
|
2186
|
+
rule: {
|
|
2187
|
+
event: this.event.name,
|
|
2188
|
+
source: "trigger.dev",
|
|
2189
|
+
payload: {
|
|
2190
|
+
client: {
|
|
2191
|
+
id: this.options.integrations.map((i) => i.id)
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
};
|
|
2196
|
+
}
|
|
2197
|
+
};
|
|
2198
|
+
__name(MissingConnectionNotification, "MissingConnectionNotification");
|
|
2199
|
+
var MissingConnectionResolvedNotification = class {
|
|
2200
|
+
constructor(options) {
|
|
2201
|
+
this.options = options;
|
|
2202
|
+
}
|
|
2203
|
+
get event() {
|
|
2204
|
+
return {
|
|
2205
|
+
name: import_core9.MISSING_CONNECTION_RESOLVED_NOTIFICATION,
|
|
2206
|
+
title: "Missing Connection Resolved Notification",
|
|
2207
|
+
source: "trigger.dev",
|
|
2208
|
+
icon: "connection-alert",
|
|
2209
|
+
parsePayload: import_core9.MissingConnectionResolvedNotificationPayloadSchema.parse,
|
|
2210
|
+
properties: [
|
|
2211
|
+
{
|
|
2212
|
+
label: "Integrations",
|
|
2213
|
+
text: this.options.integrations.map((i) => i.id).join(", ")
|
|
2214
|
+
}
|
|
2215
|
+
]
|
|
2216
|
+
};
|
|
2217
|
+
}
|
|
2218
|
+
attachToJob(triggerClient, job) {
|
|
2219
|
+
}
|
|
2220
|
+
get preprocessRuns() {
|
|
2221
|
+
return false;
|
|
2222
|
+
}
|
|
2223
|
+
toJSON() {
|
|
2224
|
+
return {
|
|
2225
|
+
type: "static",
|
|
2226
|
+
title: this.event.title,
|
|
2227
|
+
rule: {
|
|
2228
|
+
event: this.event.name,
|
|
2229
|
+
source: "trigger.dev",
|
|
2230
|
+
payload: {
|
|
2231
|
+
client: {
|
|
2232
|
+
id: this.options.integrations.map((i) => i.id)
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
};
|
|
2237
|
+
}
|
|
2238
|
+
};
|
|
2239
|
+
__name(MissingConnectionResolvedNotification, "MissingConnectionResolvedNotification");
|
|
2240
|
+
|
|
2241
|
+
// src/index.ts
|
|
2242
|
+
function redactString(strings, ...interpolations) {
|
|
2243
|
+
return {
|
|
2244
|
+
__redactedString: true,
|
|
2245
|
+
strings: strings.raw,
|
|
2246
|
+
interpolations
|
|
2247
|
+
};
|
|
2248
|
+
}
|
|
2249
|
+
__name(redactString, "redactString");
|
|
2250
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2251
|
+
0 && (module.exports = {
|
|
2252
|
+
CronTrigger,
|
|
2253
|
+
DynamicSchedule,
|
|
2254
|
+
DynamicTrigger,
|
|
2255
|
+
EventTrigger,
|
|
2256
|
+
ExternalSource,
|
|
2257
|
+
ExternalSourceTrigger,
|
|
2258
|
+
IO,
|
|
2259
|
+
IOLogger,
|
|
2260
|
+
IntervalTrigger,
|
|
2261
|
+
Job,
|
|
2262
|
+
MissingConnectionNotification,
|
|
2263
|
+
MissingConnectionResolvedNotification,
|
|
2264
|
+
TriggerClient,
|
|
2265
|
+
cronTrigger,
|
|
2266
|
+
eventTrigger,
|
|
2267
|
+
intervalTrigger,
|
|
2268
|
+
isTriggerError,
|
|
2269
|
+
missingConnectionNotification,
|
|
2270
|
+
missingConnectionResolvedNotification,
|
|
2271
|
+
omit,
|
|
2272
|
+
redactString,
|
|
2273
|
+
retry
|
|
2274
|
+
});
|
|
2275
|
+
//# sourceMappingURL=index.js.map
|