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 1
1
import type { RequestInit } from './internal/builtin-types' ;
2
+ import type { NullableHeaders } from './internal/headers' ;
3
+ import { buildHeaders } from './internal/headers' ;
2
4
import * as Errors from './error' ;
3
5
import { FinalRequestOptions } from './internal/request-options' ;
4
6
import { isObj , readEnv } from './internal/utils' ;
@@ -134,6 +136,13 @@ export class AzureOpenAI extends OpenAI {
134
136
}
135
137
return super . buildRequest ( options , props ) ;
136
138
}
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
+ }
137
146
}
138
147
139
148
const _deployments_endpoints = new Set ( [
Original file line number Diff line number Diff line change @@ -331,7 +331,7 @@ export class OpenAI {
331
331
private fetch : Fetch ;
332
332
#encoder: Opts . RequestEncoder ;
333
333
protected idempotencyHeader ?: string ;
334
- private _options : ClientOptions ;
334
+ protected _options : ClientOptions ;
335
335
336
336
/**
337
337
* 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', () => {
307
307
} ) ;
308
308
} ) ;
309
309
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
+
310
326
test ( 'with endpoint' , ( ) => {
311
327
const client = new AzureOpenAI ( { endpoint : 'https://example.com' , apiKey : 'My API Key' , apiVersion } ) ;
312
328
expect ( client . baseURL ) . toEqual ( 'https://example.com/openai' ) ;
You can’t perform that action at this time.
0 commit comments