-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathresolver.ts
More file actions
36 lines (32 loc) · 1.1 KB
/
Copy pathresolver.ts
File metadata and controls
36 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { Config } from '../config/schema';
import type { ResolvedCredential } from './types';
import { loadCredentials } from './credentials';
import { ensureFreshToken } from './refresh';
import { CLIError } from '../errors/base';
import { ExitCode } from '../errors/codes';
import { buildNoCredentialsHint } from './hints';
export async function resolveCredential(config: Config): Promise<ResolvedCredential> {
// 1. Explicit API key (--api-key takes precedence over MINIMAX_API_KEY)
if (config.apiKey) {
return {
token: config.apiKey,
method: 'api-key',
source: config.apiKeySource ?? 'flag',
};
}
// 2. OAuth credentials in config file
const oauth = await loadCredentials();
if (oauth) {
const token = await ensureFreshToken(oauth);
return { token, method: 'oauth', source: 'config.json' };
}
// 3. API key from config file
if (config.fileApiKey) {
return { token: config.fileApiKey, method: 'api-key', source: 'config.json' };
}
throw new CLIError(
'No credentials found.',
ExitCode.AUTH,
buildNoCredentialsHint(config.configPath),
);
}