/** * TypeScript types for `window.Yasan`, the storefront JavaScript API of the Yasan widgets. * Documentation: https://help.yasan.ai/developers/javascript-api/ * * Amounts are integers in the currency's minor unit: 1 298 Kč is 129800, €12.50 is 1250. * Product and variant ids are your own, as you send them to Yasan in `GET /products`. */ export type YasanCartLine = { /** Product name, not empty. */ title: string /** A whole number of 1 or more. */ quantity: number productId?: string variantId?: string /** The variant's name as the customer sees it, such as `size 42`. */ variant?: string unitPriceMinor?: number lineTotalMinor?: number /** Link to the product page. */ url?: string } export type YasanCart = { /** ISO 4217 code, such as `CZK` or `EUR`. */ currency?: string /** What the customer pays for the cart. */ totalMinor?: number /** Every line of the cart; empty when the cart is empty. */ items: YasanCartLine[] } export type YasanAddToCartItem = { productId: string variantId: string quantity: number } export type YasanAddToCartErrorCode = 'invalid-id' | 'unavailable' | 'network' /** What `addToCart` throws or rejects with; without `code` the failure counts as unknown. */ export type YasanAddToCartError = Error & { code?: YasanAddToCartErrorCode } export type YasanProvided = { /** Returns the cart, or a promise of it. */ getCart?: () => YasanCart | Promise /** Resolves once the cart holds the pieces; throws or rejects with a {@link YasanAddToCartError} when it does not. */ addToCart?: (item: YasanAddToCartItem) => Promise } export type YasanPlacedOrderItem = { productId: string variantId: string /** A whole number of 1 or more. */ quantity: number /** Price of one piece as charged in this order, after discounts, VAT included. */ unitPriceMinor: number } export type YasanPlacedOrder = { /** The same value every time this order's confirmation page is shown. */ orderId: string /** ISO 4217 code, such as `CZK` or `EUR`. */ currency: string /** What the customer paid for the whole order: products after discounts, shipping and payment fees, VAT included. */ totalMinor: number /** Products only; shipping and fees belong in `totalMinor` alone. */ items: YasanPlacedOrderItem[] } export type YasanConsent = { analytics?: boolean marketing?: boolean } export type YasanChatbotApi = { readonly isEnabled: boolean readonly isOpen: boolean readonly isVisible: boolean readonly open: () => void readonly close: () => void readonly toggle: () => void /** Hides the whole chat widget, launcher included, until `show()`. Each page load starts visible. */ readonly hide: () => void readonly show: () => void } export type YasanSearchApi = { readonly isEnabled: boolean /** Opens AI Search under your search field and, with `query`, searches for it right away. */ readonly open: (options?: { query?: string }) => void } export type YasanApi = { /** `true` once the widgets have loaded, right before the `yasan-ready` event. */ readonly isReady: boolean /** The language the widgets show, as a tag. */ readonly language: string readonly setLanguage: (tag: string) => void /** Pass only what changed. */ readonly setConsent: (consent: YasanConsent) => void /** Call it only once per page load; calling it again is not allowed. */ readonly provide: (provided: YasanProvided) => void /** Tells the widgets your cart changed, so they read it again. */ readonly refreshCart: () => void readonly reportOrder: (order: YasanPlacedOrder) => void readonly Chatbot: YasanChatbotApi readonly Search: YasanSearchApi } declare global { interface Window { /** Set once `widget.js` has run; the widgets are ready when `isReady` is `true`. */ Yasan?: YasanApi } interface WindowEventMap { 'yasan-ready': CustomEvent<{ yasan: YasanApi }> } }