-
Notifications
You must be signed in to change notification settings - Fork 6
/
parameter.ts
63 lines (51 loc) · 1.93 KB
/
parameter.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
53
54
55
56
57
58
59
60
61
62
63
import { SSM } from 'aws-sdk';
import { Refreshable } from './refreshable';
export interface ParameterProps {
/** The name of the parameter you want to query. */
name: string;
/** The parameter version. */
version?: string;
/** Return decrypted values for secure string parameters. This flag is ignored for String and StringList parameter types. */
withDecryption?: boolean;
/** The maximum amount of time in milliseconds a parameter will be considered fresh */
maxAge?: number;
/** https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#constructor-property */
ssmConfiguration?: SSM.ClientConfiguration;
}
export class Parameter extends Refreshable {
public readonly name: string;
private readonly withDecryption: boolean;
private readonly ssmClient: SSM;
private cachedResult: Promise<SSM.GetParameterResult>;
constructor(props: ParameterProps) {
super(props.maxAge);
this.name = props.version ? props.name + ':' + props.version : props.name;
this.withDecryption = props.withDecryption || true;
this.ssmClient = new SSM(props.ssmConfiguration);
}
public get value(): Promise<string | string[]> {
if (!this.cachedResult || this.isExpired()) {
this.refresh();
}
return new Promise((resolve, reject) => {
this.cachedResult.then(data => {
if (data.Parameter && data.Parameter.Value) {
return data.Parameter.Type === 'StringList'
? resolve(data.Parameter.Value.split(','))
: resolve(data.Parameter.Value);
}
return reject(`The value is missing for parameter ${this.name}`);
});
});
}
protected refreshParameter(): void {
this.cachedResult = this.getParameter(this.name, this.withDecryption);
}
private getParameter(name: string, withDecryption: boolean) {
const params = {
Name: name,
WithDecryption: withDecryption
};
return this.ssmClient.getParameter(params).promise();
}
}