@lastbrain/ai-ui-core 1.0.45 → 1.0.47
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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enhanced-gateway.d.ts","sourceRoot":"","sources":["../../../src/route-handlers/nextjs/enhanced-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"enhanced-gateway.d.ts","sourceRoot":"","sources":["../../../src/route-handlers/nextjs/enhanced-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAiNxD,wBAAsB,GAAG,CACvB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,IAAI,CACxB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,GAAG,CACvB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,MAAM,CAC1B,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD"}
|
|
@@ -65,17 +65,20 @@ function mapAuthRouteToPublicRoute(path) {
|
|
|
65
65
|
};
|
|
66
66
|
// Try exact match first
|
|
67
67
|
if (routeMappings[cleanPath]) {
|
|
68
|
+
console.log(`[Enhanced Gateway] Exact match: "${cleanPath}" -> "${routeMappings[cleanPath]}"`);
|
|
68
69
|
return routeMappings[cleanPath];
|
|
69
70
|
}
|
|
70
71
|
// Try pattern matching with priority to longer/more specific patterns first
|
|
71
|
-
const sortedPatterns = Object.entries(routeMappings)
|
|
72
|
-
|
|
72
|
+
const sortedPatterns = Object.entries(routeMappings).sort(([a], [b]) => b.length - a.length); // Longer patterns first
|
|
73
|
+
console.log(`[Enhanced Gateway] No exact match for "${cleanPath}", trying patterns...`);
|
|
73
74
|
for (const [pattern, target] of sortedPatterns) {
|
|
74
75
|
if (cleanPath.startsWith(pattern + "/") || cleanPath === pattern) {
|
|
76
|
+
console.log(`[Enhanced Gateway] Pattern match: "${pattern}" -> "${target}"`);
|
|
75
77
|
return target + cleanPath.slice(pattern.length);
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
80
|
// Fallback: return clean path as-is
|
|
81
|
+
console.log(`[Enhanced Gateway] No pattern match, using fallback: "${cleanPath}"`);
|
|
79
82
|
return cleanPath;
|
|
80
83
|
}
|
|
81
84
|
async function handleRequest(request, context) {
|
|
@@ -85,7 +88,8 @@ async function handleRequest(request, context) {
|
|
|
85
88
|
const params = await context.params;
|
|
86
89
|
const originalPath = params.path.join("/");
|
|
87
90
|
const mappedPath = mapAuthRouteToPublicRoute(originalPath);
|
|
88
|
-
|
|
91
|
+
// Ajouter automatiquement le préfixe /api/ai pour le monorepo
|
|
92
|
+
const url = `${LB_BASE_URL}/api/ai/${mappedPath}`;
|
|
89
93
|
// Preserve query params
|
|
90
94
|
const searchParams = request.nextUrl.searchParams.toString();
|
|
91
95
|
const finalUrl = searchParams ? `${url}?${searchParams}` : url;
|
|
@@ -94,22 +98,22 @@ async function handleRequest(request, context) {
|
|
|
94
98
|
console.log("[Enhanced Gateway] Bearer token:", LB_API_KEY ? `${LB_API_KEY.substring(0, 10)}...` : "MISSING");
|
|
95
99
|
try {
|
|
96
100
|
// Déterminer le type d'auth selon le path
|
|
97
|
-
const isAuthRoute = originalPath.startsWith(
|
|
101
|
+
const isAuthRoute = originalPath.startsWith("auth/") || mappedPath.startsWith("auth/");
|
|
98
102
|
const headers = {
|
|
99
103
|
"Content-Type": "application/json",
|
|
100
104
|
};
|
|
101
105
|
if (isAuthRoute) {
|
|
102
106
|
// Routes auth/* : forward les cookies de session
|
|
103
107
|
console.log("[Enhanced Gateway] Auth route - forwarding session cookies");
|
|
104
|
-
const cookies = request.headers.get(
|
|
108
|
+
const cookies = request.headers.get("cookie");
|
|
105
109
|
if (cookies) {
|
|
106
|
-
headers[
|
|
110
|
+
headers["Cookie"] = cookies;
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
else {
|
|
110
114
|
// Routes v1/* : utiliser LB_API_KEY
|
|
111
115
|
console.log("[Enhanced Gateway] API route - using LB_API_KEY");
|
|
112
|
-
headers[
|
|
116
|
+
headers["Authorization"] = `Bearer ${LB_API_KEY}`;
|
|
113
117
|
}
|
|
114
118
|
let body;
|
|
115
119
|
if (request.method === "POST" ||
|
|
@@ -127,7 +131,7 @@ async function handleRequest(request, context) {
|
|
|
127
131
|
method: request.method,
|
|
128
132
|
headers,
|
|
129
133
|
body,
|
|
130
|
-
credentials: isAuthRoute ?
|
|
134
|
+
credentials: isAuthRoute ? "include" : "omit", // Include cookies for auth routes
|
|
131
135
|
});
|
|
132
136
|
if (!response.ok) {
|
|
133
137
|
const errorText = await response.text();
|
package/package.json
CHANGED
|
@@ -29,7 +29,9 @@ function mapAuthRouteToPublicRoute(path: string): string {
|
|
|
29
29
|
// Remove leading 'ai/' if present (for routes like ai/auth/prompts)
|
|
30
30
|
const cleanPath = rawPath.startsWith("ai/") ? rawPath.slice(3) : rawPath;
|
|
31
31
|
|
|
32
|
-
console.log(
|
|
32
|
+
console.log(
|
|
33
|
+
`[Enhanced Gateway] Raw path: "${path}" -> Clean path: "${cleanPath}"`
|
|
34
|
+
);
|
|
33
35
|
|
|
34
36
|
// Route mapping: auth routes -> public routes (without api/public/v1 prefix)
|
|
35
37
|
const routeMappings: Record<string, string> = {
|
|
@@ -83,20 +85,33 @@ function mapAuthRouteToPublicRoute(path: string): string {
|
|
|
83
85
|
|
|
84
86
|
// Try exact match first
|
|
85
87
|
if (routeMappings[cleanPath]) {
|
|
88
|
+
console.log(
|
|
89
|
+
`[Enhanced Gateway] Exact match: "${cleanPath}" -> "${routeMappings[cleanPath]}"`
|
|
90
|
+
);
|
|
86
91
|
return routeMappings[cleanPath];
|
|
87
92
|
}
|
|
88
93
|
|
|
89
94
|
// Try pattern matching with priority to longer/more specific patterns first
|
|
90
|
-
const sortedPatterns = Object.entries(routeMappings)
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
const sortedPatterns = Object.entries(routeMappings).sort(
|
|
96
|
+
([a], [b]) => b.length - a.length
|
|
97
|
+
); // Longer patterns first
|
|
98
|
+
|
|
99
|
+
console.log(
|
|
100
|
+
`[Enhanced Gateway] No exact match for "${cleanPath}", trying patterns...`
|
|
101
|
+
);
|
|
93
102
|
for (const [pattern, target] of sortedPatterns) {
|
|
94
103
|
if (cleanPath.startsWith(pattern + "/") || cleanPath === pattern) {
|
|
104
|
+
console.log(
|
|
105
|
+
`[Enhanced Gateway] Pattern match: "${pattern}" -> "${target}"`
|
|
106
|
+
);
|
|
95
107
|
return target + cleanPath.slice(pattern.length);
|
|
96
108
|
}
|
|
97
109
|
}
|
|
98
110
|
|
|
99
111
|
// Fallback: return clean path as-is
|
|
112
|
+
console.log(
|
|
113
|
+
`[Enhanced Gateway] No pattern match, using fallback: "${cleanPath}"`
|
|
114
|
+
);
|
|
100
115
|
return cleanPath;
|
|
101
116
|
}
|
|
102
117
|
|
|
@@ -115,7 +130,8 @@ async function handleRequest(
|
|
|
115
130
|
const originalPath = params.path.join("/");
|
|
116
131
|
const mappedPath = mapAuthRouteToPublicRoute(originalPath);
|
|
117
132
|
|
|
118
|
-
|
|
133
|
+
// Ajouter automatiquement le préfixe /api/ai pour le monorepo
|
|
134
|
+
const url = `${LB_BASE_URL}/api/ai/${mappedPath}`;
|
|
119
135
|
|
|
120
136
|
// Preserve query params
|
|
121
137
|
const searchParams = request.nextUrl.searchParams.toString();
|
|
@@ -130,8 +146,9 @@ async function handleRequest(
|
|
|
130
146
|
|
|
131
147
|
try {
|
|
132
148
|
// Déterminer le type d'auth selon le path
|
|
133
|
-
const isAuthRoute =
|
|
134
|
-
|
|
149
|
+
const isAuthRoute =
|
|
150
|
+
originalPath.startsWith("auth/") || mappedPath.startsWith("auth/");
|
|
151
|
+
|
|
135
152
|
const headers: HeadersInit = {
|
|
136
153
|
"Content-Type": "application/json",
|
|
137
154
|
};
|
|
@@ -139,14 +156,14 @@ async function handleRequest(
|
|
|
139
156
|
if (isAuthRoute) {
|
|
140
157
|
// Routes auth/* : forward les cookies de session
|
|
141
158
|
console.log("[Enhanced Gateway] Auth route - forwarding session cookies");
|
|
142
|
-
const cookies = request.headers.get(
|
|
159
|
+
const cookies = request.headers.get("cookie");
|
|
143
160
|
if (cookies) {
|
|
144
|
-
headers[
|
|
161
|
+
headers["Cookie"] = cookies;
|
|
145
162
|
}
|
|
146
163
|
} else {
|
|
147
164
|
// Routes v1/* : utiliser LB_API_KEY
|
|
148
165
|
console.log("[Enhanced Gateway] API route - using LB_API_KEY");
|
|
149
|
-
headers[
|
|
166
|
+
headers["Authorization"] = `Bearer ${LB_API_KEY}`;
|
|
150
167
|
}
|
|
151
168
|
|
|
152
169
|
let body: string | undefined;
|
|
@@ -167,7 +184,7 @@ async function handleRequest(
|
|
|
167
184
|
method: request.method,
|
|
168
185
|
headers,
|
|
169
186
|
body,
|
|
170
|
-
credentials: isAuthRoute ?
|
|
187
|
+
credentials: isAuthRoute ? "include" : "omit", // Include cookies for auth routes
|
|
171
188
|
});
|
|
172
189
|
|
|
173
190
|
if (!response.ok) {
|