@linabase/js 0.2.1 → 0.2.3

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
@@ -191,7 +191,12 @@ var DatabaseClient = class {
191
191
  order(column, options) {
192
192
  const dir = options?.ascending === false ? "desc" : "asc";
193
193
  const nulls = options?.nullsFirst === true ? ".nullsfirst" : options?.nullsFirst === false ? ".nullslast" : "";
194
- this.params.set("order", `${column}.${dir}${nulls}`);
194
+ const existing = this.params.get("order");
195
+ if (existing) {
196
+ this.params.set("order", `${existing},${column}.${dir}${nulls}`);
197
+ } else {
198
+ this.params.set("order", `${column}.${dir}${nulls}`);
199
+ }
195
200
  return this;
196
201
  }
197
202
  limit(count) {
@@ -626,12 +631,28 @@ var AuthClient = class {
626
631
  /**
627
632
  * Set (or clear) the current session. Use this to restore a persisted
628
633
  * session on app launch (e.g., from AsyncStorage / SecureStore).
629
- * Emits INITIAL_SESSION to onAuthStateChange listeners.
634
+ * Automatically refreshes the token if it has expired.
635
+ * Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
636
+ * Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
630
637
  */
631
- setSession(session) {
638
+ setSession(session, _internal) {
632
639
  this.currentSession = session;
633
640
  if (this.onSessionChange) this.onSessionChange(session);
634
- this.emit("INITIAL_SESSION", session);
641
+ if (!_internal) {
642
+ if (session && session.expires_at <= Math.floor(Date.now() / 1e3)) {
643
+ this.refreshSession().then(({ error }) => {
644
+ if (error) {
645
+ this.currentSession = null;
646
+ if (this.onSessionChange) this.onSessionChange(null);
647
+ this.emit("SIGNED_OUT", null);
648
+ } else {
649
+ this.emit("INITIAL_SESSION", this.currentSession);
650
+ }
651
+ });
652
+ } else {
653
+ this.emit("INITIAL_SESSION", session);
654
+ }
655
+ }
635
656
  }
636
657
  // ─── Email/Password ────────────────────────────────────────
637
658
  async signUp(params) {
@@ -643,7 +664,7 @@ var AuthClient = class {
643
664
  const data = await res.json();
644
665
  if (!res.ok) return { data: null, error: data };
645
666
  const session = this.parseAuthResponse(data);
646
- this.setSession(session);
667
+ this.setSession(session, true);
647
668
  this.emit("SIGNED_IN", session);
648
669
  return { data: session, error: null };
649
670
  } catch (err) {
@@ -659,7 +680,7 @@ var AuthClient = class {
659
680
  const data = await res.json();
660
681
  if (!res.ok) return { data: null, error: data };
661
682
  const session = this.parseAuthResponse(data);
662
- this.setSession(session);
683
+ this.setSession(session, true);
663
684
  this.emit("SIGNED_IN", session);
664
685
  return { data: session, error: null };
665
686
  } catch (err) {
@@ -684,7 +705,7 @@ var AuthClient = class {
684
705
  const data = await res.json();
685
706
  if (!res.ok) return { data: null, error: data };
686
707
  const session = this.parseAuthResponse(data);
687
- this.setSession(session);
708
+ this.setSession(session, true);
688
709
  this.emit("SIGNED_IN", session);
689
710
  return { data: session, error: null };
690
711
  } catch (err) {
@@ -705,7 +726,7 @@ var AuthClient = class {
705
726
  async signOut() {
706
727
  try {
707
728
  await this.request("/auth/v1/logout", { method: "POST" });
708
- this.setSession(null);
729
+ this.setSession(null, true);
709
730
  this.emit("SIGNED_OUT", null);
710
731
  return { error: null };
711
732
  } catch (err) {
@@ -745,12 +766,12 @@ var AuthClient = class {
745
766
  });
746
767
  const data = await res.json();
747
768
  if (!res.ok) {
748
- this.setSession(null);
769
+ this.setSession(null, true);
749
770
  this.emit("SIGNED_OUT", null);
750
771
  return { data: null, error: data };
751
772
  }
752
773
  const session = this.parseAuthResponse(data);
753
- this.setSession(session);
774
+ this.setSession(session, true);
754
775
  this.emit("TOKEN_REFRESHED", session);
755
776
  return { data: session, error: null };
756
777
  } catch (err) {
package/dist/index.d.cts CHANGED
@@ -236,9 +236,11 @@ declare class AuthClient {
236
236
  /**
237
237
  * Set (or clear) the current session. Use this to restore a persisted
238
238
  * session on app launch (e.g., from AsyncStorage / SecureStore).
239
- * Emits INITIAL_SESSION to onAuthStateChange listeners.
239
+ * Automatically refreshes the token if it has expired.
240
+ * Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
241
+ * Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
240
242
  */
241
- setSession(session: AuthSession | null): void;
243
+ setSession(session: AuthSession | null, _internal?: boolean): void;
242
244
  signUp(params: {
243
245
  email: string;
244
246
  password: string;
package/dist/index.d.ts CHANGED
@@ -236,9 +236,11 @@ declare class AuthClient {
236
236
  /**
237
237
  * Set (or clear) the current session. Use this to restore a persisted
238
238
  * session on app launch (e.g., from AsyncStorage / SecureStore).
239
- * Emits INITIAL_SESSION to onAuthStateChange listeners.
239
+ * Automatically refreshes the token if it has expired.
240
+ * Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
241
+ * Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
240
242
  */
241
- setSession(session: AuthSession | null): void;
243
+ setSession(session: AuthSession | null, _internal?: boolean): void;
242
244
  signUp(params: {
243
245
  email: string;
244
246
  password: string;
package/dist/index.js CHANGED
@@ -159,7 +159,12 @@ var DatabaseClient = class {
159
159
  order(column, options) {
160
160
  const dir = options?.ascending === false ? "desc" : "asc";
161
161
  const nulls = options?.nullsFirst === true ? ".nullsfirst" : options?.nullsFirst === false ? ".nullslast" : "";
162
- this.params.set("order", `${column}.${dir}${nulls}`);
162
+ const existing = this.params.get("order");
163
+ if (existing) {
164
+ this.params.set("order", `${existing},${column}.${dir}${nulls}`);
165
+ } else {
166
+ this.params.set("order", `${column}.${dir}${nulls}`);
167
+ }
163
168
  return this;
164
169
  }
165
170
  limit(count) {
@@ -594,12 +599,28 @@ var AuthClient = class {
594
599
  /**
595
600
  * Set (or clear) the current session. Use this to restore a persisted
596
601
  * session on app launch (e.g., from AsyncStorage / SecureStore).
597
- * Emits INITIAL_SESSION to onAuthStateChange listeners.
602
+ * Automatically refreshes the token if it has expired.
603
+ * Emits INITIAL_SESSION to onAuthStateChange listeners when called externally.
604
+ * Internal callers (signIn, signUp, etc.) pass _internal=true to skip the emit.
598
605
  */
599
- setSession(session) {
606
+ setSession(session, _internal) {
600
607
  this.currentSession = session;
601
608
  if (this.onSessionChange) this.onSessionChange(session);
602
- this.emit("INITIAL_SESSION", session);
609
+ if (!_internal) {
610
+ if (session && session.expires_at <= Math.floor(Date.now() / 1e3)) {
611
+ this.refreshSession().then(({ error }) => {
612
+ if (error) {
613
+ this.currentSession = null;
614
+ if (this.onSessionChange) this.onSessionChange(null);
615
+ this.emit("SIGNED_OUT", null);
616
+ } else {
617
+ this.emit("INITIAL_SESSION", this.currentSession);
618
+ }
619
+ });
620
+ } else {
621
+ this.emit("INITIAL_SESSION", session);
622
+ }
623
+ }
603
624
  }
604
625
  // ─── Email/Password ────────────────────────────────────────
605
626
  async signUp(params) {
@@ -611,7 +632,7 @@ var AuthClient = class {
611
632
  const data = await res.json();
612
633
  if (!res.ok) return { data: null, error: data };
613
634
  const session = this.parseAuthResponse(data);
614
- this.setSession(session);
635
+ this.setSession(session, true);
615
636
  this.emit("SIGNED_IN", session);
616
637
  return { data: session, error: null };
617
638
  } catch (err) {
@@ -627,7 +648,7 @@ var AuthClient = class {
627
648
  const data = await res.json();
628
649
  if (!res.ok) return { data: null, error: data };
629
650
  const session = this.parseAuthResponse(data);
630
- this.setSession(session);
651
+ this.setSession(session, true);
631
652
  this.emit("SIGNED_IN", session);
632
653
  return { data: session, error: null };
633
654
  } catch (err) {
@@ -652,7 +673,7 @@ var AuthClient = class {
652
673
  const data = await res.json();
653
674
  if (!res.ok) return { data: null, error: data };
654
675
  const session = this.parseAuthResponse(data);
655
- this.setSession(session);
676
+ this.setSession(session, true);
656
677
  this.emit("SIGNED_IN", session);
657
678
  return { data: session, error: null };
658
679
  } catch (err) {
@@ -673,7 +694,7 @@ var AuthClient = class {
673
694
  async signOut() {
674
695
  try {
675
696
  await this.request("/auth/v1/logout", { method: "POST" });
676
- this.setSession(null);
697
+ this.setSession(null, true);
677
698
  this.emit("SIGNED_OUT", null);
678
699
  return { error: null };
679
700
  } catch (err) {
@@ -713,12 +734,12 @@ var AuthClient = class {
713
734
  });
714
735
  const data = await res.json();
715
736
  if (!res.ok) {
716
- this.setSession(null);
737
+ this.setSession(null, true);
717
738
  this.emit("SIGNED_OUT", null);
718
739
  return { data: null, error: data };
719
740
  }
720
741
  const session = this.parseAuthResponse(data);
721
- this.setSession(session);
742
+ this.setSession(session, true);
722
743
  this.emit("TOKEN_REFRESHED", session);
723
744
  return { data: session, error: null };
724
745
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linabase/js",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "JavaScript/TypeScript client SDK for Linabase (database, storage, auth)",
5
5
  "license": "MIT",
6
6
  "type": "module",