Initial microservice-importer setup with NestJS backend and Next.js frontend
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
const API_BASE =
|
||||
process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://localhost:3001';
|
||||
|
||||
export async function fetchJson<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
// Använd alltid relativ path i webbläsaren för att undvika mixed content
|
||||
const url = typeof window === 'undefined'
|
||||
? (path.startsWith('http') ? path : `${API_BASE}${path}`)
|
||||
: path;
|
||||
|
||||
const res = await fetch(url, {
|
||||
...init,
|
||||
cache: 'no-store',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(init?.headers || {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`API ${res.status}: ${text}`);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export { API_BASE };
|
||||
Reference in New Issue
Block a user