-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathcodinitEnvVariables.ts
More file actions
65 lines (60 loc) · 2.08 KB
/
Copy pathcodinitEnvVariables.ts
File metadata and controls
65 lines (60 loc) · 2.08 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
import type { CodinitProject } from './types.js';
async function withRetries<T>(operation: () => Promise<T>, maxRetries: number = 3, retryDelay: number = 500) {
for (let i = 0; i < maxRetries; i++) {
try {
return await operation();
} catch (error) {
if (i === maxRetries - 1) {
throw error;
}
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
}
}
export async function queryEnvVariableWithRetries(project: CodinitProject, name: string) {
return withRetries(() => queryEnvVariable(project, name));
}
async function queryEnvVariable(project: CodinitProject, name: string): Promise<string | null> {
const response = await fetch(`${project.deploymentUrl}/api/query`, {
method: 'POST',
body: JSON.stringify({
path: '_system/cli/queryEnvironmentVariables:get',
format: 'codinit_encoded_json',
args: [{ name }],
}),
headers: {
'Content-Type': 'application/json',
Authorization: `CodinIT ${project.token}`,
},
});
if (!response.ok) {
throw new Error('Failed to query environment variables');
}
const respJSON: any = await response.json();
if (respJSON.status !== 'success') {
throw new Error(`Failed to query environment variables: ${JSON.stringify(respJSON)}`);
}
const udfResult = respJSON.value;
return udfResult && udfResult.value;
}
export async function setEnvVariablesWithRetries(project: CodinitProject, values: Record<string, string>) {
return withRetries(() => setEnvVariables(project, values));
}
async function setEnvVariables(project: CodinitProject, values: Record<string, string>) {
const response = await fetch(`${project.deploymentUrl}/api/update_environment_variables`, {
method: 'POST',
body: JSON.stringify({
changes: Object.entries(values).map(([name, value]) => ({
name,
value,
})),
}),
headers: {
'Content-Type': 'application/json',
Authorization: `CodinIT ${project.token}`,
},
});
if (!response.ok) {
throw new Error(`Failed to set environment variables: ${await response.text()}`);
}
}