-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Expand file tree
/
Copy pathclient.ts
More file actions
60 lines (51 loc) · 1.86 KB
/
client.ts
File metadata and controls
60 lines (51 loc) · 1.86 KB
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
52
53
54
55
56
57
58
59
60
import axios, { AxiosInstance } from 'axios'
import type { RequestInterceptor } from '@/core/types'
import { type DeduplicatedClient, withDeduplication } from './deduplicatedClient'
/**
* Creates a configured axios client for API calls
* @param apiBaseUrl - Base URL of the Flowise server
* @param token - Authentication token (optional)
* @param requestInterceptor - Optional callback to customize outgoing requests
*/
export function bindApiClient(apiBaseUrl: string, token?: string, requestInterceptor?: RequestInterceptor): DeduplicatedClient {
const headers: Record<string, string> = {
'Content-Type': 'application/json'
}
if (token) {
headers['Authorization'] = `Bearer ${token}`
}
const client = axios.create({
baseURL: `${apiBaseUrl}/api/v1`,
headers
})
// Add request interceptor for consumer customization
client.interceptors.request.use(
(config) => {
if (!requestInterceptor) return config
try {
return requestInterceptor(config)
} catch (error) {
console.error('[Agentflow] requestInterceptor threw:', error)
return config
}
},
(error) => Promise.reject(error)
)
// Add response interceptor for error handling
client.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
console.error('[Agentflow] 401 Authentication error:', {
url: error.config?.url,
message: error.response?.data?.message || error.message,
hasToken: !!token,
tokenLength: token?.length
})
}
return Promise.reject(error)
}
)
return withDeduplication(client)
}
export type { AxiosInstance }