Skip to content

Commit d2c69c7

Browse files
committed
Allow customizing the expiration time of the automatic JWT created after triggering a task
1 parent af7074d commit d2c69c7

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

packages/core/src/v3/apiClient/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
OffsetLimitPageParams,
1717
OffsetLimitPageResponse,
1818
} from "./pagination.js";
19+
import { TriggerJwtOptions } from "../types/tasks.js";
1920

2021
export const defaultRetryOptions = {
2122
maxAttempts: 3,
@@ -35,6 +36,7 @@ export type ZodFetchOptions = {
3536
};
3637

3738
export type ApiRequestOptions = Pick<ZodFetchOptions, "retry">;
39+
3840
type KeysEnum<T> = { [P in keyof Required<T>]: true };
3941

4042
// This is required so that we can determine if a given object matches the ApiRequestOptions

packages/core/src/v3/apiClient/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
UpdateEnvironmentVariableParams,
5555
} from "./types.js";
5656
import { generateJWT } from "../jwt.js";
57+
import { TriggerJwtOptions } from "../types/tasks.js";
5758

5859
export type {
5960
CreateEnvironmentVariableParams,
@@ -65,6 +66,14 @@ export type TriggerOptions = {
6566
spanParentAsLink?: boolean;
6667
};
6768

69+
export type TriggerRequestOptions = ZodFetchOptions & {
70+
jwt?: TriggerJwtOptions;
71+
};
72+
73+
export type TriggerApiRequestOptions = ApiRequestOptions & {
74+
jwt?: TriggerJwtOptions;
75+
};
76+
6877
const DEFAULT_ZOD_FETCH_OPTIONS: ZodFetchOptions = {
6978
retry: {
7079
maxAttempts: 3,
@@ -155,7 +164,7 @@ export class ApiClient {
155164
taskId: string,
156165
body: TriggerTaskRequestBody,
157166
options?: TriggerOptions,
158-
requestOptions?: ZodFetchOptions
167+
requestOptions?: TriggerRequestOptions
159168
) {
160169
const encodedTaskId = encodeURIComponent(taskId);
161170

@@ -180,7 +189,7 @@ export class ApiClient {
180189
...claims,
181190
permissions: [data.id],
182191
},
183-
expirationTime: "1h",
192+
expirationTime: requestOptions?.jwt?.expirationTime ?? "1h",
184193
});
185194

186195
return {
@@ -194,7 +203,7 @@ export class ApiClient {
194203
taskId: string,
195204
body: BatchTriggerTaskRequestBody,
196205
options?: TriggerOptions,
197-
requestOptions?: ZodFetchOptions
206+
requestOptions?: TriggerRequestOptions
198207
) {
199208
const encodedTaskId = encodeURIComponent(taskId);
200209

@@ -219,7 +228,7 @@ export class ApiClient {
219228
...claims,
220229
permissions: [data.batchId],
221230
},
222-
expirationTime: "1h",
231+
expirationTime: requestOptions?.jwt?.expirationTime ?? "1h",
223232
});
224233

225234
return {

packages/core/src/v3/types/tasks.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "../schemas/index.js";
1212
import { Prettify } from "./utils.js";
1313
import { AnySchemaParseFn, inferSchemaOut, Schema } from "./schemas.js";
14+
import { TriggerApiRequestOptions } from "../apiClient/index.js";
1415

1516
type RequireOne<T, K extends keyof T> = {
1617
[X in Exclude<keyof T, K>]?: T[X];
@@ -403,7 +404,11 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
403404
* @returns RunHandle
404405
* - `id` - The id of the triggered task run.
405406
*/
406-
trigger: (payload: TInput, options?: TaskRunOptions) => Promise<RunHandle<TInput, TOutput>>;
407+
trigger: (
408+
payload: TInput,
409+
options?: TaskRunOptions,
410+
requestOptions?: TriggerApiRequestOptions
411+
) => Promise<RunHandle<TInput, TOutput>>;
407412

408413
/**
409414
* Batch trigger multiple task runs with the given payloads, and continue without waiting for the results. If you want to wait for the results, use `batchTriggerAndWait`. Returns the id of the triggered batch.
@@ -412,7 +417,10 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
412417
* - `batchId` - The id of the triggered batch.
413418
* - `runs` - The ids of the triggered task runs.
414419
*/
415-
batchTrigger: (items: Array<BatchItem<TInput>>) => Promise<BatchRunHandle<TInput, TOutput>>;
420+
batchTrigger: (
421+
items: Array<BatchItem<TInput>>,
422+
requestOptions?: TriggerApiRequestOptions
423+
) => Promise<BatchRunHandle<TInput, TOutput>>;
416424

417425
/**
418426
* Trigger a task with the given payload, and wait for the result. Returns the result of the task run
@@ -485,6 +493,15 @@ export type TaskIdentifier<TTask extends AnyTask> = TTask extends Task<infer TId
485493
? TIdentifier
486494
: never;
487495

496+
export type TriggerJwtOptions = {
497+
/**
498+
* The expiration time of the JWT. This can be a string like "1h" or a Date object.
499+
*
500+
* Defaults to 1 hour.
501+
*/
502+
expirationTime?: number | Date | string;
503+
};
504+
488505
export type TaskRunOptions = {
489506
/**
490507
* A unique key that can be used to ensure that a task is only triggered once per key.

packages/trigger-sdk/src/v3/shared.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import type {
5555
TaskRunResult,
5656
TaskSchema,
5757
TaskWithSchemaOptions,
58+
TriggerApiRequestOptions,
5859
} from "@trigger.dev/core/v3";
5960

6061
export type {
@@ -219,7 +220,7 @@ export function createSchemaTask<
219220

220221
const task: Task<TIdentifier, inferSchemaIn<TSchema>, TOutput> = {
221222
id: params.id,
222-
trigger: async (payload, options) => {
223+
trigger: async (payload, options, requestOptions) => {
223224
const taskMetadata = taskCatalog.getTaskManifest(params.id);
224225

225226
return await trigger_internal<inferSchemaIn<TSchema>, TOutput>(
@@ -232,10 +233,11 @@ export function createSchemaTask<
232233
{
233234
queue: customQueue,
234235
...options,
235-
}
236+
},
237+
requestOptions
236238
);
237239
},
238-
batchTrigger: async (items) => {
240+
batchTrigger: async (items, requestOptions) => {
239241
const taskMetadata = taskCatalog.getTaskManifest(params.id);
240242

241243
return await batchTrigger_internal<inferSchemaIn<TSchema>, TOutput>(
@@ -245,7 +247,7 @@ export function createSchemaTask<
245247
params.id,
246248
items,
247249
parsePayload,
248-
undefined,
250+
requestOptions,
249251
customQueue
250252
);
251253
},
@@ -334,7 +336,7 @@ export async function trigger<TTask extends AnyTask>(
334336
id: TaskIdentifier<TTask>,
335337
payload: TaskPayload<TTask>,
336338
options?: TaskRunOptions,
337-
requestOptions?: ApiRequestOptions
339+
requestOptions?: TriggerApiRequestOptions
338340
): Promise<RunHandle<TaskPayload<TTask>, TaskOutput<TTask>>> {
339341
return await trigger_internal<TaskPayload<TTask>, TaskOutput<TTask>>(
340342
"tasks.trigger()",
@@ -445,7 +447,7 @@ export async function triggerAndPoll<TTask extends AnyTask>(
445447
id: TaskIdentifier<TTask>,
446448
payload: TaskPayload<TTask>,
447449
options?: TaskRunOptions & PollOptions,
448-
requestOptions?: ApiRequestOptions
450+
requestOptions?: TriggerApiRequestOptions
449451
): Promise<RetrieveRunResult<RunHandle<TaskPayload<TTask>, TaskOutput<TTask>>>> {
450452
const handle = await trigger(id, payload, options, requestOptions);
451453

@@ -455,7 +457,7 @@ export async function triggerAndPoll<TTask extends AnyTask>(
455457
export async function batchTrigger<TTask extends AnyTask>(
456458
id: TaskIdentifier<TTask>,
457459
items: Array<BatchItem<TaskPayload<TTask>>>,
458-
requestOptions?: ApiRequestOptions
460+
requestOptions?: TriggerApiRequestOptions
459461
): Promise<BatchRunHandle<TaskPayload<TTask>, TaskOutput<TTask>>> {
460462
return await batchTrigger_internal<TaskPayload<TTask>, TaskOutput<TTask>>(
461463
"tasks.batchTrigger()",
@@ -472,7 +474,7 @@ async function trigger_internal<TPayload, TOutput>(
472474
payload: TPayload,
473475
parsePayload?: SchemaParseFn<TPayload>,
474476
options?: TaskRunOptions,
475-
requestOptions?: ApiRequestOptions
477+
requestOptions?: TriggerApiRequestOptions
476478
): Promise<RunHandle<TPayload, TOutput>> {
477479
const apiClient = apiClientManager.clientOrThrow();
478480

@@ -531,7 +533,7 @@ async function batchTrigger_internal<TPayload, TOutput>(
531533
id: string,
532534
items: Array<BatchItem<TPayload>>,
533535
parsePayload?: SchemaParseFn<TPayload>,
534-
requestOptions?: ApiRequestOptions,
536+
requestOptions?: TriggerApiRequestOptions,
535537
queue?: QueueOptions
536538
): Promise<BatchRunHandle<TPayload, TOutput>> {
537539
const apiClient = apiClientManager.clientOrThrow();

references/nextjs-realtime/src/app/api/uploadthing/core.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ export const ourFileRouter = {
2929

3030
console.log("file url", file.url);
3131

32-
const handle = await tasks.trigger<typeof handleUpload>("handle-upload", file);
32+
const handle = await tasks.trigger<typeof handleUpload>(
33+
"handle-upload",
34+
file,
35+
{},
36+
{
37+
jwt: {
38+
expirationTime: "24h",
39+
},
40+
}
41+
);
42+
43+
console.log("handle", handle);
3344

3445
// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
3546
return { uploadedBy: metadata.userId, jwt: handle.jwt, runId: handle.id };

0 commit comments

Comments
 (0)