@anilkumarthakur/vue3-recaptcha
Version:
A Vue 3 plugin for Google reCAPTCHA v2 (checkbox & invisible) and v3 integration
233 lines • 6.38 kB
TypeScript
import { Ref, InjectionKey } from 'vue';
export type RecaptchaVersion = 'v2' | 'v3';
export type RecaptchaV2Type = 'checkbox' | 'invisible';
export type RecaptchaTheme = 'light' | 'dark';
export type RecaptchaSize = 'normal' | 'compact' | 'invisible';
export type RecaptchaBadgePosition = 'bottomright' | 'bottomleft' | 'inline';
export interface RecaptchaPluginOptions {
/**
* Your reCAPTCHA site key from Google Console
*/
siteKey: string;
/**
* Version of reCAPTCHA to use
* @default 'v3'
*/
version?: RecaptchaVersion;
/**
* Whether to load the script automatically
* @default true
*/
autoLoad?: boolean;
/**
* Language code for reCAPTCHA widget
* @see https://developers.google.com/recaptcha/docs/language
*/
language?: string;
/**
* Custom script URL (for enterprise or self-hosted)
*/
scriptUrl?: string;
/**
* Action name for reCAPTCHA v3 analytics
* @default 'submit'
*/
action?: string;
/**
* Callback when script is loaded
*/
onLoad?: () => void;
/**
* Callback when script fails to load
*/
onError?: (error: Error) => void;
}
export interface RecaptchaV2CheckboxProps {
/**
* Site key (overrides plugin config)
*/
siteKey?: string;
/**
* Color theme of the widget
* @default 'light'
*/
theme?: RecaptchaTheme;
/**
* Size of the widget
* @default 'normal'
*/
size?: 'normal' | 'compact';
/**
* Tabindex for accessibility
* @default 0
*/
tabindex?: number;
/**
* v-model binding for token
*/
modelValue?: string;
}
export interface RecaptchaV2CheckboxEmits {
(e: 'update:modelValue', token: string): void;
(e: 'verify', token: string): void;
(e: 'expire'): void;
(e: 'error', error: Error): void;
(e: 'load'): void;
}
export interface RecaptchaV2InvisibleProps {
/**
* Site key (overrides plugin config)
*/
siteKey?: string;
/**
* Badge position
* @default 'bottomright'
*/
badge?: RecaptchaBadgePosition;
/**
* Tabindex for accessibility
* @default 0
*/
tabindex?: number;
/**
* v-model binding for token
*/
modelValue?: string;
}
export interface RecaptchaV2InvisibleEmits {
(e: 'update:modelValue', token: string): void;
(e: 'verify', token: string): void;
(e: 'expire'): void;
(e: 'error', error: Error): void;
(e: 'load'): void;
}
export interface RecaptchaV3Props {
/**
* Site key (overrides plugin config)
*/
siteKey?: string;
/**
* Action name for analytics (e.g., 'login', 'signup', 'submit')
*/
action?: string;
/**
* Badge position
* @default 'bottomright'
*/
badge?: RecaptchaBadgePosition;
/**
* Whether to hide the badge
* @default false
*/
hideBadge?: boolean;
/**
* v-model binding for token
*/
modelValue?: string;
}
export interface RecaptchaV3Emits {
(e: 'update:modelValue', token: string): void;
(e: 'verify', token: string): void;
(e: 'error', error: Error): void;
(e: 'load'): void;
}
export interface UseRecaptchaV2Options {
siteKey?: string;
theme?: RecaptchaTheme;
size?: RecaptchaSize;
badge?: RecaptchaBadgePosition;
tabindex?: number;
}
export interface UseRecaptchaV3Options {
siteKey?: string;
action?: string;
}
export interface UseRecaptchaV2Return {
token: Readonly<Ref<string>>;
isReady: Readonly<Ref<boolean>>;
isLoading: Readonly<Ref<boolean>>;
error: Readonly<Ref<Error | null>>;
widgetId: Readonly<Ref<number | null>>;
render: (container: HTMLElement | string) => Promise<number>;
execute: () => Promise<string>;
reset: () => void;
getResponse: () => string;
}
export interface UseRecaptchaV3Return {
token: Readonly<Ref<string>>;
isReady: Readonly<Ref<boolean>>;
isLoading: Readonly<Ref<boolean>>;
error: Readonly<Ref<Error | null>>;
execute: (action?: string) => Promise<string>;
}
export interface GrecaptchaV2RenderParameters {
sitekey: string;
theme?: RecaptchaTheme;
size?: RecaptchaSize;
tabindex?: number;
callback?: (token: string) => void;
'expired-callback'?: () => void;
'error-callback'?: () => void;
}
export interface GrecaptchaV2 {
render: (container: HTMLElement | string, parameters: GrecaptchaV2RenderParameters) => number;
execute: (widgetId?: number) => void;
reset: (widgetId?: number) => void;
getResponse: (widgetId?: number) => string;
}
export interface GrecaptchaV3 {
ready: (callback: () => void) => void;
execute: (siteKey: string, options: {
action: string;
}) => Promise<string>;
}
/**
* Combined grecaptcha interface with properly overloaded execute method.
* Covers both v2 (widget-based) and v3 (score-based) API surfaces.
*/
export interface Grecaptcha {
render: (container: HTMLElement | string, parameters: GrecaptchaV2RenderParameters) => number;
execute(widgetId?: number): void;
execute(siteKey: string, options: {
action: string;
}): Promise<string>;
reset: (widgetId?: number) => void;
getResponse: (widgetId?: number) => string;
ready: (callback: () => void) => void;
}
declare global {
interface Window {
grecaptcha: Grecaptcha;
___grecaptcha_cfg?: {
explicit?: boolean;
};
}
}
export interface RecaptchaV2CheckboxInstance {
reset: () => void;
getResponse: () => string;
widgetId: Ref<number | null>;
}
export interface RecaptchaV2InvisibleInstance {
execute: () => Promise<string>;
reset: () => void;
getResponse: () => string;
widgetId: Ref<number | null>;
}
export interface RecaptchaV3Instance {
load: () => Promise<void>;
/** @deprecated Use `load` instead */
loadRecaptcha: () => Promise<void>;
execute: (action?: string) => Promise<string>;
isLoaded: Ref<boolean>;
isLoading: Ref<boolean>;
error: Ref<Error | null>;
}
export declare const RECAPTCHA_INJECTION_KEY: InjectionKey<RecaptchaContext>;
export interface RecaptchaContext {
siteKey: string;
version: RecaptchaVersion;
isLoaded: Ref<boolean>;
loadScript: () => Promise<void>;
}
//# sourceMappingURL=index.d.ts.map