@behindthescenes/cart 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/browser.js +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/manifest.json +2 -2
- package/dist/types/browser.d.ts +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/index.d.ts +25 -0
- package/dist/types/types/BTSCart.types.d.ts +98 -0
- package/dist/types/types/index.types.d.ts +4 -90
- package/dist/types/utils/attribution.util.d.ts +3 -0
- package/dist/types/utils/browserEnv.util.d.ts +5 -0
- package/package.json +1 -1
- package/dist/types/attribution.d.ts +0 -2
- /package/dist/types/types/{browser.types.d.ts → BTSCartBrowser.types.d.ts} +0 -0
package/dist/browser/browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var H="https://api.bts.it.com/v2/website/cart",Q="bts-cart",R="default",T=2;function z(){try{return globalThis.window??null}catch{return null}}function E(){try{return globalThis.localStorage??z()?.localStorage??null}catch{return null}}var x=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],W=["fbclid","gclid","gbraid","wbraid","ttclid","li_fat_id","msclkid","_fbp","_fbc"];function v(B){if(!B)return{utm:{},clickIds:{}};try{let F=JSON.parse(B);if(!F||typeof F!=="object"||Array.isArray(F))return{utm:{},clickIds:{}};let G=F,J=G.utm&&typeof G.utm==="object"&&!Array.isArray(G.utm)?Object.fromEntries(Object.entries(G.utm).filter((X)=>typeof X[1]==="string"&&X[1].length>0)):{},Z=G.clickIds&&typeof G.clickIds==="object"&&!Array.isArray(G.clickIds)?Object.fromEntries(Object.entries(G.clickIds).filter((X)=>typeof X[1]==="string"&&X[1].length>0)):{};return{utm:J,clickIds:Z}}catch{return{utm:{},clickIds:{}}}}function C(B,F){let G=new URL(B);G.searchParams.set("bts_site",F);let J=E(),Z=J?.getItem("bts_analytics_jt")?.trim();if(Z)G.searchParams.set("bts_jt",Z);let{utm:X,clickIds:Y}=v(J?.getItem("bts_analytics_attr")??null);for(let V of x){let M=X[V];if(M)G.searchParams.set(V,M)}for(let V of W){let M=Y[V];if(M)G.searchParams.set(V,M)}return G.toString()}class $ extends Error{code;status;details;constructor(B,F,G,J){super(F);this.name="BTSCartError",this.code=B,this.status=G,this.details=J}}function K(){return E()??void 0}function b(B){return(B??H).replace(/\/$/,"")}function I(B){if(!Number.isFinite(B??1))return 1;return Math.max(1,Math.floor(B??1))}function P(B){return{items:B.map((F)=>({...F}))}}function _(B){let F=B?.trim();return F&&F.length>0?F:void 0}function f(B){return B.accessTierId??B.productId??B.lineItemId}function w(B){if(B.discountEligibleTierOrProductIds?.length)return[...new Set(B.discountEligibleTierOrProductIds.filter(Boolean))];return[...new Set([B.accessTierId,B.productId,B.lineItemId].filter((F)=>Boolean(F)))]}function U(B){let F=new Set;for(let G of B)for(let J of[G.lineItemId,G.accessTierId,G.productId])if(J)F.add(J);return B.map((G)=>{if(!G.discount)return G;let J=w(G);if(J.length===0||!J.some((Z)=>F.has(Z))){let Z={...G};return delete Z.discount,delete Z.discountEligibleTierOrProductIds,Z}return G})}function g(B,F){let G=new Headers(B);if(!F)return G;return new Headers(F).forEach((J,Z)=>G.set(Z,J)),G}function p(B){let F=B.checkoutLinkId.trim();try{let G=F.includes("://")?F:F.startsWith("/")?`https://bts-cart.invalid${F}`:F,J=new URL(G),Z=J.pathname.split("/").filter(Boolean),X=Z.indexOf("c"),Y=X>=0?Z[X+1]:Z.at(-1);if(Y)return{checkoutLinkId:Y,discountCode:B.discountCode??J.searchParams.get("discountCode")??void 0}}catch{}return{checkoutLinkId:F,discountCode:B.discountCode}}class D{siteKey;endpoint;debug;persist;storageKey;storage;checkoutLinks;defaultCheckoutLinkKey;requestHeaders;items=[];listeners=new Set;constructor(B){this.siteKey=B.siteKey,this.endpoint=b(B.endpoint),this.debug=B.debug??!1,this.persist=B.persist??!0,this.storageKey=B.storageKey??Q,this.storage=B.storage??K(),this.checkoutLinks=B.checkoutLinks??{},this.defaultCheckoutLinkKey=B.defaultCheckoutLinkKey??Object.keys(this.checkoutLinks)[0]??R,this.requestHeaders=B.requestHeaders,this.rehydrate()}static init(B){return new D(B)}getItems(){return P(this.items).items}getState(){return P(this.items)}getItemCount(){return this.items.reduce((B,F)=>B+F.quantity,0)}getSubtotal(){return this.items.reduce((B,F)=>B+(F.fullPrice??F.unitPrice)*F.quantity,0)}getTotal(){return this.items.reduce((B,F)=>B+F.unitPrice*F.quantity,0)}subscribe(B){return this.listeners.add(B),B(this.getState()),()=>{this.listeners.delete(B)}}destroy(){this.listeners.clear()}rehydrate(){if(!this.persist||!this.storage)return;try{let B=this.storage.getItem(this.storageKey);if(!B)return;let F=JSON.parse(B);if(Array.isArray(F)||F.version!==T){this.storage.removeItem(this.storageKey);return}let G=F.items;if(!Array.isArray(G))return;this.items=U(G.map((J)=>this.normalizeItem(J)).filter((J)=>Boolean(J))),this.emit()}catch(B){this.log("Failed to hydrate cart",B)}}addItem(B){let F=this.normalizeItem(B);if(!F)return;let G=this.items.find((J)=>J.id===F.id);if(G){this.setQuantity(G.id,G.quantity+F.quantity);return}this.items=[...this.items,F],this.commit()}removeItem(B){this.items=this.items.filter((F)=>F.id!==B),this.commit()}setQuantity(B,F){let G=Math.floor(F);this.items=G<=0?this.items.filter((J)=>J.id!==B):this.items.map((J)=>J.id===B?{...J,quantity:G}:J),this.commit()}increment(B,F=1){let G=this.items.find((J)=>J.id===B);if(G)this.setQuantity(B,G.quantity+Math.max(1,Math.floor(F)))}decrement(B,F=1){let G=this.items.find((J)=>J.id===B);if(G)this.setQuantity(B,G.quantity-Math.max(1,Math.floor(F)))}clear(){if(this.items=[],this.persist&&this.storage)try{this.storage.removeItem(this.storageKey)}catch(B){this.log("Failed to clear cart storage",B)}this.emit()}async resolveCheckoutUrl(B=this.defaultCheckoutLinkKey){let F=this.checkoutLinks[B];if(!F)throw new $("checkout_link_not_found",`Unknown checkout link key: ${B}`);let G=p(F),J=this.itemsForCheckout(B,F),Z={siteKey:this.siteKey,checkoutLinkId:G.checkoutLinkId,mode:F.mode,...G.discountCode?{discountCode:G.discountCode}:{},...J.length>0?{items:J}:{}};try{let X=await this.post("/checkout-url",Z);return C(X.checkoutUrl,this.siteKey)}catch(X){if(X instanceof $)if(X.code==="discounts_invalid")throw new $(X.code,X.message,X.status,X.details);else throw new $(X.code,X.message,X.status);throw new $("request_failed","Failed to resolve checkout URL.",void 0,void 0)}}async checkout(B=this.defaultCheckoutLinkKey){try{let F=await this.resolveCheckoutUrl(B),G=z();if(G)G.location.assign(F);return F}catch(F){if(F instanceof $)if(F.code==="discounts_invalid"){for(let G of F.details??[]){if(G.lineItemId)this.items=this.items.map((J)=>J.lineItemId===G.lineItemId?{...J,discount:void 0}:J);if(G.productId)this.items=this.items.map((J)=>J.productId===G.productId?{...J,discount:void 0}:J);if(G.accessTierId)this.items=this.items.map((J)=>J.accessTierId===G.accessTierId?{...J,discount:void 0}:J)}throw this.commit(),new $(F.code,F.message,F.status,F.details)}else throw new $(F.code,F.message,F.status);throw new $("request_failed","Failed to resolve checkout URL.",void 0,void 0)}}itemsForCheckout(B,F){if(F.mode==="static")return[];let G=this.items.filter((J)=>J.checkoutLinkKey===B).map((J)=>({lineItemId:f(J),quantity:J.quantity,...J.discount?{discount:J.discount}:{}}));if(G.length===0)throw new $("cart_empty","Your cart is empty.");return G}normalizeItem(B){let F=_(B.accessTierId),G=_(B.productId),J=_(B.lineItemId)??F??G;if(!B.id||!J||typeof B.unitPrice!=="number")return null;let Z=typeof B.name==="string"&&B.name.trim().length>0?B.name.trim():"Item";return{...B,id:B.id,lineItemId:J,...F?{accessTierId:F}:{},...G?{productId:G}:{},checkoutLinkKey:B.checkoutLinkKey??this.defaultCheckoutLinkKey,name:Z,unitPrice:B.unitPrice,quantity:I(B.quantity)}}async post(B,F){let G=`${this.endpoint}${B}`,J=JSON.stringify(F),Z={"Content-Type":"application/json"},X={body:F,bodyText:J,endpoint:this.endpoint,headers:Z,path:B,siteKey:this.siteKey,url:G},Y=typeof this.requestHeaders==="function"?await this.requestHeaders(X):this.requestHeaders,V=g(Z,Y),M=await fetch(G,{method:"POST",headers:V,body:J}),A=await M.json().catch(()=>null);if(!M.ok){let j=typeof A?.code==="string"&&A.code.length>0?A.code:"request_failed",L=typeof A?.message==="string"&&A.message.length>0?A.message:"Cart request failed";throw new $(j,L,M.status)}if(!A||typeof A!=="object"||A.ok!==!0||typeof A.checkoutUrl!=="string"||A.checkoutUrl.length===0)throw new $("request_failed","Cart response was invalid.",M.status);return A}commit(){if(this.items=U(this.items),this.persist&&this.storage)try{let B={version:T,items:this.items};this.storage.setItem(this.storageKey,JSON.stringify(B))}catch(B){this.log("Failed to persist cart",B)}this.emit()}emit(){let B=this.getState();for(let F of this.listeners)F(B)}log(...B){if(this.debug)console.log("[@behindthescenes/cart]",...B)}}function O(B){return D.init(B)}function q(B){return typeof B==="object"&&B!==null&&!Array.isArray(B)}function S(B){let F=z();if(!F)return;let[G,J,Z]=B;if(typeof G!=="string")return;if(G==="config"){if(!q(J))return;F.btsCart?.destroy?.(),F.btsCart=O(J);return}let X=F.btsCart;if(!X)return;if(G==="add"&&q(J)){X.addItem(J);return}if(G==="remove"&&typeof J==="string"){X.removeItem(J);return}if(G==="quantity"&&typeof J==="string"&&typeof Z==="number"){X.setQuantity(J,Z);return}if(G==="clear"){X.clear();return}if(G==="checkout")X.checkout(typeof J==="string"?J:void 0)}var N=z();if(N){let B=Array.isArray(N.btsCartDataLayer)?[...N.btsCartDataLayer]:[];N.btsCartDataLayer=Array.isArray(N.btsCartDataLayer)?N.btsCartDataLayer:[],N.BTSCart={BTSCart:D,createBTSCart:O},N.createBTSCart=O;for(let F of B)S(Array.from(F));N.btsCartCommand=(...F)=>{N.btsCartDataLayer?.push(F),S(F)}}export{O as createBTSCart,D as BTSCart};
|
package/dist/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var{defineProperty:
|
|
1
|
+
var{defineProperty:z,getOwnPropertyNames:S,getOwnPropertyDescriptor:b}=Object,j=Object.prototype.hasOwnProperty;function x(B){return this[B]}var K=(B)=>{var F=(_??=new WeakMap).get(B),G;if(F)return F;if(F=z({},"__esModule",{value:!0}),B&&typeof B==="object"||typeof B==="function"){for(var J of S(B))if(!j.call(F,J))z(F,J,{get:x.bind(B,J),enumerable:!(G=b(B,J))||G.enumerable})}return _.set(B,F),F},_;var L=(B)=>B;function w(B,F){this[B]=L.bind(null,F)}var f=(B,F)=>{for(var G in F)z(B,G,{get:F[G],enumerable:!0,configurable:!0,set:w.bind(F,G)})};var a={};f(a,{resolveLineItemIdForCheckout:()=>C,createBTSCart:()=>u,BTSCartError:()=>$,BTSCart:()=>Y});module.exports=K(a);var D="https://api.bts.it.com/v2/website/cart",H="bts-cart",Q="default",A=2;function O(){try{return globalThis.window??null}catch{return null}}function V(){try{return globalThis.localStorage??O()?.localStorage??null}catch{return null}}var I=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],g=["fbclid","gclid","gbraid","wbraid","ttclid","li_fat_id","msclkid","_fbp","_fbc"];function h(B){if(!B)return{utm:{},clickIds:{}};try{let F=JSON.parse(B);if(!F||typeof F!=="object"||Array.isArray(F))return{utm:{},clickIds:{}};let G=F,J=G.utm&&typeof G.utm==="object"&&!Array.isArray(G.utm)?Object.fromEntries(Object.entries(G.utm).filter((X)=>typeof X[1]==="string"&&X[1].length>0)):{},Z=G.clickIds&&typeof G.clickIds==="object"&&!Array.isArray(G.clickIds)?Object.fromEntries(Object.entries(G.clickIds).filter((X)=>typeof X[1]==="string"&&X[1].length>0)):{};return{utm:J,clickIds:Z}}catch{return{utm:{},clickIds:{}}}}function U(B,F){let G=new URL(B);G.searchParams.set("bts_site",F);let J=V(),Z=J?.getItem("bts_analytics_jt")?.trim();if(Z)G.searchParams.set("bts_jt",Z);let{utm:X,clickIds:R}=h(J?.getItem("bts_analytics_attr")??null);for(let N of I){let M=X[N];if(M)G.searchParams.set(N,M)}for(let N of g){let M=R[N];if(M)G.searchParams.set(N,M)}return G.toString()}class $ extends Error{code;status;details;constructor(B,F,G,J){super(F);this.name="BTSCartError",this.code=B,this.status=G,this.details=J}}function p(){return V()??void 0}function y(B){return(B??D).replace(/\/$/,"")}function k(B){if(!Number.isFinite(B??1))return 1;return Math.max(1,Math.floor(B??1))}function T(B){return{items:B.map((F)=>({...F}))}}function P(B){let F=B?.trim();return F&&F.length>0?F:void 0}function C(B){return B.accessTierId??B.productId??B.lineItemId}function l(B){if(B.discountEligibleTierOrProductIds?.length)return[...new Set(B.discountEligibleTierOrProductIds.filter(Boolean))];return[...new Set([B.accessTierId,B.productId,B.lineItemId].filter((F)=>Boolean(F)))]}function W(B){let F=new Set;for(let G of B)for(let J of[G.lineItemId,G.accessTierId,G.productId])if(J)F.add(J);return B.map((G)=>{if(!G.discount)return G;let J=l(G);if(J.length===0||!J.some((Z)=>F.has(Z))){let Z={...G};return delete Z.discount,delete Z.discountEligibleTierOrProductIds,Z}return G})}function c(B,F){let G=new Headers(B);if(!F)return G;return new Headers(F).forEach((J,Z)=>G.set(Z,J)),G}function d(B){let F=B.checkoutLinkId.trim();try{let G=F.includes("://")?F:F.startsWith("/")?`https://bts-cart.invalid${F}`:F,J=new URL(G),Z=J.pathname.split("/").filter(Boolean),X=Z.indexOf("c"),R=X>=0?Z[X+1]:Z.at(-1);if(R)return{checkoutLinkId:R,discountCode:B.discountCode??J.searchParams.get("discountCode")??void 0}}catch{}return{checkoutLinkId:F,discountCode:B.discountCode}}class Y{siteKey;endpoint;debug;persist;storageKey;storage;checkoutLinks;defaultCheckoutLinkKey;requestHeaders;items=[];listeners=new Set;constructor(B){this.siteKey=B.siteKey,this.endpoint=y(B.endpoint),this.debug=B.debug??!1,this.persist=B.persist??!0,this.storageKey=B.storageKey??H,this.storage=B.storage??p(),this.checkoutLinks=B.checkoutLinks??{},this.defaultCheckoutLinkKey=B.defaultCheckoutLinkKey??Object.keys(this.checkoutLinks)[0]??Q,this.requestHeaders=B.requestHeaders,this.rehydrate()}static init(B){return new Y(B)}getItems(){return T(this.items).items}getState(){return T(this.items)}getItemCount(){return this.items.reduce((B,F)=>B+F.quantity,0)}getSubtotal(){return this.items.reduce((B,F)=>B+(F.fullPrice??F.unitPrice)*F.quantity,0)}getTotal(){return this.items.reduce((B,F)=>B+F.unitPrice*F.quantity,0)}subscribe(B){return this.listeners.add(B),B(this.getState()),()=>{this.listeners.delete(B)}}destroy(){this.listeners.clear()}rehydrate(){if(!this.persist||!this.storage)return;try{let B=this.storage.getItem(this.storageKey);if(!B)return;let F=JSON.parse(B);if(Array.isArray(F)||F.version!==A){this.storage.removeItem(this.storageKey);return}let G=F.items;if(!Array.isArray(G))return;this.items=W(G.map((J)=>this.normalizeItem(J)).filter((J)=>Boolean(J))),this.emit()}catch(B){this.log("Failed to hydrate cart",B)}}addItem(B){let F=this.normalizeItem(B);if(!F)return;let G=this.items.find((J)=>J.id===F.id);if(G){this.setQuantity(G.id,G.quantity+F.quantity);return}this.items=[...this.items,F],this.commit()}removeItem(B){this.items=this.items.filter((F)=>F.id!==B),this.commit()}setQuantity(B,F){let G=Math.floor(F);this.items=G<=0?this.items.filter((J)=>J.id!==B):this.items.map((J)=>J.id===B?{...J,quantity:G}:J),this.commit()}increment(B,F=1){let G=this.items.find((J)=>J.id===B);if(G)this.setQuantity(B,G.quantity+Math.max(1,Math.floor(F)))}decrement(B,F=1){let G=this.items.find((J)=>J.id===B);if(G)this.setQuantity(B,G.quantity-Math.max(1,Math.floor(F)))}clear(){if(this.items=[],this.persist&&this.storage)try{this.storage.removeItem(this.storageKey)}catch(B){this.log("Failed to clear cart storage",B)}this.emit()}async resolveCheckoutUrl(B=this.defaultCheckoutLinkKey){let F=this.checkoutLinks[B];if(!F)throw new $("checkout_link_not_found",`Unknown checkout link key: ${B}`);let G=d(F),J=this.itemsForCheckout(B,F),Z={siteKey:this.siteKey,checkoutLinkId:G.checkoutLinkId,mode:F.mode,...G.discountCode?{discountCode:G.discountCode}:{},...J.length>0?{items:J}:{}};try{let X=await this.post("/checkout-url",Z);return U(X.checkoutUrl,this.siteKey)}catch(X){if(X instanceof $)if(X.code==="discounts_invalid")throw new $(X.code,X.message,X.status,X.details);else throw new $(X.code,X.message,X.status);throw new $("request_failed","Failed to resolve checkout URL.",void 0,void 0)}}async checkout(B=this.defaultCheckoutLinkKey){try{let F=await this.resolveCheckoutUrl(B),G=O();if(G)G.location.assign(F);return F}catch(F){if(F instanceof $)if(F.code==="discounts_invalid"){for(let G of F.details??[]){if(G.lineItemId)this.items=this.items.map((J)=>J.lineItemId===G.lineItemId?{...J,discount:void 0}:J);if(G.productId)this.items=this.items.map((J)=>J.productId===G.productId?{...J,discount:void 0}:J);if(G.accessTierId)this.items=this.items.map((J)=>J.accessTierId===G.accessTierId?{...J,discount:void 0}:J)}throw this.commit(),new $(F.code,F.message,F.status,F.details)}else throw new $(F.code,F.message,F.status);throw new $("request_failed","Failed to resolve checkout URL.",void 0,void 0)}}itemsForCheckout(B,F){if(F.mode==="static")return[];let G=this.items.filter((J)=>J.checkoutLinkKey===B).map((J)=>({lineItemId:C(J),quantity:J.quantity,...J.discount?{discount:J.discount}:{}}));if(G.length===0)throw new $("cart_empty","Your cart is empty.");return G}normalizeItem(B){let F=P(B.accessTierId),G=P(B.productId),J=P(B.lineItemId)??F??G;if(!B.id||!J||typeof B.unitPrice!=="number")return null;let Z=typeof B.name==="string"&&B.name.trim().length>0?B.name.trim():"Item";return{...B,id:B.id,lineItemId:J,...F?{accessTierId:F}:{},...G?{productId:G}:{},checkoutLinkKey:B.checkoutLinkKey??this.defaultCheckoutLinkKey,name:Z,unitPrice:B.unitPrice,quantity:k(B.quantity)}}async post(B,F){let G=`${this.endpoint}${B}`,J=JSON.stringify(F),Z={"Content-Type":"application/json"},X={body:F,bodyText:J,endpoint:this.endpoint,headers:Z,path:B,siteKey:this.siteKey,url:G},R=typeof this.requestHeaders==="function"?await this.requestHeaders(X):this.requestHeaders,N=c(Z,R),M=await fetch(G,{method:"POST",headers:N,body:J}),E=await M.json().catch(()=>null);if(!M.ok){let q=typeof E?.code==="string"&&E.code.length>0?E.code:"request_failed",v=typeof E?.message==="string"&&E.message.length>0?E.message:"Cart request failed";throw new $(q,v,M.status)}if(!E||typeof E!=="object"||E.ok!==!0||typeof E.checkoutUrl!=="string"||E.checkoutUrl.length===0)throw new $("request_failed","Cart response was invalid.",M.status);return E}commit(){if(this.items=W(this.items),this.persist&&this.storage)try{let B={version:A,items:this.items};this.storage.setItem(this.storageKey,JSON.stringify(B))}catch(B){this.log("Failed to persist cart",B)}this.emit()}emit(){let B=this.getState();for(let F of this.listeners)F(B)}log(...B){if(this.debug)console.log("[@behindthescenes/cart]",...B)}}function u(B){return Y.init(B)}
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var P="https://api.bts.it.com/v2/website/cart",_="bts-cart",D="default",Y=2;function z(){try{return globalThis.window??null}catch{return null}}function V(){try{return globalThis.localStorage??z()?.localStorage??null}catch{return null}}var C=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],q=["fbclid","gclid","gbraid","wbraid","ttclid","li_fat_id","msclkid","_fbp","_fbc"];function v(B){if(!B)return{utm:{},clickIds:{}};try{let F=JSON.parse(B);if(!F||typeof F!=="object"||Array.isArray(F))return{utm:{},clickIds:{}};let G=F,J=G.utm&&typeof G.utm==="object"&&!Array.isArray(G.utm)?Object.fromEntries(Object.entries(G.utm).filter((X)=>typeof X[1]==="string"&&X[1].length>0)):{},Z=G.clickIds&&typeof G.clickIds==="object"&&!Array.isArray(G.clickIds)?Object.fromEntries(Object.entries(G.clickIds).filter((X)=>typeof X[1]==="string"&&X[1].length>0)):{};return{utm:J,clickIds:Z}}catch{return{utm:{},clickIds:{}}}}function H(B,F){let G=new URL(B);G.searchParams.set("bts_site",F);let J=V(),Z=J?.getItem("bts_analytics_jt")?.trim();if(Z)G.searchParams.set("bts_jt",Z);let{utm:X,clickIds:R}=v(J?.getItem("bts_analytics_attr")??null);for(let N of C){let M=X[N];if(M)G.searchParams.set(N,M)}for(let N of q){let M=R[N];if(M)G.searchParams.set(N,M)}return G.toString()}class $ extends Error{code;status;details;constructor(B,F,G,J){super(F);this.name="BTSCartError",this.code=B,this.status=G,this.details=J}}function S(){return V()??void 0}function b(B){return(B??P).replace(/\/$/,"")}function j(B){if(!Number.isFinite(B??1))return 1;return Math.max(1,Math.floor(B??1))}function Q(B){return{items:B.map((F)=>({...F}))}}function A(B){let F=B?.trim();return F&&F.length>0?F:void 0}function x(B){return B.accessTierId??B.productId??B.lineItemId}function K(B){if(B.discountEligibleTierOrProductIds?.length)return[...new Set(B.discountEligibleTierOrProductIds.filter(Boolean))];return[...new Set([B.accessTierId,B.productId,B.lineItemId].filter((F)=>Boolean(F)))]}function U(B){let F=new Set;for(let G of B)for(let J of[G.lineItemId,G.accessTierId,G.productId])if(J)F.add(J);return B.map((G)=>{if(!G.discount)return G;let J=K(G);if(J.length===0||!J.some((Z)=>F.has(Z))){let Z={...G};return delete Z.discount,delete Z.discountEligibleTierOrProductIds,Z}return G})}function L(B,F){let G=new Headers(B);if(!F)return G;return new Headers(F).forEach((J,Z)=>G.set(Z,J)),G}function w(B){let F=B.checkoutLinkId.trim();try{let G=F.includes("://")?F:F.startsWith("/")?`https://bts-cart.invalid${F}`:F,J=new URL(G),Z=J.pathname.split("/").filter(Boolean),X=Z.indexOf("c"),R=X>=0?Z[X+1]:Z.at(-1);if(R)return{checkoutLinkId:R,discountCode:B.discountCode??J.searchParams.get("discountCode")??void 0}}catch{}return{checkoutLinkId:F,discountCode:B.discountCode}}class O{siteKey;endpoint;debug;persist;storageKey;storage;checkoutLinks;defaultCheckoutLinkKey;requestHeaders;items=[];listeners=new Set;constructor(B){this.siteKey=B.siteKey,this.endpoint=b(B.endpoint),this.debug=B.debug??!1,this.persist=B.persist??!0,this.storageKey=B.storageKey??_,this.storage=B.storage??S(),this.checkoutLinks=B.checkoutLinks??{},this.defaultCheckoutLinkKey=B.defaultCheckoutLinkKey??Object.keys(this.checkoutLinks)[0]??D,this.requestHeaders=B.requestHeaders,this.rehydrate()}static init(B){return new O(B)}getItems(){return Q(this.items).items}getState(){return Q(this.items)}getItemCount(){return this.items.reduce((B,F)=>B+F.quantity,0)}getSubtotal(){return this.items.reduce((B,F)=>B+(F.fullPrice??F.unitPrice)*F.quantity,0)}getTotal(){return this.items.reduce((B,F)=>B+F.unitPrice*F.quantity,0)}subscribe(B){return this.listeners.add(B),B(this.getState()),()=>{this.listeners.delete(B)}}destroy(){this.listeners.clear()}rehydrate(){if(!this.persist||!this.storage)return;try{let B=this.storage.getItem(this.storageKey);if(!B)return;let F=JSON.parse(B);if(Array.isArray(F)||F.version!==Y){this.storage.removeItem(this.storageKey);return}let G=F.items;if(!Array.isArray(G))return;this.items=U(G.map((J)=>this.normalizeItem(J)).filter((J)=>Boolean(J))),this.emit()}catch(B){this.log("Failed to hydrate cart",B)}}addItem(B){let F=this.normalizeItem(B);if(!F)return;let G=this.items.find((J)=>J.id===F.id);if(G){this.setQuantity(G.id,G.quantity+F.quantity);return}this.items=[...this.items,F],this.commit()}removeItem(B){this.items=this.items.filter((F)=>F.id!==B),this.commit()}setQuantity(B,F){let G=Math.floor(F);this.items=G<=0?this.items.filter((J)=>J.id!==B):this.items.map((J)=>J.id===B?{...J,quantity:G}:J),this.commit()}increment(B,F=1){let G=this.items.find((J)=>J.id===B);if(G)this.setQuantity(B,G.quantity+Math.max(1,Math.floor(F)))}decrement(B,F=1){let G=this.items.find((J)=>J.id===B);if(G)this.setQuantity(B,G.quantity-Math.max(1,Math.floor(F)))}clear(){if(this.items=[],this.persist&&this.storage)try{this.storage.removeItem(this.storageKey)}catch(B){this.log("Failed to clear cart storage",B)}this.emit()}async resolveCheckoutUrl(B=this.defaultCheckoutLinkKey){let F=this.checkoutLinks[B];if(!F)throw new $("checkout_link_not_found",`Unknown checkout link key: ${B}`);let G=w(F),J=this.itemsForCheckout(B,F),Z={siteKey:this.siteKey,checkoutLinkId:G.checkoutLinkId,mode:F.mode,...G.discountCode?{discountCode:G.discountCode}:{},...J.length>0?{items:J}:{}};try{let X=await this.post("/checkout-url",Z);return H(X.checkoutUrl,this.siteKey)}catch(X){if(X instanceof $)if(X.code==="discounts_invalid")throw new $(X.code,X.message,X.status,X.details);else throw new $(X.code,X.message,X.status);throw new $("request_failed","Failed to resolve checkout URL.",void 0,void 0)}}async checkout(B=this.defaultCheckoutLinkKey){try{let F=await this.resolveCheckoutUrl(B),G=z();if(G)G.location.assign(F);return F}catch(F){if(F instanceof $)if(F.code==="discounts_invalid"){for(let G of F.details??[]){if(G.lineItemId)this.items=this.items.map((J)=>J.lineItemId===G.lineItemId?{...J,discount:void 0}:J);if(G.productId)this.items=this.items.map((J)=>J.productId===G.productId?{...J,discount:void 0}:J);if(G.accessTierId)this.items=this.items.map((J)=>J.accessTierId===G.accessTierId?{...J,discount:void 0}:J)}throw this.commit(),new $(F.code,F.message,F.status,F.details)}else throw new $(F.code,F.message,F.status);throw new $("request_failed","Failed to resolve checkout URL.",void 0,void 0)}}itemsForCheckout(B,F){if(F.mode==="static")return[];let G=this.items.filter((J)=>J.checkoutLinkKey===B).map((J)=>({lineItemId:x(J),quantity:J.quantity,...J.discount?{discount:J.discount}:{}}));if(G.length===0)throw new $("cart_empty","Your cart is empty.");return G}normalizeItem(B){let F=A(B.accessTierId),G=A(B.productId),J=A(B.lineItemId)??F??G;if(!B.id||!J||typeof B.unitPrice!=="number")return null;let Z=typeof B.name==="string"&&B.name.trim().length>0?B.name.trim():"Item";return{...B,id:B.id,lineItemId:J,...F?{accessTierId:F}:{},...G?{productId:G}:{},checkoutLinkKey:B.checkoutLinkKey??this.defaultCheckoutLinkKey,name:Z,unitPrice:B.unitPrice,quantity:j(B.quantity)}}async post(B,F){let G=`${this.endpoint}${B}`,J=JSON.stringify(F),Z={"Content-Type":"application/json"},X={body:F,bodyText:J,endpoint:this.endpoint,headers:Z,path:B,siteKey:this.siteKey,url:G},R=typeof this.requestHeaders==="function"?await this.requestHeaders(X):this.requestHeaders,N=L(Z,R),M=await fetch(G,{method:"POST",headers:N,body:J}),E=await M.json().catch(()=>null);if(!M.ok){let T=typeof E?.code==="string"&&E.code.length>0?E.code:"request_failed",W=typeof E?.message==="string"&&E.message.length>0?E.message:"Cart request failed";throw new $(T,W,M.status)}if(!E||typeof E!=="object"||E.ok!==!0||typeof E.checkoutUrl!=="string"||E.checkoutUrl.length===0)throw new $("request_failed","Cart response was invalid.",M.status);return E}commit(){if(this.items=U(this.items),this.persist&&this.storage)try{let B={version:Y,items:this.items};this.storage.setItem(this.storageKey,JSON.stringify(B))}catch(B){this.log("Failed to persist cart",B)}this.emit()}emit(){let B=this.getState();for(let F of this.listeners)F(B)}log(...B){if(this.debug)console.log("[@behindthescenes/cart]",...B)}}function a(B){return O.init(B)}export{x as resolveLineItemIdForCheckout,a as createBTSCart,$ as BTSCartError,O as BTSCart};
|
package/dist/manifest.json
CHANGED
package/dist/types/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { BTSCart, createBTSCart } from "./index";
|
|
2
|
-
import "./types/
|
|
2
|
+
import "./types/BTSCartBrowser.types";
|
|
3
3
|
export { BTSCart, createBTSCart };
|
|
4
4
|
export type { BTSCartCheckoutItemPayload, BTSCartCheckoutLinkConfig, BTSCartCheckoutUrlRequest, BTSCartCheckoutUrlResult, BTSCartInit, BTSCartItem, BTSCartItemInput, BTSCartState, } from "./types/index.types";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const DEFAULT_ENDPOINT = "https://api.bts.it.com/v2/website/cart";
|
|
2
2
|
export declare const DEFAULT_STORAGE_KEY = "bts-cart";
|
|
3
3
|
export declare const DEFAULT_CHECKOUT_LINK_KEY = "default";
|
|
4
|
-
export declare const STORAGE_VERSION =
|
|
4
|
+
export declare const STORAGE_VERSION = 2;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -16,31 +16,56 @@ export declare class BTSCart {
|
|
|
16
16
|
private requestHeaders?;
|
|
17
17
|
private items;
|
|
18
18
|
private listeners;
|
|
19
|
+
/** Hydrate the cart from init options and restore any persisted state. */
|
|
19
20
|
constructor(init: BTSCartInit);
|
|
21
|
+
/** Convenience constructor matching the analytics package's static `init` API. */
|
|
20
22
|
static init(opts: BTSCartInit): BTSCart;
|
|
23
|
+
/** Return a defensive copy of cart items for read-only external consumers. */
|
|
21
24
|
getItems(): BTSCartItem[];
|
|
25
|
+
/** Return the full cart state object expected by subscribers and embedders. */
|
|
22
26
|
getState(): BTSCartState;
|
|
27
|
+
/** Count total units across all lines, not just distinct items. */
|
|
23
28
|
getItemCount(): number;
|
|
29
|
+
/** Sum undiscounted line totals using `fullPrice` when it exists. */
|
|
24
30
|
getSubtotal(): number;
|
|
31
|
+
/** Sum discounted line totals using the actual checkout unit price. */
|
|
25
32
|
getTotal(): number;
|
|
33
|
+
/** Register a listener and immediately emit the current state for simple UI synchronization. */
|
|
26
34
|
subscribe(listener: BTSCartListener): () => void;
|
|
35
|
+
/** Tear down subscriptions without mutating the cart contents. */
|
|
27
36
|
destroy(): void;
|
|
37
|
+
/** Restore a versioned cart snapshot from storage and normalize legacy item shapes on read. */
|
|
28
38
|
rehydrate(): void;
|
|
39
|
+
/** Add a new line or merge into an existing line with the same SDK item id. */
|
|
29
40
|
addItem(input: BTSCartItemInput): void;
|
|
41
|
+
/** Remove a line entirely by its SDK item id. */
|
|
30
42
|
removeItem(id: string): void;
|
|
43
|
+
/** Replace a line quantity, dropping the line when the requested quantity is zero or below. */
|
|
31
44
|
setQuantity(id: string, quantity: number): void;
|
|
45
|
+
/** Increase a line quantity using a clamped positive integer increment. */
|
|
32
46
|
increment(id: string, amount?: number): void;
|
|
47
|
+
/** Decrease a line quantity using a clamped positive integer decrement. */
|
|
33
48
|
decrement(id: string, amount?: number): void;
|
|
49
|
+
/** Empty the in-memory cart and remove the persisted snapshot when persistence is enabled. */
|
|
34
50
|
clear(): void;
|
|
51
|
+
/** Build a checkout URL via the cart API, then append browser attribution for downstream analytics. */
|
|
35
52
|
resolveCheckoutUrl(checkoutLinkKey?: string): Promise<string>;
|
|
53
|
+
/** Resolve a checkout URL and navigate the current browser when one is available. */
|
|
36
54
|
checkout(checkoutLinkKey?: string): Promise<string>;
|
|
55
|
+
/** Convert current cart lines into the API payload expected by dynamic checkout links. */
|
|
37
56
|
private itemsForCheckout;
|
|
57
|
+
/** Normalize partial external item input into the persisted cart line shape. */
|
|
38
58
|
private normalizeItem;
|
|
59
|
+
/** POST a cart request with optional caller-supplied headers and normalized API errors. */
|
|
39
60
|
private post;
|
|
61
|
+
/** Recompute derived cart state, persist it when enabled, and notify subscribers. */
|
|
40
62
|
private commit;
|
|
63
|
+
/** Fan out the latest immutable state snapshot to all listeners. */
|
|
41
64
|
private emit;
|
|
65
|
+
/** Emit debug logs only when the SDK was initialized in debug mode. */
|
|
42
66
|
private log;
|
|
43
67
|
}
|
|
68
|
+
/** Public package factory mirroring the class-based `BTSCart.init` constructor. */
|
|
44
69
|
export declare function createBTSCart(init: BTSCartInit): BTSCart;
|
|
45
70
|
export type { BTSCartCheckoutItemPayload, BTSCartCheckoutLinkConfig, BTSCartCheckoutUrlRequest, BTSCartCheckoutUrlResult, BTSCartErrorCode, BTSCartInit, BTSCartItem, BTSCartItemInput, BTSCartListener, BTSCartMode, BTSCartRequestContext, BTSCartRequestHeaders, BTSCartState, BTSCartStorage, } from "./types/index.types";
|
|
46
71
|
export { BTSCartError } from "./types/index.types";
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export type BTSCartMode = "static" | "dynamic";
|
|
2
|
+
export type BTSCartStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;
|
|
3
|
+
export type BTSCartRequestContext = {
|
|
4
|
+
body: unknown;
|
|
5
|
+
bodyText: string;
|
|
6
|
+
endpoint: string;
|
|
7
|
+
headers: Record<string, string>;
|
|
8
|
+
path: string;
|
|
9
|
+
siteKey: string;
|
|
10
|
+
url: string;
|
|
11
|
+
};
|
|
12
|
+
export type BTSCartRequestHeaders = HeadersInit | ((request: BTSCartRequestContext) => HeadersInit | Promise<HeadersInit>);
|
|
13
|
+
export type BTSCartCheckoutLinkConfig = {
|
|
14
|
+
/** Checkout link UUID, or a full checkout URL (`https://…/c/<uuid>?discountCode=…`). Relative `/c/<uuid>` is supported. */
|
|
15
|
+
checkoutLinkId: string;
|
|
16
|
+
mode: BTSCartMode;
|
|
17
|
+
discountCode?: string;
|
|
18
|
+
};
|
|
19
|
+
export type BTSCartItemInput = {
|
|
20
|
+
id: string;
|
|
21
|
+
/**
|
|
22
|
+
* Sent as `lineItemId` on `POST /v2/website/cart/checkout-url` when no {@link productId} or
|
|
23
|
+
* {@link accessTierId} is set. Must match a resolved line on the link: the row’s `id`, or its
|
|
24
|
+
* `product_id` / `access_tier_id` (see `resolveDynamicCheckoutCartLineItem` in `@bts/common`).
|
|
25
|
+
* For dynamic catalog links, prefer {@link accessTierId} or {@link productId} instead of a
|
|
26
|
+
* site-local slug.
|
|
27
|
+
*/
|
|
28
|
+
lineItemId?: string;
|
|
29
|
+
checkoutLinkKey?: string;
|
|
30
|
+
/** BTS product UUID; preferred checkout identifier for product lines on dynamic links. */
|
|
31
|
+
productId?: string;
|
|
32
|
+
/** BTS access tier UUID; preferred checkout identifier for tier lines on dynamic links. */
|
|
33
|
+
accessTierId?: string;
|
|
34
|
+
/** Display only in the SDK; defaults to `"Item"` if empty. Not sent to the cart API. */
|
|
35
|
+
name: string;
|
|
36
|
+
unitPrice: number;
|
|
37
|
+
fullPrice?: number;
|
|
38
|
+
image?: string;
|
|
39
|
+
quantity?: number;
|
|
40
|
+
/** Internal promo-code ID or customer-facing promo code. Customer codes are resolved by the BTS cart API. */
|
|
41
|
+
discount?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Optional catalog identifiers for embedders documenting promo scope. The SDK persists them but
|
|
44
|
+
* does not clear discounts client-side; the cart API validates promos on checkout-url.
|
|
45
|
+
*/
|
|
46
|
+
discountEligibleTierOrProductIds?: string[];
|
|
47
|
+
};
|
|
48
|
+
export type BTSCartItem = Required<Pick<BTSCartItemInput, "id" | "lineItemId" | "name" | "unitPrice" | "quantity">> & Omit<BTSCartItemInput, "quantity"> & {
|
|
49
|
+
checkoutLinkKey: string;
|
|
50
|
+
};
|
|
51
|
+
export type BTSCartState = {
|
|
52
|
+
items: BTSCartItem[];
|
|
53
|
+
};
|
|
54
|
+
export type BTSCartListener = (state: BTSCartState) => void;
|
|
55
|
+
export type BTSCartInit = {
|
|
56
|
+
siteKey: string;
|
|
57
|
+
endpoint?: string;
|
|
58
|
+
checkoutLinks?: Record<string, BTSCartCheckoutLinkConfig>;
|
|
59
|
+
defaultCheckoutLinkKey?: string;
|
|
60
|
+
debug?: boolean;
|
|
61
|
+
persist?: boolean;
|
|
62
|
+
storageKey?: string;
|
|
63
|
+
storage?: BTSCartStorage;
|
|
64
|
+
requestHeaders?: BTSCartRequestHeaders;
|
|
65
|
+
};
|
|
66
|
+
export type BTSCartCheckoutItemPayload = {
|
|
67
|
+
lineItemId: string;
|
|
68
|
+
quantity: number;
|
|
69
|
+
discount?: string;
|
|
70
|
+
};
|
|
71
|
+
export type BTSCartCheckoutUrlRequest = {
|
|
72
|
+
siteKey: string;
|
|
73
|
+
checkoutLinkId: string;
|
|
74
|
+
mode: BTSCartMode;
|
|
75
|
+
items?: BTSCartCheckoutItemPayload[];
|
|
76
|
+
discountCode?: string;
|
|
77
|
+
};
|
|
78
|
+
export type BTSCartCheckoutUrlResult = {
|
|
79
|
+
ok: true;
|
|
80
|
+
checkoutUrl: string;
|
|
81
|
+
checkoutLinkId: string;
|
|
82
|
+
mode: BTSCartMode;
|
|
83
|
+
items: BTSCartCheckoutItemPayload[];
|
|
84
|
+
};
|
|
85
|
+
export type BTSCartErrorCode = "unknown_site" | "domain_not_verified" | "analytics_disabled" | "checkout_link_not_found" | "checkout_link_inactive" | "checkout_link_expired" | "checkout_link_sold_out" | "checkout_link_mode_mismatch" | "cart_empty" | "line_item_not_found" | "invalid_quantity" | "line_item_sold_out" | "quantity_exceeded" | "product_unavailable" | "discounts_invalid" | "request_failed" | "discounts_invalid";
|
|
86
|
+
export declare class BTSCartError extends Error {
|
|
87
|
+
code: BTSCartErrorCode;
|
|
88
|
+
status?: number;
|
|
89
|
+
details?: {
|
|
90
|
+
lineItemId: string;
|
|
91
|
+
productId?: string;
|
|
92
|
+
accessTierId?: string;
|
|
93
|
+
discountCode: string;
|
|
94
|
+
discountId?: string;
|
|
95
|
+
reason: string;
|
|
96
|
+
}[];
|
|
97
|
+
constructor(code: BTSCartErrorCode, message: string, status?: number, details?: BTSCartError["details"]);
|
|
98
|
+
}
|
|
@@ -1,90 +1,4 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export type
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
bodyText: string;
|
|
6
|
-
endpoint: string;
|
|
7
|
-
headers: Record<string, string>;
|
|
8
|
-
path: string;
|
|
9
|
-
siteKey: string;
|
|
10
|
-
url: string;
|
|
11
|
-
};
|
|
12
|
-
export type BTSCartRequestHeaders = HeadersInit | ((request: BTSCartRequestContext) => HeadersInit | Promise<HeadersInit>);
|
|
13
|
-
export type BTSCartCheckoutLinkConfig = {
|
|
14
|
-
/** Checkout link UUID, or a full checkout URL (`https://…/c/<uuid>?discountCode=…`). Relative `/c/<uuid>` is supported. */
|
|
15
|
-
checkoutLinkId: string;
|
|
16
|
-
mode: BTSCartMode;
|
|
17
|
-
discountCode?: string;
|
|
18
|
-
};
|
|
19
|
-
export type BTSCartItemInput = {
|
|
20
|
-
id: string;
|
|
21
|
-
/**
|
|
22
|
-
* Sent as `lineItemId` on `POST /v2/website/cart/checkout-url` when no {@link productId} or
|
|
23
|
-
* {@link accessTierId} is set. Must match a resolved line on the link: the row’s `id`, or its
|
|
24
|
-
* `product_id` / `access_tier_id` (see `resolveDynamicCheckoutCartLineItem` in `@bts/common`).
|
|
25
|
-
* For dynamic catalog links, prefer {@link accessTierId} or {@link productId} instead of a
|
|
26
|
-
* site-local slug.
|
|
27
|
-
*/
|
|
28
|
-
lineItemId?: string;
|
|
29
|
-
checkoutLinkKey?: string;
|
|
30
|
-
/** BTS product UUID; preferred checkout identifier for product lines on dynamic links. */
|
|
31
|
-
productId?: string;
|
|
32
|
-
/** BTS access tier UUID; preferred checkout identifier for tier lines on dynamic links. */
|
|
33
|
-
accessTierId?: string;
|
|
34
|
-
/** Display only in the SDK; defaults to `"Item"` if empty. Not sent to the cart API. */
|
|
35
|
-
name: string;
|
|
36
|
-
unitPrice: number;
|
|
37
|
-
fullPrice?: number;
|
|
38
|
-
image?: string;
|
|
39
|
-
quantity?: number;
|
|
40
|
-
/** Internal promo-code ID or customer-facing promo code. Customer codes are resolved by the BTS cart API. */
|
|
41
|
-
discount?: string;
|
|
42
|
-
/**
|
|
43
|
-
* Optional catalog identifiers for embedders documenting promo scope. The SDK persists them but
|
|
44
|
-
* does not clear discounts client-side; the cart API validates promos on checkout-url.
|
|
45
|
-
*/
|
|
46
|
-
discountEligibleTierOrProductIds?: string[];
|
|
47
|
-
};
|
|
48
|
-
export type BTSCartItem = Required<Pick<BTSCartItemInput, "id" | "lineItemId" | "name" | "unitPrice" | "quantity">> & Omit<BTSCartItemInput, "quantity"> & {
|
|
49
|
-
checkoutLinkKey: string;
|
|
50
|
-
};
|
|
51
|
-
export type BTSCartState = {
|
|
52
|
-
items: BTSCartItem[];
|
|
53
|
-
};
|
|
54
|
-
export type BTSCartListener = (state: BTSCartState) => void;
|
|
55
|
-
export type BTSCartInit = {
|
|
56
|
-
siteKey: string;
|
|
57
|
-
endpoint?: string;
|
|
58
|
-
checkoutLinks?: Record<string, BTSCartCheckoutLinkConfig>;
|
|
59
|
-
defaultCheckoutLinkKey?: string;
|
|
60
|
-
debug?: boolean;
|
|
61
|
-
persist?: boolean;
|
|
62
|
-
storageKey?: string;
|
|
63
|
-
storage?: BTSCartStorage;
|
|
64
|
-
requestHeaders?: BTSCartRequestHeaders;
|
|
65
|
-
};
|
|
66
|
-
export type BTSCartCheckoutItemPayload = {
|
|
67
|
-
lineItemId: string;
|
|
68
|
-
quantity: number;
|
|
69
|
-
discount?: string;
|
|
70
|
-
};
|
|
71
|
-
export type BTSCartCheckoutUrlRequest = {
|
|
72
|
-
siteKey: string;
|
|
73
|
-
checkoutLinkId: string;
|
|
74
|
-
mode: BTSCartMode;
|
|
75
|
-
items?: BTSCartCheckoutItemPayload[];
|
|
76
|
-
discountCode?: string;
|
|
77
|
-
};
|
|
78
|
-
export type BTSCartCheckoutUrlResult = {
|
|
79
|
-
ok: true;
|
|
80
|
-
checkoutUrl: string;
|
|
81
|
-
checkoutLinkId: string;
|
|
82
|
-
mode: BTSCartMode;
|
|
83
|
-
items: BTSCartCheckoutItemPayload[];
|
|
84
|
-
};
|
|
85
|
-
export type BTSCartErrorCode = "unknown_site" | "domain_not_verified" | "analytics_disabled" | "checkout_link_not_found" | "checkout_link_inactive" | "checkout_link_expired" | "checkout_link_sold_out" | "checkout_link_mode_mismatch" | "cart_empty" | "line_item_not_found" | "invalid_quantity" | "line_item_sold_out" | "quantity_exceeded" | "product_unavailable" | "discount_invalid" | "request_failed";
|
|
86
|
-
export declare class BTSCartError extends Error {
|
|
87
|
-
code: BTSCartErrorCode;
|
|
88
|
-
status?: number;
|
|
89
|
-
constructor(code: BTSCartErrorCode, message: string, status?: number);
|
|
90
|
-
}
|
|
1
|
+
export type * from "./BTSCart.types";
|
|
2
|
+
export type * as BTSCartBrowserTypes from "./BTSCartBrowser.types";
|
|
3
|
+
export type * as BTSCartTypes from "./BTSCart.types";
|
|
4
|
+
export { BTSCartError } from "./BTSCart.types";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Read `window` defensively so Node / SSR bundles never crash on a missing global. */
|
|
2
|
+
declare function safeWindow(): Window | null;
|
|
3
|
+
/** Prefer a directly mocked `globalThis.localStorage`, then fall back to `window.localStorage`. */
|
|
4
|
+
declare function safeStorage(): Storage | null;
|
|
5
|
+
export { safeStorage, safeWindow };
|
package/package.json
CHANGED
|
File without changes
|