@@ -50,27 +50,18 @@ export interface ResolveOptions {
5050export async function resolveCredential ( opts : ResolveOptions = { } ) : Promise < ResolvedCredential > {
5151 const now = ( opts . now ?? ( ( ) => new Date ( ) ) ) ( ) ;
5252
53- const heygenEnv = process . env [ "HEYGEN_API_KEY" ] ;
54- if ( heygenEnv && heygenEnv . length > 0 ) {
55- if ( ! isHeaderSafe ( heygenEnv ) ) {
56- throw ErrInvalidStore ( "HEYGEN_API_KEY contains control characters" ) ;
57- }
53+ const heygenEnv = headerSafeEnv ( "HEYGEN_API_KEY" ) ;
54+ if ( heygenEnv ) {
5855 return { type : "api_key" , key : heygenEnv , source : "env" } ;
5956 }
6057
61- const hfEnv = process . env [ "HYPERFRAMES_API_KEY" ] ;
62- if ( hfEnv && hfEnv . length > 0 ) {
63- if ( ! isHeaderSafe ( hfEnv ) ) {
64- throw ErrInvalidStore ( "HYPERFRAMES_API_KEY contains control characters" ) ;
65- }
58+ const hfEnv = headerSafeEnv ( "HYPERFRAMES_API_KEY" ) ;
59+ if ( hfEnv ) {
6660 return { type : "api_key" , key : hfEnv , source : "env_alias" } ;
6761 }
6862
69- const accessToken = process . env [ "HEYGEN_ACCESS_TOKEN" ] ;
63+ const accessToken = headerSafeEnv ( "HEYGEN_ACCESS_TOKEN" ) ;
7064 if ( accessToken ) {
71- if ( ! isHeaderSafe ( accessToken ) ) {
72- throw ErrInvalidStore ( "HEYGEN_ACCESS_TOKEN contains control characters" ) ;
73- }
7465 return { type : "oauth" , access_token : accessToken , source : "env_oauth" , refreshable : false } ;
7566 }
7667
@@ -87,6 +78,14 @@ export async function resolveCredential(opts: ResolveOptions = {}): Promise<Reso
8778 throw credentials . oauth ? ErrLoginExpired ( ) : ErrNotConfigured ( ) ;
8879}
8980
81+ function headerSafeEnv ( name : string ) : string | undefined {
82+ const value = process . env [ name ] ;
83+ if ( value && ! isHeaderSafe ( value ) ) {
84+ throw ErrInvalidStore ( `${ name } contains control characters` ) ;
85+ }
86+ return value ;
87+ }
88+
9089/** Like `resolveCredential` but returns `null` instead of throwing `NOT_CONFIGURED`. */
9190export async function tryResolveCredential (
9291 opts : ResolveOptions = { } ,
0 commit comments