| 84 | * ``` |
| 85 | */ |
| 86 | export function defineConfig< |
| 87 | KnownEncryptors extends Record<string, EncryptionConfig | ConfigProvider<EncryptionConfig>>, |
| 88 | >(config: { |
| 89 | default?: keyof KnownEncryptors |
| 90 | list: KnownEncryptors |
| 91 | }): ConfigProvider<ResolvedConfig<KnownEncryptors>> { |
| 92 | /** |
| 93 | * Encryption list should always be provided |
| 94 | */ |
| 95 | if (!config.list) { |
| 96 | throw new InvalidArgumentsException('Missing "list" property in encryption config') |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * The default encryption should be mentioned in the list |
| 101 | */ |
| 102 | if (config.default && !config.list[config.default]) { |
| 103 | throw new InvalidArgumentsException( |
| 104 | `Missing "list.${String( |
| 105 | config.default |
| 106 | )}" in encryption config. It is referenced by the "default" property` |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Config provider to lazily import drivers as they are used inside |
| 112 | * the user application |
| 113 | */ |
| 114 | return configProvider.create<ResolvedConfig<KnownEncryptors>>(async (app) => { |
| 115 | debug('resolving encryption config') |
| 116 | |
| 117 | const encryptorsList = Object.keys(config.list) |
| 118 | const encryptors = {} as Record<string, EncryptionConfig | ConfigProvider<EncryptionConfig>> |
| 119 | |
| 120 | for (let encryptorName of encryptorsList) { |
| 121 | const encryptor = config.list[encryptorName] |
| 122 | if ('resolver' in encryptor) { |
| 123 | encryptors[encryptorName] = await encryptor.resolver(app) |
| 124 | } else { |
| 125 | encryptors[encryptorName] = encryptor |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return { |
| 130 | default: config.default, |
| 131 | list: encryptors as ResolvedConfig<KnownEncryptors>['list'], |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Collection of encryption driver factory functions. |