magicbell
Version:
MagicBell API wrapper
21 lines • 852 B
JavaScript
import crypto from 'crypto';
const dataKeys = ['userExternalId', 'userEmail', 'id', '_id', 'email'];
function getData(data) {
if (typeof data === 'string')
return data;
if (typeof data === 'object' && !data)
return '';
const key = dataKeys.find((key) => key in data && data[key]);
return key ? data[key] : '';
}
export function createHmac(secret, data) {
if (!crypto?.createHmac)
throw new Error('Your environment does not support crypto.createHmac');
if (!secret)
throw new Error(`You'll need to provide a secret to create an HMAC.`);
const msg = getData(data);
if (!msg || typeof msg !== 'string')
throw new Error(`You'll need to provide data to create an HMAC.`);
return crypto.createHmac('sha256', secret).update(msg).digest('base64');
}
//# sourceMappingURL=crypto.js.map