-
Notifications
You must be signed in to change notification settings - Fork 967
Expand file tree
/
Copy pathapi.ts
More file actions
138 lines (119 loc) · 3.65 KB
/
Copy pathapi.ts
File metadata and controls
138 lines (119 loc) · 3.65 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as boxen from 'boxen'
import * as e2b from 'e2b'
import { getUserConfig, UserConfig } from './user'
import { asBold, asPrimary } from './utils/format'
export let apiKey = process.env.E2B_API_KEY
export let accessToken = process.env.E2B_ACCESS_TOKEN
export const teamId = process.env.E2B_TEAM_ID
function resolveAPIKey() {
// If apiKey is not already set (either from env var or from user config), try to get it from config file
if (!apiKey) {
const userConfig = getUserConfig()
apiKey = userConfig?.teamApiKey
}
return apiKey
}
function resolveAccessToken() {
// If accessToken is not already set (either from env var or from user config), try to get it from config file
if (!accessToken) {
const userConfig = getUserConfig()
accessToken = userConfig?.accessToken
}
return accessToken
}
const authErrorBox = (keyName: string) => {
let link
let msg
switch (keyName) {
case 'E2B_API_KEY':
link = 'https://e2b.dev/dashboard?tab=keys'
msg = 'API key'
break
case 'E2B_ACCESS_TOKEN':
link = 'https://e2b.dev/dashboard?tab=personal'
msg = 'access token'
break
}
// throwing error in default in switch statement results in unreachable code,
// so we need to check if link and msg are defined here instead
if (!link || !msg) {
throw new Error(`Unknown key name: ${keyName}`)
}
return boxen.default(
`You must be logged in to use this command. Run ${asBold('e2b auth login')}.
If you are seeing this message in CI/CD you may need to set the ${asBold(
`${keyName}`
)} environment variable.
Visit ${asPrimary(link)} to get the ${msg}.`,
{
width: 70,
float: 'center',
padding: 0.5,
margin: 1,
borderStyle: 'round',
borderColor: 'redBright',
}
)
}
export function ensureAPIKey() {
const resolvedApiKey = resolveAPIKey()
if (!resolvedApiKey) {
console.error(authErrorBox('E2B_API_KEY'))
process.exit(1)
} else {
return resolvedApiKey
}
}
export function ensureUserConfig(): UserConfig {
const userConfig = getUserConfig()
if (!userConfig) {
console.error('No user config found, run `e2b auth login` to log in first.')
process.exit(1)
}
return userConfig
}
export function ensureAccessToken() {
const resolvedAccessToken = resolveAccessToken()
if (!resolvedAccessToken) {
console.error(authErrorBox('E2B_ACCESS_TOKEN'))
process.exit(1)
} else {
return resolvedAccessToken
}
}
export function ensureAPIKeyOrAccessToken() {
const resolvedApiKey = resolveAPIKey()
const resolvedAccessToken = resolveAccessToken()
if (resolvedApiKey || resolvedAccessToken) {
return { apiKey: resolvedApiKey, accessToken: resolvedAccessToken }
}
console.error(authErrorBox('E2B_API_KEY'))
process.exit(1)
}
/**
* Resolve team ID with proper precedence:
* 1. CLI --team flag
* 2. E2B_TEAM_ID env var
* 3. Local e2b.toml team_id (if provided)
* 4. ~/.e2b/config.json teamId (only if E2B_API_KEY env var is NOT set,
* to avoid mismatch between env var API key and config file team ID)
*/
export function resolveTeamId(
cliTeamId?: string,
localConfigTeamId?: string
): string | undefined {
if (cliTeamId) return cliTeamId
if (teamId) return teamId
if (localConfigTeamId) return localConfigTeamId
if (!process.env.E2B_API_KEY) {
const config = getUserConfig()
return config?.teamId
}
return undefined
}
const userConfig = getUserConfig()
export const connectionConfig = new e2b.ConnectionConfig({
accessToken: process.env.E2B_ACCESS_TOKEN || userConfig?.accessToken,
apiKey: process.env.E2B_API_KEY || userConfig?.teamApiKey,
})
export const client = new e2b.ApiClient(connectionConfig)