@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.
@@ -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 automatically prepends NEXT_PUBLIC_EGDESK_BASE_PATH
5
- * to relative URLs, solving the Next.js basePath limitation with client-side fetch.
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 src/lib/api.ts file for basePath-aware fetch wrapper
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
- * This wrapper automatically prepends NEXT_PUBLIC_EGDESK_BASE_PATH to relative URLs.
58
- * This is necessary because Next.js basePath does NOT automatically apply to
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
- const isDev = process.env.NODE_ENV === 'development';
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');