import { AnalyticsServerEvent, Geo } from "./analytics"; /** * Store set options: ttl in seconds or string representation of duration (e.g. "1d") */ export type SetOpts = number | string | { ttl: number }; /** * A key value store that exposed to a function */ export interface Store { get(key: string): Promise; // getWithMeta(key: string): Promise<{ // value: any; // lastUpdated: Date // initialTTLSeconds: number // leftTTLSeconds: number // } | undefined>; del(key: string): Promise; set(key: string, value: any, opts?: SetOpts): Promise; ttl(key: string): Promise; } export interface FunctionMetrics { status( component: string, status: "error" | "ok", errorType?: string ): { //increment / decrement counter. Supports negative values inc: (value: number) => void; }; // gauge(name: string): { // set: (value: number) => void; // //increment / decrement counter. Supports negative values // inc: (value: number) => void; // }; } export interface TTLStore extends Store { getWithTTL(key: string): Promise<{ value: any; ttl: number } | undefined>; getOrSet(key: string, value: any, opts?: SetOpts): Promise; dump?(): Promise; } /** * A special properties that user can set on an event to define how * event will be processed further */ export type EventControlOpts = { /** * Table name to store event in. Non-SQL destinations might ignore this property */ JITSU_TABLE_NAME?: string; }; export type AnyEvent = Record & EventControlOpts; export type AnyProps = Record; export type FetchResponse = Response; export type FetchType = (url: string, opts?: FetchOpts, extras?: any) => Promise; export type FetchOpts = { method?: string; headers?: Record; // Uint8Array instead of Buffer so the type is usable without @types/node. // Buffer extends Uint8Array, so passing a Buffer remains valid. body?: string | Uint8Array; }; export type FunctionLogger = { info: (message: string, ...args: any[]) => void | Promise; warn: (message: string, ...args: any[]) => void | Promise; debug: (message: string, ...args: any[]) => void | Promise; error: (message: string, ...args: any[]) => void | Promise; }; interface Warehouse { query: (sql: string, params?: Record) => Promise; } export type FunctionContext

= { log: FunctionLogger; fetch: FetchType; store: TTLStore; getWarehouse: (destinationId: string) => Warehouse; metrics?: FunctionMetrics; props: P; }; export type PrivacyOpts = { /** * IP address processing mode * "keep" means that the IP address will be kept as is * "reduce" means that the IP address will be reduced to the first 3 octets * "hash" means that the IP address will be hashed with SHA256 (not implemented yet) * * "reduce" is the default behavior */ ip?: "reduce" | "keep" | "hash"; /** * Replaces cookie based anonymous ID with a fingerprint hash * "hash1" means that anyonymous ID will be replaced with hash(ip + user_agent) */ anonymousId?: "hash1" | "keep" | ((opts: { ip: string; ip3octets: string; userAgent: string }) => string); }; export type CoreLib = { privacy(event: AnalyticsServerEvent, opts?: PrivacyOpts): AnalyticsServerEvent; }; export type AnonymousEventsStore = { addEvent(collectionName: string, anonymousId: string, event: AnalyticsServerEvent, ttlDays: number): Promise; evictEvents(collectionName: string, anonymousId: string): Promise; }; export type UserAgent = { browser: { name: string; version: string; major: string; //@deprecated }; engine: { name: string; version: string; }; os: { name: string; version: string; }; device: { model: string; type: "console" | "mobile" | "tablet" | "smarttv" | "wearable" | "embedded" | "desktop"; vendor: string; }; cpu: { architecture: string; }; bot?: boolean; }; export type EventContext = { /** * Geo data of incoming request */ geo?: Geo; /** * Parsed User Agent of incoming request */ ua?: UserAgent; /** * Headers of incoming request */ headers: Record; /** * Source of the incoming event */ source: { id: string; name?: string; //if request was sent from browser or server-to-server api type: "browser" | "s2s"; domain?: string; }; destination: { id: string; type: string; updatedAt?: Date; hash: string; }; connection: { id: string; mode?: string; options?: any; }; // available in Profile Builder transformations allConnections?: { id: string; destinationId: string; destinationName: string; type: string; mode: string; }[]; workspace: { id: string; }; // original receivedAt as it stamped by the ingest service; receivedAt: Date; // number of retries attempted retries?: number; }; /** * Parameters for a function */ export type FullContext

= EventContext & FunctionContext

; //equivalent to returning [] from a function export type FunctionCommand = "drop"; export type FuncReturn = AnyEvent[] | AnyEvent | null | undefined | FunctionCommand | false; export type SyncFunction< Dest extends AnyProps = AnyProps, Cred extends AnyProps = AnyProps, SyncProps extends AnyProps = AnyProps > = (p: { source: { package: string; credentials: Cred; syncProps: SyncProps }; destination: Dest; ctx: { log: FunctionLogger; store: Store; }; }) => Promise; export interface JitsuFunction { (event: E, ctx: FullContext

): Promise | FuncReturn; displayName?: string; //for future use - config schema of the function configSchema?: any; //It's allowed to use basic JSX description?: any; } export type BuiltinFunctionName = `builtin.${T}`; export type BuiltinDestinationFunctionName = BuiltinFunctionName<`destination.${string}`>; export type BuiltinTransformationFunctionName = BuiltinFunctionName<`transformation.${string}`>;