Skip to content

Commit

Permalink
Mailer-16: chore: cache the values from aws secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
maskeynihal committed May 28, 2024
1 parent 6c569c3 commit 490d283
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
27 changes: 16 additions & 11 deletions src/config/mail.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import getSecret from '../strategies/secretStrategies/secretStrategy';
import storageProvider, {
MAIL_CONFIG_KEYS
} from '../providers/awsConfigStorage.provider';

const getMailConfig = async () => {
const keys = [
'MAIL_FROM_ADDRESS',
'MAIL_FROM_NAME',
'MAIL_SERVER_HOST',
'MAIL_PASSWORD'
];
if (!storageProvider.size) {
const [from, name, host, password] = await Promise.all(
Object.keys(MAIL_CONFIG_KEYS).map(getSecret)
);

const [from, name, host, password] = await Promise.all(keys.map(getSecret));
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_FROM_ADDRESS, from);
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_FROM_NAME, name);
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_SERVER_HOST, host);
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_PASSWORD, password);
}

const mailConfig = {
from,
name,
from: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_FROM_ADDRESS),
name: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_FROM_NAME),
mailServer: {
host,
password
host: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_SERVER_HOST),
password: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_PASSWORD)
}
};

Expand Down
10 changes: 10 additions & 0 deletions src/providers/awsConfigStorage.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const MAIL_CONFIG_KEYS = {
MAIL_FROM_ADDRESS: 'MAIL_FROM_ADDRESS' as const,
MAIL_FROM_NAME: 'MAIL_FROM_NAME' as const,
MAIL_SERVER_HOST: 'MAIL_SERVER_HOST' as const,
MAIL_PASSWORD: 'MAIL_PASSWORD' as const
};

type MAIL_CONFIG_KEYS = keyof typeof MAIL_CONFIG_KEYS;

export default new Map<MAIL_CONFIG_KEYS, string>();
4 changes: 1 addition & 3 deletions src/strategies/secretStrategies/secretStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import type { IMailerSecretKey, IMailerSecretKeys } from './secrets';

async function getSecret(
key: IMailerSecretKey
): Promise<IMailerSecretKeys[typeof key]> {
): Promise<IMailerSecretKeys[IMailerSecretKey]> {
const awsSecret = AwsSecret.getSecrets(key);

console.log({ awsSecret, key });

if (awsSecret) {
return awsSecret;
}
Expand Down

0 comments on commit 490d283

Please sign in to comment.