File

lib/common/captcha/captcha.types.ts

Description

Interface representing the basic functionality for a CAPTCHA controller.

Index

Methods

Methods

getCaptcha
getCaptcha()

Generates a CAPTCHA data.

A promise that resolves to a CAPTCHA response.

validateCaptcha
validateCaptcha(payload: CaptchaRequest)

Validates the provided CAPTCHA response against the expected solution.

Parameters :
Name Type Optional Description
payload CaptchaRequest No
  • The CAPTCHA validation request.
Returns : Promise<literal type>

A promise that resolves to an object indicating whether the CAPTCHA validation was successful.

import { IsNotEmpty, IsString } from "class-validator";
import { Type as Class } from "@nestjs/common/interfaces/type.interface";

/**
 * Abstract class representing a CAPTCHA service.
 * @template CaptchaBody Type of data returned when generating CAPTCHA.
 */
export abstract class CaptchaService<CaptchaBody = any> {
  /**
   * Generates a CAPTCHA.
   * @returns {Promise<CaptchaBody>} A promise that resolves with the CAPTCHA data.
   */
  abstract generateCaptcha(): Promise<CaptchaBody>;

  /**
   * Validates a CAPTCHA.
   * @param {CaptchaRequest} request The data for validate the CAPTCHA.
   * @returns {Promise<boolean>} A promise that resolves with the validation result (true if the CAPTCHA is valid).
   */
  abstract validateCaptcha(request: CaptchaRequest): Promise<boolean>;
}

/**
 * Data transfer object for validate the CAPTCHA.
 * @template T Type of the CAPTCHA data.
 */
export class CaptchaRequest<T = any> {
  /**
   * The unique identifier for the CAPTCHA.
   */
  @IsString()
  @IsNotEmpty()
  id: string;

  /**
   * The data associated with the CAPTCHA request.
   */
  @IsNotEmpty()
  data: T;
}

/**
 * Type representing a CAPTCHA response for client side.
 */
export type CaptchaResponse = {
  /**
   * The type of the CAPTCHA.
   */
  type?: string;

  /**
   * The image data for the CAPTCHA.
   */
  image?: string;

  /**
   * The unique identifier for the CAPTCHA.
   */
  id?: string;

  /**
   * Whether the CAPTCHA is enabled in configuration.
   */
  enabled?: boolean;
};

/**
 * Interface representing the basic functionality for a CAPTCHA controller.
 */
export interface BasicCaptchaController {
  /**
   * Validates the provided CAPTCHA response against the expected solution.
   * @param payload - The CAPTCHA validation request.
   * @returns A promise that resolves to an object indicating whether the CAPTCHA validation was successful.
   */
  validateCaptcha(payload: CaptchaRequest): Promise<{ result: unknown }>;

  /**
   * Generates a CAPTCHA data.
   * @returns A promise that resolves to a CAPTCHA response.
   */
  getCaptcha(): Promise<CaptchaResponse>;
}

/**
 * Options for configuring the CAPTCHA module.
 */
export type CaptchaModuleOptions = {
  service: Class<CaptchaService>;
};

results matching ""

    No results matching ""