Skip to content

Commit 7218f13

Browse files
authored
Merge pull request ChatGPTNextWeb#4934 from ConnectAI-E/feature/client-headers
feat: optimize getHeaders
2 parents fa31e78 + 700b06f commit 7218f13

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

app/client/api.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -157,45 +157,58 @@ export class ClientApi {
157157

158158
export function getHeaders() {
159159
const accessStore = useAccessStore.getState();
160+
const chatStore = useChatStore.getState();
160161
const headers: Record<string, string> = {
161162
"Content-Type": "application/json",
162163
Accept: "application/json",
163164
};
164-
const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
165-
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
166-
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
167-
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
168-
const authHeader = isAzure
169-
? "api-key"
170-
: isAnthropic
171-
? "x-api-key"
172-
: "Authorization";
173-
const apiKey = isGoogle
174-
? accessStore.googleApiKey
175-
: isAzure
176-
? accessStore.azureApiKey
177-
: isAnthropic
178-
? accessStore.anthropicApiKey
179-
: accessStore.openaiApiKey;
165+
180166
const clientConfig = getClientConfig();
181-
const makeBearer = (s: string) =>
182-
`${isAzure || isAnthropic ? "" : "Bearer "}${s.trim()}`;
183-
const validString = (x: string) => x && x.length > 0;
184167

168+
function getConfig() {
169+
const modelConfig = chatStore.currentSession().mask.modelConfig;
170+
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
171+
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
172+
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
173+
const isEnabledAccessControl = accessStore.enabledAccessControl();
174+
const apiKey = isGoogle
175+
? accessStore.googleApiKey
176+
: isAzure
177+
? accessStore.azureApiKey
178+
: isAnthropic
179+
? accessStore.anthropicApiKey
180+
: accessStore.openaiApiKey;
181+
return { isGoogle, isAzure, isAnthropic, apiKey, isEnabledAccessControl };
182+
}
183+
184+
function getAuthHeader(): string {
185+
return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
186+
}
187+
188+
function getBearerToken(apiKey: string, noBearer: boolean = false): string {
189+
return validString(apiKey)
190+
? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
191+
: "";
192+
}
193+
194+
function validString(x: string): boolean {
195+
return x?.length > 0;
196+
}
197+
const { isGoogle, isAzure, isAnthropic, apiKey, isEnabledAccessControl } =
198+
getConfig();
185199
// when using google api in app, not set auth header
186-
if (!(isGoogle && clientConfig?.isApp)) {
187-
// use user's api key first
188-
if (validString(apiKey)) {
189-
headers[authHeader] = makeBearer(apiKey);
190-
} else if (
191-
accessStore.enabledAccessControl() &&
192-
validString(accessStore.accessCode)
193-
) {
194-
// access_code must send with header named `Authorization`, will using in auth middleware.
195-
headers["Authorization"] = makeBearer(
196-
ACCESS_CODE_PREFIX + accessStore.accessCode,
197-
);
198-
}
200+
if (isGoogle && clientConfig?.isApp) return headers;
201+
202+
const authHeader = getAuthHeader();
203+
204+
const bearerToken = getBearerToken(apiKey, isAzure || isAnthropic);
205+
206+
if (bearerToken) {
207+
headers[authHeader] = bearerToken;
208+
} else if (isEnabledAccessControl && validString(accessStore.accessCode)) {
209+
headers["Authorization"] = getBearerToken(
210+
ACCESS_CODE_PREFIX + accessStore.accessCode,
211+
);
199212
}
200213

201214
return headers;

0 commit comments

Comments
 (0)