PerspectPerspectDocs
Go to Admin
View as Markdown

ab-client

Interfaces

  • AbClient
  • AbForRequestResult
  • AbVariantResult
  • CreatePerspectAbOptions
  • PerspectAb

Functions

  • createPerspectAb

bucketing

Functions

  • bucketFor
  • variantForBucket

perspectapi-ts-sdk

Modules

  • ab-client
  • bucketing

Interface: AbClient

Methods

getVariant()

getVariant(flagKey): Promise<AbVariantResult>

Parameters

flagKey

string

Returns

Promise<AbVariantResult>

track()

track(event, properties?): Promise<void>

Parameters

event

string

properties?

Record<string, unknown>

Returns

Promise<void>


Interface: AbForRequestResult

Properties

ab

ab: AbClient

responseHeaders

responseHeaders: Headers

Merge into the outgoing response — may carry a freshly minted perspect_vid cookie.


Interface: AbVariantResult

Properties

config

config: Record<string, unknown> | null

enrolled

enrolled: boolean

variant

variant: string | null


Interface: CreatePerspectAbOptions

Properties

apiKey

apiKey: string

baseUrl?

optional baseUrl?: string

cache?

optional cache?: CacheAdapter

Pluggable cache adapter — CloudflareKVCacheAdapter, InMemoryCacheAdapter, or NoopCacheAdapter.

ctx?

optional ctx?: object

ExecutionContext (CF Workers) or any object with waitUntil — enables background tasks.

waitUntil()

waitUntil(p): void

Parameters
p

Promise<unknown>

Returns

void

siteName

siteName: string


Interface: PerspectAb

Methods

forRequest()

forRequest(request): Promise<AbForRequestResult>

Parameters

request

Request

Returns

Promise<AbForRequestResult>

refreshConfig()

refreshConfig(): Promise<void>

Returns

Promise<void>


Function: createPerspectAb()

createPerspectAb(options): PerspectAb

Parameters

options

CreatePerspectAbOptions

Returns

PerspectAb


Function: bucketFor()

bucketFor(visitorId, flagKey, version): number

FNV-1a 32-bit over UTF-8 bytes, reduced mod 10000. Pure function, no crypto — determinism and portability beat pre-image resistance here. The attack surface on the mapping is the same for both sides; what matters is that site and platform agree bit-for-bit.

Parameters

visitorId

string

flagKey

string

version

number

Returns

number


Function: variantForBucket()

variantForBucket(bucket, variants, trafficAllocationBp): string | null

Resolve a bucket to a variant using cumulative weightBp ranges. Variants are consumed in the order given — callers must sort variants consistently on both sides (we sort by key).

Returns null iff bucket >= trafficAllocationBp (visitor not enrolled).

Parameters

bucket

number

variants

object[]

trafficAllocationBp

number

Returns

string | null