UNPKG

@shons/next-configify

Version:
60 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AwsParameterStoreConfigurationResolver = void 0; const client_ssm_1 = require("@aws-sdk/client-ssm"); class AwsParameterStoreConfigurationResolver { constructor(ssm) { this.ssm = ssm; this.AWS_PARAMETER_STORE_YAML_KEY = 'aws-parameter-store'; this.AWS_PARAMETER_STORE_ENV_PREFIX = 'AWS_PARAMETER_STORE'; } async resolve(config) { const parameters = this.filterConfiguration(config); const promises = this.buildBulkRequest(parameters); const results = await Promise.all(promises); const errors = results.filter((r) => !r.success); if (errors && errors.length) { throw new Error(`Unable to resolve parameter:\n${errors .map((e) => `${e.key}: ${e.id} - ${e.error.message}`) .join('\n')}`); } for (const result of results) { config[result.key] = result.value; } return config; } filterConfiguration(config) { return Object.fromEntries(Object.entries(config).filter(([key]) => key.startsWith(this.AWS_PARAMETER_STORE_YAML_KEY) || key.startsWith(this.AWS_PARAMETER_STORE_ENV_PREFIX))); } async resolveSecretValue(key, id) { try { const payload = { Name: id, WithDecryption: true }; const command = new client_ssm_1.GetParameterCommand(payload); const response = await this.ssm.send(command); return { id, key, value: response.Parameter?.Value, success: true, }; } catch (e) { return { id, key, error: e, success: false, }; } } buildBulkRequest(config) { const promises = []; for (const [key, value] of Object.entries(config)) { promises.push(this.resolveSecretValue(key, value)); } return promises; } } exports.AwsParameterStoreConfigurationResolver = AwsParameterStoreConfigurationResolver; //# sourceMappingURL=parameter-store-configuration.resolver.js.map