-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyepcodeEnv.ts
More file actions
85 lines (70 loc) · 1.89 KB
/
Copy pathyepcodeEnv.ts
File metadata and controls
85 lines (70 loc) · 1.89 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
import { YepCodeApi, YepCodeApiConfig, TeamVariable } from "../api";
import { EnvVar } from "../types";
export class YepCodeEnv {
private yepCodeApi: YepCodeApi;
constructor(config: YepCodeApiConfig = {}) {
this.yepCodeApi = new YepCodeApi(config);
}
getTeamId(): string {
return this.yepCodeApi.getTeamId();
}
private async _getVariable(key: string): Promise<TeamVariable | undefined> {
const variables = await this._getVariables();
return variables.find((v) => v.key === key);
}
private async _getVariables(): Promise<TeamVariable[]> {
let page = 0;
const limit = 100;
let allVariables: TeamVariable[] = [];
while (true) {
const { hasNextPage, data: variables } =
await this.yepCodeApi.getVariables({
page,
limit,
});
if (variables) {
allVariables = allVariables.concat(variables);
}
if (!hasNextPage) {
break;
}
page++;
}
return allVariables
.sort((a, b) => a.key.localeCompare(b.key))
.map(({ id, key, value, isSensitive }) => ({
id,
key,
value,
isSensitive,
}));
}
async getEnvVars(): Promise<EnvVar[]> {
const variables = await this._getVariables();
return variables.map(({ key, value }) => ({
key,
value,
}));
}
async setEnvVar(
key: string,
value: string,
isSensitive: boolean = true
): Promise<void> {
const existingVar = await this._getVariable(key);
if (existingVar) {
await this.yepCodeApi.updateVariable(existingVar.id, {
key,
value,
});
} else {
await this.yepCodeApi.createVariable({ key, value, isSensitive });
}
}
async delEnvVar(key: string): Promise<void> {
const existingVar = await this._getVariable(key);
if (existingVar) {
await this.yepCodeApi.deleteVariable(existingVar.id);
}
}
}