(
config: CodebuffApiClientConfig = {},
)
| 270 | * Create a Codebuff API client for making authenticated requests to the Codebuff API |
| 271 | */ |
| 272 | export function createCodebuffApiClient( |
| 273 | config: CodebuffApiClientConfig = {}, |
| 274 | ): CodebuffApiClient { |
| 275 | const { |
| 276 | baseUrl = WEBSITE_URL, |
| 277 | authToken, |
| 278 | fetch: fetchFn = fetch, |
| 279 | defaultTimeoutMs = 30000, |
| 280 | retry: defaultRetryConfig = {}, |
| 281 | proxy: proxyConfig, |
| 282 | } = config |
| 283 | |
| 284 | // Resolve proxy: explicit config wins, then env vars, then no proxy. |
| 285 | // Pass proxy: null to explicitly disable even when env vars are set. |
| 286 | const proxyUrl: string | undefined = |
| 287 | proxyConfig === null |
| 288 | ? undefined |
| 289 | : (proxyConfig ?? resolveProxyUrl()) |
| 290 | |
| 291 | const mergedDefaultRetry: Required<RetryConfig> = { |
| 292 | ...DEFAULT_RETRY_CONFIG, |
| 293 | ...defaultRetryConfig, |
| 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 |
no test coverage detected