Merge pull request #4934 from ConnectAI-E/feature/client-headers
feat: optimize getHeaders
This commit is contained in:
commit
7218f13783
|
@ -157,19 +157,20 @@ export class ClientApi {
|
||||||
|
|
||||||
export function getHeaders() {
|
export function getHeaders() {
|
||||||
const accessStore = useAccessStore.getState();
|
const accessStore = useAccessStore.getState();
|
||||||
|
const chatStore = useChatStore.getState();
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
};
|
};
|
||||||
const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
|
|
||||||
|
const clientConfig = getClientConfig();
|
||||||
|
|
||||||
|
function getConfig() {
|
||||||
|
const modelConfig = chatStore.currentSession().mask.modelConfig;
|
||||||
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
|
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
|
||||||
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
|
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
|
||||||
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
|
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
|
||||||
const authHeader = isAzure
|
const isEnabledAccessControl = accessStore.enabledAccessControl();
|
||||||
? "api-key"
|
|
||||||
: isAnthropic
|
|
||||||
? "x-api-key"
|
|
||||||
: "Authorization";
|
|
||||||
const apiKey = isGoogle
|
const apiKey = isGoogle
|
||||||
? accessStore.googleApiKey
|
? accessStore.googleApiKey
|
||||||
: isAzure
|
: isAzure
|
||||||
|
@ -177,26 +178,38 @@ export function getHeaders() {
|
||||||
: isAnthropic
|
: isAnthropic
|
||||||
? accessStore.anthropicApiKey
|
? accessStore.anthropicApiKey
|
||||||
: accessStore.openaiApiKey;
|
: accessStore.openaiApiKey;
|
||||||
const clientConfig = getClientConfig();
|
return { isGoogle, isAzure, isAnthropic, apiKey, isEnabledAccessControl };
|
||||||
const makeBearer = (s: string) =>
|
}
|
||||||
`${isAzure || isAnthropic ? "" : "Bearer "}${s.trim()}`;
|
|
||||||
const validString = (x: string) => x && x.length > 0;
|
|
||||||
|
|
||||||
|
function getAuthHeader(): string {
|
||||||
|
return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBearerToken(apiKey: string, noBearer: boolean = false): string {
|
||||||
|
return validString(apiKey)
|
||||||
|
? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
|
||||||
|
: "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function validString(x: string): boolean {
|
||||||
|
return x?.length > 0;
|
||||||
|
}
|
||||||
|
const { isGoogle, isAzure, isAnthropic, apiKey, isEnabledAccessControl } =
|
||||||
|
getConfig();
|
||||||
// when using google api in app, not set auth header
|
// when using google api in app, not set auth header
|
||||||
if (!(isGoogle && clientConfig?.isApp)) {
|
if (isGoogle && clientConfig?.isApp) return headers;
|
||||||
// use user's api key first
|
|
||||||
if (validString(apiKey)) {
|
const authHeader = getAuthHeader();
|
||||||
headers[authHeader] = makeBearer(apiKey);
|
|
||||||
} else if (
|
const bearerToken = getBearerToken(apiKey, isAzure || isAnthropic);
|
||||||
accessStore.enabledAccessControl() &&
|
|
||||||
validString(accessStore.accessCode)
|
if (bearerToken) {
|
||||||
) {
|
headers[authHeader] = bearerToken;
|
||||||
// access_code must send with header named `Authorization`, will using in auth middleware.
|
} else if (isEnabledAccessControl && validString(accessStore.accessCode)) {
|
||||||
headers["Authorization"] = makeBearer(
|
headers["Authorization"] = getBearerToken(
|
||||||
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue