envis-node 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +3 -1
- package/src/main.js +103 -24
- package/src/session.js +2 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { get, logout } = require("./main");
|
|
1
|
+
const { get, getMany, getAll, logout } = require("./main");
|
|
2
2
|
const {
|
|
3
3
|
ensureSession,
|
|
4
4
|
loadSession,
|
|
@@ -10,6 +10,8 @@ const {
|
|
|
10
10
|
|
|
11
11
|
const envis = {
|
|
12
12
|
get,
|
|
13
|
+
getMany,
|
|
14
|
+
getAll,
|
|
13
15
|
logout,
|
|
14
16
|
ensureSession,
|
|
15
17
|
loadSession,
|
package/src/main.js
CHANGED
|
@@ -28,26 +28,19 @@ async function logout() {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
async function
|
|
32
|
-
if (!projectId || !secretName) {
|
|
33
|
-
throw new Error("Both project id and secret name are required.");
|
|
34
|
-
}
|
|
35
|
-
|
|
31
|
+
async function buildAuthHeaders() {
|
|
36
32
|
await ensureSession();
|
|
37
33
|
const session = await loadSession();
|
|
38
|
-
|
|
39
|
-
const headers = {
|
|
34
|
+
return {
|
|
40
35
|
Authorization: `Bearer ${session.access_token}`,
|
|
41
36
|
"Content-Type": "application/json",
|
|
42
37
|
};
|
|
38
|
+
}
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
projectId
|
|
46
|
-
)}/secrets/${encodeURIComponent(secretName)}`;
|
|
47
|
-
|
|
40
|
+
async function fetchJSON(url, options, errorAction, errorContext) {
|
|
48
41
|
let response;
|
|
49
42
|
try {
|
|
50
|
-
response = await fetch(url, {
|
|
43
|
+
response = await fetch(url, { timeout: 10000, ...options });
|
|
51
44
|
} catch (error) {
|
|
52
45
|
throw new Error(`Failed to reach Envault API: ${error.message}`);
|
|
53
46
|
}
|
|
@@ -60,7 +53,7 @@ async function get(projectId, secretName) {
|
|
|
60
53
|
detail = error.message;
|
|
61
54
|
}
|
|
62
55
|
throw new Error(
|
|
63
|
-
`Failed to
|
|
56
|
+
`Failed to ${errorAction} (${response.status || "HTTP error"}): ${
|
|
64
57
|
detail || "No response body provided."
|
|
65
58
|
}`
|
|
66
59
|
);
|
|
@@ -75,32 +68,118 @@ async function get(projectId, secretName) {
|
|
|
75
68
|
|
|
76
69
|
if (!bodyText) {
|
|
77
70
|
throw new Error(
|
|
78
|
-
|
|
71
|
+
`API returned an empty, non-JSON response when ${errorContext || errorAction}.`
|
|
79
72
|
);
|
|
80
73
|
}
|
|
81
74
|
|
|
82
75
|
try {
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
payload &&
|
|
86
|
-
typeof payload === "object" &&
|
|
87
|
-
Object.prototype.hasOwnProperty.call(payload, "value")
|
|
88
|
-
) {
|
|
89
|
-
return payload.value;
|
|
90
|
-
}
|
|
91
|
-
return payload;
|
|
76
|
+
return JSON.parse(bodyText);
|
|
92
77
|
} catch {
|
|
93
78
|
const raw = bodyText.trim();
|
|
94
79
|
if (raw) {
|
|
95
80
|
return { raw };
|
|
96
81
|
}
|
|
97
82
|
throw new Error(
|
|
98
|
-
|
|
83
|
+
`API returned an empty, non-JSON response when ${errorContext || errorAction}.`
|
|
99
84
|
);
|
|
100
85
|
}
|
|
101
86
|
}
|
|
102
87
|
|
|
88
|
+
async function get(projectId, secretName) {
|
|
89
|
+
if (!projectId || !secretName) {
|
|
90
|
+
throw new Error("Both project id and secret name are required.");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const headers = await buildAuthHeaders();
|
|
94
|
+
|
|
95
|
+
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
96
|
+
projectId
|
|
97
|
+
)}/secrets/${encodeURIComponent(secretName)}`;
|
|
98
|
+
|
|
99
|
+
const payload = await fetchJSON(
|
|
100
|
+
url,
|
|
101
|
+
{ headers },
|
|
102
|
+
"fetch secret",
|
|
103
|
+
"fetching secret"
|
|
104
|
+
);
|
|
105
|
+
if (
|
|
106
|
+
payload &&
|
|
107
|
+
typeof payload === "object" &&
|
|
108
|
+
Object.prototype.hasOwnProperty.call(payload, "value")
|
|
109
|
+
) {
|
|
110
|
+
return payload.value;
|
|
111
|
+
}
|
|
112
|
+
return payload;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function getMany(projectId, secretNames) {
|
|
116
|
+
if (!projectId || !Array.isArray(secretNames)) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"Project id and an array of secret names are required."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const names = secretNames
|
|
122
|
+
.map((name) => (typeof name === "string" ? name.trim() : ""))
|
|
123
|
+
.filter(Boolean);
|
|
124
|
+
if (names.length === 0) {
|
|
125
|
+
throw new Error("At least one secret name is required.");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const headers = await buildAuthHeaders();
|
|
129
|
+
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
130
|
+
projectId
|
|
131
|
+
)}/secrets/batch`;
|
|
132
|
+
|
|
133
|
+
const payload = await fetchJSON(
|
|
134
|
+
url,
|
|
135
|
+
{
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers,
|
|
138
|
+
body: JSON.stringify({ names }),
|
|
139
|
+
},
|
|
140
|
+
"fetch secrets",
|
|
141
|
+
"fetching secrets"
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
if (
|
|
145
|
+
payload &&
|
|
146
|
+
typeof payload === "object" &&
|
|
147
|
+
Object.prototype.hasOwnProperty.call(payload, "secrets")
|
|
148
|
+
) {
|
|
149
|
+
return payload.secrets;
|
|
150
|
+
}
|
|
151
|
+
return payload;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function getAll(projectId) {
|
|
155
|
+
if (!projectId) {
|
|
156
|
+
throw new Error("Project id is required.");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const headers = await buildAuthHeaders();
|
|
160
|
+
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
161
|
+
projectId
|
|
162
|
+
)}/secrets/all`;
|
|
163
|
+
|
|
164
|
+
const payload = await fetchJSON(
|
|
165
|
+
url,
|
|
166
|
+
{ headers },
|
|
167
|
+
"fetch secrets",
|
|
168
|
+
"fetching secrets"
|
|
169
|
+
);
|
|
170
|
+
if (
|
|
171
|
+
payload &&
|
|
172
|
+
typeof payload === "object" &&
|
|
173
|
+
Object.prototype.hasOwnProperty.call(payload, "secrets")
|
|
174
|
+
) {
|
|
175
|
+
return payload.secrets;
|
|
176
|
+
}
|
|
177
|
+
return payload;
|
|
178
|
+
}
|
|
179
|
+
|
|
103
180
|
module.exports = {
|
|
104
181
|
get,
|
|
182
|
+
getMany,
|
|
183
|
+
getAll,
|
|
105
184
|
logout,
|
|
106
185
|
};
|
package/src/session.js
CHANGED
|
@@ -30,12 +30,12 @@ function getEnvUrl(varName, fallback) {
|
|
|
30
30
|
|
|
31
31
|
const BASE_URL = getEnvUrl(
|
|
32
32
|
"ENVIS_API_URL",
|
|
33
|
-
"https://
|
|
33
|
+
"https://api.envisible.dev"
|
|
34
34
|
);
|
|
35
35
|
|
|
36
36
|
const FRONTEND_URL = getEnvUrl(
|
|
37
37
|
"ENVIS_DASH_URL",
|
|
38
|
-
"https://envisible.
|
|
38
|
+
"https://envisible.dev"
|
|
39
39
|
);
|
|
40
40
|
|
|
41
41
|
async function pathExists(targetPath) {
|