Skip to content

Commit 258c191

Browse files
committed
fix(azure): update build script (#825)
1 parent 625426c commit 258c191

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

scripts/utils/fix-index-exports.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ const indexJs =
99
let before = fs.readFileSync(indexJs, 'utf8');
1010
let after = before.replace(
1111
/^\s*exports\.default\s*=\s*(\w+)/m,
12-
'exports = module.exports = $1;\nexports.default = $1',
12+
'exports = module.exports = $1;\nmodule.exports.AzureOpenAI = AzureOpenAI;\nexports.default = $1',
1313
);
1414
fs.writeFileSync(indexJs, after, 'utf8');

src/index.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ export interface AzureClientOptions extends ClientOptions {
339339
* A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory),
340340
* which will be invoked on every request.
341341
*/
342-
azureADTokenProvider?: (() => string) | undefined;
342+
azureADTokenProvider?: (() => Promise<string>) | undefined;
343343
}
344344

345345
/** API Client for interfacing with the Azure OpenAI API. */
346346
export class AzureOpenAI extends OpenAI {
347-
private _azureADTokenProvider: (() => string) | undefined;
347+
private _azureADTokenProvider: (() => Promise<string>) | undefined;
348348
apiVersion: string = '';
349349
/**
350350
* API Client for interfacing with the Azure OpenAI API.
@@ -451,9 +451,9 @@ export class AzureOpenAI extends OpenAI {
451451
return super.buildRequest(options);
452452
}
453453

454-
private _getAzureADToken(): string | undefined {
454+
private async _getAzureADToken(): Promise<string | undefined> {
455455
if (typeof this._azureADTokenProvider === 'function') {
456-
const token = this._azureADTokenProvider();
456+
const token = await this._azureADTokenProvider();
457457
if (!token || typeof token !== 'string') {
458458
throw new Errors.OpenAIError(
459459
`Expected 'azureADTokenProvider' argument to return a string but it returned ${token}`,
@@ -465,17 +465,23 @@ export class AzureOpenAI extends OpenAI {
465465
}
466466

467467
protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
468+
return {};
469+
}
470+
471+
protected override async prepareOptions(opts: Core.FinalRequestOptions<unknown>): Promise<void> {
468472
if (opts.headers?.['Authorization'] || opts.headers?.['api-key']) {
469-
return {};
473+
return super.prepareOptions(opts);
470474
}
471-
const token = this._getAzureADToken();
475+
const token = await this._getAzureADToken();
476+
opts.headers ??= {};
472477
if (token) {
473-
return { Authorization: `Bearer ${token}` };
474-
}
475-
if (this.apiKey !== API_KEY_SENTINEL) {
476-
return { 'api-key': this.apiKey };
478+
opts.headers['Authorization'] = `Bearer ${token}`;
479+
} else if (this.apiKey !== API_KEY_SENTINEL) {
480+
opts.headers['api-key'] = this.apiKey;
481+
} else {
482+
throw new Errors.OpenAIError('Unable to handle auth');
477483
}
478-
throw new Errors.OpenAIError('Unable to handle auth');
484+
return super.prepareOptions(opts);
479485
}
480486
}
481487

tests/lib/azure.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,29 @@ describe('instantiate azure client', () => {
222222
});
223223

224224
describe('Azure Active Directory (AD)', () => {
225-
test('with azureADTokenProvider', () => {
225+
test('with azureADTokenProvider', async () => {
226+
const testFetch = async (url: RequestInfo, { headers }: RequestInit = {}): Promise<Response> => {
227+
return new Response(JSON.stringify({ a: 1 }), { headers });
228+
};
226229
const client = new AzureOpenAI({
227230
baseURL: 'http://localhost:5000/',
228-
azureADTokenProvider: () => 'my token',
231+
azureADTokenProvider: async () => 'my token',
229232
apiVersion,
233+
fetch: testFetch,
230234
});
231-
expect(client.buildRequest({ method: 'post', path: 'https://example.com' }).req.headers).toHaveProperty(
232-
'authorization',
233-
'Bearer my token',
234-
);
235+
expect(
236+
(await client.request({ method: 'post', path: 'https://example.com' }).asResponse()).headers.get(
237+
'authorization',
238+
),
239+
).toEqual('Bearer my token');
235240
});
236241

237242
test('apiKey and azureADTokenProvider cant be combined', () => {
238243
expect(
239244
() =>
240245
new AzureOpenAI({
241246
baseURL: 'http://localhost:5000/',
242-
azureADTokenProvider: () => 'my token',
247+
azureADTokenProvider: async () => 'my token',
243248
apiKey: 'My API Key',
244249
apiVersion,
245250
}),

0 commit comments

Comments
 (0)