ppt-tool/frontend/lib/apiFetch.ts
shubham.goyal@brandtech.plus d5bb04a837 fix download path
2026-05-17 20:35:21 +05:30

24 lines
861 B
TypeScript

/**
* Wrapper around fetch that prepends the Next.js basePath so API calls
* reach the correct backend when deployed under a sub-path (/ppt-tool).
*
* Must match basePath in next.config.mjs.
* Usage: apiFetch('/api/v1/...', options) — identical to fetch(), just works.
*/
const BASE_PATH = '/ppt-tool';
const PREFIXED_PATHS = ['/api/', '/app_data/', '/static/'];
function needsBasePath(path: string): boolean {
return PREFIXED_PATHS.some((p) => path.startsWith(p));
}
export function apiFetch(path: string, init?: RequestInit): Promise<Response> {
const url = needsBasePath(path) ? `${BASE_PATH}${path}` : path;
return fetch(url, init);
}
/** Returns the full URL with basePath — use for EventSource and other non-fetch calls. */
export function apiUrl(path: string): string {
return needsBasePath(path) ? `${BASE_PATH}${path}` : path;
}