@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.cjs CHANGED
@@ -3645,8 +3645,43 @@ var FluxbaseFunctions = class {
3645
3645
 
3646
3646
  // src/jobs.ts
3647
3647
  var FluxbaseJobs = class {
3648
- constructor(fetch2) {
3648
+ constructor(fetch2, isServiceRole = false) {
3649
+ this.cachedRole = null;
3650
+ this.ROLE_CACHE_TTL = 5 * 60 * 1e3;
3649
3651
  this.fetch = fetch2;
3652
+ this.isServiceRole = isServiceRole;
3653
+ }
3654
+ /**
3655
+ * Get the current user's role from user_profiles table
3656
+ * This is used to auto-populate the onBehalfOf parameter when submitting jobs
3657
+ *
3658
+ * @returns Promise resolving to the user's role or null if not found
3659
+ */
3660
+ async getCurrentUserRole(userId) {
3661
+ try {
3662
+ const now = Date.now();
3663
+ if (this.cachedRole && this.cachedRole.userId === userId && this.cachedRole.expiresAt > now) {
3664
+ return this.cachedRole.role;
3665
+ }
3666
+ const profileResult = await this.fetch.get(
3667
+ `/rest/v1/user_profiles?id=eq.${userId}&select=role`
3668
+ );
3669
+ if (!profileResult || !profileResult[0]) {
3670
+ return null;
3671
+ }
3672
+ const role = profileResult[0].role;
3673
+ if (role) {
3674
+ this.cachedRole = {
3675
+ userId,
3676
+ role,
3677
+ expiresAt: now + this.ROLE_CACHE_TTL
3678
+ };
3679
+ }
3680
+ return role;
3681
+ } catch (error) {
3682
+ console.warn("[FluxbaseJobs] Failed to fetch user role:", error);
3683
+ return null;
3684
+ }
3650
3685
  }
3651
3686
  /**
3652
3687
  * Submit a new job for execution
@@ -3691,13 +3726,27 @@ var FluxbaseJobs = class {
3691
3726
  */
3692
3727
  async submit(jobName, payload, options) {
3693
3728
  try {
3729
+ let finalOnBehalfOf = options?.onBehalfOf;
3730
+ if (this.isServiceRole && !finalOnBehalfOf) {
3731
+ const userResponse = await this.fetch.get("/api/v1/auth/user");
3732
+ if (userResponse?.user) {
3733
+ const user = userResponse.user;
3734
+ const role = await this.getCurrentUserRole(user.id);
3735
+ finalOnBehalfOf = {
3736
+ user_id: user.id,
3737
+ user_email: user.email,
3738
+ user_role: role || void 0
3739
+ // Include role if found
3740
+ };
3741
+ }
3742
+ }
3694
3743
  const request = {
3695
3744
  job_name: jobName,
3696
3745
  payload,
3697
3746
  priority: options?.priority,
3698
3747
  namespace: options?.namespace,
3699
3748
  scheduled: options?.scheduled,
3700
- on_behalf_of: options?.onBehalfOf
3749
+ on_behalf_of: finalOnBehalfOf
3701
3750
  };
3702
3751
  const data = await this.fetch.post("/api/v1/jobs/submit", request);
3703
3752
  return { data, error: null };
@@ -12538,7 +12587,7 @@ var FluxbaseClient = class {
12538
12587
  );
12539
12588
  this.storage = new FluxbaseStorage(this.fetch);
12540
12589
  this.functions = new FluxbaseFunctions(this.fetch);
12541
- this.jobs = new FluxbaseJobs(this.fetch);
12590
+ this.jobs = new FluxbaseJobs(this.fetch, options?.serviceRole ?? false);
12542
12591
  this.admin = new FluxbaseAdmin(this.fetch);
12543
12592
  this.management = new FluxbaseManagement(this.fetch);
12544
12593
  this.settings = new SettingsClient(this.fetch);
@@ -12740,18 +12789,28 @@ function getEnvVar(name) {
12740
12789
  }
12741
12790
  function createClient(fluxbaseUrl, fluxbaseKey, options) {
12742
12791
  const url = fluxbaseUrl || getEnvVar("FLUXBASE_URL") || getEnvVar("NEXT_PUBLIC_FLUXBASE_URL") || getEnvVar("VITE_FLUXBASE_URL");
12743
- 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");
12792
+ 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");
12793
+ let isServiceRole = options?.serviceRole ?? false;
12794
+ if (!fluxbaseKey && getEnvVar("FLUXBASE_SERVICE_TOKEN")) {
12795
+ isServiceRole = true;
12796
+ }
12797
+ if (options?.serviceRole !== void 0) {
12798
+ isServiceRole = options.serviceRole;
12799
+ }
12744
12800
  if (!url) {
12745
12801
  throw new Error(
12746
12802
  "Fluxbase URL is required. Pass it as the first argument or set FLUXBASE_URL environment variable."
12747
12803
  );
12748
12804
  }
12749
- if (!key) {
12805
+ if (!resolvedKey) {
12750
12806
  throw new Error(
12751
12807
  "Fluxbase key is required. Pass it as the second argument or set FLUXBASE_ANON_KEY environment variable."
12752
12808
  );
12753
12809
  }
12754
- return new FluxbaseClient(url, key, options);
12810
+ return new FluxbaseClient(url, resolvedKey, {
12811
+ ...options,
12812
+ serviceRole: isServiceRole
12813
+ });
12755
12814
  }
12756
12815
 
12757
12816
  // src/type-guards.ts