(
method: string,
path: string,
body?: unknown,
options: RequestOptions = {},
)
| 294 | } |
| 295 | |
| 296 | async function request<T>( |
| 297 | method: string, |
| 298 | path: string, |
| 299 | body?: unknown, |
| 300 | options: RequestOptions = {}, |
| 301 | ): Promise<ApiResponse<T>> { |
| 302 | const { |
| 303 | query, |
| 304 | includeAuth = true, |
| 305 | includeCookie = false, |
| 306 | timeoutMs = defaultTimeoutMs, |
| 307 | retry: retryConfig = mergedDefaultRetry, |
| 308 | headers: customHeaders = {}, |
| 309 | } = options |
| 310 | |
| 311 | // Build URL with query parameters |
| 312 | let url = `${baseUrl}${path}` |
| 313 | if (query && Object.keys(query).length > 0) { |
| 314 | const params = new URLSearchParams(query) |
| 315 | url += `?${params.toString()}` |
| 316 | } |
| 317 | |
| 318 | // Build headers |
| 319 | const headers: Record<string, string> = { ...customHeaders } |
| 320 | if (authToken && includeAuth) { |
| 321 | headers['Authorization'] = `Bearer ${authToken}` |
| 322 | } |
| 323 | if (authToken && includeCookie) { |
| 324 | headers['Cookie'] = `next-auth.session-token=${authToken};` |
| 325 | } |
| 326 | if (body !== undefined) { |
| 327 | headers['Content-Type'] = 'application/json' |
| 328 | } |
| 329 | |
| 330 | // Build fetch options |
| 331 | const fetchOptions: RequestInit = { |
| 332 | method, |
| 333 | headers, |
| 334 | } |
| 335 | if (body !== undefined) { |
| 336 | fetchOptions.body = JSON.stringify(body) |
| 337 | } |
| 338 | |
| 339 | // Determine retry config |
| 340 | const shouldRetry = retryConfig !== false |
| 341 | const retryOpts = shouldRetry |
| 342 | ? { ...mergedDefaultRetry, ...retryConfig } |
| 343 | : null |
| 344 | |
| 345 | let lastError: unknown |
| 346 | const maxAttempts = shouldRetry ? (retryOpts?.maxRetries ?? 0) + 1 : 1 |
| 347 | |
| 348 | for (let attempt = 0; attempt < maxAttempts; attempt++) { |
| 349 | // Create abort controller for timeout |
| 350 | const controller = new AbortController() |
| 351 | const timeoutId = setTimeout(() => controller.abort(), timeoutMs) |
| 352 | |
| 353 | try { |
no test coverage detected