From 878847defdb4b1a70ee404aa7895d70e5672377f Mon Sep 17 00:00:00 2001 From: Mary Hipp Date: Thu, 22 Jun 2023 14:47:55 -0400 Subject: [PATCH] use BASE and TOKEN from OpenAPI if they are set --- .../frontend/web/src/services/apiSlice.ts | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/invokeai/frontend/web/src/services/apiSlice.ts b/invokeai/frontend/web/src/services/apiSlice.ts index e2d765dd90..e9d4ec492d 100644 --- a/invokeai/frontend/web/src/services/apiSlice.ts +++ b/invokeai/frontend/web/src/services/apiSlice.ts @@ -1,4 +1,7 @@ import { + BaseQueryFn, + FetchArgs, + FetchBaseQueryError, TagDescription, createApi, fetchBaseQuery, @@ -18,6 +21,7 @@ import { BaseModelType } from './api/models/BaseModelType'; import { ModelType } from './api/models/ModelType'; import { ModelsList } from './api/models/ModelsList'; import { keyBy } from 'lodash-es'; +import { OpenAPI } from './api/core/OpenAPI'; type ListBoardsArg = { offset: number; limit: number }; type UpdateBoardArg = { board_id: string; changes: BoardChanges }; @@ -41,8 +45,30 @@ const modelsAdapter = createEntityAdapter({ const getModelId = ({ base_model, type, name }: ModelConfig) => `${base_model}/${type}/${name}`; +const dynamicBaseQuery: BaseQueryFn< + string | FetchArgs, + unknown, + FetchBaseQueryError +> = async (args, api, extraOptions) => { + let baseUrl = '/api/v1'; + if (OpenAPI.BASE) { + baseUrl = OpenAPI.BASE; + } + + const rawBaseQuery = fetchBaseQuery({ + baseUrl: `${baseUrl}/api/v1`, + prepareHeaders: (headers, { getState }) => { + if (OpenAPI.TOKEN) + headers.set('Authorization', `Bearer ${OpenAPI.TOKEN}`); + return headers; + }, + }); + + return rawBaseQuery(args, api, extraOptions); +}; + export const api = createApi({ - baseQuery: fetchBaseQuery({ baseUrl: 'http://localhost:5173/api/v1/' }), + baseQuery: dynamicBaseQuery, reducerPath: 'api', tagTypes, endpoints: (build) => ({