@egdesk/next-api-plugin 1.5.4 → 1.5.5
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-api-wrapper.d.ts +3 -3
- package/dist/generate-api-wrapper.js +8 -32
- package/dist/generate-helpers.d.ts +22 -0
- package/dist/generate-helpers.js +568 -79
- package/dist/generate-middleware.js +33 -1
- package/dist/generate-proxy.js +127 -55
- package/dist/index.d.ts +12 -1
- package/dist/index.js +25 -10
- package/dist/setup-userdata.d.ts +17 -1
- package/dist/setup-userdata.js +90 -4
- package/package.json +1 -1
- package/src/generate-api-wrapper.ts +8 -32
- package/src/generate-helpers.ts +590 -79
- package/src/generate-middleware.ts +33 -1
- package/src/generate-proxy.ts +127 -55
- package/src/index.ts +38 -12
- package/src/setup-userdata.ts +120 -6
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generate API wrapper for handling basePath in client-side fetch calls
|
|
3
3
|
*
|
|
4
|
-
* Creates src/lib/api.ts that
|
|
5
|
-
*
|
|
4
|
+
* Creates lib/api.ts (or src/lib/api.ts) that re-exports apiFetch from egdesk-helpers.ts
|
|
5
|
+
* so apps can use either import path.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import * as fs from 'fs';
|
|
@@ -36,15 +36,16 @@ function ensureLibDirectory(projectPath: string): string {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
* Generate
|
|
39
|
+
* Generate lib/api.ts file that re-exports basePath-aware fetch from egdesk-helpers
|
|
40
40
|
*/
|
|
41
41
|
export function generateApiWrapper(projectPath: string): void {
|
|
42
42
|
const libDir = ensureLibDirectory(projectPath);
|
|
43
43
|
const apiPath = path.join(libDir, 'api.ts');
|
|
44
|
+
const hasSrc = usesSrcDirectory(projectPath);
|
|
45
|
+
const helpersImport = hasSrc ? '../../egdesk-helpers' : '../egdesk-helpers';
|
|
44
46
|
|
|
45
47
|
console.log(`📝 Generating API wrapper at: ${apiPath}`);
|
|
46
48
|
|
|
47
|
-
// Check if api.ts already exists
|
|
48
49
|
if (fs.existsSync(apiPath)) {
|
|
49
50
|
console.log('⚠️ api.ts already exists - backing up to api.backup.ts');
|
|
50
51
|
const backupPath = path.join(libDir, 'api.backup.ts');
|
|
@@ -54,38 +55,13 @@ export function generateApiWrapper(projectPath: string): void {
|
|
|
54
55
|
const apiContent = `/**
|
|
55
56
|
* API Fetch Wrapper for EGDesk Tunneling
|
|
56
57
|
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* client-side fetch() calls - it's a known limitation.
|
|
58
|
+
* Re-exports apiFetch from egdesk-helpers.ts (canonical source).
|
|
59
|
+
* Use apiFetch for client-side /api/* calls — Next.js basePath does not apply to fetch().
|
|
60
60
|
*
|
|
61
61
|
* Generated by @egdesk/next-api-plugin
|
|
62
62
|
*/
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
const basePath = isDev ? '' : (process.env.NEXT_PUBLIC_EGDESK_BASE_PATH || '');
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Fetch wrapper that handles basePath for tunneled environments
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* // ❌ Wrong - will return 404 in tunneled environment
|
|
72
|
-
* fetch('/api/transactions')
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* // ✅ Correct - automatically prepends basePath
|
|
76
|
-
* apiFetch('/api/transactions')
|
|
77
|
-
*
|
|
78
|
-
* @param path - The API path (e.g., '/api/transactions')
|
|
79
|
-
* @param options - Standard fetch options
|
|
80
|
-
*/
|
|
81
|
-
export async function apiFetch(path: string, options?: RequestInit): Promise<Response> {
|
|
82
|
-
// Automatically prepend basePath to relative URLs
|
|
83
|
-
const url = path.startsWith('/') && !path.startsWith('//')
|
|
84
|
-
? \`\${basePath}\${path}\`
|
|
85
|
-
: path;
|
|
86
|
-
|
|
87
|
-
return fetch(url, options);
|
|
88
|
-
}
|
|
64
|
+
export { apiFetch, getEgdeskBasePath } from '${helpersImport}';
|
|
89
65
|
`;
|
|
90
66
|
|
|
91
67
|
fs.writeFileSync(apiPath, apiContent.replace(/\r?\n/g, os.EOL), 'utf-8');
|