slack-logs
Version:
slack-logs
2 lines (1 loc) • 1.71 kB
JavaScript
;function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});var t,o,r=e(require("axios"));exports.LogLevel=void 0,(t=exports.LogLevel||(exports.LogLevel={})).DEFAULT="DEFAULT",t.SUCCESS="SUCCESS",t.INFO="INFO",t.WARN="WARN",t.ERROR="ERROR",exports.LogColor=void 0,(o=exports.LogColor||(exports.LogColor={})).DEFAULT="#B4B4B8",o.SUCCESS="#65B741",o.INFO="#40A2D8",o.WARN="#E3651D",o.ERROR="#FF0000";const s={async log(e,t){const o=JSON.stringify(t);if(!n())return console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null;let r={text:`*${e}:* ${o}`};await l(r)},async logBlockMessage(e,t,o=exports.LogLevel.DEFAULT){if(!n())return console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null;console.log(o);const r=exports.LogColor[o],s=[],i=[],c=[];s.push({type:"divider"}),s.push({type:"header",text:{type:"plain_text",text:e,emoji:!0}}),s.push({type:"divider"}),t?.length&&t.forEach((e=>{let t=JSON.stringify(e.value);c.push({type:"section",text:{type:"mrkdwn",text:`*${e.title}:* ${t}`}})})),i.push({color:r,blocks:c});let a={text:e,blocks:s,attachments:i};await l(a)}};function n(){const e=process.env.SLACK_WEBHOOK_URL;return!(!e||null==e||void 0===e)&&e.startsWith("https://")}async function l(e){if("true"===(process?.env?.ENABLE_SLACK_LOGS??"").toString())return!1;const t=process.env.SLACK_WEBHOOK_URL;return await r.default.post(t,JSON.stringify(e),{headers:{"Content-Type":"application/json"}}).catch((e=>{console.error("🚨 Error sending log message to Slack: Webhook URL might be updated!")}))}exports.slack=s;