Skip to content

Commit 577ebc6

Browse files
committed
address feedback
1 parent b6102b3 commit 577ebc6

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

src/beta/realtime/websocket.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ export class OpenAIRealtimeWebSocket extends OpenAIRealtimeEmitter {
100100
}
101101

102102
static async create(
103-
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_setApiKey'>,
103+
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_callApiKey'>,
104104
props: { model: string; dangerouslyAllowBrowser?: boolean },
105105
): Promise<OpenAIRealtimeWebSocket> {
106-
await client._setApiKey();
106+
await client._callApiKey();
107107
return new OpenAIRealtimeWebSocket(props, client);
108108
}
109109

110110
static async azure(
111-
client: Pick<AzureOpenAI, '_setApiKey' | 'apiVersion' | 'apiKey' | 'baseURL' | 'deploymentName'>,
111+
client: Pick<AzureOpenAI, '_callApiKey' | 'apiVersion' | 'apiKey' | 'baseURL' | 'deploymentName'>,
112112
options: { deploymentName?: string; dangerouslyAllowBrowser?: boolean } = {},
113113
): Promise<OpenAIRealtimeWebSocket> {
114-
const isToken = await client._setApiKey();
114+
const isToken = await client._callApiKey();
115115
function onURL(url: URL) {
116116
if (isToken) {
117117
url.searchParams.set('Authorization', `Bearer ${client.apiKey}`);

src/beta/realtime/ws.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
5454
}
5555

5656
static async create(
57-
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_setApiKey'>,
57+
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_callApiKey'>,
5858
props: { model: string; options?: WS.ClientOptions | undefined },
5959
): Promise<OpenAIRealtimeWS> {
60-
await client._setApiKey();
60+
await client._callApiKey();
6161
return new OpenAIRealtimeWS(props, client);
6262
}
6363

6464
static async azure(
65-
client: Pick<AzureOpenAI, '_setApiKey' | 'apiVersion' | 'apiKey' | 'baseURL' | 'deploymentName'>,
65+
client: Pick<AzureOpenAI, '_callApiKey' | 'apiVersion' | 'apiKey' | 'baseURL' | 'deploymentName'>,
6666
options: { deploymentName?: string; options?: WS.ClientOptions | undefined } = {},
6767
): Promise<OpenAIRealtimeWS> {
6868
const deploymentName = options.deploymentName ?? client.deploymentName;
@@ -92,8 +92,8 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
9292
}
9393
}
9494

95-
async function getAzureHeaders(client: Pick<AzureOpenAI, '_setApiKey' | 'apiKey'>) {
96-
const isToken = await client._setApiKey();
95+
async function getAzureHeaders(client: Pick<AzureOpenAI, '_callApiKey' | 'apiKey'>) {
96+
const isToken = await client._callApiKey();
9797
if (isToken) {
9898
return { Authorization: `Bearer ${isToken}` };
9999
} else {

src/client.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,15 @@ export type ApiKeySetter = () => Promise<string>;
210210

211211
export interface ClientOptions {
212212
/**
213-
* Defaults to process.env['OPENAI_API_KEY'].
213+
* API key used for authentication.
214+
*
215+
* - Accepts either a static string or an async function that resolves to a string.
216+
* - Defaults to process.env['OPENAI_API_KEY'].
217+
* - When a function is provided, it is invoked before each request so you can rotate
218+
* or refresh credentials at runtime.
219+
* - The function must return a non-empty string; otherwise an OpenAIError is thrown.
220+
* - If the function throws, the error is wrapped in an OpenAIError with the original
221+
* error available as `cause`.
214222
*/
215223
apiKey?: string | ApiKeySetter | undefined;
216224
/**
@@ -454,30 +462,29 @@ export class OpenAI {
454462
return Errors.APIError.generate(status, error, message, headers);
455463
}
456464

457-
async _setApiKey(): Promise<boolean> {
465+
async _callApiKey(): Promise<boolean> {
458466
const apiKey = this._options.apiKey;
459-
if (typeof apiKey === 'function') {
460-
try {
461-
const token = await apiKey();
462-
if (!token || typeof token !== 'string') {
463-
throw new Errors.OpenAIError(
464-
`Expected 'apiKey' function argument to return a string but it returned ${token}`,
465-
);
466-
}
467-
this.apiKey = token;
468-
return true;
469-
} catch (err: any) {
470-
if (err instanceof Errors.OpenAIError) {
471-
throw err;
472-
}
473-
throw new Errors.OpenAIError(
474-
`Failed to get token from 'apiKey' function: ${err.message}`,
475-
// @ts-ignore
476-
{ cause: err },
477-
);
478-
}
467+
if (typeof apiKey !== 'function') return false;
468+
469+
let token: unknown;
470+
try {
471+
token = await apiKey();
472+
} catch (err: any) {
473+
if (err instanceof Errors.OpenAIError) throw err;
474+
throw new Errors.OpenAIError(
475+
`Failed to get token from 'apiKey' function: ${err.message}`,
476+
// @ts-ignore
477+
{ cause: err },
478+
);
479479
}
480-
return false;
480+
481+
if (typeof token !== 'string' || !token) {
482+
throw new Errors.OpenAIError(
483+
`Expected 'apiKey' function argument to return a string but it returned ${token}`,
484+
);
485+
}
486+
this.apiKey = token;
487+
return true;
481488
}
482489

483490
buildURL(
@@ -507,7 +514,7 @@ export class OpenAI {
507514
* Used as a callback for mutating the given `FinalRequestOptions` object.
508515
*/
509516
protected async prepareOptions(options: FinalRequestOptions): Promise<void> {
510-
await this._setApiKey();
517+
await this._callApiKey();
511518
}
512519

513520
/**

tests/lib/azure.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe('instantiate azure client', () => {
270270

271271
test('AAD token is refreshed', async () => {
272272
let fail = true;
273-
const testFetch = async (url: any, { headers }: RequestInit = {}): Promise<Response> => {
273+
const testFetch = async (url: RequestInfo, { headers }: RequestInit = {}): Promise<Response> => {
274274
if (fail) {
275275
fail = false;
276276
return new Response(undefined, {

0 commit comments

Comments
 (0)