@captchahub/core

中文文档
1.0.11发布于 2026/08/29TypeScriptES Module零依赖近 30 天下载 6560

CaptchaHub Core is a TypeScript library that serves as the foundational package for the CaptchaHub ecosystem, providing essential classes, interfaces, and tools for handling and processing captchas within web applications. This package enables seamles

原始使用文档
文档目录10 节
  1. Installation
  2. Features
  3. Usage
  4. Getting the Captcha Script
  5. Getting the Captcha Form Element
  6. Verifying the Captcha
  7. Configuration
  8. Adding New Providers
  9. Rationale
  10. Contributing

CaptchaHub Core

CaptchaHub Core is a TypeScript library that serves as the foundational package for the CaptchaHub ecosystem, providing essential classes, interfaces, and tools for handling and processing captchas within web applications. This package enables seamless integration with various captcha providers, including ReCaptcha, HCaptcha, and Turnstile, ensuring a consistent and streamlined approach to captcha implementation.

Installation

Install the package using pnpm:

pnpm add @captchahub/core

Features

  • Unified Integration: CaptchaHub Core provides a unified interface for integrating multiple captcha providers.
  • Extensible: Easily extend the core functionality to support additional captcha services.
  • Nodeless (Serverless) Support: Designed to work seamlessly in nodeless (serverless) environments, making it ideal for modern web applications.

Usage

Getting the Captcha Script

The getCaptchaScript function can be used as follows:

import { getCaptchaScript } from '@captchahub/core';

const config = {
    CAPTCHA_PROVIDER: 'your-provider', // Specify the captcha provider
    // Additional provider-specific configuration can go here
};

const CaptchaComponent = () => {
    const CaptchaScript = getCaptchaScript(config);

    return (
        <div>
            <CaptchaScript />
            {/* Additional form elements */}
        </div>
    );
};

Getting the Captcha Form Element

The getCaptchaFormElement function can be used as follows:

import { getCaptchaFormElement } from '@captchahub/core';

const config = {
    CAPTCHA_PROVIDER: 'your-provider', // Specify the captcha provider
    // Additional provider-specific configuration can go here
};

const CaptchaFormComponent = () => {
    const CaptchaFormElement = getCaptchaFormElement(config);

    return (
        <div>
            <CaptchaFormElement />
            {/* Additional form elements */}
        </div>
    );
};

Verifying the Captcha

The verifyCaptcha function can be used as follows:

import { verifyCaptcha } from '@captchahub/core';

const config = {
    CAPTCHA_PROVIDER: 'your-provider', // Specify the captcha provider
    // Additional provider-specific configuration can go here
};

const formFieldValues = {
    // Your form field values that need to be verified
};

const isValid = await verifyCaptcha(formFieldValues, config);
if (isValid) {
    console.log('Captcha verified successfully!');
} else {
    console.error('Captcha verification failed.');
}

Configuration

Configuration for the captcha provider can be defined in a configuration object passed to the factory when creating the captcha instance. The configuration object must include the CAPTCHA_PROVIDER property to specify which captcha provider to use. Additional properties may be required depending on the specific provider.

Adding New Providers

To add a new captcha provider, you can implement the CaptchaInterface. Here’s a brief example of how to implement a new provider:

  1. Create a New Folder: Create a new folder for your provider (e.g., packages/myprovider).

  2. Implement the Interface: Create a class that implements the CaptchaInterface. This class should handle the logic for verifying captchas and rendering the captcha elements.

    Example:

    import type { CaptchaInterface } from '@captchahub/core';
    
    export class MyProviderCaptcha implements CaptchaInterface {
        async verifyCaptcha(formFieldValues: Record<string, any>): Promise<boolean> {
            // Implement the logic to verify the captcha
        }
    
        getCaptchaScript(): React.FC {
            // Return the captcha script as a React component
        }
    
        getCaptchaFormElement(): React.FC {
            // Return the captcha form element as a React component
        }
    }
    
  3. Self-Registering the Package: To make your package self-registering, ensure your package registers the factory with the CaptchaFactoryRegistry and exports the factory class as the default export in index.tsx. For example:

import { CaptchaFactoryRegistry } from '@captchahub/core';
import { MyProviderCaptchaFactory } from './Factory';

CaptchaFactoryRegistry.registerCaptchaFactory('my-provider', new MyProviderCaptchaFactory());

// default export so that core factory can use it directly
export default MyProviderCaptchaFactory;

Rationale

The CaptchaHub Core package is designed to provide a consistent and extensible foundation for managing captcha workflows in TypeScript applications. By using well-defined interfaces and tools, developers can easily integrate captcha capabilities into their web applications.

Contributing

Contributions are welcome! Feel free to fork this project and submit pull requests. Before submitting, please ensure your code passes all linting and unit tests.

You can format code using:

pnpm format

You can run unit tests using:

pnpm test

版本与发布记录

@captchahub/core 共发布 11 个版本,首次发布于 2024/11/24;过去 12 个月发布了 4 个版本。

版本发布日期距今
1.0.11latest2026/08/292 个月前
1.0.102026/08/063 个月前
1.0.92026/03/297 个月前
1.0.82026/03/277 个月前
1.0.62025/08/021.3 年前
1.0.52025/03/031.7 年前

关于 @captchahub/core 的常见问题

@captchahub/core 是什么?

CaptchaHub Core is a TypeScript library that serves as the foundational package for the CaptchaHub ecosystem, providing essential classes, interfaces, and tools for handling and processing captchas within web applications. This package enables seamles

如何安装 @captchahub/core?

在项目目录运行 npm install @captchahub/core,或使用 pnpm add / yarn add / bun add。

@captchahub/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@captchahub/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@captchahub/core 可以商用吗?

@captchahub/core 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@captchahub/core 的下载量有多少?

根据 npm 官方下载统计,@captchahub/core 在 2026/10/09 – 2026/11/07 这 30 天内被下载 6,560 次,日均约 219 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@captchahub/core 还在维护吗?

最新版本 1.0.11 发布于 2026/08/29(2 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/11/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。