MCPcopy Create free account
hub / github.com/tiann/hapi / initializeToken

Function initializeToken

cli/src/ui/tokenInit.ts:21–51  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19 * Must be called before any API operations
20 */
21export async function initializeToken(): Promise<void> {
22 // Initialize API URL first (env > settings.json > default)
23 await initializeApiUrl()
24
25 // 1. Environment variable has highest priority (allows temporary override)
26 if (configuration.cliApiToken) {
27 return
28 }
29
30 // 2. Read from settings file
31 const settings = await readSettings()
32 if (settings.cliApiToken) {
33 configuration._setCliApiToken(settings.cliApiToken)
34 return
35 }
36
37 // 3. Non-TTY environment cannot prompt, fail with clear error
38 if (!process.stdin.isTTY) {
39 throw new Error('CLI_API_TOKEN is required. Set it via environment variable or run `hapi auth login`.')
40 }
41
42 // 4. Interactive prompt
43 const token = await promptForToken()
44
45 // 5. Save and update configuration
46 await updateSettings(current => ({
47 ...current,
48 cliApiToken: token
49 }))
50 configuration._setCliApiToken(token)
51}
52
53async function promptForToken(): Promise<string> {
54 const rl = readline.createInterface({ input, output })

Callers 8

resume.tsFile · 0.90
claude.tsFile · 0.90
codex.tsFile · 0.90
opencode.tsFile · 0.90
gemini.tsFile · 0.90
kimi.tsFile · 0.90
cursor.tsFile · 0.90
runner.tsFile · 0.90

Calls 5

initializeApiUrlFunction · 0.90
readSettingsFunction · 0.90
updateSettingsFunction · 0.90
promptForTokenFunction · 0.85
_setCliApiTokenMethod · 0.45

Tested by

no test coverage detected