(apiBaseUrl: string, token?: string, requestInterceptor?: RequestInterceptor)
| 11 | * @param requestInterceptor - Optional callback to customize outgoing requests |
| 12 | */ |
| 13 | export function bindApiClient(apiBaseUrl: string, token?: string, requestInterceptor?: RequestInterceptor): DeduplicatedClient { |
| 14 | const headers: Record<string, string> = { |
| 15 | 'Content-Type': 'application/json' |
| 16 | } |
| 17 | |
| 18 | if (token) { |
| 19 | headers['Authorization'] = `Bearer ${token}` |
| 20 | } |
| 21 | |
| 22 | const client = axios.create({ |
| 23 | baseURL: `${apiBaseUrl}/api/v1`, |
| 24 | headers |
| 25 | }) |
| 26 | |
| 27 | // Add request interceptor for consumer customization |
| 28 | client.interceptors.request.use( |
| 29 | (config) => { |
| 30 | if (!requestInterceptor) return config |
| 31 | try { |
| 32 | return requestInterceptor(config) |
| 33 | } catch (error) { |
| 34 | console.error('[Agentflow] requestInterceptor threw:', error) |
| 35 | return config |
| 36 | } |
| 37 | }, |
| 38 | (error) => Promise.reject(error) |
| 39 | ) |
| 40 | |
| 41 | // Add response interceptor for error handling |
| 42 | client.interceptors.response.use( |
| 43 | (response) => response, |
| 44 | (error) => { |
| 45 | if (error.response?.status === 401) { |
| 46 | console.error('[Agentflow] 401 Authentication error:', { |
| 47 | url: error.config?.url, |
| 48 | message: error.response?.data?.message || error.message, |
| 49 | hasToken: !!token, |
| 50 | tokenLength: token?.length |
| 51 | }) |
| 52 | } |
| 53 | return Promise.reject(error) |
| 54 | } |
| 55 | ) |
| 56 | |
| 57 | return withDeduplication(client) |
| 58 | } |
| 59 | |
| 60 | export type { AxiosInstance } |
no test coverage detected