(config: Config)
| 6 | import { ExitCode } from '../errors/codes'; |
| 7 | |
| 8 | export async function resolveCredential(config: Config): Promise<ResolvedCredential> { |
| 9 | // 1. --api-key flag |
| 10 | if (config.apiKey) { |
| 11 | return { token: config.apiKey, method: 'api-key', source: 'flag' }; |
| 12 | } |
| 13 | |
| 14 | // 2. OAuth credentials in config file |
| 15 | const oauth = await loadCredentials(); |
| 16 | if (oauth) { |
| 17 | const token = await ensureFreshToken(oauth); |
| 18 | return { token, method: 'oauth', source: 'config.json' }; |
| 19 | } |
| 20 | |
| 21 | // 3. API key from config file |
| 22 | if (config.fileApiKey) { |
| 23 | return { token: config.fileApiKey, method: 'api-key', source: 'config.json' }; |
| 24 | } |
| 25 | |
| 26 | throw new CLIError( |
| 27 | 'No credentials found.', |
| 28 | ExitCode.AUTH, |
| 29 | 'Log in: mmx auth login\nPass directly: --api-key sk-xxxxx', |
| 30 | ); |
| 31 | } |
no test coverage detected