Skip to content

Commit 03b5635

Browse files
authored
Merge pull request #8684 from visadb/main
openai-adapters: allow overriding authorization header
2 parents 03fd3f8 + 6ad9330 commit 03b5635

File tree

1 file changed

+28
-0
lines changed
  • packages/openai-adapters/src

1 file changed

+28
-0
lines changed

packages/openai-adapters/src/util.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,35 @@ export function customFetch(
158158
if (process.env.FEATURE_FLAG_DISABLE_CUSTOM_FETCH) {
159159
return patchedFetch;
160160
}
161+
162+
function letRequestOptionsOverrideAuthorizationHeader(init: any): any {
163+
if (
164+
!init ||
165+
!init.headers ||
166+
!requestOptions ||
167+
!requestOptions.headers ||
168+
(!requestOptions.headers["Authorization"] &&
169+
!requestOptions.headers["authorization"])
170+
) {
171+
return init;
172+
}
173+
174+
if (init.headers instanceof Headers) {
175+
init.headers.delete("Authorization");
176+
} else if (Array.isArray(init.headers)) {
177+
init.headers = init.headers.filter(
178+
(header: [string, string]) =>
179+
(header[0] ?? "").toLowerCase() !== "authorization",
180+
);
181+
} else if (typeof init.headers === "object") {
182+
delete init.headers["Authorization"];
183+
delete init.headers["authorization"];
184+
}
185+
return init;
186+
}
187+
161188
return (req: URL | string | Request, init?: any) => {
189+
init = letRequestOptionsOverrideAuthorizationHeader(init);
162190
if (typeof req === "string" || req instanceof URL) {
163191
return fetchwithRequestOptions(req, init, requestOptions);
164192
} else {

0 commit comments

Comments
 (0)