Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/azure.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -134,6 +136,13 @@ export class AzureOpenAI extends OpenAI {
}
return super.buildRequest(options, props);
}

protected override async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
if (typeof this._options.apiKey === 'string') {
return buildHeaders([{ 'api-key': this.apiKey }]);
}
return super.authHeaders(opts);
}
}

const _deployments_endpoints = new Set([
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> => {
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');
Expand Down
Loading