-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.ts
52 lines (44 loc) · 1.32 KB
/
config.ts
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
import { SSMClient, GetParametersCommand } from "@aws-sdk/client-ssm";
type Config = Record<string, string>;
let state: Config;
const AWS_REGION = "eu-west-2";
const parameterPathPrefix = "/pocketToPinboard/";
const makeParameterPaths = (parameterNames: string[]): string[] =>
parameterNames.map(
(parameterName) => `${parameterPathPrefix}${parameterName}`
);
const ssmClient = new SSMClient({
region: AWS_REGION,
});
const parameterPaths = makeParameterPaths([
"pocketAccessToken",
"pocketConsumerKey",
"pinboardToken",
]);
const fetchConfigFromSSM = async (): Promise<Config> => {
if (state) {
return Promise.resolve(state);
}
state = {};
const command = new GetParametersCommand({
Names: parameterPaths,
WithDecryption: true,
});
const response = await ssmClient.send(command);
response.Parameters?.forEach((parameter) => {
const maybeParameterName = parameter.Name;
if (maybeParameterName !== undefined) {
const parameterName = maybeParameterName.replace(parameterPathPrefix, "");
state[parameterName] = parameter.Value!;
}
});
return state;
};
export const getConfigItem = async (key: string): Promise<string> => {
const conf: Config = await fetchConfigFromSSM();
if (conf[key]) {
return conf[key];
} else {
throw new Error(`No value for key ${key}`);
}
};