@elbwalker/utils
Version:
Shared utils for walkerOS packages
121 lines (93 loc) • 8.03 kB
TypeScript
import { WalkerOS, Elb, Destination, Mapping, On, Hooks, Schema } from '@elbwalker/types';
declare function anonymizeIP(ip: string): string;
interface Assign {
merge?: boolean;
shallow?: boolean;
extend?: boolean;
}
declare function assign<T extends object, U extends object>(target: T, obj?: U, options?: Assign): T & U;
declare function getByPath(event: unknown, key?: string, defaultValue?: unknown, i?: unknown): unknown;
declare function setByPath(event: WalkerOS.Event, key: string, value: unknown): WalkerOS.Event;
declare function castValue(value: unknown): WalkerOS.PropertyType;
declare function clone<T>(org: T, visited?: WeakMap<object, unknown>): T;
declare function getGrantedConsent(required: WalkerOS.Consent | undefined, state?: WalkerOS.Consent, individual?: WalkerOS.Consent): false | WalkerOS.Consent;
declare function setConsent(instance: WalkerOS.Instance, data: WalkerOS.Consent): Promise<Elb.PushResult>;
type CommandTypes = 'Action' | 'Config' | 'Consent' | 'Context' | 'Custom' | 'Destination' | 'Elb' | 'Globals' | 'Hook' | 'Init' | 'Link' | 'On' | 'Prefix' | 'Ready' | 'Run' | 'Session' | 'User' | 'Walker';
declare const Commands: Record<CommandTypes, WalkerOS.Commands>;
type StorageType = 'cookie' | 'local' | 'session';
declare const Const: {
Commands: Record<CommandTypes, string>;
Utils: {
Storage: {
[key: string]: StorageType;
};
};
};
type HandleCommandFn<T extends WalkerOS.Instance> = (instance: T, action: string, data?: Elb.PushData, options?: Elb.PushOptions) => Promise<Elb.PushResult>;
declare function createPush<T extends WalkerOS.Instance, F extends Elb.Fn>(instance: T, handleCommand: HandleCommandFn<T>, prepareEvent: Elb.Fn<WalkerOS.PartialEvent>): F;
declare function addDestination(instance: WalkerOS.Instance, data: Destination.DestinationInit, options?: Destination.Config): Promise<Elb.PushResult>;
declare function pushToDestinations(instance: WalkerOS.Instance, event?: WalkerOS.Event, destinations?: WalkerOS.Destinations): Promise<Elb.PushResult>;
declare function destinationInit<Destination extends Destination.Destination>(instance: WalkerOS.Instance, destination: Destination): Promise<boolean>;
declare function destinationPush<Destination extends Destination.Destination>(instance: WalkerOS.Instance, destination: Destination, event: WalkerOS.Event): Promise<boolean>;
declare function createPushResult(partialResult?: Partial<Elb.PushResult>): Elb.PushResult;
declare function createEvent(props?: WalkerOS.DeepPartialEvent): WalkerOS.Event;
declare function getEvent(name?: string, props?: WalkerOS.DeepPartialEvent): WalkerOS.Event;
declare function getId(length?: number): string;
interface MarketingParameters {
[key: string]: string;
}
declare function getMarketingParameters(url: URL, custom?: MarketingParameters): WalkerOS.Properties;
declare function commonHandleCommand(instance: WalkerOS.Instance, action: string, data?: unknown, options?: unknown): Promise<Elb.PushResult | undefined>;
declare function createEventOrCommand(instance: WalkerOS.Instance, nameOrEvent: unknown, defaults?: WalkerOS.PartialEvent): {
event?: WalkerOS.Event;
command?: string;
};
declare function debounce<P extends unknown[], R>(fn: (...args: P) => R, wait?: number, immediate?: boolean): (...args: P) => Promise<R>;
declare function throttle<P extends unknown[], R>(fn: (...args: P) => R | undefined, delay?: number): (...args: P) => R | undefined;
declare function isArguments(value: unknown): value is IArguments;
declare function isArray<T>(value: unknown): value is T[];
declare function isBoolean(value: unknown): value is boolean;
declare function isCommand(entity: string): boolean;
declare function isDefined<T>(val: T | undefined): val is T;
declare function isElementOrDocument(elem: unknown): elem is Element;
declare function isNumber(value: unknown): value is number;
declare function isObject(value: unknown): value is WalkerOS.AnyObject;
declare function isSameType<T>(variable: unknown, type: T): variable is typeof type;
declare function isString(value: unknown): value is string;
declare function getMappingEvent(event: WalkerOS.PartialEvent, mapping?: Mapping.Config<unknown>): Promise<Mapping.EventMapping>;
declare function getMappingValue(value: WalkerOS.DeepPartialEvent | unknown | undefined, data?: Mapping.Data, options?: Mapping.Options): Promise<WalkerOS.Property | undefined>;
declare function on(instance: WalkerOS.Instance, type: On.Types, option: WalkerOS.SingleOrArray<On.Options>): void;
declare function onApply(instance: WalkerOS.Instance, type: On.Types, options?: Array<On.Options>, config?: WalkerOS.Consent): void;
declare function onLog(message: unknown, verbose?: boolean): void;
declare function isPropertyType(value: unknown): value is WalkerOS.PropertyType;
declare function filterValues(value: unknown): WalkerOS.Property | undefined;
declare function castToProperty(value: unknown): WalkerOS.Property | undefined;
declare function requestToData(parameter: unknown): WalkerOS.AnyObject | undefined;
declare function requestToParameter(data: WalkerOS.AnyObject | WalkerOS.PropertyType): string;
type SendDataValue = WalkerOS.Property | WalkerOS.Properties;
type SendHeaders = {
[key: string]: string;
};
interface SendResponse {
ok: boolean;
data?: unknown;
error?: string;
}
declare function transformData(data?: SendDataValue): string | undefined;
declare function getHeaders(headers?: SendHeaders): SendHeaders;
declare function throwError(error: unknown): never;
declare function trim(str: string): string;
declare function tryCatch<P extends unknown[], R, S>(fn: (...args: P) => R | undefined, onError: (err: unknown) => S, onFinally?: () => void): (...args: P) => R | S;
declare function tryCatch<P extends unknown[], R>(fn: (...args: P) => R | undefined, onError?: undefined, onFinally?: () => void): (...args: P) => R | undefined;
declare function tryCatchAsync<P extends unknown[], R, S>(fn: (...args: P) => R, onError: (err: unknown) => S, onFinally?: () => void | Promise<void>): (...args: P) => Promise<R | S>;
declare function tryCatchAsync<P extends unknown[], R>(fn: (...args: P) => R, onError?: undefined, onFinally?: () => void | Promise<void>): (...args: P) => Promise<R | undefined>;
declare function useHooks<P extends unknown[], R>(fn: (...args: P) => R, name: string, hooks: Hooks.Functions): (...args: P) => R;
declare function parseUserAgent(userAgent?: string): WalkerOS.User;
declare function getBrowser(userAgent: string): string | undefined;
declare function getBrowserVersion(userAgent: string): string | undefined;
declare function getOS(userAgent: string): string | undefined;
declare function getOSVersion(userAgent: string): string | undefined;
declare function getDeviceType(userAgent: string): string | undefined;
declare function validateEvent(obj: unknown, customContracts?: Schema.Contracts): WalkerOS.Event | never;
declare function validateProperty(obj: WalkerOS.AnyObject, key: string, value: unknown, schema: Schema.Property): WalkerOS.Property | never;
export { type CommandTypes, Commands, Const, type HandleCommandFn, type MarketingParameters, type SendDataValue, type SendHeaders, type SendResponse, type StorageType, addDestination, anonymizeIP, assign, castToProperty, castValue, clone, commonHandleCommand, createEvent, createEventOrCommand, createPush, createPushResult, debounce, destinationInit, destinationPush, filterValues, getBrowser, getBrowserVersion, getByPath, getDeviceType, getEvent, getGrantedConsent, getHeaders, getId, getMappingEvent, getMappingValue, getMarketingParameters, getOS, getOSVersion, isArguments, isArray, isBoolean, isCommand, isDefined, isElementOrDocument, isNumber, isObject, isPropertyType, isSameType, isString, on, onApply, onLog, parseUserAgent, pushToDestinations, requestToData, requestToParameter, setByPath, setConsent, throttle, throwError, transformData, trim, tryCatch, tryCatchAsync, useHooks, validateEvent, validateProperty };