Skip to content

Commit

Permalink
Finish renaming jwt -> publicAccessToken and automatically give the J…
Browse files Browse the repository at this point in the history
…WT read access to the tags when using trigger
  • Loading branch information
ericallam committed Oct 21, 2024
1 parent 002b9eb commit f2ceb9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,4 @@
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js"
}
}
12 changes: 7 additions & 5 deletions packages/core/src/v3/apiClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export type TriggerOptions = {
};

export type TriggerRequestOptions = ZodFetchOptions & {
jwt?: TriggerJwtOptions;
publicAccessToken?: TriggerJwtOptions;
};

export type TriggerApiRequestOptions = ApiRequestOptions & {
jwt?: TriggerJwtOptions;
publicAccessToken?: TriggerJwtOptions;
};

const DEFAULT_ZOD_FETCH_OPTIONS: ZodFetchOptions = {
Expand Down Expand Up @@ -189,9 +189,11 @@ export class ApiClient {
secretKey: this.accessToken,
payload: {
...claims,
scopes: [`read:runs:${data.id}`],
scopes: [`read:runs:${data.id}`].concat(
body.options?.tags ? Array.from(body.options?.tags).map((t) => `read:tags:${t}`) : []
),
},
expirationTime: requestOptions?.jwt?.expirationTime ?? "1h",
expirationTime: requestOptions?.publicAccessToken?.expirationTime ?? "1h",
});

return {
Expand Down Expand Up @@ -230,7 +232,7 @@ export class ApiClient {
...claims,
scopes: [`read:batch:${data.batchId}`],
},
expirationTime: requestOptions?.jwt?.expirationTime ?? "1h",
expirationTime: requestOptions?.publicAccessToken?.expirationTime ?? "1h",
});

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js"
}
}
20 changes: 13 additions & 7 deletions references/v3-catalog/src/clientUsage.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { runs, tasks, auth, AnyTask, Task } from "@trigger.dev/sdk/v3";
import { auth, runs, tasks } from "@trigger.dev/sdk/v3";
import type { task1, task2 } from "./trigger/taskTypes.js";
import { randomUUID } from "crypto";

async function main() {
const userId = randomUUID();

const anyHandle = await tasks.trigger<typeof task1>(
"types/task-1",
{
foo: "baz",
},
{
tags: ["user:1234"],
tags: [`user:${userId}`],
},
{
publicAccessToken: {
expirationTime: "24hr",
},
}
);

const jwt = await auth.createPublicToken({ scopes: { read: { tags: "user:1234" } } });

console.log("Generated JWT:", jwt);
console.log("Auto JWT", anyHandle.publicAccessToken);

await auth.withAuth({ accessToken: jwt }, async () => {
const subscription = runs.subscribeToRunsWithTag<typeof task1 | typeof task2>("user:1234");
await auth.withAuth({ accessToken: anyHandle.publicAccessToken }, async () => {
const subscription = runs.subscribeToRunsWithTag<typeof task1 | typeof task2>(`user:${userId}`);

for await (const run of subscription) {
switch (run.taskIdentifier) {
Expand Down

0 comments on commit f2ceb9d

Please sign in to comment.