diff --git a/src/azure.ts b/src/azure.ts index 5719fc40c..97765df6d 100644 --- a/src/azure.ts +++ b/src/azure.ts @@ -1,4 +1,6 @@ import type { RequestInit } from './internal/builtin-types'; +import type { NullableHeaders } from './internal/headers'; +import { buildHeaders } from './internal/headers'; import * as Errors from './error'; import { FinalRequestOptions } from './internal/request-options'; import { isObj, readEnv } from './internal/utils'; @@ -134,6 +136,13 @@ export class AzureOpenAI extends OpenAI { } return super.buildRequest(options, props); } + + protected override async authHeaders(opts: FinalRequestOptions): Promise { + if (typeof this._options.apiKey === 'string') { + return buildHeaders([{ 'api-key': this.apiKey }]); + } + return super.authHeaders(opts); + } } const _deployments_endpoints = new Set([ diff --git a/src/client.ts b/src/client.ts index cc2cc0a30..b3c899b44 100644 --- a/src/client.ts +++ b/src/client.ts @@ -331,7 +331,7 @@ export class OpenAI { private fetch: Fetch; #encoder: Opts.RequestEncoder; protected idempotencyHeader?: string; - private _options: ClientOptions; + protected _options: ClientOptions; /** * API Client for interfacing with the OpenAI API. diff --git a/tests/lib/azure.test.ts b/tests/lib/azure.test.ts index b93defab0..82af3ec85 100644 --- a/tests/lib/azure.test.ts +++ b/tests/lib/azure.test.ts @@ -307,6 +307,22 @@ describe('instantiate azure client', () => { }); }); + test('uses api-key header when apiKey is provided', async () => { + const testFetch = async (url: RequestInfo, { headers }: RequestInit = {}): Promise => { + return new Response(JSON.stringify({ a: 1 }), { headers: headers ?? [] }); + }; + const client = new AzureOpenAI({ + baseURL: 'http://localhost:5000/', + apiKey: 'My API Key', + apiVersion, + fetch: testFetch, + }); + + const res = await client.request({ method: 'post', path: 'https://example.com' }).asResponse(); + expect(res.headers.get('api-key')).toEqual('My API Key'); + expect(res.headers.get('authorization')).toEqual(null); + }); + test('with endpoint', () => { const client = new AzureOpenAI({ endpoint: 'https://example.com', apiKey: 'My API Key', apiVersion }); expect(client.baseURL).toEqual('https://example.com/openai');