1
1
import {
2
+ BaseQueryFn ,
3
+ FetchArgs ,
4
+ FetchBaseQueryError ,
2
5
TagDescription ,
3
6
createApi ,
4
7
fetchBaseQuery ,
@@ -18,6 +21,7 @@ import { BaseModelType } from './api/models/BaseModelType';
18
21
import { ModelType } from './api/models/ModelType' ;
19
22
import { ModelsList } from './api/models/ModelsList' ;
20
23
import { keyBy } from 'lodash-es' ;
24
+ import { OpenAPI } from './api/core/OpenAPI' ;
21
25
22
26
type ListBoardsArg = { offset : number ; limit : number } ;
23
27
type UpdateBoardArg = { board_id : string ; changes : BoardChanges } ;
@@ -41,8 +45,27 @@ const modelsAdapter = createEntityAdapter<ModelConfig>({
41
45
const getModelId = ( { base_model, type, name } : ModelConfig ) =>
42
46
`${ base_model } /${ type } /${ name } ` ;
43
47
48
+ const dynamicBaseQuery : BaseQueryFn <
49
+ string | FetchArgs ,
50
+ unknown ,
51
+ FetchBaseQueryError
52
+ > = async ( args , api , extraOptions ) => {
53
+ const baseUrl = OpenAPI . BASE || '' ;
54
+
55
+ const rawBaseQuery = fetchBaseQuery ( {
56
+ baseUrl : `${ baseUrl } /api/v1` ,
57
+ prepareHeaders : ( headers , { getState } ) => {
58
+ if ( OpenAPI . TOKEN )
59
+ headers . set ( 'Authorization' , `Bearer ${ OpenAPI . TOKEN } ` ) ;
60
+ return headers ;
61
+ } ,
62
+ } ) ;
63
+
64
+ return rawBaseQuery ( args , api , extraOptions ) ;
65
+ } ;
66
+
44
67
export const api = createApi ( {
45
- baseQuery : fetchBaseQuery ( { baseUrl : 'http://localhost:5173/api/v1/' } ) ,
68
+ baseQuery : dynamicBaseQuery ,
46
69
reducerPath : 'api' ,
47
70
tagTypes,
48
71
endpoints : ( build ) => ( {
0 commit comments