@@ -24,16 +24,48 @@ export function configure(options: ApiClientConfiguration) {
2424
2525export const auth = {
2626 configure,
27- generateJWT ,
28- context ,
27+ createPublicToken ,
28+ withAuth ,
2929} ;
3030
31- export type GenerateJWTOptions = {
32- permissions ?: string [ ] ;
31+ type PublicTokenPermissionAction = "read" ; // Add more actions as needed
32+
33+ type PublicTokenPermissionProperties = {
34+ tags ?: string | string [ ] ;
35+ runs ?: string | string [ ] ;
36+ } ;
37+
38+ export type PublicTokenPermissions = {
39+ [ key in PublicTokenPermissionAction ] ?: PublicTokenPermissionProperties | true ;
40+ } ;
41+
42+ export type CreatePublicTokenOptions = {
43+ permissions ?: PublicTokenPermissions ;
3344 expirationTime ?: number | Date | string ;
3445} ;
3546
36- async function generateJWT ( options ?: GenerateJWTOptions ) : Promise < string > {
47+ /**
48+ * Creates a public token using the provided options.
49+ *
50+ * @param options - Optional parameters for creating the public token.
51+ * @param options.permissions - An array of permissions to be included in the token.
52+ * @param options.expirationTime - The expiration time for the token.
53+ * @returns A promise that resolves to a string representing the generated public token.
54+ *
55+ * @example
56+ *
57+ * ```typescript
58+ * import { auth } from "@trigger.dev/sdk/v3";
59+ *
60+ * const publicToken = await auth.createPublicToken({
61+ * permissions: {
62+ * read: {
63+ * tags: ["file:1234"]
64+ * }
65+ * });
66+ * ```
67+ */
68+ async function createPublicToken ( options ?: CreatePublicTokenOptions ) : Promise < string > {
3769 const apiClient = apiClientManager . clientOrThrow ( ) ;
3870
3971 const claims = await apiClient . generateJWTClaims ( ) ;
@@ -42,15 +74,47 @@ async function generateJWT(options?: GenerateJWTOptions): Promise<string> {
4274 secretKey : apiClient . accessToken ,
4375 payload : {
4476 ...claims ,
45- permissions : options ?. permissions ,
77+ permissions : options ?. permissions ? flattenPermissions ( options . permissions ) : undefined ,
4678 } ,
4779 expirationTime : options ?. expirationTime ,
4880 } ) ;
4981}
5082
51- async function context < R extends ( ...args : any [ ] ) => Promise < any > > (
83+ /**
84+ * Executes a provided asynchronous function with a specified API client configuration.
85+ *
86+ * @template R - The type of the asynchronous function to be executed.
87+ * @param {ApiClientConfiguration } config - The configuration for the API client.
88+ * @param {R } fn - The asynchronous function to be executed.
89+ * @returns {Promise<ReturnType<R>> } A promise that resolves to the return type of the provided function.
90+ */
91+ async function withAuth < R extends ( ...args : any [ ] ) => Promise < any > > (
5292 config : ApiClientConfiguration ,
5393 fn : R
5494) : Promise < ReturnType < R > > {
5595 return apiClientManager . runWithConfig ( config , fn ) ;
5696}
97+
98+ function flattenPermissions ( permissions : PublicTokenPermissions ) : string [ ] {
99+ const flattenedPermissions : string [ ] = [ ] ;
100+
101+ for ( const [ action , properties ] of Object . entries ( permissions ) ) {
102+ if ( properties ) {
103+ if ( typeof properties === "boolean" && properties ) {
104+ flattenedPermissions . push ( action ) ;
105+ } else if ( typeof properties === "object" ) {
106+ for ( const [ property , value ] of Object . entries ( properties ) ) {
107+ if ( Array . isArray ( value ) ) {
108+ for ( const item of value ) {
109+ flattenedPermissions . push ( `${ action } :${ property } :${ item } ` ) ;
110+ }
111+ } else if ( typeof value === "string" ) {
112+ flattenedPermissions . push ( `${ action } :${ property } :${ value } ` ) ;
113+ }
114+ }
115+ }
116+ }
117+ }
118+
119+ return flattenedPermissions ;
120+ }
0 commit comments