feat(F-158): completed feature

This commit is contained in:
chattie
2026-08-22 17:50:55 +02:00
parent 7159baf851
commit 3e1a447e43
16 changed files with 348 additions and 77 deletions

View File

@@ -1,32 +1,79 @@
import { NextRequest, NextResponse } from 'next/server';
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3000';
const BACKEND_URL = process.env.POS_BACKEND_URL ?? 'http://127.0.0.1:3000';
function apiPath(request: NextRequest): string {
return request.nextUrl.pathname.replace(/^\/api\//, '');
}
function backendUrl(request: NextRequest): string {
return `${BACKEND_URL}/${apiPath(request)}${request.nextUrl.search}`;
}
function requestHeaders(request: NextRequest, hasBody = false): Headers {
const headers = new Headers();
const cookie = request.headers.get('cookie');
const terminalId = request.headers.get('x-terminal-id') ?? request.cookies.get('pos_terminal_id')?.value;
if (cookie) headers.set('cookie', cookie);
if (terminalId) headers.set('x-terminal-id', terminalId);
if (hasBody) headers.set('content-type', request.headers.get('content-type') ?? 'application/json');
return headers;
}
function proxyResponse(backendResponse: Response): NextResponse {
const response = new NextResponse(backendResponse.body, {
status: backendResponse.status,
statusText: backendResponse.statusText,
});
const contentType = backendResponse.headers.get('content-type');
const setCookie = backendResponse.headers.get('set-cookie');
if (contentType) response.headers.set('content-type', contentType);
if (setCookie) response.headers.set('set-cookie', setCookie);
return response;
}
export async function GET(request: NextRequest) {
const path = request.nextUrl.pathname.replace('/api/', '');
const search = request.nextUrl.search;
const cookie = request.headers.get('cookie') ?? '';
const terminalId = request.headers.get('x-terminal-id');
const headers: Record<string, string> = { Cookie: cookie };
if (terminalId) headers['x-terminal-id'] = terminalId;
const res = await fetch(`${API}/${path}${search}`, { headers, credentials: 'include' });
const body = await res.text();
return new NextResponse(body, { status: res.status, headers: { 'content-type': res.headers.get('content-type') ?? 'application/json' } });
try {
const response = await fetch(backendUrl(request), {
headers: requestHeaders(request),
cache: 'no-store',
});
return proxyResponse(response);
} catch {
return NextResponse.json({ message: 'Backend no disponible' }, { status: 502 });
}
}
export async function POST(request: NextRequest) {
const path = request.nextUrl.pathname.replace('/api/', '');
const search = request.nextUrl.search;
const cookie = request.headers.get('cookie') ?? '';
const terminalId = request.headers.get('x-terminal-id');
const body = await request.text();
try {
const body = await request.text();
const response = await fetch(backendUrl(request), {
method: 'POST',
headers: requestHeaders(request, true),
body,
cache: 'no-store',
});
const bindingPayload = response.ok && apiPath(request) === 'pos/terminals/bind'
? await response.clone().json() as { terminalId?: string }
: null;
const proxied = proxyResponse(response);
const headers: Record<string, string> = { 'Content-Type': 'application/json', Cookie: cookie };
if (terminalId) headers['x-terminal-id'] = terminalId;
if (bindingPayload?.terminalId) {
proxied.cookies.set('pos_terminal_id', bindingPayload.terminalId, {
httpOnly: true,
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
maxAge: 365 * 24 * 60 * 60,
path: '/',
});
}
const res = await fetch(`${API}/${path}${search}`, { method: 'POST', headers, body, credentials: 'include' });
const resBody = await res.text();
return new NextResponse(resBody, { status: res.status, headers: { 'content-type': res.headers.get('content-type') ?? 'application/json' } });
return proxied;
} catch {
return NextResponse.json({ message: 'Backend no disponible' }, { status: 502 });
}
}