@ideal-postcodes/address-finder
Version:
Address Finder JS library backed by the Ideal Postcodes UK address search API
101 lines (100 loc) • 2.73 kB
TypeScript
import { ControllerOptions, Controller } from "./controller";
interface OnBindOptions {
config: ControllerOptions;
options: WatchOptions;
}
interface OnBindAttempt {
(options: OnBindOptions): void;
}
interface OnAnchorFoundOptions {
anchor: HTMLElement;
scope: HTMLElement;
config: ControllerOptions;
}
interface OnAnchorFound {
(options: OnAnchorFoundOptions): void;
}
/**
* Configures behaviour of watch method
*/
export interface WatchOptions {
/**
* Callback executed when an AddressFinder instance successfully binds
*
* @default `NOOP`
*/
onBind?: (c: Controller) => void;
/**
* Callback executed each time bind is attempted
*
* @default `NOOP`
*/
onBindAttempt?: OnBindAttempt;
/**
* Callback executed when anchor and its scope is detected
*
* @default `NOOP`
*/
onAnchorFound?: OnAnchorFound;
/**
* Custom anchor. Set this if you do not wish to anchor the `watch` method using inputField or outputFields.line_1
*
* @default `undefined`
*/
anchor?: string;
/**
* Sets the scope in which the AddressFinder can operate. Set to `null` to mark the whole page as in scope
*
* A function which provides the anchor as the first argument. The operable scope must be returned. By default the parent `<form>` element is returned
*
* @default Immediate <form> parent of anchor
*/
getScope?: (anchor: HTMLElement) => HTMLElement | null;
/**
* Set the periodicity of page checks in milliseconds
*
* @default `1000`
*/
interval?: number;
/**
* A function executed on every interval to determine whether binding should take place
*
* @default `() => true`
*/
pageTest?: () => boolean;
/**
* Error callback. Executes when API Key check fails. Does not execute if
* API request succeeds but key is marked as unavailable
*/
onError?: (error: unknown) => void;
/**
* Whether to immediately invoke `bind`
*
* @default `true`
*/
immediate?: boolean;
/**
* Configure marker for solution is attached to specific context/anchor
* in DOM
*/
marker?: string;
}
export interface BindResult {
bind: any;
start: any;
stop: any;
}
export interface Watch {
(config: ControllerOptions, options?: WatchOptions): Promise<BindResult | null>;
}
/**
* Dynamically apply AddressFinder when relevant fields appear
* - Exits if page test is fails
* - Check if key usable
* - Creates a bind method
* - Retrives parent scope
* - Marks anchor if completed
* - Creates timer tools
*/
export declare const watch: Watch;
export {};