NextApi
interface NextApi extends LegacyApi {
version: string;
analytics: { on: any };
getState: () => Readonly<State>;
log: Pick<Logger, 'level' | 'entries' | 'stream'>;
actions: NextActions;
on<K extends PublicEventName>(name: '*' | 'ready' | K, fn: (data: EventMap[K], event: SdkEvent<K>) => void): () => void;
snapshot(): Snapshot;
context(): EventContext;
bind(root: ParentNode): void;
query(path: string): Promise<unknown>;
watch(path: string, fn: (value: unknown) => void): () => void;
define(type: ModelType, name: string, derive: (object: any) => unknown, options?: { format?: string }): void;
addItem(o: { packageId: number; quantity?: number }): Promise<void>;
clearCart(): Promise<void>;
swapCart(items: CartLine[]): Promise<void>;
applyCoupon(code: string): Promise<boolean>;
getPackage(id: number): Package | undefined;
getCampaignData(): Campaign | undefined;
getCartData(): LegacyCartData;
exitIntent(options: ExitIntentOptions): Promise<() => void>;
getShippingMethods(): ShippingOption[];
setShippingMethod(id: number): Promise<void>;
getSelectedShippingMethod(): ShippingOption | undefined;
}Source: boot.ts:46
Hierarchy
LegacyApiNextApi
Properties
version: string
analytics: { on: any }
Properties
on
getState: () => Readonly<State>
The SDK's internal state, for debugging; its shape changes between releases. Read snapshot() instead.
log: Pick<Logger, 'level' | 'entries' | 'stream'>
actions: NextActions
Every action data-next-action names, taking one object of the model's field names and resolving to { ok, errors }.
Methods
on<
K extends PublicEventName,
>(
name: '*' | 'ready' | K,
fn: (data: EventMap[K], event: SdkEvent<K>) => void,
): () => void
Calls fn with every name event, or every event for *, starting with the ones this page sent before the call. A 0.4.x name gets its 0.4.x payload, and only the events after the call unless 0.4.x pages listen for it late (campaign:loaded, checkout:started, …).
Type Parameters
KextendsPublicEventName
Parameters
name("*" | "ready" | K)fn((data: EventMap[K], event: SdkEvent<K>) => void)
Returns
() => void
snapshot(): Snapshot
What the page shows now: the campaign, cart, checkout, order and selectors, as the events carry them.
Returns
Snapshot
context(): EventContext
The page, campaign, session, attribution and browser now, as on tracking events.
Returns
EventContext
bind(root: ParentNode): void
Parameters
root(ParentNode)
Returns
void
query(path: string): Promise<unknown>
Reads a path of the object model, as data-next-query does: await next.query('cart.total'). Its code loads on the first call.
Parameters
path(string)
Returns
Promise<unknown>
watch(path: string, fn: (value: unknown) => void): () => void
Calls fn with the path's value once its code has loaded, then each time it changes; returns an unsubscribe function.
Parameters
path(string)fn((value: unknown) => void)
Returns
() => void
define(
type: ModelType,
name: string,
derive: (object: any) => unknown,
options?: { format?: string },
): void
Adds a field the page computes to every object of a type, read like the SDK's own: next.define('Option', 'per_day', (o) => o.total / 30, { format: 'currency' }).
Parameters
type(ModelType)name(string)derive((object: any) => unknown)options({ format?: string }, optional)
Properties
format(string, optional)
Returns
void
addItem(
o: { packageId: number; quantity?: number },
): Promise<void>
Inherited from addItem
Use `next.actions.addItem(packageId, quantity)`.
Parameters
o({ packageId: number; quantity?: number })
Properties
packageId(number)quantity(number, optional)
Returns
Promise<void>
clearCart(): Promise<void>
Inherited from clearCart
Use `next.actions.clear()`. Keeps coupons, like the old SDK.
Returns
Promise<void>
swapCart(items: CartLine[]): Promise<void>
Inherited from swapCart
Use `next.actions.setGroup(id, { lines, vouchers })`.
Parameters
items(CartLine[])
Returns
Promise<void>
applyCoupon(code: string): Promise<boolean>
Inherited from applyCoupon
Use `next.actions.applyCoupon(code)`. A code entered on an empty cart answers `true` at once, as in 0.4.x.
Parameters
code(string)
Returns
Promise<boolean>
getPackage(id: number): Package | undefined
Inherited from getPackage
A package's product, variant and image come on every event's `CartItem`; `next.getState()` has the campaign for debugging.
Parameters
id(number)
Returns
Package | undefined
getCampaignData(): Campaign | undefined
Inherited from getCampaignData
Use `next.snapshot().campaign`.
Returns
Campaign | undefined
getCartData(): LegacyCartData
Inherited from getCartData
Use `next.snapshot().cart`. Returns the 0.4.x shape.
Returns
LegacyCartData
exitIntent(options: ExitIntentOptions): Promise<() => void>
Inherited from exitIntent
Exit intent will move to the page kit.
Parameters
options(ExitIntentOptions)
Returns
Promise<() => void>
getShippingMethods(): ShippingOption[]
Inherited from getShippingMethods
The chosen method is `next.snapshot().cart.shipping_method`; `next.getState()` has the campaign's list for debugging.
Returns
ShippingOption[]
setShippingMethod(id: number): Promise<void>
Inherited from setShippingMethod
Use `next.actions.setShipping(id)`.
Parameters
id(number)
Returns
Promise<void>
getSelectedShippingMethod(): ShippingOption | undefined
Inherited from getSelectedShippingMethod
Use `next.snapshot().cart.shipping_method`.
Returns
ShippingOption | undefined