@mochabug/adapt-web
Version:
Browser client library for Adapt automation platform
121 lines (120 loc) • 3.83 kB
TypeScript
/**
* Standalone Cap.js widget for proof-of-work challenges.
*
* Creates and mounts a `<cap-widget>` element inside the given container.
* The widget handles challenge creation, solving, and redemption via
* the provided `AutomationClient`. After a successful solve the widget
* auto-destroys and calls `onSolve` with the redeemed token.
*
* **Styling:** The widget is themed through CSS custom properties
* (`--mb-cap-*` on the `.mb-adapt-cap` wrapper -> mapped to Cap.js `--cap-*`
* variables). Avoid styling Cap.js internals directly; this wrapper only
* touches Cap's attribution node so Adapt embeds do not show Cap branding.
*
* @example
* ```typescript
* import { AdaptCapWidget } from "@mochabug/adapt-web";
* import { createConnectClient } from "@mochabug/adapt-core/connect";
*
* const widget = new AdaptCapWidget({
* container: "cap-container",
* automationId: "my-automation",
* client: createConnectClient({ id: "my-automation" }),
* onSolve: (token, expires) => {
* console.log("Solved:", token);
* },
* });
*
* // Toggle dark mode at runtime
* widget.setDarkMode(true);
* ```
*/
import type { AutomationClient } from "@mochabug/adapt-core";
import type { CapWidgetI18n } from "./types.js";
import "@cap.js/widget";
/**
* Options for creating an {@link AdaptCapWidget}.
*/
export interface AdaptCapWidgetOptions {
/**
* Where to mount the widget. Accepts an element ID string or an `HTMLElement`.
*
* @example
* ```typescript
* container: "cap-container" // by ID
* container: document.body // by reference
* ```
*/
container: string | HTMLElement;
/**
* The automation ID used to create and redeem challenges.
*/
automationId: string;
/**
* The automation client used for challenge API calls.
* Create via `createConnectClient({ id })` from `@mochabug/adapt-core/connect`.
*/
client: AutomationClient;
/**
* Called when the challenge is successfully solved.
* Pass the token to `startSession` / `run` as `challengeToken`.
*
* @param token - Redeemed challenge token
* @param expires - When the token expires
*/
onSolve: (token: string, expires: Date) => void;
/**
* Called when challenge solving fails.
*/
onError?: (error: Error) => void;
/**
* Number of Web Workers used by Cap.js to solve challenges in parallel.
* @default navigator.hardwareConcurrency || 8
*/
workerCount?: number;
/**
* Name of the hidden token input generated by Cap.js.
* @default "cap-token"
*/
hiddenFieldName?: string;
/**
* Custom URL for Cap.js's troubleshooting link when instrumentation blocks a user.
*/
troubleshootingUrl?: string;
/**
* Disable Cap.js haptic feedback for this widget.
*/
disableHaptics?: boolean;
/**
* Custom label overrides for the Cap.js widget UI.
*/
i18n?: CapWidgetI18n;
}
/**
* Standalone Cap.js widget for proof-of-work challenges.
*
* This component can be used independently of AdaptWebClient to handle
* PoW challenges. After solving, the token can be used with any session
* starting method in @mochabug/adapt-core.
*/
export declare class AdaptCapWidget {
private containerElement;
private widgetContainer;
private widgetElement;
private attributionObserver;
private destroyed;
private options;
constructor(options: AdaptCapWidgetOptions);
private injectStyles;
private mountWidget;
private hideCapAttribution;
/**
* Enable or disable dark mode styling.
*/
setDarkMode(dark: boolean): void;
/**
* Clean up and remove the widget from DOM.
* Called automatically after successful solve.
*/
destroy(): void;
}