@@ -215,11 +215,7 @@ export interface ClientOptions {
215215 /**
216216 * Defaults to process.env['OPENAI_API_KEY'].
217217 */
218- apiKey ?: string | undefined ;
219- /**
220- * A function that returns a token to use for authentication.
221- */
222- tokenProvider ?: TokenProvider | undefined ;
218+ apiKey ?: string | TokenProvider | undefined ;
223219 /**
224220 * Defaults to process.env['OPENAI_ORG_ID'].
225221 */
@@ -330,7 +326,6 @@ export class OpenAI {
330326 #encoder: Opts . RequestEncoder ;
331327 protected idempotencyHeader ?: string ;
332328 private _options : ClientOptions ;
333- private _tokenProvider : TokenProvider | undefined ;
334329
335330 /**
336331 * API Client for interfacing with the OpenAI API.
@@ -354,18 +349,11 @@ export class OpenAI {
354349 organization = readEnv ( 'OPENAI_ORG_ID' ) ?? null ,
355350 project = readEnv ( 'OPENAI_PROJECT_ID' ) ?? null ,
356351 webhookSecret = readEnv ( 'OPENAI_WEBHOOK_SECRET' ) ?? null ,
357- tokenProvider,
358352 ...opts
359353 } : ClientOptions = { } ) {
360- if ( apiKey === undefined && ! tokenProvider ) {
361- throw new Errors . OpenAIError (
362- 'Missing credentials. Please pass one of `apiKey` and `tokenProvider`, or set the `OPENAI_API_KEY` environment variable.' ,
363- ) ;
364- }
365-
366- if ( tokenProvider && apiKey ) {
354+ if ( apiKey === undefined ) {
367355 throw new Errors . OpenAIError (
368- 'The `apiKey` and `tokenProvider` arguments are mutually exclusive; only one can be passed at a time .' ,
356+ 'Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable .' ,
369357 ) ;
370358 }
371359
@@ -374,7 +362,6 @@ export class OpenAI {
374362 organization,
375363 project,
376364 webhookSecret,
377- tokenProvider,
378365 ...opts ,
379366 baseURL : baseURL || `https://api.openai.com/v1` ,
380367 } ;
@@ -402,8 +389,7 @@ export class OpenAI {
402389
403390 this . _options = options ;
404391
405- this . apiKey = apiKey ?? 'Missing Key' ;
406- this . _tokenProvider = tokenProvider ;
392+ this . apiKey = typeof apiKey === 'string' ? apiKey : 'Missing Key' ;
407393 this . organization = organization ;
408394 this . project = project ;
409395 this . webhookSecret = webhookSecret ;
@@ -423,7 +409,6 @@ export class OpenAI {
423409 fetch : this . fetch ,
424410 fetchOptions : this . fetchOptions ,
425411 apiKey : this . apiKey ,
426- tokenProvider : this . _tokenProvider ,
427412 organization : this . organization ,
428413 project : this . project ,
429414 webhookSecret : this . webhookSecret ,
@@ -473,12 +458,13 @@ export class OpenAI {
473458 }
474459
475460 async _setToken ( ) : Promise < boolean > {
476- if ( typeof this . _tokenProvider === 'function' ) {
461+ const apiKey = this . _options . apiKey ;
462+ if ( typeof apiKey === 'function' ) {
477463 try {
478- const token = await this . _tokenProvider ( ) ;
464+ const token = await apiKey ( ) ;
479465 if ( ! token || typeof token . token !== 'string' ) {
480466 throw new Errors . OpenAIError (
481- `Expected 'tokenProvider' argument to return a string but it returned ${ token } ` ,
467+ `Expected 'apiKey' function argument to return a string but it returned ${ token } ` ,
482468 ) ;
483469 }
484470 this . apiKey = token . token ;
@@ -488,7 +474,7 @@ export class OpenAI {
488474 throw err ;
489475 }
490476 throw new Errors . OpenAIError (
491- `Failed to get token from 'tokenProvider ' function: ${ err . message } ` ,
477+ `Failed to get token from 'apiKey ' function: ${ err . message } ` ,
492478 // @ts -ignore
493479 { cause : err } ,
494480 ) ;
0 commit comments