Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 40x 40x 28x 28x 27x 1x 2x 26x 1x 2x 25x 28x 6x 6x 3x 3x 1x 28x 40x 15x 15x 15x 15x 15x | import { getAuthToken } from './tokenHelpers';
export function buildHeaders(
requiresAuth: boolean,
baseHeaders: HeadersInit = {}
): HeadersInit {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (baseHeaders) {
if (baseHeaders instanceof Headers) {
baseHeaders.forEach((value, key) => {
headers[key] = value;
});
} else if (Array.isArray(baseHeaders)) {
baseHeaders.forEach(([key, value]) => {
headers[key] = value;
});
} else {
Object.assign(headers, baseHeaders);
}
}
if (requiresAuth) {
const token = getAuthToken();
if (token) {
headers['Authorization'] = `Bearer ${token}`;
} else {
if (process.env.NODE_ENV === 'development') {
console.warn('⚠️ Auth required but no token found!');
}
}
}
return headers;
}
export async function makeRequest(
endpoint: string,
options: RequestInit,
requiresAuth: boolean
): Promise<Response> {
const headers = buildHeaders(requiresAuth, options.headers);
const baseUrl = process.env.NEXT_PUBLIC_API_URL;
const cleanEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
const fullUrl = `${baseUrl}${cleanEndpoint}`;
return fetch(fullUrl, {
...options,
headers,
});
} |