@egdesk/next-api-plugin 1.5.116 → 1.5.123

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.
@@ -33,5 +33,5 @@ export interface GenerateHelpersOptions {
33
33
  apiKeyNames?: string[];
34
34
  }
35
35
  /** Bump when generated helper/middleware behavior changes; EGDesk regenerates stale projects. */
36
- export declare const EGDESK_HELPERS_GENERATION_VERSION = "3";
36
+ export declare const EGDESK_HELPERS_GENERATION_VERSION = "4";
37
37
  export declare function generateHelpers(projectPath: string, options?: GenerateHelpersOptions): void;
@@ -134,7 +134,7 @@ function ensureSchemaFile(projectPath, tables) {
134
134
  };
135
135
  }
136
136
  /** Bump when generated helper/middleware behavior changes; EGDesk regenerates stale projects. */
137
- exports.EGDESK_HELPERS_GENERATION_VERSION = '3';
137
+ exports.EGDESK_HELPERS_GENERATION_VERSION = '4';
138
138
  function generateHelpers(projectPath, options = {}) {
139
139
  const helperPath = path.join(projectPath, 'egdesk-helpers.ts');
140
140
  const helperContent = `/**
@@ -212,6 +212,8 @@ export function getNextAuthBasePath(): string {
212
212
  return root ? \`\${root}/api/auth\` : '/api/auth';
213
213
  }
214
214
 
215
+ const VISITOR_SESSION_KEY = 'egdesk_visitor_session';
216
+
215
217
  /**
216
218
  * Fetch wrapper that prepends EGDesk basePath to relative URLs.
217
219
  * Use for /api/* routes and /__*_proxy paths in client components.
@@ -220,7 +222,30 @@ export async function apiFetch(path: string, options?: RequestInit): Promise<Res
220
222
  const basePath = getEgdeskBasePath();
221
223
  const url =
222
224
  path.startsWith('/') && !path.startsWith('//') ? \`\${basePath}\${path}\` : path;
223
- return fetch(url, options);
225
+
226
+ const headers = new Headers(options?.headers);
227
+ if (typeof window !== 'undefined') {
228
+ const sessId = window.localStorage.getItem(VISITOR_SESSION_KEY);
229
+ if (
230
+ sessId &&
231
+ !headers.has('x-visitor-session-id') &&
232
+ !headers.has('x-egdesk-visitor-session')
233
+ ) {
234
+ headers.set('x-visitor-session-id', sessId);
235
+ }
236
+ if (sessId && !document.cookie.includes(\`\${VISITOR_SESSION_KEY}=\`)) {
237
+ const isSecure = window.location.protocol === 'https:';
238
+ document.cookie = \`\${VISITOR_SESSION_KEY}=\${encodeURIComponent(sessId)}; path=/; max-age=2592000; SameSite=Lax\${
239
+ isSecure ? '; Secure' : ''
240
+ }\`;
241
+ }
242
+ }
243
+
244
+ return fetch(url, {
245
+ ...options,
246
+ headers,
247
+ credentials: options?.credentials ?? 'same-origin',
248
+ });
224
249
  }
225
250
 
226
251
  /** Build EGDesk routing headers (project-id + env) for server-side direct calls. */
@@ -255,11 +280,73 @@ function resolveServerEgdeskApiUrl(): string {
255
280
  if (pub && String(pub).trim()) {
256
281
  return String(pub).replace(/\\/$/, '');
257
282
  }
283
+ const tunnelEnv = process.env?.NEXT_PUBLIC_EGDESK_TUNNEL_URL;
284
+ if (tunnelEnv && String(tunnelEnv).trim()) {
285
+ return String(tunnelEnv).replace(/\\/$/, '');
286
+ }
287
+ }
288
+ const tunnelConfig = (EGDESK_CONFIG as { tunnelUrl?: string }).tunnelUrl;
289
+ if (tunnelConfig && String(tunnelConfig).trim()) {
290
+ return String(tunnelConfig).replace(/\\/$/, '');
258
291
  }
259
292
  return String(EGDESK_CONFIG.apiUrl || '').replace(/\\/$/, '');
260
293
  }
261
294
 
262
- const VISITOR_SESSION_KEY = 'egdesk_visitor_session';
295
+ /** Server MCP root for SSE upstream (realtime hub, etc.). */
296
+ export function resolveServerEgdeskMcpApiUrlForSse(): string {
297
+ return resolveServerEgdeskApiUrl();
298
+ }
299
+
300
+ function trimMcpRoot(url: string): string {
301
+ return url.replace(/\\/$/, '');
302
+ }
303
+
304
+ function isLocalMcpUrl(url: string): boolean {
305
+ if (!url) return false;
306
+ try {
307
+ const host = new URL(url).hostname;
308
+ return host === 'localhost' || host === '127.0.0.1' || host === '[::1]';
309
+ } catch {
310
+ return url.includes('localhost') || url.includes('127.0.0.1');
311
+ }
312
+ }
313
+
314
+ function remoteMcpRootFromEnvAndConfig(): string {
315
+ const configured = trimMcpRoot(
316
+ (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_API_URL) || '',
317
+ );
318
+ const tunnelEnv = trimMcpRoot(
319
+ (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_TUNNEL_URL) || '',
320
+ );
321
+ const tunnelConfig = trimMcpRoot((EGDESK_CONFIG as { tunnelUrl?: string }).tunnelUrl || '');
322
+ if (tunnelEnv && !isLocalMcpUrl(tunnelEnv)) return tunnelEnv;
323
+ if (configured && !isLocalMcpUrl(configured)) return configured;
324
+ if (tunnelConfig && !isLocalMcpUrl(tunnelConfig)) return tunnelConfig;
325
+ return '';
326
+ }
327
+
328
+ /**
329
+ * Browser MCP root for \`/user-data/sse\` EventSource.
330
+ * Published sites must not use EGDESK_CONFIG.apiUrl when it points at localhost.
331
+ */
332
+ export function resolveBrowserEgdeskMcpApiUrl(): string {
333
+ if (typeof window === 'undefined') {
334
+ return resolveServerEgdeskMcpApiUrlForSse();
335
+ }
336
+ if (isLoopbackDevHost(window.location.hostname)) {
337
+ const fromEnv = trimMcpRoot(
338
+ (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_API_URL) || '',
339
+ );
340
+ if (fromEnv) return fromEnv;
341
+ return trimMcpRoot(EGDESK_CONFIG.apiUrl || 'http://localhost:8080');
342
+ }
343
+ const remote = remoteMcpRootFromEnvAndConfig();
344
+ if (remote) return remote;
345
+ console.error(
346
+ '[EGDesk] No remote MCP URL for user-data SSE on this host. Set NEXT_PUBLIC_EGDESK_API_URL or egdesk.config tunnelUrl.',
347
+ );
348
+ return '';
349
+ }
263
350
 
264
351
  export type WorkspaceVisitorCallOptions = {
265
352
  asVisitor?: boolean;
@@ -1633,12 +1720,18 @@ export function onUserDataChanged(
1633
1720
  return () => {};
1634
1721
  }
1635
1722
 
1636
- const apiUrl = EGDESK_CONFIG.apiUrl || '';
1723
+ const apiUrl = resolveBrowserEgdeskMcpApiUrl();
1637
1724
  const apiKey = EGDESK_CONFIG.apiKey || '';
1638
1725
  const projectId =
1639
1726
  (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_PROJECT_ID) || '';
1640
1727
  const egdeskEnv =
1641
1728
  (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_ENV) || '';
1729
+ if (!apiUrl) {
1730
+ console.warn(
1731
+ '[EGDesk] User-data SSE skipped: no MCP URL for this host (check NEXT_PUBLIC_EGDESK_API_URL or tunnelUrl).',
1732
+ );
1733
+ return () => {};
1734
+ }
1642
1735
  const sseParams = new URLSearchParams();
1643
1736
  if (apiKey) sseParams.set('key', apiKey);
1644
1737
  if (projectId) sseParams.set('egdeskId', projectId);
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * The minimum EGDesk app version this plugin version is compatible with.
9
9
  * The app checks this at setup time and warns on mismatch.
10
10
  */
11
- export declare const COMPATIBLE_APP_VERSION = "1.4.123";
11
+ export declare const COMPATIBLE_APP_VERSION = "1.4.134";
12
12
  export interface SetupOptions {
13
13
  egdeskUrl?: string;
14
14
  /** Production EGDesk server URL — written to .env.production.local. Falls back to egdeskUrl. */
package/dist/index.js CHANGED
@@ -46,7 +46,7 @@ exports.setupNextApiPlugin = setupNextApiPlugin;
46
46
  * The minimum EGDesk app version this plugin version is compatible with.
47
47
  * The app checks this at setup time and warns on mismatch.
48
48
  */
49
- exports.COMPATIBLE_APP_VERSION = '1.4.123';
49
+ exports.COMPATIBLE_APP_VERSION = '1.4.134';
50
50
  const fs = __importStar(require("fs"));
51
51
  const path = __importStar(require("path"));
52
52
  const setup_userdata_1 = require("./setup-userdata");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egdesk/next-api-plugin",
3
- "version": "1.5.116",
3
+ "version": "1.5.123",
4
4
  "description": "Next.js plugin for EGDesk database proxy integration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",