@beamimpact/web-sdk
Version:
The Beam SDK enables brands to connect with their customers over shared values, not transactional discounts, to build stronger loyalty. Our integration achieves this by allowing customers to (a) choose a nonprofit where the brand will donate part of their
124 lines (121 loc) • 5.58 kB
TypeScript
import { b as TBeamCartLocalStorage } from '../chunks/cart-contents-CLT7p7Gd.esm.js';
import { a as TPromoCodeAttribute, T as TPromoCodeRequest, b as TPromoCodeResponse, c as TValidatedPromoResponse } from '../chunks/promo-types-7r9-UuiP.esm.js';
import '../chunks/openapi-spec-Bk7E2QQP.esm.js';
/**
* Retrieves all validated promo codes stored in the "beam_promo_codes" cookie and parses it
*
* @returns {TPromoCodeResponse | undefined} An array of `ValidatedPromoCode` objects if found and parsed successfully, otherwise `undefined`
*/
declare const getAllPromoCodesFromCookie: () => TPromoCodeResponse | undefined;
/**
* Stores the validated promo codes in the "beam_promo_codes" cookie, replaces and does not merge
*
* @param {TValidatedPromoResponse} validatedPromoCodes
* @param {string} domain
*/
declare const setPromoCodeInCookie: ({ validatedPromoCodes, domain, }: {
validatedPromoCodes: TValidatedPromoResponse;
domain?: string | undefined;
}) => void;
/**
* Retrieves promo codes from localStorage, scoped by API key
* @param {string} apiKey API key used for storage scoping
* @returns {TPromoCodeRequest | null} - Returns stored promo codes object containing both
* validated and unvalidated codes
* - If no codes exist, returns a fresh object with empty arrays
*
*/
declare const getPromoCodesLocalStorage: ({ apiKey }: {
apiKey: string;
}) => TPromoCodeRequest | null;
/**
* Sets promo codes in localStorage
* @param {TPromoCodeRequest} promoCodes New promo codes to add
* @param {string} apiKey API key for storage scoping
*/
declare const setPromoCodesInLocalStorage: ({ apiKey, promoCodes, }: {
apiKey?: string | undefined;
promoCodes: Partial<TPromoCodeRequest>;
}) => void;
/**
* Merges new promo codes with existing ones and returns the combined result
* @param {Partial<TPromoCodeRequest>} newPromos New promo codes to add
* @param {string} apiKey API key for storage scoping
* @returns {TPromoCodeRequest} An object containing both validated and unvalidated promo codes
* @example
* const result = mergePromoCodes({
* apiKey: 'beam_123',
* newPromos: {
* unvalidatedPromoCodes: [
* { type: 'manual', attributes: { value: 'WINTER20' } },
* { type: 'url', attributes: { url: 'www.example.com/?promo=HOLIDAY25' } }
* ]
* }
* });
*/
declare const mergeStoreAndReturnPromos: ({ newPromos, apiKey, }: {
newPromos: Partial<TPromoCodeRequest>;
apiKey: string;
}) => TPromoCodeRequest;
/**
* Formats unvalidated promo codes by determining their source and attributes
* The function will assign `QueryParam` as the source if a `url` exists,
* otherwise, it will assign `Manual` as the source
*
* @param {TPromoCodeAttribute[]} promoAttributes An array of promo attributes to be formatted
* @returns {TUnvalidatedPromoRequest} - An array of UnvalidatedPromoCode objects,
* each with a source and attributes object
*/
declare const mapToUnvalidatedPromoCodes: (promoAttributes: TPromoCodeAttribute[]) => (Partial<{
type?: "manual" | "url" | undefined;
} & {
attributes?: {
value?: string | undefined;
} | undefined;
}> & Partial<{
type?: "manual" | "url" | undefined;
} & {
attributes?: {
url?: string | undefined;
} | undefined;
}>)[];
/**
* @param {TPromoCodeAttribute[]} promoAttributes An array of promo attributes to be formatted
* @param {string} apiKey API key for storage scoping
* @returns {TPromoCodeRequest} An object containing both validated and unvalidated promo codes
*/
declare const formatStoreAndReturnPromoCodes: (promoAttributes: TPromoCodeAttribute[], apiKey: string) => TPromoCodeRequest;
/**
* Extracts applicable promo codes from the cart object
* This function retrieves discounts stored in the cart's `content.discounts` array,
* filters out only the applicable promo codes, and returns them as an array of strings
*
* @param {TBeamCartLocalStorage} cart The cart object retrieved from local storage
* @returns {string[]} An array of applicable promo codes
*/
declare const getPromoCodesFromCart: (cart: TBeamCartLocalStorage) => string[];
/**
* Gets promo code exclusively from Shopify discount cookie
* @returns {string | null} Single discount code or null if not found
*/
declare const getPromoCodeFromShopifyCookie: () => string | null;
/**
* Safely parses a JSON string into an array of strings
* If parsing fails or the input is not a valid JSON array, it returns an empty array
*
* @param {string | undefined | null} jsonString The JSON string to parse
* @returns {string[]} The parsed array of strings or an empty array if parsing fails
*/
declare const parseJsonStringArray: (jsonString: string | undefined | null) => string[];
/**
* Retrieves and sanitizes the current URL for storage, preserving only safe query parameters
*
* @returns {TPromoCodeAttribute} Returns an object with only the `url` property set
*/
declare const getSanitizedUrlForStorage: () => TPromoCodeAttribute | undefined;
declare const promoCodesAreEqual: (a: TPromoCodeRequest, b: TPromoCodeRequest) => boolean;
declare const isEmptyPromoData: (promos: {
unvalidatedPromoCodes?: unknown[];
validatedPromoCodes?: unknown[];
}) => boolean;
export { formatStoreAndReturnPromoCodes, getAllPromoCodesFromCookie, getPromoCodeFromShopifyCookie, getPromoCodesFromCart, getPromoCodesLocalStorage, getSanitizedUrlForStorage, isEmptyPromoData, mapToUnvalidatedPromoCodes, mergeStoreAndReturnPromos, parseJsonStringArray, promoCodesAreEqual, setPromoCodeInCookie, setPromoCodesInLocalStorage };