@egdesk/next-api-plugin 1.3.0 → 1.3.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.
@@ -58,6 +58,45 @@ function generateHelpers(projectPath) {
58
58
 
59
59
  import { EGDESK_CONFIG } from './egdesk.config';
60
60
 
61
+ /**
62
+ * Parse EGDesk MCP \`/tools/call\` JSON so \`error\` is shown even when HTTP status is 500.
63
+ */
64
+ async function parseEgdeskMcpToolResponse(response: Response): Promise<any> {
65
+ const result = await response.json().catch(() => null);
66
+
67
+ if (result && typeof result === 'object' && result.success === false) {
68
+ const errMsg =
69
+ typeof result.error === 'string'
70
+ ? result.error
71
+ : result.error != null
72
+ ? String(result.error)
73
+ : 'Tool call failed';
74
+ throw new Error(errMsg);
75
+ }
76
+
77
+ if (!response.ok) {
78
+ let fromBody = '';
79
+ if (result && typeof result === 'object') {
80
+ if (typeof result.error === 'string') fromBody = result.error;
81
+ else if (typeof (result as { message?: string }).message === 'string') {
82
+ fromBody = (result as { message: string }).message;
83
+ }
84
+ }
85
+ throw new Error(fromBody || \`HTTP \${response.status}: \${response.statusText}\`);
86
+ }
87
+
88
+ if (!result || result.success !== true) {
89
+ throw new Error(
90
+ result && typeof result === 'object' && typeof result.error === 'string'
91
+ ? result.error
92
+ : 'Tool call failed'
93
+ );
94
+ }
95
+
96
+ const content = result.result?.content?.[0]?.text;
97
+ return content ? JSON.parse(content) : null;
98
+ }
99
+
61
100
  /**
62
101
  * Call EGDesk user-data MCP tool
63
102
  *
@@ -103,19 +142,7 @@ export async function callUserDataTool(
103
142
  });
104
143
  }
105
144
 
106
- if (!response.ok) {
107
- throw new Error(\`HTTP \${response.status}: \${response.statusText}\`);
108
- }
109
-
110
- const result = await response.json();
111
-
112
- if (!result.success) {
113
- throw new Error(result.error || 'Tool call failed');
114
- }
115
-
116
- // Parse MCP response format
117
- const content = result.result?.content?.[0]?.text;
118
- return content ? JSON.parse(content) : null;
145
+ return parseEgdeskMcpToolResponse(response);
119
146
  }
120
147
 
121
148
  /**
@@ -329,18 +356,7 @@ export async function callFinanceHubTool(
329
356
  });
330
357
  }
331
358
 
332
- if (!response.ok) {
333
- throw new Error(\`HTTP \${response.status}: \${response.statusText}\`);
334
- }
335
-
336
- const result = await response.json();
337
-
338
- if (!result.success) {
339
- throw new Error(result.error || 'Tool call failed');
340
- }
341
-
342
- const content = result.result?.content?.[0]?.text;
343
- return content ? JSON.parse(content) : null;
359
+ return parseEgdeskMcpToolResponse(response);
344
360
  }
345
361
 
346
362
  /**
@@ -485,18 +501,7 @@ export async function callBrowserRecordingTool(
485
501
  });
486
502
  }
487
503
 
488
- if (!response.ok) {
489
- throw new Error(\`HTTP \${response.status}: \${response.statusText}\`);
490
- }
491
-
492
- const result = await response.json();
493
-
494
- if (!result.success) {
495
- throw new Error(result.error || 'Tool call failed');
496
- }
497
-
498
- const content = result.result?.content?.[0]?.text;
499
- return content ? JSON.parse(content) : null;
504
+ return parseEgdeskMcpToolResponse(response);
500
505
  }
501
506
 
502
507
  /** List saved *.spec.js files in the EGDesk browser-recorder-tests output folder */
@@ -66,12 +66,20 @@ async function discoverTables(egdeskUrl = 'http://localhost:8080', apiKey) {
66
66
  arguments: {}
67
67
  })
68
68
  });
69
+ const listResult = await listResponse.json().catch(() => null);
70
+ if (listResult && typeof listResult === 'object' && listResult.success === false) {
71
+ throw new Error(listResult.error || 'Failed to list tables');
72
+ }
69
73
  if (!listResponse.ok) {
70
- throw new Error(`Failed to list tables: ${listResponse.statusText}`);
74
+ const fromBody = listResult && typeof listResult === 'object' && typeof listResult.error === 'string'
75
+ ? listResult.error
76
+ : '';
77
+ throw new Error(fromBody || `Failed to list tables: ${listResponse.status} ${listResponse.statusText}`);
71
78
  }
72
- const listResult = await listResponse.json();
73
- if (!listResult.success) {
74
- throw new Error(listResult.error || 'Failed to list tables');
79
+ if (!listResult || !listResult.success) {
80
+ throw new Error(listResult && typeof listResult === 'object' && listResult.error
81
+ ? String(listResult.error)
82
+ : 'Failed to list tables');
75
83
  }
76
84
  // Parse MCP response
77
85
  const content = listResult.result?.content?.[0]?.text;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egdesk/next-api-plugin",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Next.js plugin for EGDesk database proxy integration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,6 +25,45 @@ export function generateHelpers(projectPath: string): void {
25
25
 
26
26
  import { EGDESK_CONFIG } from './egdesk.config';
27
27
 
28
+ /**
29
+ * Parse EGDesk MCP \`/tools/call\` JSON so \`error\` is shown even when HTTP status is 500.
30
+ */
31
+ async function parseEgdeskMcpToolResponse(response: Response): Promise<any> {
32
+ const result = await response.json().catch(() => null);
33
+
34
+ if (result && typeof result === 'object' && result.success === false) {
35
+ const errMsg =
36
+ typeof result.error === 'string'
37
+ ? result.error
38
+ : result.error != null
39
+ ? String(result.error)
40
+ : 'Tool call failed';
41
+ throw new Error(errMsg);
42
+ }
43
+
44
+ if (!response.ok) {
45
+ let fromBody = '';
46
+ if (result && typeof result === 'object') {
47
+ if (typeof result.error === 'string') fromBody = result.error;
48
+ else if (typeof (result as { message?: string }).message === 'string') {
49
+ fromBody = (result as { message: string }).message;
50
+ }
51
+ }
52
+ throw new Error(fromBody || \`HTTP \${response.status}: \${response.statusText}\`);
53
+ }
54
+
55
+ if (!result || result.success !== true) {
56
+ throw new Error(
57
+ result && typeof result === 'object' && typeof result.error === 'string'
58
+ ? result.error
59
+ : 'Tool call failed'
60
+ );
61
+ }
62
+
63
+ const content = result.result?.content?.[0]?.text;
64
+ return content ? JSON.parse(content) : null;
65
+ }
66
+
28
67
  /**
29
68
  * Call EGDesk user-data MCP tool
30
69
  *
@@ -70,19 +109,7 @@ export async function callUserDataTool(
70
109
  });
71
110
  }
72
111
 
73
- if (!response.ok) {
74
- throw new Error(\`HTTP \${response.status}: \${response.statusText}\`);
75
- }
76
-
77
- const result = await response.json();
78
-
79
- if (!result.success) {
80
- throw new Error(result.error || 'Tool call failed');
81
- }
82
-
83
- // Parse MCP response format
84
- const content = result.result?.content?.[0]?.text;
85
- return content ? JSON.parse(content) : null;
112
+ return parseEgdeskMcpToolResponse(response);
86
113
  }
87
114
 
88
115
  /**
@@ -296,18 +323,7 @@ export async function callFinanceHubTool(
296
323
  });
297
324
  }
298
325
 
299
- if (!response.ok) {
300
- throw new Error(\`HTTP \${response.status}: \${response.statusText}\`);
301
- }
302
-
303
- const result = await response.json();
304
-
305
- if (!result.success) {
306
- throw new Error(result.error || 'Tool call failed');
307
- }
308
-
309
- const content = result.result?.content?.[0]?.text;
310
- return content ? JSON.parse(content) : null;
326
+ return parseEgdeskMcpToolResponse(response);
311
327
  }
312
328
 
313
329
  /**
@@ -452,18 +468,7 @@ export async function callBrowserRecordingTool(
452
468
  });
453
469
  }
454
470
 
455
- if (!response.ok) {
456
- throw new Error(\`HTTP \${response.status}: \${response.statusText}\`);
457
- }
458
-
459
- const result = await response.json();
460
-
461
- if (!result.success) {
462
- throw new Error(result.error || 'Tool call failed');
463
- }
464
-
465
- const content = result.result?.content?.[0]?.text;
466
- return content ? JSON.parse(content) : null;
471
+ return parseEgdeskMcpToolResponse(response);
467
472
  }
468
473
 
469
474
  /** List saved *.spec.js files in the EGDesk browser-recorder-tests output folder */
@@ -53,14 +53,26 @@ export async function discoverTables(
53
53
  })
54
54
  });
55
55
 
56
- if (!listResponse.ok) {
57
- throw new Error(`Failed to list tables: ${listResponse.statusText}`);
56
+ const listResult = await listResponse.json().catch(() => null);
57
+
58
+ if (listResult && typeof listResult === 'object' && listResult.success === false) {
59
+ throw new Error(listResult.error || 'Failed to list tables');
58
60
  }
59
61
 
60
- const listResult = await listResponse.json();
62
+ if (!listResponse.ok) {
63
+ const fromBody =
64
+ listResult && typeof listResult === 'object' && typeof listResult.error === 'string'
65
+ ? listResult.error
66
+ : '';
67
+ throw new Error(fromBody || `Failed to list tables: ${listResponse.status} ${listResponse.statusText}`);
68
+ }
61
69
 
62
- if (!listResult.success) {
63
- throw new Error(listResult.error || 'Failed to list tables');
70
+ if (!listResult || !listResult.success) {
71
+ throw new Error(
72
+ listResult && typeof listResult === 'object' && listResult.error
73
+ ? String(listResult.error)
74
+ : 'Failed to list tables'
75
+ );
64
76
  }
65
77
 
66
78
  // Parse MCP response