@trigger.dev/sdk 0.0.0-cross-runtime-20231204215427 → 0.0.0-cross-runtime-20231205160729

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.mts CHANGED
@@ -6,6 +6,17 @@ import { z, ZodType, TypeOf } from 'zod';
6
6
  import { Buffer } from 'buffer';
7
7
  import { BinaryToTextEncoding, BinaryLike, KeyObject } from 'crypto';
8
8
 
9
+ type ConcurrencyLimitOptions = {
10
+ id: string;
11
+ limit: number;
12
+ };
13
+ declare class ConcurrencyLimit {
14
+ private options;
15
+ constructor(options: ConcurrencyLimitOptions);
16
+ get id(): string;
17
+ get limit(): number;
18
+ }
19
+
9
20
  type QueryKeyValueStoreFunction = (action: "DELETE" | "GET" | "HAS" | "SET", data: {
10
21
  key: string;
11
22
  value?: string;
@@ -17,7 +28,7 @@ declare class KeyValueStoreClient implements AsyncMap {
17
28
  private namespace;
18
29
  constructor(queryStore: QueryKeyValueStoreFunction, type?: string | null, namespace?: string);
19
30
  delete(key: string): Promise<boolean>;
20
- get<T extends Json<T>>(key: string): Promise<T>;
31
+ get<T extends Json<T>>(key: string): Promise<T | undefined>;
21
32
  has(key: string): Promise<boolean>;
22
33
  set<T extends Json<T>>(key: string, value: T): Promise<T>;
23
34
  }
@@ -1416,17 +1427,6 @@ declare class DynamicSchedule implements Trigger<ScheduledEventSpecification> {
1416
1427
  toJSON(): TriggerMetadata;
1417
1428
  }
1418
1429
 
1419
- type ConcurrencyLimitOptions = {
1420
- id: string;
1421
- limit: number;
1422
- };
1423
- declare class ConcurrencyLimit {
1424
- private options;
1425
- constructor(options: ConcurrencyLimitOptions);
1426
- get id(): string;
1427
- get limit(): number;
1428
- }
1429
-
1430
1430
  declare class KeyValueStore {
1431
1431
  #private;
1432
1432
  private apiClient;
@@ -1435,8 +1435,8 @@ declare class KeyValueStore {
1435
1435
  constructor(apiClient: ApiClient, type?: string | null, namespace?: string);
1436
1436
  delete(cacheKey: string | any[], key: string): Promise<boolean>;
1437
1437
  delete(key: string): Promise<boolean>;
1438
- get<T extends Json<T> = any>(cacheKey: string | any[], key: string): Promise<T>;
1439
- get<T extends Json<T> = any>(key: string): Promise<T>;
1438
+ get<T extends Json<T> = any>(cacheKey: string | any[], key: string): Promise<T | undefined>;
1439
+ get<T extends Json<T> = any>(key: string): Promise<T | undefined>;
1440
1440
  has(cacheKey: string | any[], key: string): Promise<boolean>;
1441
1441
  has(key: string): Promise<boolean>;
1442
1442
  set<T extends Json<T>>(cacheKey: string | any[], key: string, value: T): Promise<T>;
@@ -2375,11 +2375,12 @@ type JobIO<TJob> = TJob extends Job<any, infer TIntegrations> ? IOWithIntegratio
2375
2375
  declare class Job<TTrigger extends Trigger<EventSpecification<any>>, TIntegrations extends Record<string, TriggerIntegration> = {}, TOutput extends any = any> {
2376
2376
  #private;
2377
2377
  readonly options: JobOptions<TTrigger, TIntegrations, TOutput>;
2378
- client: TriggerClient;
2379
- constructor(
2380
- /** An instance of [TriggerClient](/sdk/triggerclient) that is used to send events
2381
- to the Trigger API. */
2382
- client: TriggerClient, options: JobOptions<TTrigger, TIntegrations, TOutput>);
2378
+ client?: TriggerClient;
2379
+ constructor(options: JobOptions<TTrigger, TIntegrations, TOutput>);
2380
+ /**
2381
+ * Attaches the job to a client. This is called automatically when you define a job using `client.defineJob()`.
2382
+ */
2383
+ attachToClient(client: TriggerClient): void;
2383
2384
  get id(): string;
2384
2385
  get enabled(): boolean;
2385
2386
  get name(): string;
package/dist/index.d.ts CHANGED
@@ -6,6 +6,17 @@ import { z, ZodType, TypeOf } from 'zod';
6
6
  import { Buffer } from 'buffer';
7
7
  import { BinaryToTextEncoding, BinaryLike, KeyObject } from 'crypto';
8
8
 
9
+ type ConcurrencyLimitOptions = {
10
+ id: string;
11
+ limit: number;
12
+ };
13
+ declare class ConcurrencyLimit {
14
+ private options;
15
+ constructor(options: ConcurrencyLimitOptions);
16
+ get id(): string;
17
+ get limit(): number;
18
+ }
19
+
9
20
  type QueryKeyValueStoreFunction = (action: "DELETE" | "GET" | "HAS" | "SET", data: {
10
21
  key: string;
11
22
  value?: string;
@@ -17,7 +28,7 @@ declare class KeyValueStoreClient implements AsyncMap {
17
28
  private namespace;
18
29
  constructor(queryStore: QueryKeyValueStoreFunction, type?: string | null, namespace?: string);
19
30
  delete(key: string): Promise<boolean>;
20
- get<T extends Json<T>>(key: string): Promise<T>;
31
+ get<T extends Json<T>>(key: string): Promise<T | undefined>;
21
32
  has(key: string): Promise<boolean>;
22
33
  set<T extends Json<T>>(key: string, value: T): Promise<T>;
23
34
  }
@@ -1416,17 +1427,6 @@ declare class DynamicSchedule implements Trigger<ScheduledEventSpecification> {
1416
1427
  toJSON(): TriggerMetadata;
1417
1428
  }
1418
1429
 
1419
- type ConcurrencyLimitOptions = {
1420
- id: string;
1421
- limit: number;
1422
- };
1423
- declare class ConcurrencyLimit {
1424
- private options;
1425
- constructor(options: ConcurrencyLimitOptions);
1426
- get id(): string;
1427
- get limit(): number;
1428
- }
1429
-
1430
1430
  declare class KeyValueStore {
1431
1431
  #private;
1432
1432
  private apiClient;
@@ -1435,8 +1435,8 @@ declare class KeyValueStore {
1435
1435
  constructor(apiClient: ApiClient, type?: string | null, namespace?: string);
1436
1436
  delete(cacheKey: string | any[], key: string): Promise<boolean>;
1437
1437
  delete(key: string): Promise<boolean>;
1438
- get<T extends Json<T> = any>(cacheKey: string | any[], key: string): Promise<T>;
1439
- get<T extends Json<T> = any>(key: string): Promise<T>;
1438
+ get<T extends Json<T> = any>(cacheKey: string | any[], key: string): Promise<T | undefined>;
1439
+ get<T extends Json<T> = any>(key: string): Promise<T | undefined>;
1440
1440
  has(cacheKey: string | any[], key: string): Promise<boolean>;
1441
1441
  has(key: string): Promise<boolean>;
1442
1442
  set<T extends Json<T>>(cacheKey: string | any[], key: string, value: T): Promise<T>;
@@ -2375,11 +2375,12 @@ type JobIO<TJob> = TJob extends Job<any, infer TIntegrations> ? IOWithIntegratio
2375
2375
  declare class Job<TTrigger extends Trigger<EventSpecification<any>>, TIntegrations extends Record<string, TriggerIntegration> = {}, TOutput extends any = any> {
2376
2376
  #private;
2377
2377
  readonly options: JobOptions<TTrigger, TIntegrations, TOutput>;
2378
- client: TriggerClient;
2379
- constructor(
2380
- /** An instance of [TriggerClient](/sdk/triggerclient) that is used to send events
2381
- to the Trigger API. */
2382
- client: TriggerClient, options: JobOptions<TTrigger, TIntegrations, TOutput>);
2378
+ client?: TriggerClient;
2379
+ constructor(options: JobOptions<TTrigger, TIntegrations, TOutput>);
2380
+ /**
2381
+ * Attaches the job to a client. This is called automatically when you define a job using `client.defineJob()`.
2382
+ */
2383
+ attachToClient(client: TriggerClient): void;
2383
2384
  get id(): string;
2384
2385
  get enabled(): boolean;
2385
2386
  get name(): string;
package/dist/index.js CHANGED
@@ -45,14 +45,6 @@ var __privateMethod = (obj, member, method) => {
45
45
  __accessCheck(obj, member, "access private method");
46
46
  return method;
47
47
  };
48
-
49
- // src/utils.ts
50
- function slugifyId(input) {
51
- const replaceSpacesWithDash = input.toLowerCase().replace(/\s+/g, "-");
52
- const removeNonUrlSafeChars = replaceSpacesWithDash.replace(/[^a-zA-Z0-9-._~]/g, "");
53
- return removeNonUrlSafeChars;
54
- }
55
- __name(slugifyId, "slugifyId");
56
48
  var _TypedAsyncLocalStorage = class _TypedAsyncLocalStorage {
57
49
  constructor() {
58
50
  this.storage = new node_async_hooks.AsyncLocalStorage();
@@ -70,17 +62,30 @@ var TypedAsyncLocalStorage = _TypedAsyncLocalStorage;
70
62
  // src/runLocalStorage.ts
71
63
  var runLocalStorage = new TypedAsyncLocalStorage();
72
64
 
65
+ // src/utils.ts
66
+ function slugifyId(input) {
67
+ const replaceSpacesWithDash = input.toLowerCase().replace(/\s+/g, "-");
68
+ const removeNonUrlSafeChars = replaceSpacesWithDash.replace(/[^a-zA-Z0-9-._~]/g, "");
69
+ return removeNonUrlSafeChars;
70
+ }
71
+ __name(slugifyId, "slugifyId");
72
+
73
73
  // src/job.ts
74
74
  var _validate, validate_fn;
75
75
  var _Job = class _Job {
76
- constructor(client, options) {
76
+ constructor(options) {
77
77
  // Make sure the id is valid (must only contain alphanumeric characters and dashes)
78
78
  // Make sure the version is valid (must be a valid semver version)
79
79
  __privateAdd(this, _validate);
80
- this.client = client;
81
80
  this.options = options;
82
81
  __privateMethod(this, _validate, validate_fn).call(this);
83
- client.attach(this);
82
+ }
83
+ /**
84
+ * Attaches the job to a client. This is called automatically when you define a job using `client.defineJob()`.
85
+ */
86
+ attachToClient(client) {
87
+ this.client = client;
88
+ this.trigger.attachToJob(client, this);
84
89
  }
85
90
  get id() {
86
91
  return slugifyId(this.options.id);
@@ -131,6 +136,10 @@ var _Job = class _Job {
131
136
  };
132
137
  }
133
138
  async invoke(param1, param2 = void 0, param3 = void 0) {
139
+ const triggerClient = this.client;
140
+ if (!triggerClient) {
141
+ throw new Error("Cannot invoke a job that is not attached to a client. Make sure you attach the job to a client before invoking it.");
142
+ }
134
143
  const runStore = runLocalStorage.getStore();
135
144
  if (typeof param1 === "string") {
136
145
  if (!runStore) {
@@ -138,7 +147,7 @@ var _Job = class _Job {
138
147
  }
139
148
  const options = param3 ?? {};
140
149
  return await runStore.io.runTask(param1, async (task) => {
141
- const result = await this.client.invokeJob(this.id, param2, {
150
+ const result = await triggerClient.invokeJob(this.id, param2, {
142
151
  idempotencyKey: task.idempotencyKey,
143
152
  ...options
144
153
  });
@@ -169,9 +178,13 @@ var _Job = class _Job {
169
178
  if (runStore) {
170
179
  throw new Error("Cannot invoke a job from within a run without a cacheKey.");
171
180
  }
172
- return await this.client.invokeJob(this.id, param1, param3);
181
+ return await triggerClient.invokeJob(this.id, param1, param3);
173
182
  }
174
183
  async invokeAndWaitForCompletion(cacheKey, payload, timeoutInSeconds = 60 * 60, options = {}) {
184
+ const triggerClient = this.client;
185
+ if (!triggerClient) {
186
+ throw new Error("Cannot invoke a job that is not attached to a client. Make sure you attach the job to a client before invoking it.");
187
+ }
175
188
  const runStore = runLocalStorage.getStore();
176
189
  if (!runStore) {
177
190
  throw new Error("Cannot invoke a job from outside of a run using invokeAndWaitForCompletion. Make sure you are running the job from within a run or use the invoke method instead.");
@@ -179,7 +192,7 @@ var _Job = class _Job {
179
192
  const { io, ctx } = runStore;
180
193
  return await io.runTask(cacheKey, async (task) => {
181
194
  const parsedPayload = this.trigger.event.parseInvokePayload ? this.trigger.event.parseInvokePayload(payload) ? payload : void 0 : payload;
182
- const result = await this.client.invokeJob(this.id, parsedPayload, {
195
+ const result = await triggerClient.invokeJob(this.id, parsedPayload, {
183
196
  idempotencyKey: task.idempotencyKey,
184
197
  callbackUrl: task.callbackUrl ?? void 0,
185
198
  ...options
@@ -2917,7 +2930,7 @@ __name(_DynamicSchedule, "DynamicSchedule");
2917
2930
  var DynamicSchedule = _DynamicSchedule;
2918
2931
 
2919
2932
  // package.json
2920
- var version = "0.0.0-cross-runtime-20231204215427";
2933
+ var version = "0.0.0-cross-runtime-20231205160729";
2921
2934
 
2922
2935
  // src/concurrencyLimit.ts
2923
2936
  var _ConcurrencyLimit = class _ConcurrencyLimit {
@@ -3380,7 +3393,9 @@ var _TriggerClient = class _TriggerClient {
3380
3393
  if (existingRegisteredJob) {
3381
3394
  console.warn(`[@trigger.dev/sdk] Warning: The Job "${existingRegisteredJob.id}" you're attempting to define has already been defined. Please assign a different ID to the job.`);
3382
3395
  }
3383
- return new Job(this, options);
3396
+ const job = new Job(options);
3397
+ this.attach(job);
3398
+ return job;
3384
3399
  }
3385
3400
  defineAuthResolver(integration, resolver) {
3386
3401
  __privateGet(this, _authResolvers)[integration.id] = resolver;
@@ -3412,7 +3427,7 @@ var _TriggerClient = class _TriggerClient {
3412
3427
  }
3413
3428
  attach(job) {
3414
3429
  __privateGet(this, _registeredJobs)[job.id] = job;
3415
- job.trigger.attachToJob(this, job);
3430
+ job.attachToClient(this);
3416
3431
  }
3417
3432
  attachDynamicTrigger(trigger) {
3418
3433
  __privateGet(this, _registeredDynamicTriggers)[trigger.id] = trigger;
@@ -3482,7 +3497,7 @@ var _TriggerClient = class _TriggerClient {
3482
3497
  }, options.options ?? {});
3483
3498
  registeredSource.options = deepMergeOptions(registeredSource.options, newOptions);
3484
3499
  __privateGet(this, _registeredSources)[options.key] = registeredSource;
3485
- new Job(this, {
3500
+ this.defineJob({
3486
3501
  id: options.key,
3487
3502
  name: options.key,
3488
3503
  version: options.source.version,
@@ -3549,7 +3564,7 @@ var _TriggerClient = class _TriggerClient {
3549
3564
  registeredWebhook.config = deepMergeOptions(registeredWebhook.config, options.config);
3550
3565
  }
3551
3566
  __privateGet(this, _registeredWebhooks)[options.key] = registeredWebhook;
3552
- new Job(this, {
3567
+ this.defineJob({
3553
3568
  id: `webhook.register.${options.key}`,
3554
3569
  name: `webhook.register.${options.key}`,
3555
3570
  version: source.version,