diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 5f8b241b2..aefe28544 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "4.41.0" + ".": "4.41.1" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 6671f0bf4..39187ece0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 4.41.1 (2024-05-06) + +Full Changelog: [v4.41.0...v4.41.1](https://github.com/openai/openai-node/compare/v4.41.0...v4.41.1) + +### Bug Fixes + +* **azure:** update build script ([#825](https://github.com/openai/openai-node/issues/825)) ([8afc6e7](https://github.com/openai/openai-node/commit/8afc6e7b49507b3be0228e93913d51b4c3211add)) + ## 4.41.0 (2024-05-05) Full Changelog: [v4.40.2...v4.41.0](https://github.com/openai/openai-node/compare/v4.40.2...v4.41.0) diff --git a/README.md b/README.md index d8e0fb0a5..d911ccc63 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ You can import in Deno via: ```ts -import OpenAI from 'https://deno.land/x/openai@v4.41.0/mod.ts'; +import OpenAI from 'https://deno.land/x/openai@v4.41.1/mod.ts'; ``` diff --git a/package.json b/package.json index b698340af..86b5bfc11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openai", - "version": "4.41.0", + "version": "4.41.1", "description": "The official TypeScript library for the OpenAI API", "author": "OpenAI ", "types": "dist/index.d.ts", diff --git a/scripts/build-deno b/scripts/build-deno index 8df5b0651..5216721fc 100755 --- a/scripts/build-deno +++ b/scripts/build-deno @@ -16,7 +16,7 @@ This is a build produced from https://github.com/openai/openai-node – please g Usage: \`\`\`ts -import OpenAI from "https://deno.land/x/openai@v4.41.0/mod.ts"; +import OpenAI from "https://deno.land/x/openai@v4.41.1/mod.ts"; const client = new OpenAI(); \`\`\` diff --git a/scripts/utils/fix-index-exports.cjs b/scripts/utils/fix-index-exports.cjs index 72b0b8fd0..ee5cebb85 100644 --- a/scripts/utils/fix-index-exports.cjs +++ b/scripts/utils/fix-index-exports.cjs @@ -9,6 +9,6 @@ const indexJs = let before = fs.readFileSync(indexJs, 'utf8'); let after = before.replace( /^\s*exports\.default\s*=\s*(\w+)/m, - 'exports = module.exports = $1;\nexports.default = $1', + 'exports = module.exports = $1;\nmodule.exports.AzureOpenAI = AzureOpenAI;\nexports.default = $1', ); fs.writeFileSync(indexJs, after, 'utf8'); diff --git a/src/index.ts b/src/index.ts index dbade2f86..438a46779 100644 --- a/src/index.ts +++ b/src/index.ts @@ -339,12 +339,12 @@ export interface AzureClientOptions extends ClientOptions { * A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory), * which will be invoked on every request. */ - azureADTokenProvider?: (() => string) | undefined; + azureADTokenProvider?: (() => Promise) | undefined; } /** API Client for interfacing with the Azure OpenAI API. */ export class AzureOpenAI extends OpenAI { - private _azureADTokenProvider: (() => string) | undefined; + private _azureADTokenProvider: (() => Promise) | undefined; apiVersion: string = ''; /** * API Client for interfacing with the Azure OpenAI API. @@ -451,9 +451,9 @@ export class AzureOpenAI extends OpenAI { return super.buildRequest(options); } - private _getAzureADToken(): string | undefined { + private async _getAzureADToken(): Promise { if (typeof this._azureADTokenProvider === 'function') { - const token = this._azureADTokenProvider(); + const token = await this._azureADTokenProvider(); if (!token || typeof token !== 'string') { throw new Errors.OpenAIError( `Expected 'azureADTokenProvider' argument to return a string but it returned ${token}`, @@ -465,17 +465,23 @@ export class AzureOpenAI extends OpenAI { } protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers { + return {}; + } + + protected override async prepareOptions(opts: Core.FinalRequestOptions): Promise { if (opts.headers?.['Authorization'] || opts.headers?.['api-key']) { - return {}; + return super.prepareOptions(opts); } - const token = this._getAzureADToken(); + const token = await this._getAzureADToken(); + opts.headers ??= {}; if (token) { - return { Authorization: `Bearer ${token}` }; - } - if (this.apiKey !== API_KEY_SENTINEL) { - return { 'api-key': this.apiKey }; + opts.headers['Authorization'] = `Bearer ${token}`; + } else if (this.apiKey !== API_KEY_SENTINEL) { + opts.headers['api-key'] = this.apiKey; + } else { + throw new Errors.OpenAIError('Unable to handle auth'); } - throw new Errors.OpenAIError('Unable to handle auth'); + return super.prepareOptions(opts); } } diff --git a/src/version.ts b/src/version.ts index 1ab180911..afa714f81 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const VERSION = '4.41.0'; // x-release-please-version +export const VERSION = '4.41.1'; // x-release-please-version diff --git a/tests/lib/azure.test.ts b/tests/lib/azure.test.ts index e2b967903..4895273be 100644 --- a/tests/lib/azure.test.ts +++ b/tests/lib/azure.test.ts @@ -222,16 +222,21 @@ describe('instantiate azure client', () => { }); describe('Azure Active Directory (AD)', () => { - test('with azureADTokenProvider', () => { + test('with azureADTokenProvider', async () => { + const testFetch = async (url: RequestInfo, { headers }: RequestInit = {}): Promise => { + return new Response(JSON.stringify({ a: 1 }), { headers }); + }; const client = new AzureOpenAI({ baseURL: 'http://localhost:5000/', - azureADTokenProvider: () => 'my token', + azureADTokenProvider: async () => 'my token', apiVersion, + fetch: testFetch, }); - expect(client.buildRequest({ method: 'post', path: 'https://example.com' }).req.headers).toHaveProperty( - 'authorization', - 'Bearer my token', - ); + expect( + (await client.request({ method: 'post', path: 'https://example.com' }).asResponse()).headers.get( + 'authorization', + ), + ).toEqual('Bearer my token'); }); test('apiKey and azureADTokenProvider cant be combined', () => { @@ -239,7 +244,7 @@ describe('instantiate azure client', () => { () => new AzureOpenAI({ baseURL: 'http://localhost:5000/', - azureADTokenProvider: () => 'my token', + azureADTokenProvider: async () => 'my token', apiKey: 'My API Key', apiVersion, }),