@insforge/sdk 1.3.0-ssr.1 → 1.3.0-ssr.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/README.md CHANGED
@@ -37,9 +37,25 @@ import { createClient } from "@insforge/sdk";
37
37
 
38
38
  const insforge = createClient({
39
39
  baseUrl: "http://localhost:7130", // Your InsForge backend URL
40
+ anonKey: "your-anon-key", // Optional public anon key
40
41
  });
41
42
  ```
42
43
 
44
+ ### Server Admin Client
45
+
46
+ Use `createAdminClient()` only in trusted server code that needs project-admin privileges:
47
+
48
+ ```typescript
49
+ import { createAdminClient } from "@insforge/sdk";
50
+
51
+ const admin = createAdminClient({
52
+ baseUrl: "http://localhost:7130",
53
+ apiKey: process.env.INSFORGE_API_KEY!,
54
+ });
55
+ ```
56
+
57
+ `apiKey` belongs in `createAdminClient()`. Public and user-scoped clients use `anonKey`.
58
+
43
59
  ### Authentication
44
60
 
45
61
  ```javascript
@@ -261,6 +277,8 @@ const insforge = createClient({
261
277
  });
262
278
  ```
263
279
 
280
+ For project-admin keys, use `createAdminClient({ apiKey })` in server-only code.
281
+
264
282
  ### SSR / Next.js
265
283
 
266
284
  Use `@insforge/sdk/ssr` for apps that need the same auth session in Server Components, Client Components, Storage, and Realtime.
@@ -295,7 +313,19 @@ import { createRefreshAuthRouter } from "@insforge/sdk/ssr";
295
313
  export const { POST } = createRefreshAuthRouter();
296
314
  ```
297
315
 
298
- For server-owned refresh cookies, run sign-in in a Route Handler or Server Action and use `setAuthCookies()` from `@insforge/sdk/ssr` with the auth response.
316
+ For server-owned refresh cookies, run sign-in in a Route Handler or Server Action and use `setAuthCookies()` from `@insforge/sdk/ssr` with the framework cookie writer. In Next.js Route Handlers, pass `response.cookies`:
317
+
318
+ ```typescript
319
+ import { NextResponse } from "next/server";
320
+ import { setAuthCookies } from "@insforge/sdk/ssr";
321
+
322
+ const response = NextResponse.json({ user: data.user });
323
+ setAuthCookies(response.cookies, {
324
+ accessToken: data.accessToken,
325
+ refreshToken: data.refreshToken,
326
+ });
327
+ return response;
328
+ ```
299
329
 
300
330
  If your refresh route needs custom side effects:
301
331
 
@@ -78,6 +78,12 @@ interface InsForgeConfig {
78
78
  */
79
79
  retryDelay?: number;
80
80
  }
81
+ type InsForgeAdminConfig = Omit<InsForgeConfig, 'anonKey' | 'edgeFunctionToken' | 'isServerMode'> & {
82
+ /**
83
+ * Project admin API key. Keep this server-side only.
84
+ */
85
+ apiKey: string;
86
+ };
81
87
  interface AuthSession {
82
88
  user: UserSchema;
83
89
  accessToken: string;
@@ -1118,4 +1124,4 @@ declare class InsForgeClient {
1118
1124
  setAccessToken(token: string | null): void;
1119
1125
  }
1120
1126
 
1121
- export { type AuthSession 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, TokenManager as T, type InsForgeConfig as a, type ApiError as b, type InsForgeErrorCode as c, InsForgeError as d, Auth as e, StorageBucket as f, type StorageResponse as g, AI as h, type FunctionInvokeOptions as i, type PaymentsResponse as j, type EventCallback as k, type AuthRefreshResponse as l };
1127
+ export { type AuthSession 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, TokenManager as T, type InsForgeConfig as a, type InsForgeAdminConfig as b, type ApiError as c, type InsForgeErrorCode as d, InsForgeError as e, Auth as f, StorageBucket as g, type StorageResponse as h, AI as i, type FunctionInvokeOptions as j, type PaymentsResponse as k, type EventCallback as l, type AuthRefreshResponse as m };
@@ -78,6 +78,12 @@ interface InsForgeConfig {
78
78
  */
79
79
  retryDelay?: number;
80
80
  }
81
+ type InsForgeAdminConfig = Omit<InsForgeConfig, 'anonKey' | 'edgeFunctionToken' | 'isServerMode'> & {
82
+ /**
83
+ * Project admin API key. Keep this server-side only.
84
+ */
85
+ apiKey: string;
86
+ };
81
87
  interface AuthSession {
82
88
  user: UserSchema;
83
89
  accessToken: string;
@@ -1118,4 +1124,4 @@ declare class InsForgeClient {
1118
1124
  setAccessToken(token: string | null): void;
1119
1125
  }
1120
1126
 
1121
- export { type AuthSession 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, TokenManager as T, type InsForgeConfig as a, type ApiError as b, type InsForgeErrorCode as c, InsForgeError as d, Auth as e, StorageBucket as f, type StorageResponse as g, AI as h, type FunctionInvokeOptions as i, type PaymentsResponse as j, type EventCallback as k, type AuthRefreshResponse as l };
1127
+ export { type AuthSession 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, TokenManager as T, type InsForgeConfig as a, type InsForgeAdminConfig as b, type ApiError as c, type InsForgeErrorCode as d, InsForgeError as e, Auth as f, StorageBucket as g, type StorageResponse as h, AI as i, type FunctionInvokeOptions as j, type PaymentsResponse as k, type EventCallback as l, type AuthRefreshResponse as m };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { I as InsForgeClient, a as InsForgeConfig } from './client-B8ykVESe.mjs';
2
- export { h as AI, b as ApiError, e as Auth, A as AuthSession, C as ConnectionState, D as Database, E as Emails, k as EventCallback, i as FunctionInvokeOptions, F as Functions, H as HttpClient, d as InsForgeError, c as InsForgeErrorCode, L as Logger, P as Payments, j as PaymentsResponse, R as Realtime, S as Storage, f as StorageBucket, g as StorageResponse, T as TokenManager } from './client-B8ykVESe.mjs';
1
+ import { I as InsForgeClient, a as InsForgeConfig, b as InsForgeAdminConfig } from './client-CQfw9UsO.mjs';
2
+ export { i as AI, c as ApiError, f as Auth, A as AuthSession, C as ConnectionState, D as Database, E as Emails, l as EventCallback, j as FunctionInvokeOptions, F as Functions, H as HttpClient, e as InsForgeError, d as InsForgeErrorCode, L as Logger, P as Payments, k as PaymentsResponse, R as Realtime, S as Storage, g as StorageBucket, h as StorageResponse, T as TokenManager } from './client-CQfw9UsO.mjs';
3
3
  export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
4
4
  import '@supabase/postgrest-js';
5
5
 
@@ -9,6 +9,7 @@ import '@supabase/postgrest-js';
9
9
  * @packageDocumentation
10
10
  */
11
11
 
12
- declare function createClient(config: InsForgeConfig): InsForgeClient;
12
+ declare function createClient(config?: InsForgeConfig): InsForgeClient;
13
+ declare function createAdminClient(config: InsForgeAdminConfig): InsForgeClient;
13
14
 
14
- export { InsForgeConfig as ClientOptions, InsForgeClient, InsForgeConfig, createClient, InsForgeClient as default };
15
+ export { InsForgeConfig as ClientOptions, InsForgeAdminConfig, InsForgeClient, InsForgeConfig, createAdminClient, createClient, InsForgeClient as default };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { I as InsForgeClient, a as InsForgeConfig } from './client-B8ykVESe.js';
2
- export { h as AI, b as ApiError, e as Auth, A as AuthSession, C as ConnectionState, D as Database, E as Emails, k as EventCallback, i as FunctionInvokeOptions, F as Functions, H as HttpClient, d as InsForgeError, c as InsForgeErrorCode, L as Logger, P as Payments, j as PaymentsResponse, R as Realtime, S as Storage, f as StorageBucket, g as StorageResponse, T as TokenManager } from './client-B8ykVESe.js';
1
+ import { I as InsForgeClient, a as InsForgeConfig, b as InsForgeAdminConfig } from './client-CQfw9UsO.js';
2
+ export { i as AI, c as ApiError, f as Auth, A as AuthSession, C as ConnectionState, D as Database, E as Emails, l as EventCallback, j as FunctionInvokeOptions, F as Functions, H as HttpClient, e as InsForgeError, d as InsForgeErrorCode, L as Logger, P as Payments, k as PaymentsResponse, R as Realtime, S as Storage, g as StorageBucket, h as StorageResponse, T as TokenManager } from './client-CQfw9UsO.js';
3
3
  export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
4
4
  import '@supabase/postgrest-js';
5
5
 
@@ -9,6 +9,7 @@ import '@supabase/postgrest-js';
9
9
  * @packageDocumentation
10
10
  */
11
11
 
12
- declare function createClient(config: InsForgeConfig): InsForgeClient;
12
+ declare function createClient(config?: InsForgeConfig): InsForgeClient;
13
+ declare function createAdminClient(config: InsForgeAdminConfig): InsForgeClient;
13
14
 
14
- export { InsForgeConfig as ClientOptions, InsForgeClient, InsForgeConfig, createClient, InsForgeClient as default };
15
+ export { InsForgeConfig as ClientOptions, InsForgeAdminConfig, InsForgeClient, InsForgeConfig, createAdminClient, createClient, InsForgeClient as default };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -34,6 +44,7 @@ __export(index_exports, {
34
44
  Storage: () => Storage,
35
45
  StorageBucket: () => StorageBucket,
36
46
  TokenManager: () => TokenManager,
47
+ createAdminClient: () => createAdminClient,
37
48
  createClient: () => createClient,
38
49
  default: () => index_default
39
50
  });
@@ -2218,7 +2229,6 @@ var Functions = class _Functions {
2218
2229
  };
2219
2230
 
2220
2231
  // src/modules/realtime.ts
2221
- var import_socket = require("socket.io-client");
2222
2232
  var CONNECT_TIMEOUT = 1e4;
2223
2233
  var Realtime = class {
2224
2234
  constructor(baseUrl, tokenManager, anonKey) {
@@ -2253,61 +2263,69 @@ var Realtime = class {
2253
2263
  if (this.connectPromise) {
2254
2264
  return this.connectPromise;
2255
2265
  }
2256
- this.connectPromise = new Promise((resolve, reject) => {
2257
- const token = this.tokenManager.getAccessToken() ?? this.anonKey;
2258
- this.socket = (0, import_socket.io)(this.baseUrl, {
2259
- transports: ["websocket"],
2260
- auth: token ? { token } : void 0
2261
- });
2262
- let initialConnection = true;
2263
- let timeoutId = null;
2264
- const cleanup = () => {
2265
- if (timeoutId) {
2266
- clearTimeout(timeoutId);
2267
- timeoutId = null;
2268
- }
2269
- };
2270
- timeoutId = setTimeout(() => {
2271
- if (initialConnection) {
2272
- initialConnection = false;
2273
- this.connectPromise = null;
2274
- this.socket?.disconnect();
2275
- this.socket = null;
2276
- reject(new Error(`Connection timeout after ${CONNECT_TIMEOUT}ms`));
2277
- }
2278
- }, CONNECT_TIMEOUT);
2279
- this.socket.on("connect", () => {
2280
- cleanup();
2281
- for (const channel of this.subscribedChannels) {
2282
- this.socket.emit("realtime:subscribe", { channel });
2283
- }
2284
- this.notifyListeners("connect");
2285
- if (initialConnection) {
2286
- initialConnection = false;
2287
- this.connectPromise = null;
2288
- resolve();
2289
- }
2290
- });
2291
- this.socket.on("connect_error", (error) => {
2292
- cleanup();
2293
- this.notifyListeners("connect_error", error);
2294
- if (initialConnection) {
2295
- initialConnection = false;
2296
- this.connectPromise = null;
2297
- reject(error);
2298
- }
2299
- });
2300
- this.socket.on("disconnect", (reason) => {
2301
- this.notifyListeners("disconnect", reason);
2302
- });
2303
- this.socket.on("realtime:error", (error) => {
2304
- this.notifyListeners("error", error);
2305
- });
2306
- this.socket.onAny((event, message) => {
2307
- if (event === "realtime:error") return;
2308
- this.notifyListeners(event, message);
2309
- });
2310
- });
2266
+ this.connectPromise = (async () => {
2267
+ try {
2268
+ const { io } = await import("socket.io-client");
2269
+ await new Promise((resolve, reject) => {
2270
+ const token = this.tokenManager.getAccessToken() ?? this.anonKey;
2271
+ this.socket = io(this.baseUrl, {
2272
+ transports: ["websocket"],
2273
+ auth: token ? { token } : void 0
2274
+ });
2275
+ let initialConnection = true;
2276
+ let timeoutId = null;
2277
+ const cleanup = () => {
2278
+ if (timeoutId) {
2279
+ clearTimeout(timeoutId);
2280
+ timeoutId = null;
2281
+ }
2282
+ };
2283
+ timeoutId = setTimeout(() => {
2284
+ if (initialConnection) {
2285
+ initialConnection = false;
2286
+ this.connectPromise = null;
2287
+ this.socket?.disconnect();
2288
+ this.socket = null;
2289
+ reject(new Error(`Connection timeout after ${CONNECT_TIMEOUT}ms`));
2290
+ }
2291
+ }, CONNECT_TIMEOUT);
2292
+ this.socket.on("connect", () => {
2293
+ cleanup();
2294
+ for (const channel of this.subscribedChannels) {
2295
+ this.socket.emit("realtime:subscribe", { channel });
2296
+ }
2297
+ this.notifyListeners("connect");
2298
+ if (initialConnection) {
2299
+ initialConnection = false;
2300
+ this.connectPromise = null;
2301
+ resolve();
2302
+ }
2303
+ });
2304
+ this.socket.on("connect_error", (error) => {
2305
+ cleanup();
2306
+ this.notifyListeners("connect_error", error);
2307
+ if (initialConnection) {
2308
+ initialConnection = false;
2309
+ this.connectPromise = null;
2310
+ reject(error);
2311
+ }
2312
+ });
2313
+ this.socket.on("disconnect", (reason) => {
2314
+ this.notifyListeners("disconnect", reason);
2315
+ });
2316
+ this.socket.on("realtime:error", (error) => {
2317
+ this.notifyListeners("error", error);
2318
+ });
2319
+ this.socket.onAny((event, message) => {
2320
+ if (event === "realtime:error") return;
2321
+ this.notifyListeners(event, message);
2322
+ });
2323
+ });
2324
+ } catch (error) {
2325
+ this.connectPromise = null;
2326
+ throw error;
2327
+ }
2328
+ })();
2311
2329
  return this.connectPromise;
2312
2330
  }
2313
2331
  /**
@@ -2631,9 +2649,20 @@ var InsForgeClient = class {
2631
2649
  };
2632
2650
 
2633
2651
  // src/index.ts
2634
- function createClient(config) {
2652
+ function createClient(config = {}) {
2635
2653
  return new InsForgeClient(config);
2636
2654
  }
2655
+ function createAdminClient(config) {
2656
+ if (!config?.apiKey) {
2657
+ throw new Error("Missing apiKey. Pass apiKey to createAdminClient().");
2658
+ }
2659
+ const { apiKey, ...clientConfig } = config;
2660
+ return new InsForgeClient({
2661
+ ...clientConfig,
2662
+ edgeFunctionToken: apiKey,
2663
+ isServerMode: true
2664
+ });
2665
+ }
2637
2666
  var index_default = InsForgeClient;
2638
2667
  // Annotate the CommonJS export names for ESM import in node:
2639
2668
  0 && (module.exports = {
@@ -2651,6 +2680,7 @@ var index_default = InsForgeClient;
2651
2680
  Storage,
2652
2681
  StorageBucket,
2653
2682
  TokenManager,
2683
+ createAdminClient,
2654
2684
  createClient
2655
2685
  });
2656
2686
  //# sourceMappingURL=index.js.map