UNPKG

novu

Version:

Novu CLI. Run Novu Studio and sync workflows with Novu Cloud

49 lines (48 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sync = sync; exports.executeSync = executeSync; exports.buildSignature = buildSignature; exports.buildHmac = buildHmac; const axios_1 = __importDefault(require("axios")); const crypto_1 = require("crypto"); async function sync(bridgeUrl, secretKey, apiUrl) { if (!bridgeUrl) { throw new Error('A bridge URL is required for the sync command, please supply it when running the command'); } if (!secretKey) { throw new Error('A secret key is required for the sync command, please supply it when running the command'); } if (!apiUrl) { throw new Error('An API url is required for the sync command, please omit the configuration option entirely or supply a valid API url when running the command'); } const syncResult = await executeSync(apiUrl, bridgeUrl, secretKey); if (syncResult.status >= 400) { console.error(new Error(JSON.stringify(syncResult.data))); process.exit(1); } return syncResult.data; } async function executeSync(apiUrl, bridgeUrl, secretKey) { const url = `${apiUrl}/v1/bridge/sync?source=cli`; return await axios_1.default.post(url, { bridgeUrl, }, { headers: { 'Content-Type': 'application/json', Authorization: `ApiKey ${secretKey}`, }, }); } function buildSignature(secretKey) { const timestamp = Date.now(); return `t=${timestamp},v1=${buildHmac(secretKey, timestamp)}`; } function buildHmac(secretKey, timestamp) { return (0, crypto_1.createHmac)('sha256', secretKey) .update(`${timestamp}.${JSON.stringify({})}`) .digest('hex'); }