@insforge/sdk 1.3.0-ssr.0 → 1.3.0-ssr.1

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 CHANGED
@@ -264,7 +264,7 @@ const insforge = createClient({
264
264
  ### SSR / Next.js
265
265
 
266
266
  Use `@insforge/sdk/ssr` for apps that need the same auth session in Server Components, Client Components, Storage, and Realtime.
267
- The helper reads `NEXT_PUBLIC_INSFORGE_URL` / `NEXT_PUBLIC_INSFORGE_ANON_KEY` in the browser and `INSFORGE_URL` / `INSFORGE_ANON_KEY` on the server, with public env fallbacks.
267
+ The helper uses explicit `baseUrl` / `anonKey` when provided. Otherwise it reads `NEXT_PUBLIC_INSFORGE_URL` / `NEXT_PUBLIC_INSFORGE_ANON_KEY`. Missing config throws a clear error.
268
268
 
269
269
  By default, the SSR helpers use:
270
270
 
package/dist/ssr.d.mts CHANGED
@@ -2,11 +2,6 @@ import { a as InsForgeConfig, I as InsForgeClient, l as AuthRefreshResponse, d a
2
2
  import '@insforge/shared-schemas';
3
3
  import '@supabase/postgrest-js';
4
4
 
5
- type SsrClientConfig = Omit<InsForgeConfig, 'baseUrl' | 'anonKey' | 'edgeFunctionToken' | 'isServerMode' | 'auth'> & {
6
- baseUrl?: string;
7
- anonKey?: string;
8
- };
9
-
10
5
  declare const DEFAULT_ACCESS_TOKEN_COOKIE = "insforge_access_token";
11
6
  declare const DEFAULT_REFRESH_TOKEN_COOKIE = "insforge_refresh_token";
12
7
  interface AuthCookieNames {
@@ -48,19 +43,19 @@ declare function setAuthCookies(target: Headers | CookieStore | undefined, token
48
43
  }, settings?: AuthCookieSettings): void;
49
44
  declare function clearAuthCookies(target: Headers | CookieStore | undefined, settings?: AuthCookieSettings): void;
50
45
 
51
- interface CreateBrowserClientOptions extends SsrClientConfig, AuthCookieSettings {
46
+ interface CreateBrowserClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
52
47
  refreshUrl?: string;
53
48
  refreshLeewaySeconds?: number;
54
49
  }
55
50
  declare function createBrowserClient(options?: CreateBrowserClientOptions): InsForgeClient;
56
51
 
57
- interface CreateServerClientOptions extends SsrClientConfig, AuthCookieSettings {
52
+ interface CreateServerClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
58
53
  cookies?: Pick<CookieStore, 'get'>;
59
54
  accessToken?: string;
60
55
  }
61
56
  declare function createServerClient(options?: CreateServerClientOptions): InsForgeClient;
62
57
 
63
- interface RefreshAuthOptions extends SsrClientConfig, AuthCookieSettings {
58
+ interface RefreshAuthOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
64
59
  request?: Request;
65
60
  cookies?: Pick<CookieStore, 'get'>;
66
61
  refreshToken?: string;
@@ -78,7 +73,7 @@ declare function createRefreshAuthRouter(options?: Omit<RefreshAuthOptions, 'req
78
73
  POST: RefreshAuthRouteHandler;
79
74
  };
80
75
 
81
- interface UpdateSessionOptions extends SsrClientConfig, AuthCookieSettings {
76
+ interface UpdateSessionOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
82
77
  requestCookies: CookieStore;
83
78
  responseCookies: CookieStore;
84
79
  refreshLeewaySeconds?: number;
package/dist/ssr.d.ts CHANGED
@@ -2,11 +2,6 @@ import { a as InsForgeConfig, I as InsForgeClient, l as AuthRefreshResponse, d a
2
2
  import '@insforge/shared-schemas';
3
3
  import '@supabase/postgrest-js';
4
4
 
5
- type SsrClientConfig = Omit<InsForgeConfig, 'baseUrl' | 'anonKey' | 'edgeFunctionToken' | 'isServerMode' | 'auth'> & {
6
- baseUrl?: string;
7
- anonKey?: string;
8
- };
9
-
10
5
  declare const DEFAULT_ACCESS_TOKEN_COOKIE = "insforge_access_token";
11
6
  declare const DEFAULT_REFRESH_TOKEN_COOKIE = "insforge_refresh_token";
12
7
  interface AuthCookieNames {
@@ -48,19 +43,19 @@ declare function setAuthCookies(target: Headers | CookieStore | undefined, token
48
43
  }, settings?: AuthCookieSettings): void;
49
44
  declare function clearAuthCookies(target: Headers | CookieStore | undefined, settings?: AuthCookieSettings): void;
50
45
 
51
- interface CreateBrowserClientOptions extends SsrClientConfig, AuthCookieSettings {
46
+ interface CreateBrowserClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
52
47
  refreshUrl?: string;
53
48
  refreshLeewaySeconds?: number;
54
49
  }
55
50
  declare function createBrowserClient(options?: CreateBrowserClientOptions): InsForgeClient;
56
51
 
57
- interface CreateServerClientOptions extends SsrClientConfig, AuthCookieSettings {
52
+ interface CreateServerClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
58
53
  cookies?: Pick<CookieStore, 'get'>;
59
54
  accessToken?: string;
60
55
  }
61
56
  declare function createServerClient(options?: CreateServerClientOptions): InsForgeClient;
62
57
 
63
- interface RefreshAuthOptions extends SsrClientConfig, AuthCookieSettings {
58
+ interface RefreshAuthOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
64
59
  request?: Request;
65
60
  cookies?: Pick<CookieStore, 'get'>;
66
61
  refreshToken?: string;
@@ -78,7 +73,7 @@ declare function createRefreshAuthRouter(options?: Omit<RefreshAuthOptions, 'req
78
73
  POST: RefreshAuthRouteHandler;
79
74
  };
80
75
 
81
- interface UpdateSessionOptions extends SsrClientConfig, AuthCookieSettings {
76
+ interface UpdateSessionOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
82
77
  requestCookies: CookieStore;
83
78
  responseCookies: CookieStore;
84
79
  refreshLeewaySeconds?: number;
package/dist/ssr.js CHANGED
@@ -2659,26 +2659,6 @@ function isJwtExpiredOrExpiring(token, leewaySeconds = 60) {
2659
2659
  return expires.getTime() <= Date.now() + leewaySeconds * 1e3;
2660
2660
  }
2661
2661
 
2662
- // src/ssr/config.ts
2663
- function env(name) {
2664
- if (typeof process === "undefined") return void 0;
2665
- return process.env[name];
2666
- }
2667
- function resolveBrowserConfig(config = {}) {
2668
- return {
2669
- ...config,
2670
- baseUrl: config.baseUrl ?? env("NEXT_PUBLIC_INSFORGE_URL"),
2671
- anonKey: config.anonKey ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
2672
- };
2673
- }
2674
- function resolveServerConfig(config = {}) {
2675
- return {
2676
- ...config,
2677
- baseUrl: config.baseUrl ?? env("INSFORGE_URL") ?? env("NEXT_PUBLIC_INSFORGE_URL"),
2678
- anonKey: config.anonKey ?? env("INSFORGE_ANON_KEY") ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
2679
- };
2680
- }
2681
-
2682
2662
  // src/ssr/browser-client.ts
2683
2663
  var import_shared_schemas2 = require("@insforge/shared-schemas");
2684
2664
 
@@ -2873,6 +2853,17 @@ function withAuthHeader(init, token) {
2873
2853
  };
2874
2854
  }
2875
2855
  function createBrowserClient(options = {}) {
2856
+ let { baseUrl, anonKey } = options;
2857
+ try {
2858
+ baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
2859
+ anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
2860
+ } catch {
2861
+ }
2862
+ if (!baseUrl || !anonKey) {
2863
+ throw new Error(
2864
+ "Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to createBrowserClient() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
2865
+ );
2866
+ }
2876
2867
  let accessToken = getBrowserCookie(
2877
2868
  getAccessTokenCookieName(options.names)
2878
2869
  );
@@ -2949,7 +2940,9 @@ function createBrowserClient(options = {}) {
2949
2940
  return fetchImpl(input, withAuthHeader(init, refreshed.accessToken));
2950
2941
  };
2951
2942
  client = new InsForgeClient({
2952
- ...resolveBrowserConfig(options),
2943
+ ...options,
2944
+ baseUrl,
2945
+ anonKey,
2953
2946
  fetch: ssrFetch
2954
2947
  });
2955
2948
  const setAccessToken = client.setAccessToken.bind(client);
@@ -2968,12 +2961,25 @@ function createBrowserClient(options = {}) {
2968
2961
 
2969
2962
  // src/ssr/server-client.ts
2970
2963
  function createServerClient(options = {}) {
2964
+ let { baseUrl, anonKey } = options;
2965
+ try {
2966
+ baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
2967
+ anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
2968
+ } catch {
2969
+ }
2970
+ if (!baseUrl || !anonKey) {
2971
+ throw new Error(
2972
+ "Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to createServerClient() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
2973
+ );
2974
+ }
2971
2975
  const accessToken = options.accessToken ?? getCookieValue(
2972
2976
  options.cookies,
2973
2977
  getAccessTokenCookieName(options.names)
2974
2978
  );
2975
2979
  return new InsForgeClient({
2976
- ...resolveServerConfig(options),
2980
+ ...options,
2981
+ baseUrl,
2982
+ anonKey,
2977
2983
  isServerMode: true,
2978
2984
  edgeFunctionToken: accessToken ?? void 0
2979
2985
  });
@@ -2990,12 +2996,25 @@ function jsonResponse(body, init = {}, headers = new Headers(init.headers)) {
2990
2996
  }
2991
2997
  function normalizeError(error) {
2992
2998
  if (error instanceof InsForgeError) return error;
2999
+ if (error && typeof error === "object") {
3000
+ const body = error;
3001
+ return new InsForgeError(
3002
+ typeof body.message === "string" ? body.message : "Failed to refresh auth session",
3003
+ typeof body.statusCode === "number" ? body.statusCode : 500,
3004
+ typeof body.error === "string" ? body.error : import_shared_schemas3.ERROR_CODES.UNKNOWN_ERROR
3005
+ );
3006
+ }
2993
3007
  return new InsForgeError(
2994
3008
  error instanceof Error ? error.message : "Failed to refresh auth session",
2995
3009
  500,
2996
3010
  import_shared_schemas3.ERROR_CODES.UNKNOWN_ERROR
2997
3011
  );
2998
3012
  }
3013
+ async function readJson(response) {
3014
+ const contentType = response.headers.get("content-type");
3015
+ if (!contentType?.includes("json")) return null;
3016
+ return response.json();
3017
+ }
2999
3018
  function readRefreshToken(options) {
3000
3019
  if (options.refreshToken) return options.refreshToken;
3001
3020
  const refreshCookieName = getRefreshTokenCookieName(options.names);
@@ -3032,11 +3051,53 @@ async function refreshAuth(options = {}) {
3032
3051
  error: error2
3033
3052
  };
3034
3053
  }
3035
- const client = new InsForgeClient({
3036
- ...resolveServerConfig(options),
3037
- isServerMode: true
3038
- });
3039
- const { data, error } = await client.auth.refreshSession({ refreshToken });
3054
+ let { baseUrl, anonKey } = options;
3055
+ try {
3056
+ baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
3057
+ anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
3058
+ } catch {
3059
+ }
3060
+ if (!baseUrl || !anonKey) {
3061
+ throw new Error(
3062
+ "Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to refreshAuth() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
3063
+ );
3064
+ }
3065
+ const fetchImpl = options.fetch ?? (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
3066
+ if (!fetchImpl) {
3067
+ throw new Error(
3068
+ "Fetch is not available. Please provide a fetch implementation."
3069
+ );
3070
+ }
3071
+ const requestHeaders = new Headers(options.headers);
3072
+ requestHeaders.set("Authorization", `Bearer ${anonKey}`);
3073
+ requestHeaders.set("Content-Type", "application/json");
3074
+ requestHeaders.set("Accept", "application/json");
3075
+ let data = null;
3076
+ let error = null;
3077
+ try {
3078
+ const response = await fetchImpl(
3079
+ new URL("/api/auth/refresh?client_type=mobile", baseUrl).toString(),
3080
+ {
3081
+ method: "POST",
3082
+ headers: requestHeaders,
3083
+ body: JSON.stringify({ refresh_token: refreshToken })
3084
+ }
3085
+ );
3086
+ const body = await readJson(response);
3087
+ if (!response.ok) {
3088
+ error = normalizeError(
3089
+ body ?? {
3090
+ message: "Failed to refresh auth session",
3091
+ statusCode: response.status,
3092
+ error: import_shared_schemas3.ERROR_CODES.UNKNOWN_ERROR
3093
+ }
3094
+ );
3095
+ } else {
3096
+ data = body;
3097
+ }
3098
+ } catch (caught) {
3099
+ error = normalizeError(caught);
3100
+ }
3040
3101
  if (error || !data?.accessToken) {
3041
3102
  clearAuthCookies(headers, options);
3042
3103
  const normalized = normalizeError(error);