File tree Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 11import type { RequestInit } from './internal/builtin-types' ;
2+ import type { NullableHeaders } from './internal/headers' ;
3+ import { buildHeaders } from './internal/headers' ;
24import * as Errors from './error' ;
35import { FinalRequestOptions } from './internal/request-options' ;
46import { isObj , readEnv } from './internal/utils' ;
@@ -134,6 +136,13 @@ export class AzureOpenAI extends OpenAI {
134136 }
135137 return super . buildRequest ( options , props ) ;
136138 }
139+
140+ protected override async authHeaders ( opts : FinalRequestOptions ) : Promise < NullableHeaders | undefined > {
141+ if ( typeof this . _options . apiKey === 'string' ) {
142+ return buildHeaders ( [ { 'api-key' : this . apiKey } ] ) ;
143+ }
144+ return super . authHeaders ( opts ) ;
145+ }
137146}
138147
139148const _deployments_endpoints = new Set ( [
Original file line number Diff line number Diff line change @@ -331,7 +331,7 @@ export class OpenAI {
331331 private fetch : Fetch ;
332332 #encoder: Opts . RequestEncoder ;
333333 protected idempotencyHeader ?: string ;
334- private _options : ClientOptions ;
334+ protected _options : ClientOptions ;
335335
336336 /**
337337 * API Client for interfacing with the OpenAI API.
Original file line number Diff line number Diff line change @@ -307,6 +307,22 @@ describe('instantiate azure client', () => {
307307 } ) ;
308308 } ) ;
309309
310+ test ( 'uses api-key header when apiKey is provided' , async ( ) => {
311+ const testFetch = async ( url : RequestInfo , { headers } : RequestInit = { } ) : Promise < Response > => {
312+ return new Response ( JSON . stringify ( { a : 1 } ) , { headers : headers ?? [ ] } ) ;
313+ } ;
314+ const client = new AzureOpenAI ( {
315+ baseURL : 'http://localhost:5000/' ,
316+ apiKey : 'My API Key' ,
317+ apiVersion,
318+ fetch : testFetch ,
319+ } ) ;
320+
321+ const res = await client . request ( { method : 'post' , path : 'https://example.com' } ) . asResponse ( ) ;
322+ expect ( res . headers . get ( 'api-key' ) ) . toEqual ( 'My API Key' ) ;
323+ expect ( res . headers . get ( 'authorization' ) ) . toEqual ( null ) ;
324+ } ) ;
325+
310326 test ( 'with endpoint' , ( ) => {
311327 const client = new AzureOpenAI ( { endpoint : 'https://example.com' , apiKey : 'My API Key' , apiVersion } ) ;
312328 expect ( client . baseURL ) . toEqual ( 'https://example.com/openai' ) ;
You can’t perform that action at this time.
0 commit comments