@egdesk/next-api-plugin 1.5.59 → 1.5.61

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
@@ -126,10 +126,23 @@ import { callSheetsTool, getSheetRange } from './egdesk-helpers';
126
126
  // Owner EGDesk credentials (default)
127
127
  await getSheetRange(spreadsheetId, 'Sheet1!A1:B2');
128
128
 
129
- // Website visitor from startVisitorGoogleLogin()
129
+ // Website visitor from startVisitorGoogleLogin() (browser)
130
130
  await callSheetsTool('sheets_get_range', { spreadsheetId, range: 'Sheet1!A1:B2' }, { asVisitor: true });
131
+
132
+ // Server / terminal: session + the origin the visitor logged in from
133
+ await callSheetsTool(
134
+ 'sheets_get_range',
135
+ { spreadsheetId, range: 'Sheet1!A1:B2' },
136
+ {
137
+ asVisitor: true,
138
+ visitorSessionId,
139
+ visitorOrigin: 'http://localhost:4003',
140
+ },
141
+ );
131
142
  ```
132
143
 
144
+ On the server, `window` is missing so EGDesk cannot infer the site. Pass `visitorOrigin` or set `NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN` (falls back to `NEXT_PUBLIC_SITE_URL`). It must match the origin used at login. The API key authenticates this EGDesk instance; it does not replace origin.
145
+
133
146
  Watch / sync / `drive_auth_login` stay owner-only even with `asVisitor`.
134
147
 
135
148
  ### Custom Setup
@@ -216,6 +216,8 @@ const VISITOR_SESSION_KEY = 'egdesk_visitor_session';
216
216
  export type WorkspaceVisitorCallOptions = {
217
217
  asVisitor?: boolean;
218
218
  visitorSessionId?: string;
219
+ /** Origin the visitor logged in from. Required on the server (no window). */
220
+ visitorOrigin?: string;
219
221
  };
220
222
 
221
223
  function getBrowserVisitorSessionId(): string | null {
@@ -223,6 +225,30 @@ function getBrowserVisitorSessionId(): string | null {
223
225
  return window.localStorage.getItem(VISITOR_SESSION_KEY);
224
226
  }
225
227
 
228
+ function originFromValue(value?: string | null): string | null {
229
+ const raw = (value || '').trim();
230
+ if (!raw) return null;
231
+ try {
232
+ return new URL(raw).origin;
233
+ } catch {
234
+ try {
235
+ return new URL(\`https://\${raw}\`).origin;
236
+ } catch {
237
+ return null;
238
+ }
239
+ }
240
+ }
241
+
242
+ /** Browser origin, then visitorOrigin, then NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN / NEXT_PUBLIC_SITE_URL. */
243
+ function resolveVisitorSiteOrigin(options: WorkspaceVisitorCallOptions = {}): string | null {
244
+ if (typeof window !== 'undefined') return window.location.origin;
245
+ const fromEnv =
246
+ (typeof process !== 'undefined' &&
247
+ (process.env?.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN || process.env?.NEXT_PUBLIC_SITE_URL)) ||
248
+ '';
249
+ return originFromValue(options.visitorOrigin) || originFromValue(fromEnv);
250
+ }
251
+
226
252
  function buildWorkspaceVisitorHeaders(options: WorkspaceVisitorCallOptions = {}): Record<string, string> {
227
253
  const headers: Record<string, string> = {};
228
254
  if (!options.asVisitor) return headers;
@@ -234,8 +260,14 @@ function buildWorkspaceVisitorHeaders(options: WorkspaceVisitorCallOptions = {})
234
260
  }
235
261
  headers['Authorization'] = \`Bearer \${sessionId}\`;
236
262
  headers['X-EGDesk-As-Visitor'] = 'true';
237
- if (typeof window !== 'undefined') {
238
- headers['X-Visitor-Origin'] = window.location.origin;
263
+ const origin = resolveVisitorSiteOrigin(options);
264
+ if (origin) {
265
+ headers['Origin'] = origin;
266
+ headers['X-Visitor-Origin'] = origin;
267
+ } else if (typeof window === 'undefined') {
268
+ throw new Error(
269
+ 'asVisitor is set but no site origin is available. Pass visitorOrigin matching startVisitorGoogleLogin, or set NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN.',
270
+ );
239
271
  }
240
272
  return headers;
241
273
  }
@@ -2024,7 +2056,7 @@ export async function listBrowserRecordingSessions() {
2024
2056
  return callBrowserRecordingTool('browser_recording_list_sessions', {});
2025
2057
  }
2026
2058
 
2027
- /** Close Chrome. Does not save a recording or page HTML — only when fully done. */
2059
+ // Close Chrome. Does not save a recording or page HTML — only when fully done.
2028
2060
  export async function closeBrowserRecordingSession(sessionId: string) {
2029
2061
  return callBrowserRecordingTool('browser_recording_close_session', { sessionId });
2030
2062
  }
@@ -3240,6 +3272,7 @@ export async function deletePageIndexDocument(docId: string) {
3240
3272
  * Owner MCP auth on EGDesk: GOOGLE_SERVICE_ACCOUNT_JSON or Google Workspace sign-in
3241
3273
  * via startDriveAuthLogin() — configures THIS EGDesk instance.
3242
3274
  * Pass { asVisitor: true } to act as the website visitor from startVisitorGoogleLogin().
3275
+ * On the server also pass visitorSessionId and visitorOrigin (or NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN).
3243
3276
  * Watch / sync / drive_auth_login stay owner-only even with asVisitor.
3244
3277
  *
3245
3278
  * - Server: \`POST {apiUrl}/drive/tools/call\`
@@ -3452,6 +3485,7 @@ export async function syncDrive(options: {
3452
3485
  *
3453
3486
  * Auth: personal OAuth, service account, or domain-wide delegation (call getSheetsAuthStatus() first).
3454
3487
  * Pass { asVisitor: true } to act as the website visitor from startVisitorGoogleLogin().
3488
+ * On the server also pass visitorSessionId and visitorOrigin (or NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN).
3455
3489
  * - Server: \`POST {apiUrl}/sheets/tools/call\`
3456
3490
  * - Client: \`POST /__sheets_proxy\`
3457
3491
  */
@@ -5071,6 +5105,7 @@ export async function listYouTubeHistory(options: {
5071
5105
  return callYouTubeTool('youtube_list_history', options);
5072
5106
  }
5073
5107
  `;
5074
- fs.writeFileSync(helperPath, helperContent.replace(/\r?\n/g, os.EOL), 'utf-8');
5108
+ const sanitized = helperContent.replace(/^(\/\*\*[^\n]*\*\/)\}$/gm, '$1');
5109
+ fs.writeFileSync(helperPath, sanitized.replace(/\r?\n/g, os.EOL), 'utf-8');
5075
5110
  console.log(`✅ Generated ${helperPath}`);
5076
5111
  }
@@ -105,6 +105,8 @@ function workspaceUpstreamHeaders(request: NextRequest): Record<string, string>
105
105
  const origin =
106
106
  request.headers.get('x-visitor-origin') ||
107
107
  request.headers.get('origin') ||
108
+ process.env.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN ||
109
+ process.env.NEXT_PUBLIC_SITE_URL ||
108
110
  request.nextUrl.origin;
109
111
  headers['Origin'] = origin;
110
112
  headers['X-Visitor-Origin'] = origin;
@@ -113,6 +113,8 @@ function workspaceForwardHeaders(request: NextRequest): Record<string, string> {
113
113
  const origin =
114
114
  request.headers.get('x-visitor-origin') ||
115
115
  request.headers.get('origin') ||
116
+ process.env.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN ||
117
+ process.env.NEXT_PUBLIC_SITE_URL ||
116
118
  request.nextUrl.origin;
117
119
  headers['Origin'] = origin;
118
120
  headers['X-Visitor-Origin'] = origin;
@@ -224,9 +224,33 @@ async function parseEgdeskJson(response: Response): Promise<any> {
224
224
  return result;
225
225
  }
226
226
 
227
+ function originFromValue(value?: string | null): string | null {
228
+ const raw = (value || '').trim();
229
+ if (!raw) return null;
230
+ try {
231
+ return new URL(raw).origin;
232
+ } catch {
233
+ try {
234
+ return new URL(\`https://\${raw}\`).origin;
235
+ } catch {
236
+ return null;
237
+ }
238
+ }
239
+ }
240
+
241
+ function resolveVisitorSiteOrigin(): string | null {
242
+ if (typeof window !== 'undefined') return window.location.origin;
243
+ const fromEnv =
244
+ (typeof process !== 'undefined' &&
245
+ (process.env?.NEXT_PUBLIC_EGDESK_VISITOR_ORIGIN || process.env?.NEXT_PUBLIC_SITE_URL)) ||
246
+ '';
247
+ return originFromValue(fromEnv);
248
+ }
249
+
227
250
  function visitorOriginHeaders(): Record<string, string> {
228
- if (typeof window === 'undefined') return {};
229
- return { 'X-Visitor-Origin': window.location.origin };
251
+ const origin = resolveVisitorSiteOrigin();
252
+ if (!origin) return {};
253
+ return { Origin: origin, 'X-Visitor-Origin': origin };
230
254
  }
231
255
 
232
256
  /** Tunnel production apps live under /t/{id}/p/{project}. Bare /auth/callback 404s. */
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.67";
11
+ export declare const COMPATIBLE_APP_VERSION = "1.4.69";
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.67';
49
+ exports.COMPATIBLE_APP_VERSION = '1.4.69';
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.59",
3
+ "version": "1.5.61",
4
4
  "description": "Next.js plugin for EGDesk database proxy integration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",