@fluxbase/sdk 2026.2.3-rc.21 → 2026.2.3-rc.23

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.cts CHANGED
@@ -25,6 +25,12 @@ interface FluxbaseClientOptions {
25
25
  */
26
26
  persist?: boolean;
27
27
  };
28
+ /**
29
+ * Whether this is a service role client
30
+ * When true, enables service_role features like onBehalfOf for job submission
31
+ * @internal
32
+ */
33
+ serviceRole?: boolean;
28
34
  /**
29
35
  * Global headers to include in all requests
30
36
  */
@@ -5109,7 +5115,17 @@ declare class FluxbaseFunctions {
5109
5115
  */
5110
5116
  declare class FluxbaseJobs {
5111
5117
  private fetch;
5112
- constructor(fetch: FluxbaseFetch);
5118
+ private cachedRole;
5119
+ private readonly ROLE_CACHE_TTL;
5120
+ private isServiceRole;
5121
+ constructor(fetch: FluxbaseFetch, isServiceRole?: boolean);
5122
+ /**
5123
+ * Get the current user's role from user_profiles table
5124
+ * This is used to auto-populate the onBehalfOf parameter when submitting jobs
5125
+ *
5126
+ * @returns Promise resolving to the user's role or null if not found
5127
+ */
5128
+ private getCurrentUserRole;
5113
5129
  /**
5114
5130
  * Submit a new job for execution
5115
5131
  *
@@ -5159,6 +5175,9 @@ declare class FluxbaseJobs {
5159
5175
  * Submit job on behalf of another user (service_role only).
5160
5176
  * The job will be created with the specified user's identity,
5161
5177
  * allowing them to see the job and its logs via RLS.
5178
+ *
5179
+ * If not provided, the current user's identity and role from user_profiles
5180
+ * will be automatically included.
5162
5181
  */
5163
5182
  onBehalfOf?: OnBehalfOf;
5164
5183
  }): Promise<{
package/dist/index.d.ts CHANGED
@@ -25,6 +25,12 @@ interface FluxbaseClientOptions {
25
25
  */
26
26
  persist?: boolean;
27
27
  };
28
+ /**
29
+ * Whether this is a service role client
30
+ * When true, enables service_role features like onBehalfOf for job submission
31
+ * @internal
32
+ */
33
+ serviceRole?: boolean;
28
34
  /**
29
35
  * Global headers to include in all requests
30
36
  */
@@ -5109,7 +5115,17 @@ declare class FluxbaseFunctions {
5109
5115
  */
5110
5116
  declare class FluxbaseJobs {
5111
5117
  private fetch;
5112
- constructor(fetch: FluxbaseFetch);
5118
+ private cachedRole;
5119
+ private readonly ROLE_CACHE_TTL;
5120
+ private isServiceRole;
5121
+ constructor(fetch: FluxbaseFetch, isServiceRole?: boolean);
5122
+ /**
5123
+ * Get the current user's role from user_profiles table
5124
+ * This is used to auto-populate the onBehalfOf parameter when submitting jobs
5125
+ *
5126
+ * @returns Promise resolving to the user's role or null if not found
5127
+ */
5128
+ private getCurrentUserRole;
5113
5129
  /**
5114
5130
  * Submit a new job for execution
5115
5131
  *
@@ -5159,6 +5175,9 @@ declare class FluxbaseJobs {
5159
5175
  * Submit job on behalf of another user (service_role only).
5160
5176
  * The job will be created with the specified user's identity,
5161
5177
  * allowing them to see the job and its logs via RLS.
5178
+ *
5179
+ * If not provided, the current user's identity and role from user_profiles
5180
+ * will be automatically included.
5162
5181
  */
5163
5182
  onBehalfOf?: OnBehalfOf;
5164
5183
  }): Promise<{
package/dist/index.js CHANGED
@@ -3643,8 +3643,43 @@ var FluxbaseFunctions = class {
3643
3643
 
3644
3644
  // src/jobs.ts
3645
3645
  var FluxbaseJobs = class {
3646
- constructor(fetch2) {
3646
+ constructor(fetch2, isServiceRole = false) {
3647
+ this.cachedRole = null;
3648
+ this.ROLE_CACHE_TTL = 5 * 60 * 1e3;
3647
3649
  this.fetch = fetch2;
3650
+ this.isServiceRole = isServiceRole;
3651
+ }
3652
+ /**
3653
+ * Get the current user's role from user_profiles table
3654
+ * This is used to auto-populate the onBehalfOf parameter when submitting jobs
3655
+ *
3656
+ * @returns Promise resolving to the user's role or null if not found
3657
+ */
3658
+ async getCurrentUserRole(userId) {
3659
+ try {
3660
+ const now = Date.now();
3661
+ if (this.cachedRole && this.cachedRole.userId === userId && this.cachedRole.expiresAt > now) {
3662
+ return this.cachedRole.role;
3663
+ }
3664
+ const profileResult = await this.fetch.get(
3665
+ `/rest/v1/user_profiles?id=eq.${userId}&select=role`
3666
+ );
3667
+ if (!profileResult || !profileResult[0]) {
3668
+ return null;
3669
+ }
3670
+ const role = profileResult[0].role;
3671
+ if (role) {
3672
+ this.cachedRole = {
3673
+ userId,
3674
+ role,
3675
+ expiresAt: now + this.ROLE_CACHE_TTL
3676
+ };
3677
+ }
3678
+ return role;
3679
+ } catch (error) {
3680
+ console.warn("[FluxbaseJobs] Failed to fetch user role:", error);
3681
+ return null;
3682
+ }
3648
3683
  }
3649
3684
  /**
3650
3685
  * Submit a new job for execution
@@ -3689,13 +3724,27 @@ var FluxbaseJobs = class {
3689
3724
  */
3690
3725
  async submit(jobName, payload, options) {
3691
3726
  try {
3727
+ let finalOnBehalfOf = options?.onBehalfOf;
3728
+ if (this.isServiceRole && !finalOnBehalfOf) {
3729
+ const userResponse = await this.fetch.get("/api/v1/auth/user");
3730
+ if (userResponse?.user) {
3731
+ const user = userResponse.user;
3732
+ const role = await this.getCurrentUserRole(user.id);
3733
+ finalOnBehalfOf = {
3734
+ user_id: user.id,
3735
+ user_email: user.email,
3736
+ user_role: role || void 0
3737
+ // Include role if found
3738
+ };
3739
+ }
3740
+ }
3692
3741
  const request = {
3693
3742
  job_name: jobName,
3694
3743
  payload,
3695
3744
  priority: options?.priority,
3696
3745
  namespace: options?.namespace,
3697
3746
  scheduled: options?.scheduled,
3698
- on_behalf_of: options?.onBehalfOf
3747
+ on_behalf_of: finalOnBehalfOf
3699
3748
  };
3700
3749
  const data = await this.fetch.post("/api/v1/jobs/submit", request);
3701
3750
  return { data, error: null };
@@ -12536,7 +12585,7 @@ var FluxbaseClient = class {
12536
12585
  );
12537
12586
  this.storage = new FluxbaseStorage(this.fetch);
12538
12587
  this.functions = new FluxbaseFunctions(this.fetch);
12539
- this.jobs = new FluxbaseJobs(this.fetch);
12588
+ this.jobs = new FluxbaseJobs(this.fetch, options?.serviceRole ?? false);
12540
12589
  this.admin = new FluxbaseAdmin(this.fetch);
12541
12590
  this.management = new FluxbaseManagement(this.fetch);
12542
12591
  this.settings = new SettingsClient(this.fetch);
@@ -12738,18 +12787,28 @@ function getEnvVar(name) {
12738
12787
  }
12739
12788
  function createClient(fluxbaseUrl, fluxbaseKey, options) {
12740
12789
  const url = fluxbaseUrl || getEnvVar("FLUXBASE_URL") || getEnvVar("NEXT_PUBLIC_FLUXBASE_URL") || getEnvVar("VITE_FLUXBASE_URL");
12741
- const key = fluxbaseKey || getEnvVar("FLUXBASE_ANON_KEY") || getEnvVar("FLUXBASE_SERVICE_TOKEN") || getEnvVar("FLUXBASE_JOB_TOKEN") || getEnvVar("NEXT_PUBLIC_FLUXBASE_ANON_KEY") || getEnvVar("VITE_FLUXBASE_ANON_KEY");
12790
+ const resolvedKey = fluxbaseKey || getEnvVar("FLUXBASE_SERVICE_TOKEN") || getEnvVar("FLUXBASE_ANON_KEY") || getEnvVar("FLUXBASE_JOB_TOKEN") || getEnvVar("NEXT_PUBLIC_FLUXBASE_ANON_KEY") || getEnvVar("VITE_FLUXBASE_ANON_KEY");
12791
+ let isServiceRole = options?.serviceRole ?? false;
12792
+ if (!fluxbaseKey && getEnvVar("FLUXBASE_SERVICE_TOKEN")) {
12793
+ isServiceRole = true;
12794
+ }
12795
+ if (options?.serviceRole !== void 0) {
12796
+ isServiceRole = options.serviceRole;
12797
+ }
12742
12798
  if (!url) {
12743
12799
  throw new Error(
12744
12800
  "Fluxbase URL is required. Pass it as the first argument or set FLUXBASE_URL environment variable."
12745
12801
  );
12746
12802
  }
12747
- if (!key) {
12803
+ if (!resolvedKey) {
12748
12804
  throw new Error(
12749
12805
  "Fluxbase key is required. Pass it as the second argument or set FLUXBASE_ANON_KEY environment variable."
12750
12806
  );
12751
12807
  }
12752
- return new FluxbaseClient(url, key, options);
12808
+ return new FluxbaseClient(url, resolvedKey, {
12809
+ ...options,
12810
+ serviceRole: isServiceRole
12811
+ });
12753
12812
  }
12754
12813
 
12755
12814
  // src/type-guards.ts