Skip to content

Instantly share code, notes, and snippets.

@gcnyin
Created September 19, 2020 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcnyin/da51affaca4a85cb1578f7c8e011d46f to your computer and use it in GitHub Desktop.
Save gcnyin/da51affaca4a85cb1578f7c8e011d46f to your computer and use it in GitHub Desktop.
replaceEncryptedFields
const replaceEncryptedFields = async (
// eslint-disable-next-line
obj: any,
kmsDecryptFn: (encryptedValue: string) => Promise<string>
): Promise<void> => {
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'string' && value.startsWith(KMS_PREFIX)) {
const encryptedValue = value.slice(KMS_PREFIX.length, value.length);
obj[key] = await kmsDecryptFn(encryptedValue);
} else if (typeof value === 'object') {
await replaceEncryptedFields(value, kmsDecryptFn);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment