Merge pull request 'fix(#264): apiFetch — global X-Session-Id header (custom POI scoring в UI)' (#266) from fix/264-session-id-global-apifetch into main
All checks were successful
Deploy / changes (push) Successful in 5s
Deploy / build-backend (push) Has been skipped
Deploy / build-worker (push) Has been skipped
Deploy / build-frontend (push) Successful in 2m51s
Deploy / deploy (push) Successful in 38s

This commit is contained in:
lekss361 2026-05-17 07:42:00 +00:00
commit 9bac676b98

View file

@ -1,16 +1,38 @@
import { getOrCreateSessionId } from "@/lib/sessionId";
// Empty default = same-origin relative URLs.
// In prod Caddy proxies /api/* to backend; in dev Next.js rewrites do the same.
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL ?? "";
/**
* Merges a global X-Session-Id header into the request init.
* Only runs in browser (SSR guard). User-supplied headers win over the
* auto-injected value so explicit overrides remain backward-compatible.
*/
function withSessionHeader(init?: RequestInit): RequestInit {
const base: RequestInit = init ?? {};
if (typeof window === "undefined") return base;
const sessionId = getOrCreateSessionId();
if (!sessionId) return base;
return {
...base,
headers: {
"X-Session-Id": sessionId,
...(base.headers ?? {}),
},
};
}
export async function apiFetch<T>(
path: string,
init?: RequestInit,
): Promise<T> {
const merged = withSessionHeader(init);
const response = await fetch(`${API_BASE_URL}${path}`, {
...init,
...merged,
headers: {
"Content-Type": "application/json",
...(init?.headers ?? {}),
...(merged.headers ?? {}),
},
});
if (!response.ok) {
@ -39,11 +61,12 @@ export async function apiFetchWithStatus<T>(
path: string,
init?: RequestInit,
): Promise<{ status: number; body: T }> {
const merged = withSessionHeader(init);
const response = await fetch(`${API_BASE_URL}${path}`, {
...init,
...merged,
headers: {
"Content-Type": "application/json",
...(init?.headers ?? {}),
...(merged.headers ?? {}),
},
});
let body: unknown = null;