Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions invokeai/frontend/web/src/app/components/InvokeAIUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { addMiddleware, resetMiddlewares } from 'redux-dynamic-middlewares';
import Loading from '../../common/components/Loading/Loading';

import { Middleware } from '@reduxjs/toolkit';
import { $authToken, $baseUrl } from 'services/api/client';
import { $authToken, $baseUrl, $projectId } from 'services/api/client';
import { socketMiddleware } from 'services/events/middleware';
import '../../i18n';
import { AddImageToBoardContextProvider } from '../contexts/AddImageToBoardContext';
Expand All @@ -37,6 +37,7 @@ const InvokeAIUI = ({
config,
headerComponent,
middleware,
projectId,
}: Props) => {
useEffect(() => {
// configure API client token
Expand All @@ -49,6 +50,11 @@ const InvokeAIUI = ({
$baseUrl.set(apiUrl);
}

// configure API client project header
if (projectId) {
$projectId.set(projectId);
}

// reset dynamically added middlewares
resetMiddlewares();

Expand All @@ -68,8 +74,9 @@ const InvokeAIUI = ({
// Reset the API client token and base url on unmount
$baseUrl.set(undefined);
$authToken.set(undefined);
$projectId.set(undefined);
};
}, [apiUrl, token, middleware]);
}, [apiUrl, token, middleware, projectId]);

return (
<React.StrictMode>
Expand Down
12 changes: 10 additions & 2 deletions invokeai/frontend/web/src/services/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const $authToken = atom<string | undefined>();
*/
export const $baseUrl = atom<string | undefined>();

/**
* The optional project-id header.
*/
export const $projectId = atom<string | undefined>();

/**
* Autogenerated, type-safe fetch client for the API. Used when RTK Query is not an option.
* Dynamically updates when the token or base url changes.
Expand All @@ -24,9 +29,12 @@ export const $baseUrl = atom<string | undefined>();
* @example
* const { get, post, del } = $client.get();
*/
export const $client = computed([$authToken, $baseUrl], (authToken, baseUrl) =>
export const $client = computed([$authToken, $baseUrl, $projectId], (authToken, baseUrl, projectId) =>
createClient<paths>({
headers: authToken ? { Authorization: `Bearer ${authToken}` } : {},
headers: {
...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
...(projectId ? { "project-id": projectId } : {})
},
// do not include `api/v1` in the base url for this client
baseUrl: `${baseUrl ?? ''}`,
})
Expand Down
6 changes: 5 additions & 1 deletion invokeai/frontend/web/src/services/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createApi,
fetchBaseQuery,
} from '@reduxjs/toolkit/query/react';
import { $authToken, $baseUrl } from 'services/api/client';
import { $authToken, $baseUrl, $projectId } from 'services/api/client';

export const tagTypes = [
'Board',
Expand All @@ -30,13 +30,17 @@ const dynamicBaseQuery: BaseQueryFn<
> = async (args, api, extraOptions) => {
const baseUrl = $baseUrl.get();
const authToken = $authToken.get();
const projectId = $projectId.get();

const rawBaseQuery = fetchBaseQuery({
baseUrl: `${baseUrl ?? ''}/api/v1`,
prepareHeaders: (headers) => {
if (authToken) {
headers.set('Authorization', `Bearer ${authToken}`);
}
if (projectId) {
headers.set("project-id", projectId)
}

return headers;
},
Expand Down