Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.2 KB
Raw Blame
const logTypeToEnv = {
  alerts: process.env.DUB_SLACK_HOOK_ALERTS,
  cron: process.env.DUB_SLACK_HOOK_CRON,
  errors: process.env.DUB_SLACK_HOOK_ERRORS,
  links: process.env.DUB_SLACK_HOOK_LINKS,
};

export const log = async ({
  message,
  type,
  mention = false,
}: {
  message: string;
  type: "alerts" | "cron" | "errors" | "links";
  mention?: boolean;
}) => {
  /* Log a message to the console */
  console.log(message);

  if (process.env.VERCEL_ENV !== "production") {
    console.log("Skipping log to Dub Slack in non-production environment.");
    return;
  }

  const HOOK = logTypeToEnv[type];
  if (!HOOK) return;
  try {
    return await fetch(HOOK, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        blocks: [
          {
            type: "section",
            text: {
              type: "mrkdwn",
              // prettier-ignore
              text: `${mention ? "<@U0404G6J3NJ> " : ""}${(type === "alerts" || type === "errors") ? ":alert: " : ""}${message}`,
            },
          },
        ],
      }),
    });
  } catch (e) {
    console.log(`Failed to log to Dub Slack. Error: ${e}`);
  }
};