@@ -200,11 +200,7 @@ export interface ClientOptions {
200200 /**
201201 * Defaults to process.env['OPENAI_API_KEY'].
202202 */
203- apiKey ?: string | undefined ;
204- /**
205- * A function that returns a token to use for authentication.
206- */
207- tokenProvider ?: TokenProvider | undefined ;
203+ apiKey ?: string | TokenProvider | undefined ;
208204 /**
209205 * Defaults to process.env['OPENAI_ORG_ID'].
210206 */
@@ -299,7 +295,7 @@ export interface ClientOptions {
299295 * API Client for interfacing with the OpenAI API.
300296 */
301297export class OpenAI {
302- apiKey : string ;
298+ apiKey : string | TokenProvider ;
303299 organization : string | null ;
304300 project : string | null ;
305301 webhookSecret : string | null ;
@@ -315,7 +311,6 @@ export class OpenAI {
315311 #encoder: Opts . RequestEncoder ;
316312 protected idempotencyHeader ?: string ;
317313 private _options : ClientOptions ;
318- private _tokenProvider : TokenProvider | undefined ;
319314
320315 /**
321316 * API Client for interfacing with the OpenAI API.
@@ -339,18 +334,11 @@ export class OpenAI {
339334 organization = readEnv ( 'OPENAI_ORG_ID' ) ?? null ,
340335 project = readEnv ( 'OPENAI_PROJECT_ID' ) ?? null ,
341336 webhookSecret = readEnv ( 'OPENAI_WEBHOOK_SECRET' ) ?? null ,
342- tokenProvider,
343337 ...opts
344338 } : ClientOptions = { } ) {
345- if ( apiKey === undefined && ! tokenProvider ) {
346- throw new Errors . OpenAIError (
347- 'Missing credentials. Please pass one of `apiKey` and `tokenProvider`, or set the `OPENAI_API_KEY` environment variable.' ,
348- ) ;
349- }
350-
351- if ( tokenProvider && apiKey ) {
339+ if ( apiKey === undefined ) {
352340 throw new Errors . OpenAIError (
353- 'The `apiKey` and `tokenProvider` arguments are mutually exclusive; only one can be passed at a time .' ,
341+ 'Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable .' ,
354342 ) ;
355343 }
356344
@@ -359,7 +347,6 @@ export class OpenAI {
359347 organization,
360348 project,
361349 webhookSecret,
362- tokenProvider,
363350 ...opts ,
364351 baseURL : baseURL || `https://api.openai.com/v1` ,
365352 } ;
@@ -388,7 +375,6 @@ export class OpenAI {
388375 this . _options = options ;
389376
390377 this . apiKey = apiKey ?? 'Missing Key' ;
391- this . _tokenProvider = tokenProvider ;
392378 this . organization = organization ;
393379 this . project = project ;
394380 this . webhookSecret = webhookSecret ;
@@ -408,7 +394,6 @@ export class OpenAI {
408394 fetch : this . fetch ,
409395 fetchOptions : this . fetchOptions ,
410396 apiKey : this . apiKey ,
411- tokenProvider : this . _tokenProvider ,
412397 organization : this . organization ,
413398 project : this . project ,
414399 webhookSecret : this . webhookSecret ,
@@ -458,12 +443,12 @@ export class OpenAI {
458443 }
459444
460445 async _setToken ( ) : Promise < boolean > {
461- if ( typeof this . _tokenProvider === 'function' ) {
446+ if ( typeof this . apiKey === 'function' ) {
462447 try {
463- const token = await this . _tokenProvider ( ) ;
448+ const token = await this . apiKey ( ) ;
464449 if ( ! token || typeof token . token !== 'string' ) {
465450 throw new Errors . OpenAIError (
466- `Expected 'tokenProvider' argument to return a string but it returned ${ token } ` ,
451+ `Expected 'apiKey' function argument to return a string but it returned ${ token } ` ,
467452 ) ;
468453 }
469454 this . apiKey = token . token ;
@@ -473,7 +458,7 @@ export class OpenAI {
473458 throw err ;
474459 }
475460 throw new Errors . OpenAIError (
476- `Failed to get token from 'tokenProvider ' function: ${ err . message } ` ,
461+ `Failed to get token from 'apiKey ' function: ${ err . message } ` ,
477462 // @ts -ignore
478463 { cause : err } ,
479464 ) ;
0 commit comments