@spacelr/sdk 0.1.8 → 0.1.10

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.mts CHANGED
@@ -58,8 +58,10 @@ interface RegisterResponse {
58
58
  username: string;
59
59
  displayName?: string;
60
60
  };
61
- access_token: string;
62
- refresh_token: string;
61
+ /** Omitted by the server when `emailVerificationRequired` is true. */
62
+ access_token?: string;
63
+ /** Omitted by the server when `emailVerificationRequired` is true. */
64
+ refresh_token?: string;
63
65
  emailVerificationRequired?: boolean;
64
66
  }
65
67
  interface TokenResponse {
@@ -430,11 +432,58 @@ declare class RealtimeClient {
430
432
  private resubscribeAll;
431
433
  }
432
434
 
435
+ type AuthState = 'authenticated' | 'unauthenticated';
436
+ type AuthStateListener = (state: AuthState) => void | Promise<void>;
433
437
  declare class AuthModule {
434
438
  private http;
435
439
  private tokenManager;
436
440
  private config;
441
+ private stateListeners;
442
+ private unsubscribeAuthLost;
443
+ private lastEmittedState;
437
444
  constructor(http: HttpClient, tokenManager: TokenManager, config: SpacelrClientConfig);
445
+ /**
446
+ * Returns true if a non-expired access token is currently in storage.
447
+ * Does NOT make a network request — safe for route guards and other
448
+ * hot paths that run on every navigation.
449
+ *
450
+ * A token within the refresh buffer (about to expire) still counts as
451
+ * authenticated because the next protected request will auto-refresh it.
452
+ *
453
+ * Any error from the underlying TokenStorage (corrupt JSON, quota, etc.)
454
+ * is treated as "not authenticated" rather than propagated, so route
455
+ * guards can't be crashed by a misbehaving storage backend.
456
+ */
457
+ isAuthenticated(): Promise<boolean>;
458
+ /**
459
+ * Subscribe to auth-state transitions. The callback fires:
460
+ * - 'authenticated' after a successful login/register/exchange/2FA-verify
461
+ * - 'unauthenticated' after logout or when a token refresh fails
462
+ *
463
+ * Only fires for transitions that happen after the subscription. If the
464
+ * user is already logged in at subscribe time (e.g. tokens restored from
465
+ * storage on app boot), no 'authenticated' event is emitted — call
466
+ * `isAuthenticated()` once up-front for the initial state.
467
+ *
468
+ * Silent token refreshes do NOT produce an event (auth state is
469
+ * unchanged). Subscribe to `spacelrClient.onTokenRefreshed(...)` if you
470
+ * need to observe successful refreshes.
471
+ *
472
+ * Listener may return `void` or `Promise<void>`. Rejections are swallowed
473
+ * so one broken subscriber can't poison others or the auth flow. The
474
+ * dispatch is fire-and-forget: `logout()` / `login()` resolve as soon as
475
+ * the dispatch loop returns, without awaiting async listeners.
476
+ *
477
+ * Returns an unsubscribe function.
478
+ */
479
+ onAuthStateChange(listener: AuthStateListener): () => void;
480
+ /**
481
+ * Detach this AuthModule from the TokenManager. Call when discarding the
482
+ * client (tests, HMR, multi-client setups) to avoid leaking the internal
483
+ * onAuthLost subscription. Idempotent — safe to call more than once.
484
+ */
485
+ dispose(): void;
486
+ private emitState;
438
487
  login(params: LoginParams): Promise<LoginResponse>;
439
488
  register(params: RegisterParams): Promise<RegisterResponse>;
440
489
  refresh(refreshToken: string): Promise<TokenResponse>;
@@ -692,7 +741,20 @@ declare class NotificationsModule {
692
741
  }
693
742
 
694
743
  interface FunctionInvokeOptions {
695
- secret: string;
744
+ /** Webhook secret — used for `webhook` and `hybrid` invokeMode. */
745
+ secret?: string;
746
+ /**
747
+ * Whether to attach the signed-in user's bearer token via `Authorization`
748
+ * (managed by the SDK's TokenManager).
749
+ *
750
+ * Default: `true` when no `secret` is provided, `false` when `secret` is
751
+ * set. Override explicitly for the hybrid case (provide both) or to
752
+ * suppress the header even when logged in.
753
+ *
754
+ * If `true` but the user is not signed in, the header is simply omitted —
755
+ * safe for `public` invokeMode.
756
+ */
757
+ authenticated?: boolean;
696
758
  payload?: Record<string, unknown>;
697
759
  }
698
760
  interface FunctionInvokeResult {
@@ -704,11 +766,20 @@ declare class FunctionsModule {
704
766
  private http;
705
767
  constructor(http: HttpClient);
706
768
  /**
707
- * Invoke a function via webhook trigger.
708
- * Calls POST /api/v1/functions/:projectId/:functionId/invoke
709
- * with X-Webhook-Secret header.
769
+ * Invoke a function.
770
+ * Calls POST `/functions/:projectId/:functionId/invoke`, resolved against
771
+ * `config.apiUrl` (which already carries the `/api/v1` prefix).
772
+ *
773
+ * Auth defaults, based on `invokeMode` semantics:
774
+ * - webhook: pass `secret` → Authorization is NOT attached
775
+ * - authenticated: pass nothing → Authorization IS attached (from token manager)
776
+ * - public: pass nothing → Authorization is attached if logged in, else omitted
777
+ * - hybrid: pass both `secret` and `authenticated: true`
778
+ *
779
+ * To force a specific behaviour, set `authenticated` explicitly — it wins
780
+ * over the `secret`-based default.
710
781
  */
711
- invoke(projectId: string, functionId: string, options: FunctionInvokeOptions): Promise<FunctionInvokeResult>;
782
+ invoke(projectId: string, functionId: string, options?: FunctionInvokeOptions): Promise<FunctionInvokeResult>;
712
783
  }
713
784
 
714
785
  interface SpacelrClient {
@@ -768,4 +839,4 @@ interface SpacelrClient {
768
839
  }
769
840
  declare function createClient(config: SpacelrClientConfig): SpacelrClient;
770
841
 
771
- export { type ApiResponse, type AuthLostReason, type AuthorizationUrlParams, BrowserTokenStorage, CodeChallengeMethod, type ConnectionState, type DatabaseChangeEvent, type DownloadUrlResponse, type ExchangeCodeParams, type FileInfo, type FileListResponse, FileVisibility, type FunctionInvokeOptions, type FunctionInvokeResult, GrantType, type InitMultipartUploadParams, type InitMultipartUploadResponse, type JWK, type JWKSResponse, type ListFilesParams, type LoginParams, type LoginResponse, MemoryTokenStorage, type OpenIDConfiguration, type PKCEChallenge, type PartEtag, type PushSubscriptionInfo, type QuotaInfo, type RegisterParams, type RegisterResponse, type SearchOptions, type ShareFileParams, SharePermission, SpacelrAuthError, type SpacelrClient, type SpacelrClientConfig, SpacelrEmailVerificationRequiredError, SpacelrError, SpacelrNetworkError, SpacelrTimeoutError, SpacelrTwoFactorRequiredError, type StoredTokens, type SubscribeHandlers, type TokenResponse, type TokenStorage, type TwoFactorResponse, type TwoFactorVerifyParams, type UnshareFileParams, type UploadFileParams, type UploadLargeFileParams, type UploadProgress, type UserInfo, type UserProfile, type VapidKeyResponse, createClient, generatePKCEChallenge };
842
+ export { type ApiResponse, type AuthLostReason, type AuthState, type AuthStateListener, type AuthorizationUrlParams, BrowserTokenStorage, CodeChallengeMethod, type ConnectionState, type DatabaseChangeEvent, type DownloadUrlResponse, type ExchangeCodeParams, type FileInfo, type FileListResponse, FileVisibility, type FunctionInvokeOptions, type FunctionInvokeResult, GrantType, type InitMultipartUploadParams, type InitMultipartUploadResponse, type JWK, type JWKSResponse, type ListFilesParams, type LoginParams, type LoginResponse, MemoryTokenStorage, type OpenIDConfiguration, type PKCEChallenge, type PartEtag, type PushSubscriptionInfo, type QuotaInfo, type RegisterParams, type RegisterResponse, type SearchOptions, type ShareFileParams, SharePermission, SpacelrAuthError, type SpacelrClient, type SpacelrClientConfig, SpacelrEmailVerificationRequiredError, SpacelrError, SpacelrNetworkError, SpacelrTimeoutError, SpacelrTwoFactorRequiredError, type StoredTokens, type SubscribeHandlers, type TokenResponse, type TokenStorage, type TwoFactorResponse, type TwoFactorVerifyParams, type UnshareFileParams, type UploadFileParams, type UploadLargeFileParams, type UploadProgress, type UserInfo, type UserProfile, type VapidKeyResponse, createClient, generatePKCEChallenge };
package/dist/index.d.ts CHANGED
@@ -58,8 +58,10 @@ interface RegisterResponse {
58
58
  username: string;
59
59
  displayName?: string;
60
60
  };
61
- access_token: string;
62
- refresh_token: string;
61
+ /** Omitted by the server when `emailVerificationRequired` is true. */
62
+ access_token?: string;
63
+ /** Omitted by the server when `emailVerificationRequired` is true. */
64
+ refresh_token?: string;
63
65
  emailVerificationRequired?: boolean;
64
66
  }
65
67
  interface TokenResponse {
@@ -430,11 +432,58 @@ declare class RealtimeClient {
430
432
  private resubscribeAll;
431
433
  }
432
434
 
435
+ type AuthState = 'authenticated' | 'unauthenticated';
436
+ type AuthStateListener = (state: AuthState) => void | Promise<void>;
433
437
  declare class AuthModule {
434
438
  private http;
435
439
  private tokenManager;
436
440
  private config;
441
+ private stateListeners;
442
+ private unsubscribeAuthLost;
443
+ private lastEmittedState;
437
444
  constructor(http: HttpClient, tokenManager: TokenManager, config: SpacelrClientConfig);
445
+ /**
446
+ * Returns true if a non-expired access token is currently in storage.
447
+ * Does NOT make a network request — safe for route guards and other
448
+ * hot paths that run on every navigation.
449
+ *
450
+ * A token within the refresh buffer (about to expire) still counts as
451
+ * authenticated because the next protected request will auto-refresh it.
452
+ *
453
+ * Any error from the underlying TokenStorage (corrupt JSON, quota, etc.)
454
+ * is treated as "not authenticated" rather than propagated, so route
455
+ * guards can't be crashed by a misbehaving storage backend.
456
+ */
457
+ isAuthenticated(): Promise<boolean>;
458
+ /**
459
+ * Subscribe to auth-state transitions. The callback fires:
460
+ * - 'authenticated' after a successful login/register/exchange/2FA-verify
461
+ * - 'unauthenticated' after logout or when a token refresh fails
462
+ *
463
+ * Only fires for transitions that happen after the subscription. If the
464
+ * user is already logged in at subscribe time (e.g. tokens restored from
465
+ * storage on app boot), no 'authenticated' event is emitted — call
466
+ * `isAuthenticated()` once up-front for the initial state.
467
+ *
468
+ * Silent token refreshes do NOT produce an event (auth state is
469
+ * unchanged). Subscribe to `spacelrClient.onTokenRefreshed(...)` if you
470
+ * need to observe successful refreshes.
471
+ *
472
+ * Listener may return `void` or `Promise<void>`. Rejections are swallowed
473
+ * so one broken subscriber can't poison others or the auth flow. The
474
+ * dispatch is fire-and-forget: `logout()` / `login()` resolve as soon as
475
+ * the dispatch loop returns, without awaiting async listeners.
476
+ *
477
+ * Returns an unsubscribe function.
478
+ */
479
+ onAuthStateChange(listener: AuthStateListener): () => void;
480
+ /**
481
+ * Detach this AuthModule from the TokenManager. Call when discarding the
482
+ * client (tests, HMR, multi-client setups) to avoid leaking the internal
483
+ * onAuthLost subscription. Idempotent — safe to call more than once.
484
+ */
485
+ dispose(): void;
486
+ private emitState;
438
487
  login(params: LoginParams): Promise<LoginResponse>;
439
488
  register(params: RegisterParams): Promise<RegisterResponse>;
440
489
  refresh(refreshToken: string): Promise<TokenResponse>;
@@ -692,7 +741,20 @@ declare class NotificationsModule {
692
741
  }
693
742
 
694
743
  interface FunctionInvokeOptions {
695
- secret: string;
744
+ /** Webhook secret — used for `webhook` and `hybrid` invokeMode. */
745
+ secret?: string;
746
+ /**
747
+ * Whether to attach the signed-in user's bearer token via `Authorization`
748
+ * (managed by the SDK's TokenManager).
749
+ *
750
+ * Default: `true` when no `secret` is provided, `false` when `secret` is
751
+ * set. Override explicitly for the hybrid case (provide both) or to
752
+ * suppress the header even when logged in.
753
+ *
754
+ * If `true` but the user is not signed in, the header is simply omitted —
755
+ * safe for `public` invokeMode.
756
+ */
757
+ authenticated?: boolean;
696
758
  payload?: Record<string, unknown>;
697
759
  }
698
760
  interface FunctionInvokeResult {
@@ -704,11 +766,20 @@ declare class FunctionsModule {
704
766
  private http;
705
767
  constructor(http: HttpClient);
706
768
  /**
707
- * Invoke a function via webhook trigger.
708
- * Calls POST /api/v1/functions/:projectId/:functionId/invoke
709
- * with X-Webhook-Secret header.
769
+ * Invoke a function.
770
+ * Calls POST `/functions/:projectId/:functionId/invoke`, resolved against
771
+ * `config.apiUrl` (which already carries the `/api/v1` prefix).
772
+ *
773
+ * Auth defaults, based on `invokeMode` semantics:
774
+ * - webhook: pass `secret` → Authorization is NOT attached
775
+ * - authenticated: pass nothing → Authorization IS attached (from token manager)
776
+ * - public: pass nothing → Authorization is attached if logged in, else omitted
777
+ * - hybrid: pass both `secret` and `authenticated: true`
778
+ *
779
+ * To force a specific behaviour, set `authenticated` explicitly — it wins
780
+ * over the `secret`-based default.
710
781
  */
711
- invoke(projectId: string, functionId: string, options: FunctionInvokeOptions): Promise<FunctionInvokeResult>;
782
+ invoke(projectId: string, functionId: string, options?: FunctionInvokeOptions): Promise<FunctionInvokeResult>;
712
783
  }
713
784
 
714
785
  interface SpacelrClient {
@@ -768,4 +839,4 @@ interface SpacelrClient {
768
839
  }
769
840
  declare function createClient(config: SpacelrClientConfig): SpacelrClient;
770
841
 
771
- export { type ApiResponse, type AuthLostReason, type AuthorizationUrlParams, BrowserTokenStorage, CodeChallengeMethod, type ConnectionState, type DatabaseChangeEvent, type DownloadUrlResponse, type ExchangeCodeParams, type FileInfo, type FileListResponse, FileVisibility, type FunctionInvokeOptions, type FunctionInvokeResult, GrantType, type InitMultipartUploadParams, type InitMultipartUploadResponse, type JWK, type JWKSResponse, type ListFilesParams, type LoginParams, type LoginResponse, MemoryTokenStorage, type OpenIDConfiguration, type PKCEChallenge, type PartEtag, type PushSubscriptionInfo, type QuotaInfo, type RegisterParams, type RegisterResponse, type SearchOptions, type ShareFileParams, SharePermission, SpacelrAuthError, type SpacelrClient, type SpacelrClientConfig, SpacelrEmailVerificationRequiredError, SpacelrError, SpacelrNetworkError, SpacelrTimeoutError, SpacelrTwoFactorRequiredError, type StoredTokens, type SubscribeHandlers, type TokenResponse, type TokenStorage, type TwoFactorResponse, type TwoFactorVerifyParams, type UnshareFileParams, type UploadFileParams, type UploadLargeFileParams, type UploadProgress, type UserInfo, type UserProfile, type VapidKeyResponse, createClient, generatePKCEChallenge };
842
+ export { type ApiResponse, type AuthLostReason, type AuthState, type AuthStateListener, type AuthorizationUrlParams, BrowserTokenStorage, CodeChallengeMethod, type ConnectionState, type DatabaseChangeEvent, type DownloadUrlResponse, type ExchangeCodeParams, type FileInfo, type FileListResponse, FileVisibility, type FunctionInvokeOptions, type FunctionInvokeResult, GrantType, type InitMultipartUploadParams, type InitMultipartUploadResponse, type JWK, type JWKSResponse, type ListFilesParams, type LoginParams, type LoginResponse, MemoryTokenStorage, type OpenIDConfiguration, type PKCEChallenge, type PartEtag, type PushSubscriptionInfo, type QuotaInfo, type RegisterParams, type RegisterResponse, type SearchOptions, type ShareFileParams, SharePermission, SpacelrAuthError, type SpacelrClient, type SpacelrClientConfig, SpacelrEmailVerificationRequiredError, SpacelrError, SpacelrNetworkError, SpacelrTimeoutError, SpacelrTwoFactorRequiredError, type StoredTokens, type SubscribeHandlers, type TokenResponse, type TokenStorage, type TwoFactorResponse, type TwoFactorVerifyParams, type UnshareFileParams, type UploadFileParams, type UploadLargeFileParams, type UploadProgress, type UserInfo, type UserProfile, type VapidKeyResponse, createClient, generatePKCEChallenge };
package/dist/index.js CHANGED
@@ -901,6 +901,8 @@ var RealtimeClient = class {
901
901
  // libs/sdk/src/modules/auth.module.ts
902
902
  var AuthModule = class {
903
903
  constructor(http, tokenManager, config) {
904
+ this.stateListeners = /* @__PURE__ */ new Set();
905
+ this.lastEmittedState = null;
904
906
  this.http = http;
905
907
  this.tokenManager = tokenManager;
906
908
  this.config = config;
@@ -913,6 +915,84 @@ var AuthModule = class {
913
915
  expiresAt
914
916
  };
915
917
  });
918
+ this.unsubscribeAuthLost = this.tokenManager.onAuthLost(
919
+ () => this.emitState("unauthenticated")
920
+ );
921
+ }
922
+ /**
923
+ * Returns true if a non-expired access token is currently in storage.
924
+ * Does NOT make a network request — safe for route guards and other
925
+ * hot paths that run on every navigation.
926
+ *
927
+ * A token within the refresh buffer (about to expire) still counts as
928
+ * authenticated because the next protected request will auto-refresh it.
929
+ *
930
+ * Any error from the underlying TokenStorage (corrupt JSON, quota, etc.)
931
+ * is treated as "not authenticated" rather than propagated, so route
932
+ * guards can't be crashed by a misbehaving storage backend.
933
+ */
934
+ async isAuthenticated() {
935
+ try {
936
+ const tokens = await this.tokenManager.getStoredTokens();
937
+ if (!tokens?.accessToken) return false;
938
+ if (tokens.expiresAt && tokens.expiresAt * 1e3 <= Date.now()) return false;
939
+ return true;
940
+ } catch {
941
+ return false;
942
+ }
943
+ }
944
+ /**
945
+ * Subscribe to auth-state transitions. The callback fires:
946
+ * - 'authenticated' after a successful login/register/exchange/2FA-verify
947
+ * - 'unauthenticated' after logout or when a token refresh fails
948
+ *
949
+ * Only fires for transitions that happen after the subscription. If the
950
+ * user is already logged in at subscribe time (e.g. tokens restored from
951
+ * storage on app boot), no 'authenticated' event is emitted — call
952
+ * `isAuthenticated()` once up-front for the initial state.
953
+ *
954
+ * Silent token refreshes do NOT produce an event (auth state is
955
+ * unchanged). Subscribe to `spacelrClient.onTokenRefreshed(...)` if you
956
+ * need to observe successful refreshes.
957
+ *
958
+ * Listener may return `void` or `Promise<void>`. Rejections are swallowed
959
+ * so one broken subscriber can't poison others or the auth flow. The
960
+ * dispatch is fire-and-forget: `logout()` / `login()` resolve as soon as
961
+ * the dispatch loop returns, without awaiting async listeners.
962
+ *
963
+ * Returns an unsubscribe function.
964
+ */
965
+ onAuthStateChange(listener) {
966
+ this.stateListeners.add(listener);
967
+ return () => {
968
+ this.stateListeners.delete(listener);
969
+ };
970
+ }
971
+ /**
972
+ * Detach this AuthModule from the TokenManager. Call when discarding the
973
+ * client (tests, HMR, multi-client setups) to avoid leaking the internal
974
+ * onAuthLost subscription. Idempotent — safe to call more than once.
975
+ */
976
+ dispose() {
977
+ this.unsubscribeAuthLost();
978
+ this.unsubscribeAuthLost = () => {
979
+ };
980
+ this.stateListeners.clear();
981
+ this.lastEmittedState = null;
982
+ }
983
+ emitState(state) {
984
+ if (state === this.lastEmittedState) return;
985
+ this.lastEmittedState = state;
986
+ for (const listener of this.stateListeners) {
987
+ try {
988
+ const result = listener(state);
989
+ if (result && typeof result.then === "function") {
990
+ result.then(void 0, () => {
991
+ });
992
+ }
993
+ } catch {
994
+ }
995
+ }
916
996
  }
917
997
  async login(params) {
918
998
  const response = await this.http.request({
@@ -958,6 +1038,7 @@ var AuthModule = class {
958
1038
  } catch {
959
1039
  }
960
1040
  await this.tokenManager.clearTokens();
1041
+ this.emitState("unauthenticated");
961
1042
  }
962
1043
  async verifyEmail(token) {
963
1044
  return this.http.request({
@@ -1028,6 +1109,7 @@ var AuthModule = class {
1028
1109
  refreshToken: response.refresh_token,
1029
1110
  expiresAt
1030
1111
  });
1112
+ this.emitState("authenticated");
1031
1113
  return response;
1032
1114
  }
1033
1115
  async generatePKCE() {
@@ -1110,6 +1192,7 @@ var AuthModule = class {
1110
1192
  refreshToken: response.refresh_token,
1111
1193
  expiresAt
1112
1194
  });
1195
+ this.emitState("authenticated");
1113
1196
  }
1114
1197
  async storeTokensFromRegister(response) {
1115
1198
  if (!response.access_token) return;
@@ -1117,6 +1200,7 @@ var AuthModule = class {
1117
1200
  accessToken: response.access_token,
1118
1201
  refreshToken: response.refresh_token
1119
1202
  });
1203
+ this.emitState("authenticated");
1120
1204
  }
1121
1205
  };
1122
1206
 
@@ -1688,19 +1772,32 @@ var FunctionsModule = class {
1688
1772
  this.http = http;
1689
1773
  }
1690
1774
  /**
1691
- * Invoke a function via webhook trigger.
1692
- * Calls POST /api/v1/functions/:projectId/:functionId/invoke
1693
- * with X-Webhook-Secret header.
1775
+ * Invoke a function.
1776
+ * Calls POST `/functions/:projectId/:functionId/invoke`, resolved against
1777
+ * `config.apiUrl` (which already carries the `/api/v1` prefix).
1778
+ *
1779
+ * Auth defaults, based on `invokeMode` semantics:
1780
+ * - webhook: pass `secret` → Authorization is NOT attached
1781
+ * - authenticated: pass nothing → Authorization IS attached (from token manager)
1782
+ * - public: pass nothing → Authorization is attached if logged in, else omitted
1783
+ * - hybrid: pass both `secret` and `authenticated: true`
1784
+ *
1785
+ * To force a specific behaviour, set `authenticated` explicitly — it wins
1786
+ * over the `secret`-based default.
1694
1787
  */
1695
- async invoke(projectId, functionId, options) {
1788
+ async invoke(projectId, functionId, options = {}) {
1789
+ const hasSecret = (options.secret?.length ?? 0) > 0;
1790
+ const headers = {};
1791
+ if (hasSecret) {
1792
+ headers["X-Webhook-Secret"] = options.secret;
1793
+ }
1794
+ const authenticated = options.authenticated ?? !hasSecret;
1696
1795
  return this.http.request({
1697
1796
  method: "POST",
1698
- path: `/api/v1/functions/${encodeURIComponent(projectId)}/${encodeURIComponent(functionId)}/invoke`,
1699
- headers: {
1700
- "X-Webhook-Secret": options.secret
1701
- },
1797
+ path: `/functions/${encodeURIComponent(projectId)}/${encodeURIComponent(functionId)}/invoke`,
1798
+ headers,
1702
1799
  body: options.payload ?? {},
1703
- authenticated: false
1800
+ authenticated
1704
1801
  });
1705
1802
  }
1706
1803
  };