(opts: ResolveOptions = {})
| 47 | } |
| 48 | |
| 49 | export async function resolveCredential(opts: ResolveOptions = {}): Promise<ResolvedCredential> { |
| 50 | const now = (opts.now ?? (() => new Date()))(); |
| 51 | |
| 52 | const heygenEnv = process.env["HEYGEN_API_KEY"]; |
| 53 | if (heygenEnv && heygenEnv.length > 0) { |
| 54 | if (!isHeaderSafe(heygenEnv)) { |
| 55 | throw ErrInvalidStore("HEYGEN_API_KEY contains control characters"); |
| 56 | } |
| 57 | return { type: "api_key", key: heygenEnv, source: "env" }; |
| 58 | } |
| 59 | |
| 60 | const hfEnv = process.env["HYPERFRAMES_API_KEY"]; |
| 61 | if (hfEnv && hfEnv.length > 0) { |
| 62 | if (!isHeaderSafe(hfEnv)) { |
| 63 | throw ErrInvalidStore("HYPERFRAMES_API_KEY contains control characters"); |
| 64 | } |
| 65 | return { type: "api_key", key: hfEnv, source: "env_alias" }; |
| 66 | } |
| 67 | |
| 68 | const { credentials, source } = await readStore(); |
| 69 | if (source === "absent") throw ErrNotConfigured(); |
| 70 | |
| 71 | const fileSource: CredentialSource = source === "file_legacy" ? "file_legacy" : "file_json"; |
| 72 | |
| 73 | if (credentials.oauth) { |
| 74 | const oauth = pickOAuth(credentials.oauth, now, fileSource); |
| 75 | if (oauth) return oauth; |
| 76 | } |
| 77 | if (credentials.api_key) { |
| 78 | return { type: "api_key", key: credentials.api_key, source: fileSource }; |
| 79 | } |
| 80 | throw ErrNotConfigured(); |
| 81 | } |
| 82 | |
| 83 | /** Like `resolveCredential` but returns `null` instead of throwing `NOT_CONFIGURED`. */ |
| 84 | export async function tryResolveCredential( |
no test coverage detected