UtilsCaptcha.mjs

import svgCaptcha from "svg-captcha";

/**
 * Utility functions for captcha generation.
 * @module Captcha
 */

/**
 * @typedef {Object} ReturnGenerateSvgCaptcha
 * @property {string} text - The text of the mathematical expression.
 * @property {string} data - The SVG data of the captcha image.
 */

/**
 * Generates an SVG captcha with a mathematical expression.
 *
 * @param {Object} options - Options for generating the captcha.
 * @param {number} [options.width=150] - The width of the captcha image.
 * @param {number} [options.height=50] - The height of the captcha image.
 * @returns {ReturnGenerateSvgCaptcha} An object containing the captcha text and data.
 *
 * @example
 * import { generateSvgCaptcha } from "nsuite";
 * const { text, data } = await generateSvgCaptcha({
 *   width: 148,
 *   height: 48,
 * });
 */
export function generateSvgCaptcha({ width = 150, height = 50 }) {
  const captcha = svgCaptcha.createMathExpr({
    mathMin: 1,
    mathMax: 9,
    mathOperator: "+-",
    background: "#ffffff",
    color: true,
    width,
    height,
  });

  return {
    text: captcha.text,
    data: captcha.data,
  };
}