@insforge/sdk 1.4.3 → 1.4.4
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/README.md +124 -143
- package/SDK-REFERENCE.md +146 -168
- package/dist/{client-B6eZHolm.d.mts → client-BZyLCjO1.d.mts} +44 -120
- package/dist/{client-DUmOm_3W.d.ts → client-Dh7GOydb.d.ts} +44 -120
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +556 -423
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +559 -423
- package/dist/index.mjs.map +1 -1
- package/dist/ssr/middleware.js +71 -52
- package/dist/ssr/middleware.js.map +1 -1
- package/dist/ssr/middleware.mjs +71 -52
- package/dist/ssr/middleware.mjs.map +1 -1
- package/dist/ssr.d.mts +1 -1
- package/dist/ssr.d.ts +1 -1
- package/dist/ssr.js +651 -539
- package/dist/ssr.js.map +1 -1
- package/dist/ssr.mjs +655 -540
- package/dist/ssr.mjs.map +1 -1
- package/package.json +12 -5
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { A as AuthSession, I as InsForgeConfig, e as AuthRefreshResponse, d as InsForgeError } from './types-MKmYAYeg.mjs';
|
|
2
|
-
import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse, StripeEnvironment, CreateCheckoutSessionBody, CreateCheckoutSessionResponse, CreateCustomerPortalSessionBody, CreateCustomerPortalSessionResponse, RazorpayEnvironment, CreateRazorpayOrderBody, CreateRazorpayOrderResponse, VerifyRazorpayOrderBody, VerifyRazorpayOrderResponse, CreateRazorpaySubscriptionBody, CreateRazorpaySubscriptionResponse, VerifyRazorpaySubscriptionBody, VerifyRazorpaySubscriptionResponse, CancelRazorpaySubscriptionBodyInput, CancelRazorpaySubscriptionResponse, PauseRazorpaySubscriptionResponse, ResumeRazorpaySubscriptionResponse } from '@insforge/shared-schemas';
|
|
2
|
+
import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, PresenceMember, SendRawEmailRequest, SendEmailResponse, StripeEnvironment, CreateCheckoutSessionBody, CreateCheckoutSessionResponse, CreateCustomerPortalSessionBody, CreateCustomerPortalSessionResponse, RazorpayEnvironment, CreateRazorpayOrderBody, CreateRazorpayOrderResponse, VerifyRazorpayOrderBody, VerifyRazorpayOrderResponse, CreateRazorpaySubscriptionBody, CreateRazorpaySubscriptionResponse, VerifyRazorpaySubscriptionBody, VerifyRazorpaySubscriptionResponse, CancelRazorpaySubscriptionBodyInput, CancelRazorpaySubscriptionResponse, PauseRazorpaySubscriptionResponse, ResumeRazorpaySubscriptionResponse } from '@insforge/shared-schemas';
|
|
3
3
|
import * as _supabase_postgrest_js from '@supabase/postgrest-js';
|
|
4
4
|
import { PostgrestClient } from '@supabase/postgrest-js';
|
|
5
5
|
|
|
@@ -73,15 +73,22 @@ declare class Logger {
|
|
|
73
73
|
* Memory-only token storage.
|
|
74
74
|
*/
|
|
75
75
|
|
|
76
|
+
declare const AuthChangeEvent: {
|
|
77
|
+
readonly SIGNED_IN: "signedIn";
|
|
78
|
+
readonly SIGNED_OUT: "signedOut";
|
|
79
|
+
readonly TOKEN_REFRESHED: "tokenRefreshed";
|
|
80
|
+
};
|
|
81
|
+
type AuthChangeEvent = (typeof AuthChangeEvent)[keyof typeof AuthChangeEvent];
|
|
82
|
+
type AuthStateChangeCallback = (event: AuthChangeEvent) => void;
|
|
76
83
|
declare class TokenManager {
|
|
77
84
|
private accessToken;
|
|
78
85
|
private user;
|
|
79
|
-
|
|
86
|
+
private authStateChangeCallbacks;
|
|
80
87
|
constructor();
|
|
81
88
|
/**
|
|
82
89
|
* Save session in memory
|
|
83
90
|
*/
|
|
84
|
-
saveSession(session: AuthSession): void;
|
|
91
|
+
saveSession(session: AuthSession, event?: AuthChangeEvent): void;
|
|
85
92
|
/**
|
|
86
93
|
* Get current session
|
|
87
94
|
*/
|
|
@@ -93,7 +100,7 @@ declare class TokenManager {
|
|
|
93
100
|
/**
|
|
94
101
|
* Set access token
|
|
95
102
|
*/
|
|
96
|
-
setAccessToken(token: string): void;
|
|
103
|
+
setAccessToken(token: string, event?: AuthChangeEvent): void;
|
|
97
104
|
/**
|
|
98
105
|
* Get user
|
|
99
106
|
*/
|
|
@@ -106,6 +113,8 @@ declare class TokenManager {
|
|
|
106
113
|
* Clear in-memory session
|
|
107
114
|
*/
|
|
108
115
|
clearSession(): void;
|
|
116
|
+
onAuthStateChange(callback: AuthStateChangeCallback): () => void;
|
|
117
|
+
private notifyAuthStateChange;
|
|
109
118
|
}
|
|
110
119
|
|
|
111
120
|
type JsonRequestBody = Record<string, unknown> | unknown[] | null;
|
|
@@ -194,6 +203,8 @@ declare class HttpClient {
|
|
|
194
203
|
/** Returns the current default headers including the authorization header if set. */
|
|
195
204
|
getHeaders(): Record<string, string>;
|
|
196
205
|
refreshAccessToken(): Promise<AuthRefreshResponse>;
|
|
206
|
+
/** Returns a token safe to use for a new connection handshake. */
|
|
207
|
+
getValidAccessToken(leewaySeconds?: number): Promise<string | null>;
|
|
197
208
|
private refreshAndSaveSession;
|
|
198
209
|
private clearAuthSession;
|
|
199
210
|
}
|
|
@@ -222,6 +233,8 @@ declare class Auth {
|
|
|
222
233
|
private authCallbackHandled;
|
|
223
234
|
constructor(http: HttpClient, tokenManager: TokenManager, options?: AuthOptions);
|
|
224
235
|
private isServerMode;
|
|
236
|
+
/** Subscribe to SDK authentication state changes. */
|
|
237
|
+
onAuthStateChange(callback: AuthStateChangeCallback): () => void;
|
|
225
238
|
/**
|
|
226
239
|
* Save session from API response
|
|
227
240
|
* Handles token storage, CSRF token, and HTTP auth header
|
|
@@ -794,135 +807,42 @@ declare class Functions {
|
|
|
794
807
|
type ConnectionState = 'disconnected' | 'connecting' | 'connected';
|
|
795
808
|
type EventCallback<T = unknown> = (payload: T) => void;
|
|
796
809
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
* @example
|
|
800
|
-
* ```typescript
|
|
801
|
-
* const { realtime } = client;
|
|
802
|
-
*
|
|
803
|
-
* // Connect to the realtime server
|
|
804
|
-
* await realtime.connect();
|
|
805
|
-
*
|
|
806
|
-
* // Subscribe to a channel
|
|
807
|
-
* const response = await realtime.subscribe('orders:123');
|
|
808
|
-
* if (!response.ok) {
|
|
809
|
-
* console.error('Failed to subscribe:', response.error);
|
|
810
|
-
* }
|
|
811
|
-
*
|
|
812
|
-
* // Listen for specific events
|
|
813
|
-
* realtime.on('order_updated', (payload) => {
|
|
814
|
-
* console.log('Order updated:', payload);
|
|
815
|
-
* });
|
|
816
|
-
*
|
|
817
|
-
* // Listen for connection events
|
|
818
|
-
* realtime.on('connect', () => console.log('Connected!'));
|
|
819
|
-
* realtime.on('connect_error', (err) => console.error('Connection failed:', err));
|
|
820
|
-
* realtime.on('disconnect', (reason) => console.log('Disconnected:', reason));
|
|
821
|
-
* realtime.on('error', (error) => console.error('Realtime error:', error));
|
|
822
|
-
*
|
|
823
|
-
* // Publish a message to a channel
|
|
824
|
-
* await realtime.publish('orders:123', 'status_changed', { status: 'shipped' });
|
|
825
|
-
*
|
|
826
|
-
* // Unsubscribe and disconnect when done
|
|
827
|
-
* realtime.unsubscribe('orders:123');
|
|
828
|
-
* realtime.disconnect();
|
|
829
|
-
* ```
|
|
810
|
+
* Socket.IO realtime client. Authentication is evaluated for every handshake,
|
|
811
|
+
* while an established socket remains authenticated until it disconnects.
|
|
830
812
|
*/
|
|
831
813
|
declare class Realtime {
|
|
832
814
|
private baseUrl;
|
|
833
815
|
private tokenManager;
|
|
816
|
+
private anonKey?;
|
|
817
|
+
private getValidAccessToken;
|
|
834
818
|
private socket;
|
|
835
819
|
private connectPromise;
|
|
836
|
-
private
|
|
820
|
+
private connectionAttempt;
|
|
821
|
+
private nextConnectionAttemptId;
|
|
822
|
+
private subscriptions;
|
|
837
823
|
private eventListeners;
|
|
838
|
-
|
|
839
|
-
constructor(baseUrl: string, tokenManager: TokenManager, anonKey?: string);
|
|
824
|
+
constructor(baseUrl: string, tokenManager: TokenManager, anonKey?: string | undefined, getValidAccessToken?: () => Promise<string | null>);
|
|
840
825
|
private notifyListeners;
|
|
841
|
-
|
|
842
|
-
* Connect to the realtime server
|
|
843
|
-
* @returns Promise that resolves when connected
|
|
844
|
-
*/
|
|
826
|
+
private getHandshakeToken;
|
|
845
827
|
connect(): Promise<void>;
|
|
846
|
-
/**
|
|
847
|
-
* Disconnect from the realtime server
|
|
848
|
-
*/
|
|
849
828
|
disconnect(): void;
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
private
|
|
856
|
-
/**
|
|
857
|
-
* Check if connected to the realtime server
|
|
858
|
-
*/
|
|
829
|
+
private reconnectForAuthChange;
|
|
830
|
+
private handleDisconnect;
|
|
831
|
+
private resubscribeChannels;
|
|
832
|
+
private requestSubscription;
|
|
833
|
+
private settleSubscription;
|
|
834
|
+
private applyPresenceEvent;
|
|
859
835
|
get isConnected(): boolean;
|
|
860
|
-
/**
|
|
861
|
-
* Get the current connection state
|
|
862
|
-
*/
|
|
863
836
|
get connectionState(): ConnectionState;
|
|
864
|
-
/**
|
|
865
|
-
* Get the socket ID (if connected)
|
|
866
|
-
*/
|
|
867
837
|
get socketId(): string | undefined;
|
|
868
|
-
/**
|
|
869
|
-
* Subscribe to a channel
|
|
870
|
-
*
|
|
871
|
-
* Automatically connects if not already connected.
|
|
872
|
-
*
|
|
873
|
-
* @param channel - Channel name (e.g., 'orders:123', 'broadcast')
|
|
874
|
-
* @returns Promise with the subscription response
|
|
875
|
-
*/
|
|
876
838
|
subscribe(channel: string): Promise<SubscribeResponse>;
|
|
877
|
-
/**
|
|
878
|
-
* Unsubscribe from a channel (fire-and-forget)
|
|
879
|
-
*
|
|
880
|
-
* @param channel - Channel name to unsubscribe from
|
|
881
|
-
*/
|
|
882
839
|
unsubscribe(channel: string): void;
|
|
883
|
-
/**
|
|
884
|
-
* Publish a message to a channel
|
|
885
|
-
*
|
|
886
|
-
* @param channel - Channel name
|
|
887
|
-
* @param event - Event name
|
|
888
|
-
* @param payload - Message payload
|
|
889
|
-
*/
|
|
890
840
|
publish<T = unknown>(channel: string, event: string, payload: T): Promise<void>;
|
|
891
|
-
/**
|
|
892
|
-
* Listen for events
|
|
893
|
-
*
|
|
894
|
-
* Reserved event names:
|
|
895
|
-
* - 'connect' - Fired when connected to the server
|
|
896
|
-
* - 'connect_error' - Fired when connection fails (payload: Error)
|
|
897
|
-
* - 'disconnect' - Fired when disconnected (payload: reason string)
|
|
898
|
-
* - 'error' - Fired when a realtime error occurs (payload: RealtimeErrorPayload)
|
|
899
|
-
*
|
|
900
|
-
* All other events receive a `SocketMessage` payload with metadata.
|
|
901
|
-
*
|
|
902
|
-
* @param event - Event name to listen for
|
|
903
|
-
* @param callback - Callback function when event is received
|
|
904
|
-
*/
|
|
905
841
|
on<T = SocketMessage>(event: string, callback: EventCallback<T>): void;
|
|
906
|
-
/**
|
|
907
|
-
* Remove a listener for a specific event
|
|
908
|
-
*
|
|
909
|
-
* @param event - Event name
|
|
910
|
-
* @param callback - The callback function to remove
|
|
911
|
-
*/
|
|
912
842
|
off<T = SocketMessage>(event: string, callback: EventCallback<T>): void;
|
|
913
|
-
/**
|
|
914
|
-
* Listen for an event only once, then automatically remove the listener
|
|
915
|
-
*
|
|
916
|
-
* @param event - Event name to listen for
|
|
917
|
-
* @param callback - Callback function when event is received
|
|
918
|
-
*/
|
|
919
843
|
once<T = SocketMessage>(event: string, callback: EventCallback<T>): void;
|
|
920
|
-
/**
|
|
921
|
-
* Get all currently subscribed channels
|
|
922
|
-
*
|
|
923
|
-
* @returns Array of channel names
|
|
924
|
-
*/
|
|
925
844
|
getSubscribedChannels(): string[];
|
|
845
|
+
getPresenceState(channel: string): PresenceMember[];
|
|
926
846
|
}
|
|
927
847
|
|
|
928
848
|
/**
|
|
@@ -1030,6 +950,7 @@ declare class Payments {
|
|
|
1030
950
|
constructor(http: HttpClient);
|
|
1031
951
|
}
|
|
1032
952
|
|
|
953
|
+
type AccessTokenChangeEvent = typeof AuthChangeEvent.SIGNED_IN | typeof AuthChangeEvent.TOKEN_REFRESHED;
|
|
1033
954
|
/**
|
|
1034
955
|
* Main InsForge SDK Client
|
|
1035
956
|
*
|
|
@@ -1099,27 +1020,30 @@ declare class InsForgeClient {
|
|
|
1099
1020
|
/**
|
|
1100
1021
|
* Set the access token used by every SDK surface. Updates both the HTTP
|
|
1101
1022
|
* client (database / storage / functions / AI / emails) and the realtime
|
|
1102
|
-
* token manager
|
|
1103
|
-
*
|
|
1023
|
+
* token manager. Pass `null` to sign out. By default a token replacement is
|
|
1024
|
+
* treated as a sign-in boundary and reconnects realtime. Pass
|
|
1025
|
+
* `AuthChangeEvent.TOKEN_REFRESHED` for a same-identity refresh to preserve a live socket; the
|
|
1026
|
+
* refreshed token is then used at the next handshake.
|
|
1104
1027
|
*
|
|
1105
1028
|
* Use this when an external auth provider (Better Auth, Clerk, Auth0,
|
|
1106
1029
|
* WorkOS, Kinde, Stytch, …) issues the JWT and you need to keep the
|
|
1107
1030
|
* long-lived InsForge client in sync. Without this, you'd have to call
|
|
1108
1031
|
* `client.getHttpClient().setAuthToken(token)` AND reach into the private
|
|
1109
|
-
*
|
|
1110
|
-
* forgetting the second one silently breaks realtime auth.
|
|
1032
|
+
* realtime token manager separately.
|
|
1111
1033
|
*
|
|
1112
1034
|
* @example
|
|
1113
1035
|
* ```typescript
|
|
1036
|
+
* import { AuthChangeEvent } from '@insforge/sdk';
|
|
1037
|
+
*
|
|
1114
1038
|
* // Refresh a third-party-issued JWT periodically
|
|
1115
1039
|
* const { token } = await fetch('/api/insforge-token').then((r) => r.json());
|
|
1116
|
-
* client.setAccessToken(token);
|
|
1040
|
+
* client.setAccessToken(token, AuthChangeEvent.TOKEN_REFRESHED);
|
|
1117
1041
|
*
|
|
1118
1042
|
* // Sign-out
|
|
1119
1043
|
* client.setAccessToken(null);
|
|
1120
1044
|
* ```
|
|
1121
1045
|
*/
|
|
1122
|
-
setAccessToken(token: string | null): void;
|
|
1046
|
+
setAccessToken(token: string | null, event?: AccessTokenChangeEvent): void;
|
|
1123
1047
|
}
|
|
1124
1048
|
|
|
1125
|
-
export {
|
|
1049
|
+
export { type AccessTokenChangeEvent as A, type ConnectionState as C, Database as D, Emails as E, Functions as F, HttpClient as H, InsForgeClient as I, Logger as L, Payments as P, Realtime as R, Storage as S, Auth as a, StorageBucket as b, type StorageResponse as c, AI as d, type FunctionInvokeOptions as e, type PaymentsResponse as f, type EventCallback as g, AuthChangeEvent as h, type AuthStateChangeCallback as i };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { A as AuthSession, I as InsForgeConfig, e as AuthRefreshResponse, d as InsForgeError } from './types-MKmYAYeg.js';
|
|
2
|
-
import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse, StripeEnvironment, CreateCheckoutSessionBody, CreateCheckoutSessionResponse, CreateCustomerPortalSessionBody, CreateCustomerPortalSessionResponse, RazorpayEnvironment, CreateRazorpayOrderBody, CreateRazorpayOrderResponse, VerifyRazorpayOrderBody, VerifyRazorpayOrderResponse, CreateRazorpaySubscriptionBody, CreateRazorpaySubscriptionResponse, VerifyRazorpaySubscriptionBody, VerifyRazorpaySubscriptionResponse, CancelRazorpaySubscriptionBodyInput, CancelRazorpaySubscriptionResponse, PauseRazorpaySubscriptionResponse, ResumeRazorpaySubscriptionResponse } from '@insforge/shared-schemas';
|
|
2
|
+
import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, PresenceMember, SendRawEmailRequest, SendEmailResponse, StripeEnvironment, CreateCheckoutSessionBody, CreateCheckoutSessionResponse, CreateCustomerPortalSessionBody, CreateCustomerPortalSessionResponse, RazorpayEnvironment, CreateRazorpayOrderBody, CreateRazorpayOrderResponse, VerifyRazorpayOrderBody, VerifyRazorpayOrderResponse, CreateRazorpaySubscriptionBody, CreateRazorpaySubscriptionResponse, VerifyRazorpaySubscriptionBody, VerifyRazorpaySubscriptionResponse, CancelRazorpaySubscriptionBodyInput, CancelRazorpaySubscriptionResponse, PauseRazorpaySubscriptionResponse, ResumeRazorpaySubscriptionResponse } from '@insforge/shared-schemas';
|
|
3
3
|
import * as _supabase_postgrest_js from '@supabase/postgrest-js';
|
|
4
4
|
import { PostgrestClient } from '@supabase/postgrest-js';
|
|
5
5
|
|
|
@@ -73,15 +73,22 @@ declare class Logger {
|
|
|
73
73
|
* Memory-only token storage.
|
|
74
74
|
*/
|
|
75
75
|
|
|
76
|
+
declare const AuthChangeEvent: {
|
|
77
|
+
readonly SIGNED_IN: "signedIn";
|
|
78
|
+
readonly SIGNED_OUT: "signedOut";
|
|
79
|
+
readonly TOKEN_REFRESHED: "tokenRefreshed";
|
|
80
|
+
};
|
|
81
|
+
type AuthChangeEvent = (typeof AuthChangeEvent)[keyof typeof AuthChangeEvent];
|
|
82
|
+
type AuthStateChangeCallback = (event: AuthChangeEvent) => void;
|
|
76
83
|
declare class TokenManager {
|
|
77
84
|
private accessToken;
|
|
78
85
|
private user;
|
|
79
|
-
|
|
86
|
+
private authStateChangeCallbacks;
|
|
80
87
|
constructor();
|
|
81
88
|
/**
|
|
82
89
|
* Save session in memory
|
|
83
90
|
*/
|
|
84
|
-
saveSession(session: AuthSession): void;
|
|
91
|
+
saveSession(session: AuthSession, event?: AuthChangeEvent): void;
|
|
85
92
|
/**
|
|
86
93
|
* Get current session
|
|
87
94
|
*/
|
|
@@ -93,7 +100,7 @@ declare class TokenManager {
|
|
|
93
100
|
/**
|
|
94
101
|
* Set access token
|
|
95
102
|
*/
|
|
96
|
-
setAccessToken(token: string): void;
|
|
103
|
+
setAccessToken(token: string, event?: AuthChangeEvent): void;
|
|
97
104
|
/**
|
|
98
105
|
* Get user
|
|
99
106
|
*/
|
|
@@ -106,6 +113,8 @@ declare class TokenManager {
|
|
|
106
113
|
* Clear in-memory session
|
|
107
114
|
*/
|
|
108
115
|
clearSession(): void;
|
|
116
|
+
onAuthStateChange(callback: AuthStateChangeCallback): () => void;
|
|
117
|
+
private notifyAuthStateChange;
|
|
109
118
|
}
|
|
110
119
|
|
|
111
120
|
type JsonRequestBody = Record<string, unknown> | unknown[] | null;
|
|
@@ -194,6 +203,8 @@ declare class HttpClient {
|
|
|
194
203
|
/** Returns the current default headers including the authorization header if set. */
|
|
195
204
|
getHeaders(): Record<string, string>;
|
|
196
205
|
refreshAccessToken(): Promise<AuthRefreshResponse>;
|
|
206
|
+
/** Returns a token safe to use for a new connection handshake. */
|
|
207
|
+
getValidAccessToken(leewaySeconds?: number): Promise<string | null>;
|
|
197
208
|
private refreshAndSaveSession;
|
|
198
209
|
private clearAuthSession;
|
|
199
210
|
}
|
|
@@ -222,6 +233,8 @@ declare class Auth {
|
|
|
222
233
|
private authCallbackHandled;
|
|
223
234
|
constructor(http: HttpClient, tokenManager: TokenManager, options?: AuthOptions);
|
|
224
235
|
private isServerMode;
|
|
236
|
+
/** Subscribe to SDK authentication state changes. */
|
|
237
|
+
onAuthStateChange(callback: AuthStateChangeCallback): () => void;
|
|
225
238
|
/**
|
|
226
239
|
* Save session from API response
|
|
227
240
|
* Handles token storage, CSRF token, and HTTP auth header
|
|
@@ -794,135 +807,42 @@ declare class Functions {
|
|
|
794
807
|
type ConnectionState = 'disconnected' | 'connecting' | 'connected';
|
|
795
808
|
type EventCallback<T = unknown> = (payload: T) => void;
|
|
796
809
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
* @example
|
|
800
|
-
* ```typescript
|
|
801
|
-
* const { realtime } = client;
|
|
802
|
-
*
|
|
803
|
-
* // Connect to the realtime server
|
|
804
|
-
* await realtime.connect();
|
|
805
|
-
*
|
|
806
|
-
* // Subscribe to a channel
|
|
807
|
-
* const response = await realtime.subscribe('orders:123');
|
|
808
|
-
* if (!response.ok) {
|
|
809
|
-
* console.error('Failed to subscribe:', response.error);
|
|
810
|
-
* }
|
|
811
|
-
*
|
|
812
|
-
* // Listen for specific events
|
|
813
|
-
* realtime.on('order_updated', (payload) => {
|
|
814
|
-
* console.log('Order updated:', payload);
|
|
815
|
-
* });
|
|
816
|
-
*
|
|
817
|
-
* // Listen for connection events
|
|
818
|
-
* realtime.on('connect', () => console.log('Connected!'));
|
|
819
|
-
* realtime.on('connect_error', (err) => console.error('Connection failed:', err));
|
|
820
|
-
* realtime.on('disconnect', (reason) => console.log('Disconnected:', reason));
|
|
821
|
-
* realtime.on('error', (error) => console.error('Realtime error:', error));
|
|
822
|
-
*
|
|
823
|
-
* // Publish a message to a channel
|
|
824
|
-
* await realtime.publish('orders:123', 'status_changed', { status: 'shipped' });
|
|
825
|
-
*
|
|
826
|
-
* // Unsubscribe and disconnect when done
|
|
827
|
-
* realtime.unsubscribe('orders:123');
|
|
828
|
-
* realtime.disconnect();
|
|
829
|
-
* ```
|
|
810
|
+
* Socket.IO realtime client. Authentication is evaluated for every handshake,
|
|
811
|
+
* while an established socket remains authenticated until it disconnects.
|
|
830
812
|
*/
|
|
831
813
|
declare class Realtime {
|
|
832
814
|
private baseUrl;
|
|
833
815
|
private tokenManager;
|
|
816
|
+
private anonKey?;
|
|
817
|
+
private getValidAccessToken;
|
|
834
818
|
private socket;
|
|
835
819
|
private connectPromise;
|
|
836
|
-
private
|
|
820
|
+
private connectionAttempt;
|
|
821
|
+
private nextConnectionAttemptId;
|
|
822
|
+
private subscriptions;
|
|
837
823
|
private eventListeners;
|
|
838
|
-
|
|
839
|
-
constructor(baseUrl: string, tokenManager: TokenManager, anonKey?: string);
|
|
824
|
+
constructor(baseUrl: string, tokenManager: TokenManager, anonKey?: string | undefined, getValidAccessToken?: () => Promise<string | null>);
|
|
840
825
|
private notifyListeners;
|
|
841
|
-
|
|
842
|
-
* Connect to the realtime server
|
|
843
|
-
* @returns Promise that resolves when connected
|
|
844
|
-
*/
|
|
826
|
+
private getHandshakeToken;
|
|
845
827
|
connect(): Promise<void>;
|
|
846
|
-
/**
|
|
847
|
-
* Disconnect from the realtime server
|
|
848
|
-
*/
|
|
849
828
|
disconnect(): void;
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
private
|
|
856
|
-
/**
|
|
857
|
-
* Check if connected to the realtime server
|
|
858
|
-
*/
|
|
829
|
+
private reconnectForAuthChange;
|
|
830
|
+
private handleDisconnect;
|
|
831
|
+
private resubscribeChannels;
|
|
832
|
+
private requestSubscription;
|
|
833
|
+
private settleSubscription;
|
|
834
|
+
private applyPresenceEvent;
|
|
859
835
|
get isConnected(): boolean;
|
|
860
|
-
/**
|
|
861
|
-
* Get the current connection state
|
|
862
|
-
*/
|
|
863
836
|
get connectionState(): ConnectionState;
|
|
864
|
-
/**
|
|
865
|
-
* Get the socket ID (if connected)
|
|
866
|
-
*/
|
|
867
837
|
get socketId(): string | undefined;
|
|
868
|
-
/**
|
|
869
|
-
* Subscribe to a channel
|
|
870
|
-
*
|
|
871
|
-
* Automatically connects if not already connected.
|
|
872
|
-
*
|
|
873
|
-
* @param channel - Channel name (e.g., 'orders:123', 'broadcast')
|
|
874
|
-
* @returns Promise with the subscription response
|
|
875
|
-
*/
|
|
876
838
|
subscribe(channel: string): Promise<SubscribeResponse>;
|
|
877
|
-
/**
|
|
878
|
-
* Unsubscribe from a channel (fire-and-forget)
|
|
879
|
-
*
|
|
880
|
-
* @param channel - Channel name to unsubscribe from
|
|
881
|
-
*/
|
|
882
839
|
unsubscribe(channel: string): void;
|
|
883
|
-
/**
|
|
884
|
-
* Publish a message to a channel
|
|
885
|
-
*
|
|
886
|
-
* @param channel - Channel name
|
|
887
|
-
* @param event - Event name
|
|
888
|
-
* @param payload - Message payload
|
|
889
|
-
*/
|
|
890
840
|
publish<T = unknown>(channel: string, event: string, payload: T): Promise<void>;
|
|
891
|
-
/**
|
|
892
|
-
* Listen for events
|
|
893
|
-
*
|
|
894
|
-
* Reserved event names:
|
|
895
|
-
* - 'connect' - Fired when connected to the server
|
|
896
|
-
* - 'connect_error' - Fired when connection fails (payload: Error)
|
|
897
|
-
* - 'disconnect' - Fired when disconnected (payload: reason string)
|
|
898
|
-
* - 'error' - Fired when a realtime error occurs (payload: RealtimeErrorPayload)
|
|
899
|
-
*
|
|
900
|
-
* All other events receive a `SocketMessage` payload with metadata.
|
|
901
|
-
*
|
|
902
|
-
* @param event - Event name to listen for
|
|
903
|
-
* @param callback - Callback function when event is received
|
|
904
|
-
*/
|
|
905
841
|
on<T = SocketMessage>(event: string, callback: EventCallback<T>): void;
|
|
906
|
-
/**
|
|
907
|
-
* Remove a listener for a specific event
|
|
908
|
-
*
|
|
909
|
-
* @param event - Event name
|
|
910
|
-
* @param callback - The callback function to remove
|
|
911
|
-
*/
|
|
912
842
|
off<T = SocketMessage>(event: string, callback: EventCallback<T>): void;
|
|
913
|
-
/**
|
|
914
|
-
* Listen for an event only once, then automatically remove the listener
|
|
915
|
-
*
|
|
916
|
-
* @param event - Event name to listen for
|
|
917
|
-
* @param callback - Callback function when event is received
|
|
918
|
-
*/
|
|
919
843
|
once<T = SocketMessage>(event: string, callback: EventCallback<T>): void;
|
|
920
|
-
/**
|
|
921
|
-
* Get all currently subscribed channels
|
|
922
|
-
*
|
|
923
|
-
* @returns Array of channel names
|
|
924
|
-
*/
|
|
925
844
|
getSubscribedChannels(): string[];
|
|
845
|
+
getPresenceState(channel: string): PresenceMember[];
|
|
926
846
|
}
|
|
927
847
|
|
|
928
848
|
/**
|
|
@@ -1030,6 +950,7 @@ declare class Payments {
|
|
|
1030
950
|
constructor(http: HttpClient);
|
|
1031
951
|
}
|
|
1032
952
|
|
|
953
|
+
type AccessTokenChangeEvent = typeof AuthChangeEvent.SIGNED_IN | typeof AuthChangeEvent.TOKEN_REFRESHED;
|
|
1033
954
|
/**
|
|
1034
955
|
* Main InsForge SDK Client
|
|
1035
956
|
*
|
|
@@ -1099,27 +1020,30 @@ declare class InsForgeClient {
|
|
|
1099
1020
|
/**
|
|
1100
1021
|
* Set the access token used by every SDK surface. Updates both the HTTP
|
|
1101
1022
|
* client (database / storage / functions / AI / emails) and the realtime
|
|
1102
|
-
* token manager
|
|
1103
|
-
*
|
|
1023
|
+
* token manager. Pass `null` to sign out. By default a token replacement is
|
|
1024
|
+
* treated as a sign-in boundary and reconnects realtime. Pass
|
|
1025
|
+
* `AuthChangeEvent.TOKEN_REFRESHED` for a same-identity refresh to preserve a live socket; the
|
|
1026
|
+
* refreshed token is then used at the next handshake.
|
|
1104
1027
|
*
|
|
1105
1028
|
* Use this when an external auth provider (Better Auth, Clerk, Auth0,
|
|
1106
1029
|
* WorkOS, Kinde, Stytch, …) issues the JWT and you need to keep the
|
|
1107
1030
|
* long-lived InsForge client in sync. Without this, you'd have to call
|
|
1108
1031
|
* `client.getHttpClient().setAuthToken(token)` AND reach into the private
|
|
1109
|
-
*
|
|
1110
|
-
* forgetting the second one silently breaks realtime auth.
|
|
1032
|
+
* realtime token manager separately.
|
|
1111
1033
|
*
|
|
1112
1034
|
* @example
|
|
1113
1035
|
* ```typescript
|
|
1036
|
+
* import { AuthChangeEvent } from '@insforge/sdk';
|
|
1037
|
+
*
|
|
1114
1038
|
* // Refresh a third-party-issued JWT periodically
|
|
1115
1039
|
* const { token } = await fetch('/api/insforge-token').then((r) => r.json());
|
|
1116
|
-
* client.setAccessToken(token);
|
|
1040
|
+
* client.setAccessToken(token, AuthChangeEvent.TOKEN_REFRESHED);
|
|
1117
1041
|
*
|
|
1118
1042
|
* // Sign-out
|
|
1119
1043
|
* client.setAccessToken(null);
|
|
1120
1044
|
* ```
|
|
1121
1045
|
*/
|
|
1122
|
-
setAccessToken(token: string | null): void;
|
|
1046
|
+
setAccessToken(token: string | null, event?: AccessTokenChangeEvent): void;
|
|
1123
1047
|
}
|
|
1124
1048
|
|
|
1125
|
-
export {
|
|
1049
|
+
export { type AccessTokenChangeEvent as A, type ConnectionState as C, Database as D, Emails as E, Functions as F, HttpClient as H, InsForgeClient as I, Logger as L, Payments as P, Realtime as R, Storage as S, Auth as a, StorageBucket as b, type StorageResponse as c, AI as d, type FunctionInvokeOptions as e, type PaymentsResponse as f, type EventCallback as g, AuthChangeEvent as h, type AuthStateChangeCallback as i };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as InsForgeClient } from './client-
|
|
2
|
-
export {
|
|
1
|
+
import { I as InsForgeClient } from './client-BZyLCjO1.mjs';
|
|
2
|
+
export { d as AI, A as AccessTokenChangeEvent, a as Auth, h as AuthChangeEvent, i as AuthStateChangeCallback, C as ConnectionState, D as Database, E as Emails, g as EventCallback, e as FunctionInvokeOptions, F as Functions, H as HttpClient, L as Logger, P as Payments, f as PaymentsResponse, R as Realtime, S as Storage, b as StorageBucket, c as StorageResponse } from './client-BZyLCjO1.mjs';
|
|
3
3
|
import { I as InsForgeConfig, a as InsForgeAdminConfig } from './types-MKmYAYeg.mjs';
|
|
4
4
|
export { b as ApiError, A as AuthSession, d as InsForgeError, c as InsForgeErrorCode } from './types-MKmYAYeg.mjs';
|
|
5
5
|
export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as InsForgeClient } from './client-
|
|
2
|
-
export {
|
|
1
|
+
import { I as InsForgeClient } from './client-Dh7GOydb.js';
|
|
2
|
+
export { d as AI, A as AccessTokenChangeEvent, a as Auth, h as AuthChangeEvent, i as AuthStateChangeCallback, C as ConnectionState, D as Database, E as Emails, g as EventCallback, e as FunctionInvokeOptions, F as Functions, H as HttpClient, L as Logger, P as Payments, f as PaymentsResponse, R as Realtime, S as Storage, b as StorageBucket, c as StorageResponse } from './client-Dh7GOydb.js';
|
|
3
3
|
import { I as InsForgeConfig, a as InsForgeAdminConfig } from './types-MKmYAYeg.js';
|
|
4
4
|
export { b as ApiError, A as AuthSession, d as InsForgeError, c as InsForgeErrorCode } from './types-MKmYAYeg.js';
|
|
5
5
|
export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
|