Last active
July 4, 2022 15:50
-
-
Save cyppan/9355cfe440979a10761c8fec9752c7e2 to your computer and use it in GitHub Desktop.
Typescript Config Builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SsmParameter { | |
ssmName: string; | |
ssmType: "String" | "SecureString"; | |
configType?: "string" | "number" | "boolean"; | |
} | |
class SsmConfig<Config, C extends Record<string, SsmParameter> = {}> { | |
constructor(private config: C) {} | |
static builder<Config>(): SsmConfig<Config, {}> { | |
return new SsmConfig<Config, {}>({}); | |
} | |
key<K extends keyof Config>( | |
configName: K, | |
ssmParameter: SsmParameter | |
): SsmConfig<Config, C & { [k in K]: SsmParameter }> { | |
return new SsmConfig({ | |
...this.config, | |
...({ [configName]: ssmParameter } as { | |
[k in K]: SsmParameter; | |
}), | |
}); | |
} | |
build(): { [K in keyof C]: C[K] } { | |
return null as any; | |
} | |
} | |
// usage | |
interface Config { | |
bucketName: string; | |
awsAccessKeyId: string; | |
awsSecretKey: string; | |
jwtSecret: string | |
} | |
const config: Config = SsmConfig.builder<Config>() | |
.key("bucketName", { ssmName: "bucket-name-media", ssmType: "String" }) | |
.key("awsAccessKeyId", {ssmName: "/api/aws-access-key", ssmType: "String"}) | |
.key("awsSecretKey", {ssmName: "/api/aws-secret-key", ssmType: "SecureString"}) | |
.key("jwtSecret", {ssmName: "jwt-secret", ssmType: "SecureString"}) | |
.build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment