@nosto/nosto-react 2.12.1 → 2.12.2
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/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("react"),require("react/jsx-runtime"),require("react-dom/client")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime","react-dom/client"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r["@nosto/nosto-react"]={},r.React,r["react/jsx-runtime"],r.client))})(this,function(r,a,w,H){"use strict";const y=a.createContext({account:"",currentVariation:"",responseMode:"HTML",clientScriptLoaded:!1});function g(){return a.useContext(y)}const P=e=>String(e)==="[object Object]";function O(e){if(!P(e))return!1;const t=e.constructor;if(t===void 0)return!0;const n=t.prototype;return!(!P(n)||!n.hasOwnProperty("isPrototypeOf"))}function N(e,t){if(e===t)return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof Array&&t instanceof Array)return e.length!==t.length?!1:e.every((n,o)=>N(n,t[o]));if(O(e)&&O(t)){const n=Object.entries(e);return n.length!==Object.keys(t).length?!1:n.every(([o,s])=>N(s,t[o]))}return!1}function v(e,t){return a.useEffect(e,D(t))}function D(e){const t=a.useRef(e),n=a.useRef(0);return N(e,t.current)||(t.current=e,n.current+=1),a.useMemo(()=>t.current,[n.current])}function S(){window.nostojs=window.nostojs??function(e){(window.nostojs.q=window.nostojs.q??[]).push(e)}}async function p(e){return window.nostojs(e)}typeof window<"u"&&(S(),p(e=>{e.internal.getSettings()}));function V(){return typeof window.nosto<"u"}const F={production:"https://connect.nosto.com/",staging:"https://connect.staging.nosto.com/",local:"https://connect.nosto.com/"};function j(e){return F[e??"production"]}function z({merchantId:e,env:t,options:n,shopifyInternational:o,scriptLoader:s}){const c=document.querySelector("script[nosto-language], script[nosto-market-id]"),i=String(o?.marketId||""),u=o?.language||"",d=c?.getAttribute("nosto-language")!==u||c?.getAttribute("nosto-market-id")!==i;if(!c||d){const l=document.querySelector("#nosto-sandbox");c?.parentNode?.removeChild(c),l?.parentNode?.removeChild(l);const C=new URL("/script/shopify/market/nosto.js",j(t));C.searchParams.append("merchant",e),C.searchParams.append("market",i),C.searchParams.append("locale",u.toLowerCase());const ie={...n?.attributes,"nosto-language":u,"nosto-market-id":i};return(s??L)(C.toString(),{...n,attributes:ie})}return Promise.resolve()}function U(e){if(e.shopifyInternational)return z(e);const{merchantId:t,env:n,options:o,scriptLoader:s}=e,c=s??L,i=new URL(`/include/${t}`,j(n));return c(i.toString(),o)}function L(e,t){return new Promise((n,o)=>{const s=document.createElement("script");s.src=e,s.async=!0,s.type="text/javascript",s.onload=()=>n(),s.onerror=()=>o(),Object.entries(t?.attributes??{}).forEach(([c,i])=>s.setAttribute(c,i)),t?.position==="head"?document.head.appendChild(s):document.body.appendChild(s)})}typeof window<"u"&&S();function f(e,t,n){const{clientScriptLoaded:o}=g();(n?.deep?v:a.useEffect)(()=>{o&&p(e)},[o,...t??[]])}function $(e){return a.cloneElement(e.recommendationComponent,{nostoRecommendation:e.nostoRecommendation})}function R(e){p(t=>t.placements.injectCampaigns(e))}function G(e){if(!window.nostojs)throw new Error("Nosto has not yet been initialized");R(e.recommendations)}function m(){const{responseMode:e,recommendationComponent:t}=g(),n=a.useRef({});if(e=="HTML")return{renderCampaigns:G};if(!t)throw new Error("recommendationComponent is required for client-side rendering using hook");function o(s){R(s.campaigns?.content??{});const c=s.campaigns?.recommendations??{};for(const i in c){const u=c[i],d="#"+i,l=document.querySelector(d);l&&(n.current[i]||(n.current[i]=H.createRoot(l)),n.current[i].render(w.jsx($,{recommendationComponent:t,nostoRecommendation:u})))}}return{renderCampaigns:o}}function E(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewNotFound().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function J(e){return E(e),null}function k({category:e,placements:t}){const{renderCampaigns:n}=m();f(async o=>{const s=await o.defaultSession().viewCategory(e).setPlacements(t||o.placements.getPlacements()).load();n(s)},[e])}function W(e){return k(e),null}function b(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewCart().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function Z(e){return b(e),null}function A(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewFrontPage().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function B(e){return A(e),null}function h(e){return!e||typeof e!="object"||K(e)||Q(e)?e:Array.isArray(e)?e.map(h):Object.keys(e).reduce((t,n)=>{const o=n[0].toLowerCase()+n.slice(1).replace(/([A-Z]+)/g,(s,c)=>"_"+c.toLowerCase());return t[o]=h(e[n]),t},{})}function K(e){return Object.prototype.toString.call(e)==="[object Date]"}function Q(e){return Object.prototype.toString.call(e)==="[object RegExp]"}function M({order:e,placements:t}){const{renderCampaigns:n}=m();f(async o=>{const s=await o.defaultSession().addOrder(h(e)).setPlacements(t||o.placements.getPlacements()).load();n(s)},[e],{deep:!0})}function X(e){return M(e),null}function I(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewOther().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function Y(e){return I(e),null}function x({id:e,pageType:t,children:n}){return w.jsx("div",{className:"nosto_element",id:e,children:n},e+(t||""))}function T({product:e,tagging:t,placements:n,reference:o}){const{renderCampaigns:s}=m();if(t&&!t.product_id)throw new Error("The product object must contain a product_id property");const c=t?.product_id??e;f(async i=>{const u=i.defaultSession().viewProduct(t??e).setPlacements(n||i.placements.getPlacements());o&&u.setRef(c,o);const d=await u.load();s(d)},[c,t?.selected_sku_id])}function ee(e){return T(e),null}function te(e,t){return new Promise((n,o)=>{const s=document.createElement("script");s.type="text/javascript",s.src=e,s.async=!0,s.onload=()=>n(),s.onerror=()=>o(),Object.entries(t?.attributes??{}).forEach(([c,i])=>s.setAttribute(c,i)),t?.position==="head"?document.head.appendChild(s):document.body.appendChild(s)})}const ne={"nosto-client-script":""};function oe(e){const{scriptLoader:t=te,account:n,shopifyMarkets:o,loadScript:s=!0}=e,[c,i]=a.useState(!1);return a.useEffect(()=>{function u(){i(!0)}if(S(),p(l=>l.setAutoLoad(!1)),!s){p(u);return}async function d(){await U({merchantId:n,shopifyInternational:o,options:{attributes:ne},scriptLoader:t}),u()}(!V()||o)&&d()},[o?.marketId,o?.language]),{clientScriptLoaded:c}}function se(e){const{account:t,multiCurrency:n=!1,children:o,recommendationComponent:s,renderMode:c}=e,i=n?e.currentVariation:"";if(s&&!a.isValidElement(s))throw new Error("The recommendationComponent prop must be a valid React element. Please provide a valid React element.");const u=c||(s?"JSON_ORIGINAL":"HTML"),{clientScriptLoaded:d}=oe(e);return d&&p(l=>{l.defaultSession().setVariation(i).setResponseMode(u)}),w.jsx(y.Provider,{value:{account:t,clientScriptLoaded:d,currentVariation:i,responseMode:u,recommendationComponent:s},children:o})}function _({query:e,placements:t}){const{renderCampaigns:n}=m();f(async o=>{const s=await o.defaultSession().viewSearch(e).setPlacements(t||o.placements.getPlacements()).load();n(s)},[e])}function re(e){return _(e),null}function q({cart:e,customer:t}={}){const{clientScriptLoaded:n}=g();v(()=>{const o=e?h(e):void 0,s=t?h(t):void 0;n&&p(c=>{c.defaultSession().setCart(o).setCustomer(s).viewOther().load({skipPageViews:!0})})},[n,e,t])}function ce(e){return q(e),null}r.Nosto404=J,r.NostoCategory=W,r.NostoCheckout=Z,r.NostoContext=y,r.NostoHome=B,r.NostoOrder=X,r.NostoOther=Y,r.NostoPlacement=x,r.NostoProduct=ee,r.NostoProvider=se,r.NostoSearch=re,r.NostoSession=ce,r.useNosto404=E,r.useNostoCategory=k,r.useNostoCheckout=b,r.useNostoContext=g,r.useNostoHome=A,r.useNostoOrder=M,r.useNostoOther=I,r.useNostoProduct=T,r.useNostoSearch=_,r.useNostoSession=q,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("react"),require("react/jsx-runtime"),require("react-dom/client")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime","react-dom/client"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r["@nosto/nosto-react"]={},r.React,r["react/jsx-runtime"],r.client))})(this,(function(r,a,w,H){"use strict";const y=a.createContext({account:"",currentVariation:"",responseMode:"HTML",clientScriptLoaded:!1});function g(){return a.useContext(y)}const P=e=>String(e)==="[object Object]";function O(e){if(!P(e))return!1;const t=e.constructor;if(t===void 0)return!0;const n=t.prototype;return!(!P(n)||!n.hasOwnProperty("isPrototypeOf"))}function N(e,t){if(e===t)return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof Array&&t instanceof Array)return e.length!==t.length?!1:e.every((n,o)=>N(n,t[o]));if(O(e)&&O(t)){const n=Object.entries(e);return n.length!==Object.keys(t).length?!1:n.every(([o,s])=>N(s,t[o]))}return!1}function v(e,t){return a.useEffect(e,D(t))}function D(e){const t=a.useRef(e),n=a.useRef(0);return N(e,t.current)||(t.current=e,n.current+=1),a.useMemo(()=>t.current,[n.current])}function S(){window.nostojs=window.nostojs??function(e){(window.nostojs.q=window.nostojs.q??[]).push(e)}}async function p(e){return window.nostojs(e)}typeof window<"u"&&(S(),p(e=>{e.internal.getSettings()}));function V(){return typeof window.nosto<"u"}const F={production:"https://connect.nosto.com/",staging:"https://connect.staging.nosto.com/",local:"https://connect.nosto.com/"};function j(e){return F[e??"production"]}function z({merchantId:e,env:t,options:n,shopifyInternational:o,scriptLoader:s}){const c=document.querySelector("script[nosto-language], script[nosto-market-id]"),i=String(o?.marketId||""),u=o?.language||"",d=c?.getAttribute("nosto-language")!==u||c?.getAttribute("nosto-market-id")!==i;if(!c||d){const l=document.querySelector("#nosto-sandbox");c?.parentNode?.removeChild(c),l?.parentNode?.removeChild(l);const C=new URL("/script/shopify/market/nosto.js",j(t));C.searchParams.append("merchant",e),C.searchParams.append("market",i),C.searchParams.append("locale",u.toLowerCase());const ie={...n?.attributes,"nosto-language":u,"nosto-market-id":i};return(s??L)(C.toString(),{...n,attributes:ie})}return Promise.resolve()}function U(e){if(e.shopifyInternational)return z(e);const{merchantId:t,env:n,options:o,scriptLoader:s}=e,c=s??L,i=new URL(`/include/${t}`,j(n));return c(i.toString(),o)}function L(e,t){return new Promise((n,o)=>{const s=document.createElement("script");s.src=e,s.async=!0,s.type="text/javascript",s.onload=()=>n(),s.onerror=()=>o(),Object.entries(t?.attributes??{}).forEach(([c,i])=>s.setAttribute(c,i)),t?.position==="head"?document.head.appendChild(s):document.body.appendChild(s)})}typeof window<"u"&&S();function f(e,t,n){const{clientScriptLoaded:o}=g();(n?.deep?v:a.useEffect)(()=>{o&&p(e)},[o,...t??[]])}function $(e){return a.cloneElement(e.recommendationComponent,{nostoRecommendation:e.nostoRecommendation})}function R(e){p(t=>t.placements.injectCampaigns(e))}function G(e){if(!window.nostojs)throw new Error("Nosto has not yet been initialized");R(e.recommendations)}function m(){const{responseMode:e,recommendationComponent:t}=g(),n=a.useRef({});if(e=="HTML")return{renderCampaigns:G};if(!t)throw new Error("recommendationComponent is required for client-side rendering using hook");function o(s){R(s.campaigns?.content??{});const c=s.campaigns?.recommendations??{};for(const i in c){const u=c[i],d="#"+i,l=document.querySelector(d);l&&(n.current[i]||(n.current[i]=H.createRoot(l)),n.current[i].render(w.jsx($,{recommendationComponent:t,nostoRecommendation:u})))}}return{renderCampaigns:o}}function E(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewNotFound().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function J(e){return E(e),null}function k({category:e,placements:t}){const{renderCampaigns:n}=m();f(async o=>{const s=await o.defaultSession().viewCategory(e).setPlacements(t||o.placements.getPlacements()).load();n(s)},[e])}function W(e){return k(e),null}function b(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewCart().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function Z(e){return b(e),null}function A(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewFrontPage().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function B(e){return A(e),null}function h(e){return!e||typeof e!="object"||K(e)||Q(e)?e:Array.isArray(e)?e.map(h):Object.keys(e).reduce((t,n)=>{const o=n[0].toLowerCase()+n.slice(1).replace(/([A-Z]+)/g,(s,c)=>"_"+c.toLowerCase());return t[o]=h(e[n]),t},{})}function K(e){return Object.prototype.toString.call(e)==="[object Date]"}function Q(e){return Object.prototype.toString.call(e)==="[object RegExp]"}function M({order:e,placements:t}){const{renderCampaigns:n}=m();f(async o=>{const s=await o.defaultSession().addOrder(h(e)).setPlacements(t||o.placements.getPlacements()).load();n(s)},[e],{deep:!0})}function X(e){return M(e),null}function I(e){const{renderCampaigns:t}=m();f(async n=>{const o=await n.defaultSession().viewOther().setPlacements(e?.placements||n.placements.getPlacements()).load();t(o)})}function Y(e){return I(e),null}function x({id:e,pageType:t,children:n}){return w.jsx("div",{className:"nosto_element",id:e,children:n},e+(t||""))}function T({product:e,tagging:t,placements:n,reference:o}){const{renderCampaigns:s}=m();if(t&&!t.product_id)throw new Error("The product object must contain a product_id property");const c=t?.product_id??e;f(async i=>{const u=i.defaultSession().viewProduct(t??e).setPlacements(n||i.placements.getPlacements());o&&u.setRef(c,o);const d=await u.load();s(d)},[c,t?.selected_sku_id])}function ee(e){return T(e),null}function te(e,t){return new Promise((n,o)=>{const s=document.createElement("script");s.type="text/javascript",s.src=e,s.async=!0,s.onload=()=>n(),s.onerror=()=>o(),Object.entries(t?.attributes??{}).forEach(([c,i])=>s.setAttribute(c,i)),t?.position==="head"?document.head.appendChild(s):document.body.appendChild(s)})}const ne={"nosto-client-script":""};function oe(e){const{scriptLoader:t=te,account:n,shopifyMarkets:o,loadScript:s=!0}=e,[c,i]=a.useState(!1);return a.useEffect(()=>{function u(){i(!0)}if(S(),p(l=>l.setAutoLoad(!1)),!s){p(u);return}async function d(){await U({merchantId:n,shopifyInternational:o,options:{attributes:ne},scriptLoader:t}),u()}(!V()||o)&&d()},[o?.marketId,o?.language]),{clientScriptLoaded:c}}function se(e){const{account:t,multiCurrency:n=!1,children:o,recommendationComponent:s,renderMode:c}=e,i=n?e.currentVariation:"";if(s&&!a.isValidElement(s))throw new Error("The recommendationComponent prop must be a valid React element. Please provide a valid React element.");const u=c||(s?"JSON_ORIGINAL":"HTML"),{clientScriptLoaded:d}=oe(e);return d&&p(l=>{l.defaultSession().setVariation(i).setResponseMode(u)}),w.jsx(y.Provider,{value:{account:t,clientScriptLoaded:d,currentVariation:i,responseMode:u,recommendationComponent:s},children:o})}function _({query:e,placements:t}){const{renderCampaigns:n}=m();f(async o=>{const s=await o.defaultSession().viewSearch(e).setPlacements(t||o.placements.getPlacements()).load();n(s)},[e])}function re(e){return _(e),null}function q({cart:e,customer:t}={}){const{clientScriptLoaded:n}=g();v(()=>{const o=e?h(e):void 0,s=t?h(t):void 0;n&&p(c=>{c.defaultSession().setCart(o).setCustomer(s).viewOther().load({skipPageViews:!0})})},[n,e,t])}function ce(e){return q(e),null}r.Nosto404=J,r.NostoCategory=W,r.NostoCheckout=Z,r.NostoContext=y,r.NostoHome=B,r.NostoOrder=X,r.NostoOther=Y,r.NostoPlacement=x,r.NostoProduct=ee,r.NostoProvider=se,r.NostoSearch=re,r.NostoSession=ce,r.useNosto404=E,r.useNostoCategory=k,r.useNostoCheckout=b,r.useNostoContext=g,r.useNostoHome=A,r.useNostoOrder=M,r.useNostoOther=I,r.useNostoProduct=T,r.useNostoSearch=_,r.useNostoSession=q,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED