@@ -206,20 +206,13 @@ import {
206206} from './internal/utils/log' ;
207207import { isEmptyObj } from './internal/utils/values' ;
208208
209- export interface AccessToken {
210- token : string ;
211- }
212- export type TokenProvider = ( ) => Promise < AccessToken > ;
209+ export type ApiKeySetter = ( ) => Promise < string > ;
213210
214211export interface ClientOptions {
215212 /**
216213 * Defaults to process.env['OPENAI_API_KEY'].
217214 */
218- apiKey ?: string | undefined ;
219- /**
220- * A function that returns a token to use for authentication.
221- */
222- tokenProvider ?: TokenProvider | undefined ;
215+ apiKey ?: string | ApiKeySetter | undefined ;
223216 /**
224217 * Defaults to process.env['OPENAI_ORG_ID'].
225218 */
@@ -330,7 +323,6 @@ export class OpenAI {
330323 #encoder: Opts . RequestEncoder ;
331324 protected idempotencyHeader ?: string ;
332325 private _options : ClientOptions ;
333- private _tokenProvider : TokenProvider | undefined ;
334326
335327 /**
336328 * API Client for interfacing with the OpenAI API.
@@ -354,18 +346,11 @@ export class OpenAI {
354346 organization = readEnv ( 'OPENAI_ORG_ID' ) ?? null ,
355347 project = readEnv ( 'OPENAI_PROJECT_ID' ) ?? null ,
356348 webhookSecret = readEnv ( 'OPENAI_WEBHOOK_SECRET' ) ?? null ,
357- tokenProvider,
358349 ...opts
359350 } : 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 ) {
351+ if ( apiKey === undefined ) {
367352 throw new Errors . OpenAIError (
368- 'The `apiKey` and `tokenProvider` arguments are mutually exclusive; only one can be passed at a time .' ,
353+ 'Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable .' ,
369354 ) ;
370355 }
371356
@@ -374,7 +359,6 @@ export class OpenAI {
374359 organization,
375360 project,
376361 webhookSecret,
377- tokenProvider,
378362 ...opts ,
379363 baseURL : baseURL || `https://api.openai.com/v1` ,
380364 } ;
@@ -402,8 +386,7 @@ export class OpenAI {
402386
403387 this . _options = options ;
404388
405- this . apiKey = apiKey ?? 'Missing Key' ;
406- this . _tokenProvider = tokenProvider ;
389+ this . apiKey = typeof apiKey === 'string' ? apiKey : 'Missing Key' ;
407390 this . organization = organization ;
408391 this . project = project ;
409392 this . webhookSecret = webhookSecret ;
@@ -423,7 +406,6 @@ export class OpenAI {
423406 fetch : this . fetch ,
424407 fetchOptions : this . fetchOptions ,
425408 apiKey : this . apiKey ,
426- tokenProvider : this . _tokenProvider ,
427409 organization : this . organization ,
428410 project : this . project ,
429411 webhookSecret : this . webhookSecret ,
@@ -472,23 +454,24 @@ export class OpenAI {
472454 return Errors . APIError . generate ( status , error , message , headers ) ;
473455 }
474456
475- async _setToken ( ) : Promise < boolean > {
476- if ( typeof this . _tokenProvider === 'function' ) {
457+ async _setApiKey ( ) : Promise < boolean > {
458+ const apiKey = this . _options . apiKey ;
459+ if ( typeof apiKey === 'function' ) {
477460 try {
478- const token = await this . _tokenProvider ( ) ;
479- if ( ! token || typeof token . token !== 'string' ) {
461+ const token = await apiKey ( ) ;
462+ if ( ! token || typeof token !== 'string' ) {
480463 throw new Errors . OpenAIError (
481- `Expected 'tokenProvider' argument to return a string but it returned ${ token } ` ,
464+ `Expected 'apiKey' function argument to return a string but it returned ${ token } ` ,
482465 ) ;
483466 }
484- this . apiKey = token . token ;
467+ this . apiKey = token ;
485468 return true ;
486469 } catch ( err : any ) {
487470 if ( err instanceof Errors . OpenAIError ) {
488471 throw err ;
489472 }
490473 throw new Errors . OpenAIError (
491- `Failed to get token from 'tokenProvider ' function: ${ err . message } ` ,
474+ `Failed to get token from 'apiKey ' function: ${ err . message } ` ,
492475 // @ts -ignore
493476 { cause : err } ,
494477 ) ;
@@ -524,7 +507,7 @@ export class OpenAI {
524507 * Used as a callback for mutating the given `FinalRequestOptions` object.
525508 */
526509 protected async prepareOptions ( options : FinalRequestOptions ) : Promise < void > {
527- await this . _setToken ( ) ;
510+ await this . _setApiKey ( ) ;
528511 }
529512
530513 /**
0 commit comments