@egdesk/next-api-plugin 1.5.5 → 1.5.7
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/dist/generate-helpers.d.ts +5 -1
- package/dist/generate-helpers.js +547 -4
- package/dist/generate-middleware.js +33 -1
- package/dist/generate-proxy.js +68 -2
- package/dist/index.d.ts +8 -1
- package/dist/index.js +104 -7
- package/package.json +8 -1
- package/src/cli.ts +0 -63
- package/src/generate-api-wrapper.ts +0 -69
- package/src/generate-helpers.ts +0 -1829
- package/src/generate-middleware.ts +0 -206
- package/src/generate-proxy.ts +0 -308
- package/src/index.ts +0 -144
- package/src/setup-userdata.ts +0 -373
- package/tsconfig.json +0 -18
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate Next.js middleware for EGDesk database proxy
|
|
3
|
-
*
|
|
4
|
-
* Creates middleware.ts that intercepts __user_data_proxy requests
|
|
5
|
-
* and forwards them to the EGDesk MCP server.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import * as fs from 'fs';
|
|
9
|
-
import * as path from 'path';
|
|
10
|
-
import * as os from 'os';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Detect if project uses src/ directory structure
|
|
14
|
-
*/
|
|
15
|
-
function usesSrcDirectory(projectPath: string): boolean {
|
|
16
|
-
const srcPath = path.join(projectPath, 'src');
|
|
17
|
-
const srcAppPath = path.join(projectPath, 'src', 'app');
|
|
18
|
-
return fs.existsSync(srcPath) && fs.existsSync(srcAppPath);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Generate middleware.ts file in the correct location (root or src/)
|
|
23
|
-
*/
|
|
24
|
-
export function generateMiddleware(projectPath: string): void {
|
|
25
|
-
// Detect if project uses src/ directory
|
|
26
|
-
const hasSrc = usesSrcDirectory(projectPath);
|
|
27
|
-
const targetDir = hasSrc ? path.join(projectPath, 'src') : projectPath;
|
|
28
|
-
const middlewarePath = path.join(targetDir, 'middleware.ts');
|
|
29
|
-
|
|
30
|
-
console.log(`📁 Detected ${hasSrc ? 'src/' : 'root'} directory structure`);
|
|
31
|
-
console.log(`📝 Generating middleware at: ${middlewarePath}`);
|
|
32
|
-
|
|
33
|
-
// Check if middleware.ts already exists
|
|
34
|
-
if (fs.existsSync(middlewarePath)) {
|
|
35
|
-
console.log('⚠️ middleware.ts already exists - backing up to middleware.backup.ts');
|
|
36
|
-
const backupPath = path.join(targetDir, 'middleware.backup.ts');
|
|
37
|
-
fs.copyFileSync(middlewarePath, backupPath);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const middlewareContent = `import { NextResponse } from 'next/server';
|
|
41
|
-
import type { NextRequest } from 'next/server';
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* EGDesk Database Proxy Middleware
|
|
45
|
-
*
|
|
46
|
-
* Intercepts __user_data_proxy, __browser_recording_proxy, __internal_knowledge_proxy, and __company_research_proxy requests
|
|
47
|
-
* and forwards them to the EGDesk MCP HTTP server.
|
|
48
|
-
* This allows CORS-free database access in both local and tunneled environments.
|
|
49
|
-
*
|
|
50
|
-
* Generated by @egdesk/next-api-plugin
|
|
51
|
-
*/
|
|
52
|
-
export async function middleware(request: NextRequest) {
|
|
53
|
-
const { pathname } = request.nextUrl;
|
|
54
|
-
|
|
55
|
-
// Intercept __user_data_proxy requests
|
|
56
|
-
if (pathname.includes('__user_data_proxy')) {
|
|
57
|
-
try {
|
|
58
|
-
const body = await request.text();
|
|
59
|
-
|
|
60
|
-
// Read API key and URL from environment
|
|
61
|
-
const apiKey = process.env.NEXT_PUBLIC_EGDESK_API_KEY;
|
|
62
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
63
|
-
|
|
64
|
-
const headers: HeadersInit = {
|
|
65
|
-
'Content-Type': 'application/json',
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
if (apiKey) {
|
|
69
|
-
headers['X-Api-Key'] = apiKey;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Forward to EGDesk MCP server
|
|
73
|
-
const response = await fetch(\`\${apiUrl}/user-data/tools/call\`, {
|
|
74
|
-
method: 'POST',
|
|
75
|
-
headers,
|
|
76
|
-
body,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const result = await response.json();
|
|
80
|
-
|
|
81
|
-
return NextResponse.json(result, { status: response.status });
|
|
82
|
-
} catch (error: any) {
|
|
83
|
-
return NextResponse.json(
|
|
84
|
-
{ error: 'Proxy error', message: error.message },
|
|
85
|
-
{ status: 500 }
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (pathname.includes('__browser_recording_proxy')) {
|
|
91
|
-
try {
|
|
92
|
-
const body = await request.text();
|
|
93
|
-
|
|
94
|
-
const apiKey = process.env.NEXT_PUBLIC_EGDESK_API_KEY;
|
|
95
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
96
|
-
|
|
97
|
-
const headers: HeadersInit = {
|
|
98
|
-
'Content-Type': 'application/json',
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
if (apiKey) {
|
|
102
|
-
headers['X-Api-Key'] = apiKey;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const response = await fetch(\`\${apiUrl}/browser-recording/tools/call\`, {
|
|
106
|
-
method: 'POST',
|
|
107
|
-
headers,
|
|
108
|
-
body,
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const result = await response.json();
|
|
112
|
-
|
|
113
|
-
return NextResponse.json(result, { status: response.status });
|
|
114
|
-
} catch (error: any) {
|
|
115
|
-
return NextResponse.json(
|
|
116
|
-
{ error: 'Proxy error', message: error.message },
|
|
117
|
-
{ status: 500 }
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (pathname.includes('__internal_knowledge_proxy')) {
|
|
123
|
-
try {
|
|
124
|
-
const body = await request.text();
|
|
125
|
-
|
|
126
|
-
const apiKey = process.env.NEXT_PUBLIC_EGDESK_API_KEY;
|
|
127
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
128
|
-
|
|
129
|
-
const headers: HeadersInit = {
|
|
130
|
-
'Content-Type': 'application/json',
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
if (apiKey) {
|
|
134
|
-
headers['X-Api-Key'] = apiKey;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const response = await fetch(\`\${apiUrl}/internal-knowledge/tools/call\`, {
|
|
138
|
-
method: 'POST',
|
|
139
|
-
headers,
|
|
140
|
-
body,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const result = await response.json();
|
|
144
|
-
|
|
145
|
-
return NextResponse.json(result, { status: response.status });
|
|
146
|
-
} catch (error: any) {
|
|
147
|
-
return NextResponse.json(
|
|
148
|
-
{ error: 'Proxy error', message: error.message },
|
|
149
|
-
{ status: 500 }
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (pathname.includes('__company_research_proxy')) {
|
|
155
|
-
try {
|
|
156
|
-
const body = await request.text();
|
|
157
|
-
|
|
158
|
-
const apiKey = process.env.NEXT_PUBLIC_EGDESK_API_KEY;
|
|
159
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
160
|
-
|
|
161
|
-
const headers: HeadersInit = {
|
|
162
|
-
'Content-Type': 'application/json',
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
if (apiKey) {
|
|
166
|
-
headers['X-Api-Key'] = apiKey;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const response = await fetch(\`\${apiUrl}/company-research/tools/call\`, {
|
|
170
|
-
method: 'POST',
|
|
171
|
-
headers,
|
|
172
|
-
body,
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
const result = await response.json();
|
|
176
|
-
|
|
177
|
-
return NextResponse.json(result, { status: response.status });
|
|
178
|
-
} catch (error: any) {
|
|
179
|
-
return NextResponse.json(
|
|
180
|
-
{ error: 'Proxy error', message: error.message },
|
|
181
|
-
{ status: 500 }
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Continue to next middleware or route
|
|
187
|
-
return NextResponse.next();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export const config = {
|
|
191
|
-
matcher: [
|
|
192
|
-
/*
|
|
193
|
-
* Match all request paths except for the ones starting with:
|
|
194
|
-
* - api (API routes)
|
|
195
|
-
* - _next/static (static files)
|
|
196
|
-
* - _next/image (image optimization files)
|
|
197
|
-
* - favicon.ico (favicon file)
|
|
198
|
-
*/
|
|
199
|
-
'/((?!api|_next/static|_next/image|favicon.ico).*)',
|
|
200
|
-
],
|
|
201
|
-
};
|
|
202
|
-
`;
|
|
203
|
-
|
|
204
|
-
fs.writeFileSync(middlewarePath, middlewareContent.replace(/\r?\n/g, os.EOL), 'utf-8');
|
|
205
|
-
console.log(`✅ Generated ${middlewarePath}`);
|
|
206
|
-
}
|
package/src/generate-proxy.ts
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate Next.js 16+ proxy for EGDesk database proxy
|
|
3
|
-
*
|
|
4
|
-
* Creates proxy.ts that intercepts __user_data_proxy requests
|
|
5
|
-
* and forwards them to the EGDesk MCP server.
|
|
6
|
-
*
|
|
7
|
-
* This is the Next.js 16+ recommended approach (replaces middleware.ts)
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import * as fs from 'fs';
|
|
11
|
-
import * as path from 'path';
|
|
12
|
-
import * as os from 'os';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Detect if project uses src/ directory structure
|
|
16
|
-
*/
|
|
17
|
-
function usesSrcDirectory(projectPath: string): boolean {
|
|
18
|
-
const srcPath = path.join(projectPath, 'src');
|
|
19
|
-
const srcAppPath = path.join(projectPath, 'src', 'app');
|
|
20
|
-
return fs.existsSync(srcPath) && fs.existsSync(srcAppPath);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Generate proxy.ts file in the correct location (root or src/)
|
|
25
|
-
*/
|
|
26
|
-
export function generateProxy(projectPath: string): void {
|
|
27
|
-
// Detect if project uses src/ directory
|
|
28
|
-
const hasSrc = usesSrcDirectory(projectPath);
|
|
29
|
-
const targetDir = hasSrc ? path.join(projectPath, 'src') : projectPath;
|
|
30
|
-
const proxyPath = path.join(targetDir, 'proxy.ts');
|
|
31
|
-
|
|
32
|
-
console.log(`📁 Detected ${hasSrc ? 'src/' : 'root'} directory structure`);
|
|
33
|
-
console.log(`📝 Generating proxy at: ${proxyPath}`);
|
|
34
|
-
|
|
35
|
-
// Check if proxy.ts already exists
|
|
36
|
-
if (fs.existsSync(proxyPath)) {
|
|
37
|
-
console.log('⚠️ proxy.ts already exists - backing up to proxy.backup.ts');
|
|
38
|
-
const backupPath = path.join(targetDir, 'proxy.backup.ts');
|
|
39
|
-
fs.copyFileSync(proxyPath, backupPath);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const proxyContent = `import { NextResponse } from 'next/server';
|
|
43
|
-
import type { NextRequest } from 'next/server';
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* EGDesk Database Proxy (Next.js 16+)
|
|
47
|
-
*
|
|
48
|
-
* Intercepts __user_data_proxy, __browser_recording_proxy, __internal_knowledge_proxy, __company_research_proxy, __ai_center_proxy, __korean_law_proxy, __seo_proxy, __ssl_proxy, __local_agent_proxy, and __pageindex_proxy requests
|
|
49
|
-
* and forwards them to the EGDesk MCP HTTP server. This avoids CORS in local and tunneled environments.
|
|
50
|
-
*
|
|
51
|
-
* Generated by @egdesk/next-api-plugin
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
/** Build EGDesk routing headers from env vars (injected into every upstream call). */
|
|
55
|
-
function buildEgdeskHeaders(): Record<string, string> {
|
|
56
|
-
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
57
|
-
const apiKey = process.env.NEXT_PUBLIC_EGDESK_API_KEY;
|
|
58
|
-
const projectId = process.env.NEXT_PUBLIC_EGDESK_PROJECT_ID;
|
|
59
|
-
const egdeskEnv = process.env.NEXT_PUBLIC_EGDESK_ENV;
|
|
60
|
-
if (apiKey) headers['X-Api-Key'] = apiKey;
|
|
61
|
-
if (projectId) headers['X-EGDesk-Project-Id'] = projectId;
|
|
62
|
-
if (egdeskEnv) headers['X-EGDesk-Env'] = egdeskEnv;
|
|
63
|
-
return headers;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export async function proxy(request: NextRequest) {
|
|
67
|
-
const { pathname } = request.nextUrl;
|
|
68
|
-
|
|
69
|
-
// Intercept __user_data_proxy requests
|
|
70
|
-
if (pathname.includes('__user_data_proxy')) {
|
|
71
|
-
try {
|
|
72
|
-
const body = await request.text();
|
|
73
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
74
|
-
|
|
75
|
-
// Forward to EGDesk MCP server
|
|
76
|
-
const response = await fetch(\`\${apiUrl}/user-data/tools/call\`, {
|
|
77
|
-
method: 'POST',
|
|
78
|
-
headers: buildEgdeskHeaders(),
|
|
79
|
-
body,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const result = await response.json();
|
|
83
|
-
|
|
84
|
-
return NextResponse.json(result, { status: response.status });
|
|
85
|
-
} catch (error: any) {
|
|
86
|
-
return NextResponse.json(
|
|
87
|
-
{ error: 'Proxy error', message: error.message },
|
|
88
|
-
{ status: 500 }
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (pathname.includes('__browser_recording_proxy')) {
|
|
94
|
-
try {
|
|
95
|
-
const body = await request.text();
|
|
96
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
97
|
-
|
|
98
|
-
const response = await fetch(\`\${apiUrl}/browser-recording/tools/call\`, {
|
|
99
|
-
method: 'POST',
|
|
100
|
-
headers: buildEgdeskHeaders(),
|
|
101
|
-
body,
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const result = await response.json();
|
|
105
|
-
|
|
106
|
-
return NextResponse.json(result, { status: response.status });
|
|
107
|
-
} catch (error: any) {
|
|
108
|
-
return NextResponse.json(
|
|
109
|
-
{ error: 'Proxy error', message: error.message },
|
|
110
|
-
{ status: 500 }
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (pathname.includes('__internal_knowledge_proxy')) {
|
|
116
|
-
try {
|
|
117
|
-
const body = await request.text();
|
|
118
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
119
|
-
|
|
120
|
-
const response = await fetch(\`\${apiUrl}/internal-knowledge/tools/call\`, {
|
|
121
|
-
method: 'POST',
|
|
122
|
-
headers: buildEgdeskHeaders(),
|
|
123
|
-
body,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
const result = await response.json();
|
|
127
|
-
|
|
128
|
-
return NextResponse.json(result, { status: response.status });
|
|
129
|
-
} catch (error: any) {
|
|
130
|
-
return NextResponse.json(
|
|
131
|
-
{ error: 'Proxy error', message: error.message },
|
|
132
|
-
{ status: 500 }
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (pathname.includes('__ai_center_proxy')) {
|
|
138
|
-
try {
|
|
139
|
-
const body = await request.text();
|
|
140
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
141
|
-
|
|
142
|
-
const response = await fetch(\`\${apiUrl}/ai-center/tools/call\`, {
|
|
143
|
-
method: 'POST',
|
|
144
|
-
headers: buildEgdeskHeaders(),
|
|
145
|
-
body,
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const result = await response.json();
|
|
149
|
-
|
|
150
|
-
return NextResponse.json(result, { status: response.status });
|
|
151
|
-
} catch (error: any) {
|
|
152
|
-
return NextResponse.json(
|
|
153
|
-
{ error: 'Proxy error', message: error.message },
|
|
154
|
-
{ status: 500 }
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (pathname.includes('__korean_law_proxy')) {
|
|
160
|
-
try {
|
|
161
|
-
const body = await request.text();
|
|
162
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
163
|
-
|
|
164
|
-
const response = await fetch(\`\${apiUrl}/korean-law/tools/call\`, {
|
|
165
|
-
method: 'POST',
|
|
166
|
-
headers: buildEgdeskHeaders(),
|
|
167
|
-
body,
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
const result = await response.json();
|
|
171
|
-
|
|
172
|
-
return NextResponse.json(result, { status: response.status });
|
|
173
|
-
} catch (error: any) {
|
|
174
|
-
return NextResponse.json(
|
|
175
|
-
{ error: 'Proxy error', message: error.message },
|
|
176
|
-
{ status: 500 }
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (pathname.includes('__seo_proxy')) {
|
|
182
|
-
try {
|
|
183
|
-
const body = await request.text();
|
|
184
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
185
|
-
|
|
186
|
-
const response = await fetch(\`\${apiUrl}/seo/tools/call\`, {
|
|
187
|
-
method: 'POST',
|
|
188
|
-
headers: buildEgdeskHeaders(),
|
|
189
|
-
body,
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
const result = await response.json();
|
|
193
|
-
|
|
194
|
-
return NextResponse.json(result, { status: response.status });
|
|
195
|
-
} catch (error: any) {
|
|
196
|
-
return NextResponse.json(
|
|
197
|
-
{ error: 'Proxy error', message: error.message },
|
|
198
|
-
{ status: 500 }
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (pathname.includes('__ssl_proxy')) {
|
|
204
|
-
try {
|
|
205
|
-
const body = await request.text();
|
|
206
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
207
|
-
|
|
208
|
-
const response = await fetch(\`\${apiUrl}/ssl/tools/call\`, {
|
|
209
|
-
method: 'POST',
|
|
210
|
-
headers: buildEgdeskHeaders(),
|
|
211
|
-
body,
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
const result = await response.json();
|
|
215
|
-
|
|
216
|
-
return NextResponse.json(result, { status: response.status });
|
|
217
|
-
} catch (error: any) {
|
|
218
|
-
return NextResponse.json(
|
|
219
|
-
{ error: 'Proxy error', message: error.message },
|
|
220
|
-
{ status: 500 }
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (pathname.includes('__company_research_proxy')) {
|
|
226
|
-
try {
|
|
227
|
-
const body = await request.text();
|
|
228
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
229
|
-
|
|
230
|
-
const response = await fetch(\`\${apiUrl}/company-research/tools/call\`, {
|
|
231
|
-
method: 'POST',
|
|
232
|
-
headers: buildEgdeskHeaders(),
|
|
233
|
-
body,
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
const result = await response.json();
|
|
237
|
-
|
|
238
|
-
return NextResponse.json(result, { status: response.status });
|
|
239
|
-
} catch (error: any) {
|
|
240
|
-
return NextResponse.json(
|
|
241
|
-
{ error: 'Proxy error', message: error.message },
|
|
242
|
-
{ status: 500 }
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (pathname.includes('__local_agent_proxy')) {
|
|
248
|
-
try {
|
|
249
|
-
const body = await request.text();
|
|
250
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
251
|
-
|
|
252
|
-
const response = await fetch(\`\${apiUrl}/local-agent/tools/call\`, {
|
|
253
|
-
method: 'POST',
|
|
254
|
-
headers: buildEgdeskHeaders(),
|
|
255
|
-
body,
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
const result = await response.json();
|
|
259
|
-
|
|
260
|
-
return NextResponse.json(result, { status: response.status });
|
|
261
|
-
} catch (error: any) {
|
|
262
|
-
return NextResponse.json(
|
|
263
|
-
{ error: 'Proxy error', message: error.message },
|
|
264
|
-
{ status: 500 }
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
if (pathname.includes('__pageindex_proxy')) {
|
|
270
|
-
try {
|
|
271
|
-
const body = await request.text();
|
|
272
|
-
const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
|
|
273
|
-
|
|
274
|
-
const response = await fetch(\`\${apiUrl}/pageindex/tools/call\`, {
|
|
275
|
-
method: 'POST',
|
|
276
|
-
headers: buildEgdeskHeaders(),
|
|
277
|
-
body,
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
const result = await response.json();
|
|
281
|
-
|
|
282
|
-
return NextResponse.json(result, { status: response.status });
|
|
283
|
-
} catch (error: any) {
|
|
284
|
-
return NextResponse.json(
|
|
285
|
-
{ error: 'Proxy error', message: error.message },
|
|
286
|
-
{ status: 500 }
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Continue to next proxy or route
|
|
292
|
-
return NextResponse.next();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export const config = {
|
|
296
|
-
matcher: [
|
|
297
|
-
/*
|
|
298
|
-
* Match all paths except static assets
|
|
299
|
-
* The proxy function filters for __user_data_proxy / __browser_recording_proxy / __internal_knowledge_proxy / __company_research_proxy / __ai_center_proxy / __korean_law_proxy / __seo_proxy / __ssl_proxy / __local_agent_proxy / __pageindex_proxy
|
|
300
|
-
*/
|
|
301
|
-
'/:path*',
|
|
302
|
-
],
|
|
303
|
-
};
|
|
304
|
-
`;
|
|
305
|
-
|
|
306
|
-
fs.writeFileSync(proxyPath, proxyContent.replace(/\r?\n/g, os.EOL), 'utf-8');
|
|
307
|
-
console.log(`✅ Generated ${proxyPath}`);
|
|
308
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @egdesk/next-api-plugin
|
|
3
|
-
*
|
|
4
|
-
* Next.js plugin for EGDesk database proxy integration.
|
|
5
|
-
* Provides middleware-based database access for Next.js applications.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
discoverTables,
|
|
10
|
-
updateEnvFiles,
|
|
11
|
-
generateConfigFile,
|
|
12
|
-
type UserDataConfig,
|
|
13
|
-
type UserDataTable
|
|
14
|
-
} from './setup-userdata';
|
|
15
|
-
import { generateMiddleware } from './generate-middleware';
|
|
16
|
-
import { generateProxy } from './generate-proxy';
|
|
17
|
-
import { generateHelpers, ensureSchemaFile } from './generate-helpers';
|
|
18
|
-
import { generateApiWrapper } from './generate-api-wrapper';
|
|
19
|
-
|
|
20
|
-
export interface SetupOptions {
|
|
21
|
-
egdeskUrl?: string;
|
|
22
|
-
/** Production EGDesk server URL — written to .env.production.local. Falls back to egdeskUrl. */
|
|
23
|
-
prodEgdeskUrl?: string;
|
|
24
|
-
/**
|
|
25
|
-
* URL used only for table discovery (discoverTables). Defaults to egdeskUrl.
|
|
26
|
-
* Set this to the EGDesk internal server URL (e.g. http://localhost:8080) when egdeskUrl
|
|
27
|
-
* is a per-project proxy port that doesn't serve the EGDesk MCP HTTP API.
|
|
28
|
-
*/
|
|
29
|
-
discoveryUrl?: string;
|
|
30
|
-
/** Stable EGDesk project UUID — written as NEXT_PUBLIC_EGDESK_PROJECT_ID in env files */
|
|
31
|
-
egdeskProjectId?: string;
|
|
32
|
-
apiKey?: string;
|
|
33
|
-
useProxy?: boolean; // Use proxy.ts (Next.js 16+) instead of middleware.ts
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Main setup function for Next.js projects
|
|
38
|
-
*
|
|
39
|
-
* Discovers tables and generates all necessary files:
|
|
40
|
-
* - proxy.ts or middleware.ts (proxy interceptor)
|
|
41
|
-
* - egdesk.config.ts (table definitions)
|
|
42
|
-
* - egdesk-helpers.ts (helper functions)
|
|
43
|
-
* - .env.local (environment variables)
|
|
44
|
-
*/
|
|
45
|
-
export async function setupNextApiPlugin(
|
|
46
|
-
projectPath: string,
|
|
47
|
-
options: SetupOptions = {}
|
|
48
|
-
): Promise<void> {
|
|
49
|
-
const { egdeskUrl = 'http://localhost:8080', prodEgdeskUrl, discoveryUrl, egdeskProjectId, apiKey, useProxy = true } = options;
|
|
50
|
-
|
|
51
|
-
console.log('🔍 Discovering EGDesk user-data tables...');
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
const [devTables, prodTables] = await Promise.all([
|
|
55
|
-
discoverTables(discoveryUrl ?? egdeskUrl, apiKey, egdeskProjectId, 'development'),
|
|
56
|
-
discoverTables(discoveryUrl ?? egdeskUrl, apiKey, egdeskProjectId, 'production'),
|
|
57
|
-
]);
|
|
58
|
-
|
|
59
|
-
if (devTables.length === 0) {
|
|
60
|
-
console.warn('⚠️ No tables found. Import data in EGDesk first.');
|
|
61
|
-
console.log('');
|
|
62
|
-
console.log('Generating files anyway with empty table list...');
|
|
63
|
-
} else {
|
|
64
|
-
console.log(`✅ Found ${devTables.length} dev table(s), ${prodTables.length} prod table(s):`);
|
|
65
|
-
devTables.forEach((table, index) => {
|
|
66
|
-
console.log(` ${index + 1}. ${table.displayName} (${table.rowCount} rows)`);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const config: UserDataConfig = {
|
|
71
|
-
apiKey: apiKey || null,
|
|
72
|
-
baseUrl: egdeskUrl,
|
|
73
|
-
prodBaseUrl: prodEgdeskUrl,
|
|
74
|
-
egdeskProjectId,
|
|
75
|
-
tables: devTables,
|
|
76
|
-
generatedAt: new Date().toISOString()
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// Generate all configuration files
|
|
80
|
-
console.log('');
|
|
81
|
-
console.log('📝 Generating configuration files...');
|
|
82
|
-
|
|
83
|
-
// Use proxy.ts (Next.js 16+) or middleware.ts (legacy)
|
|
84
|
-
if (useProxy) {
|
|
85
|
-
console.log('🔄 Using proxy.ts (Next.js 16+ recommended)');
|
|
86
|
-
generateProxy(projectPath);
|
|
87
|
-
} else {
|
|
88
|
-
console.log('🔄 Using middleware.ts (legacy)');
|
|
89
|
-
generateMiddleware(projectPath);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
generateConfigFile(projectPath, config);
|
|
93
|
-
ensureSchemaFile(
|
|
94
|
-
projectPath,
|
|
95
|
-
devTables.map((table) => ({
|
|
96
|
-
tableName: table.tableName,
|
|
97
|
-
displayName: table.displayName,
|
|
98
|
-
columns: table.columns,
|
|
99
|
-
})),
|
|
100
|
-
);
|
|
101
|
-
generateHelpers(projectPath);
|
|
102
|
-
generateApiWrapper(projectPath);
|
|
103
|
-
updateEnvFiles(projectPath, config, prodTables);
|
|
104
|
-
|
|
105
|
-
const proxyFile = useProxy ? 'proxy.ts' : 'middleware.ts';
|
|
106
|
-
|
|
107
|
-
console.log('');
|
|
108
|
-
console.log('✅ Setup complete! Files generated:');
|
|
109
|
-
console.log(` - ${proxyFile} (database proxy)`);
|
|
110
|
-
console.log(' - egdesk.config.ts (type-safe config)');
|
|
111
|
-
console.log(' - egdesk.schema.ts (committed seed schema — add to git if newly created)');
|
|
112
|
-
console.log(' - egdesk-helpers.ts (helper functions)');
|
|
113
|
-
console.log(' - src/lib/api.ts (basePath-aware fetch wrapper)');
|
|
114
|
-
console.log(' - .env.development.local (dev environment variables)');
|
|
115
|
-
console.log(' - .env.production.local (prod environment variables)');
|
|
116
|
-
console.log('');
|
|
117
|
-
console.log('📝 Next steps:');
|
|
118
|
-
console.log(' 1. Add .env.*.local to your .gitignore');
|
|
119
|
-
console.log(' 2. Restart your Next.js dev server');
|
|
120
|
-
console.log(' 3. Import and use helpers in your components:');
|
|
121
|
-
console.log(' import { queryTable } from "./egdesk-helpers"');
|
|
122
|
-
console.log(' import { TABLES } from "./egdesk.config"');
|
|
123
|
-
console.log(' import { apiFetch } from "@/lib/api"');
|
|
124
|
-
console.log('');
|
|
125
|
-
console.log('Example usage:');
|
|
126
|
-
console.log(' // Database queries');
|
|
127
|
-
console.log(' const data = await queryTable(TABLES.table1.name, { limit: 10 });');
|
|
128
|
-
console.log(' ');
|
|
129
|
-
console.log(' // API routes (use apiFetch for basePath support)');
|
|
130
|
-
console.log(' const response = await apiFetch("/api/transactions");');
|
|
131
|
-
} catch (error) {
|
|
132
|
-
console.error('❌ Setup failed:', error);
|
|
133
|
-
throw error;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Export types and utilities
|
|
138
|
-
export type { UserDataTable, UserDataConfig } from './setup-userdata';
|
|
139
|
-
export { discoverTables } from './setup-userdata';
|
|
140
|
-
export { generateMiddleware } from './generate-middleware';
|
|
141
|
-
export { generateProxy } from './generate-proxy';
|
|
142
|
-
export { generateHelpers, generateSchemaFile, ensureSchemaFile, formatEgdeskSchemaFileContent } from './generate-helpers';
|
|
143
|
-
export type { SchemaTableDefinition, EnsureSchemaFileResult } from './generate-helpers';
|
|
144
|
-
export { generateApiWrapper } from './generate-api-wrapper';
|