CaptchaHub 核心软件
CaptchaHub Core是一个TypeScript库,它作为CaptchaHub生态系统的基础软件包,提供网络应用中处理和处理Captcha的必要类,接口,以及工具. 这个软件包能够与包括ReCaptcha、HCaptcha和Turnstile在内的各种Captcha供应商进行无缝整合,确保以一致和简化的方式实施Captcha.
安装
使用 pnpm 安装软件包 :
pnpm add @captchahub/core
特征
- 统一一体化翻译: CaptchaHub Core为整合多个cabtcha供应商提供了统一的接口.
- 广泛: 方便地扩展了核心功能,以支持额外的captcha服务.
- 无节点( 无服务) 支持: 被设计成在无节点(无服务器)环境中无缝工作,使其成为现代网络应用的理想.
使用量
获取 Captcha 脚本
这个 getCaptchaScript 函数可用如下:
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>
);
};
获取 Captcha 窗体元素
这个 getCaptchaFormElement 函数可用如下:
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>
);
};
正在验证 Captcha
这个 verifyCaptcha 函数可用如下:
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.');
}
配置
Captcha 提供者的配置可以在创建 Captcha 实例时被传递到工厂的配置对象中定义. 配置对象必须包含 CAPTCHA_PROVIDER 属性以指定要使用的 Captcha 提供者。 根据具体提供者的不同,可能需要其他属性.
添加新供应商
要添加新的 Captcha 提供者, 您可以执行 CaptchaInterface以下是如何实施一个新的提供者的简单例子:
-
创建新文件夹: 为您的提供者创建一个新文件夹( 例如 ),
packages/myprovider) (中文(简体) ). -
执行接口: 创建执行
CaptchaInterface。该类应处理用于验证卡普查和渲染卡普查元素的逻辑.示例
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 } } -
自注册软件包:为了让您的包裹自我注册,确保您的包裹与
CaptchaFactoryRegistry并导出工厂类作为默认导出单位index.tsx。例如:
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;
理由
CaptchaHub Core包旨在为TypeScript应用程序中的Captcha工作流程管理提供一致而可扩展的基础. 通过使用定义明确的界面和工具,开发者可以轻松地将captcha能力整合到他们的网络应用中.
捐款
欢迎捐款! 请随意将这个项目叉出并提交拉动请求. 在提交之前,请确保您的代码通过所有起伏和单元测试.
您可以使用 :
pnpm format
您可以使用 :
pnpm test