-
Notifications
You must be signed in to change notification settings - Fork 4
/
doppler.js
57 lines (53 loc) · 1.82 KB
/
doppler.js
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
import https from "https";
import { VERSION } from "./meta.js";
/**
* Fetch secrets from Doppler the API
* @param {string} dopplerToken
* @param {string | null} [dopplerProject]
* @param {string | null} [dopplerConfig]
* @returns {() => Promise<Record<string, Record>>}
*/
async function fetch(dopplerToken, dopplerProject, dopplerConfig) {
return new Promise(function (resolve, reject) {
const encodedAuthData = Buffer.from(`${dopplerToken}:`).toString("base64");
const authHeader = `Basic ${encodedAuthData}`;
const userAgent = `secrets-fetch-github-action/${VERSION}`;
const url = new URL("https://api.doppler.com/v3/configs/config/secrets");
if (dopplerProject && dopplerConfig) {
url.searchParams.append("project", dopplerProject);
url.searchParams.append("config", dopplerConfig);
}
https
.get(
url.href,
{
headers: {
Authorization: authHeader,
"user-agent": userAgent,
"accepts": "application/json",
},
},
(res) => {
let payload = "";
res.on("data", (data) => (payload += data));
res.on("end", () => {
if (res.statusCode === 200) {
resolve(JSON.parse(payload).secrets);
} else {
try {
const error = JSON.parse(payload).messages.join(" ");
reject(new Error(`Doppler API Error: ${error}`));
} catch (error) {
// In the event an upstream issue occurs and no JSON payload is supplied
reject(new Error(`Doppler API Error: ${res.statusCode} ${res.statusMessage}`));
}
}
});
}
)
.on("error", (error) => {
reject(new Error(`Doppler API Error: ${error}`));
});
});
}
export default fetch;