@nosto/nosto-react 2.2.2 → 2.4.0

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.es.js CHANGED
@@ -1,93 +1,109 @@
1
- import { createContext as I, useContext as k, useEffect as g, useRef as P, useMemo as V, cloneElement as b, useState as q, isValidElement as F } from "react";
2
- import { jsx as v } from "react/jsx-runtime";
3
- import H from "react-dom";
4
- const T = I({
1
+ import { createContext as D, useContext as V, useEffect as O, useRef as N, useMemo as x, cloneElement as H, useState as q, isValidElement as F } from "react";
2
+ import { jsx as j } from "react/jsx-runtime";
3
+ import { createRoot as M } from "react-dom/client";
4
+ const _ = D({
5
5
  account: "",
6
6
  currentVariation: "",
7
7
  responseMode: "HTML",
8
8
  clientScriptLoaded: !1
9
9
  });
10
- function E() {
11
- return k(T);
10
+ function L() {
11
+ return V(_);
12
12
  }
13
- const L = (e) => String(e) === "[object Object]";
14
- function _(e) {
15
- if (!L(e))
16
- return !1;
13
+ const g = (e) => String(e) === "[object Object]";
14
+ function A(e) {
15
+ if (!g(e)) return !1;
17
16
  const t = e.constructor;
18
- if (t === void 0)
19
- return !0;
17
+ if (t === void 0) return !0;
20
18
  const n = t.prototype;
21
- return !(!L(n) || !n.hasOwnProperty("isPrototypeOf"));
19
+ return !(!g(n) || !n.hasOwnProperty("isPrototypeOf"));
22
20
  }
23
- function h(e, t) {
21
+ function v(e, t) {
24
22
  if (e === t)
25
23
  return !0;
26
24
  if (e instanceof Date && t instanceof Date)
27
25
  return e.getTime() === t.getTime();
28
26
  if (e instanceof Array && t instanceof Array)
29
- return e.length !== t.length ? !1 : e.every((n, r) => h(n, t[r]));
30
- if (_(e) && _(t)) {
27
+ return e.length !== t.length ? !1 : e.every((n, r) => v(n, t[r]));
28
+ if (A(e) && A(t)) {
31
29
  const n = Object.entries(e);
32
- return n.length !== Object.keys(t).length ? !1 : n.every(([r, o]) => h(o, t[r]));
30
+ return n.length !== Object.keys(t).length ? !1 : n.every(([r, o]) => v(o, t[r]));
33
31
  }
34
32
  return !1;
35
33
  }
36
- function D(e, t) {
37
- return g(e, x(t));
38
- }
39
- function x(e) {
40
- const t = P(e), n = P(0);
41
- return h(e, t.current) || (t.current = e, n.current += 1), V(() => t.current, [n.current]);
34
+ function I(e, t) {
35
+ return O(e, z(t));
42
36
  }
43
- function u(e, t, n) {
44
- const { clientScriptLoaded: r } = E();
45
- (n != null && n.deep ? D : g)(() => {
46
- r && window.nostojs(e);
47
- }, [r, ...t ?? []]);
37
+ function z(e) {
38
+ const t = N(e), n = N(0);
39
+ return v(e, t.current) || (t.current = e, n.current += 1), x(() => t.current, [n.current]);
48
40
  }
49
- var O, y = H;
50
- if (process.env.NODE_ENV === "production")
51
- O = y.createRoot, y.hydrateRoot;
52
- else {
53
- var A = y.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
54
- O = function(e, t) {
55
- A.usingClientEntryPoint = !0;
56
- try {
57
- return y.createRoot(e, t);
58
- } finally {
59
- A.usingClientEntryPoint = !1;
60
- }
41
+ function k() {
42
+ window.nostojs = window.nostojs ?? function(e) {
43
+ (window.nostojs.q = window.nostojs.q ?? []).push(e);
61
44
  };
62
45
  }
63
- function M(e) {
64
- return b(e.recommendationComponent, {
46
+ function G() {
47
+ return typeof window.nosto < "u";
48
+ }
49
+ const J = {
50
+ production: "https://connect.nosto.com/",
51
+ staging: "https://connect.staging.nosto.com/",
52
+ local: "https://connect.nosto.com/"
53
+ };
54
+ function U(e) {
55
+ return J[e ?? "production"];
56
+ }
57
+ function W({ merchantId: e, env: t, options: n, scriptLoader: r }) {
58
+ const o = new URL(`/include/${e}`, U(t));
59
+ return (r ?? Z)(o.toString(), n);
60
+ }
61
+ function Z(e, t) {
62
+ return new Promise((n, r) => {
63
+ const o = document.createElement("script");
64
+ o.src = e, o.async = !0, o.type = "text/javascript", o.onload = () => n(), o.onerror = () => r(), Object.entries((t == null ? void 0 : t.attributes) ?? {}).forEach(([c, s]) => o.setAttribute(c, s)), (t == null ? void 0 : t.position) === "head" ? document.head.appendChild(o) : document.body.appendChild(o);
65
+ });
66
+ }
67
+ async function C(e) {
68
+ return window.nostojs(e);
69
+ }
70
+ k();
71
+ function d(e, t, n) {
72
+ const { clientScriptLoaded: r } = L();
73
+ (n != null && n.deep ? I : O)(() => {
74
+ r && C(e);
75
+ }, [r, ...t ?? []]);
76
+ }
77
+ function B(e) {
78
+ return H(e.recommendationComponent, {
65
79
  // eslint-disable-next-line react/prop-types
66
80
  nostoRecommendation: e.nostoRecommendation
67
81
  });
68
82
  }
69
- function z(e) {
83
+ function T(e) {
84
+ C((t) => t.placements.injectCampaigns(e));
85
+ }
86
+ function K(e) {
70
87
  if (!window.nostojs)
71
88
  throw new Error("Nosto has not yet been initialized");
72
- window.nostojs((t) => {
73
- t.placements.injectCampaigns(e.recommendations);
74
- });
89
+ T(e.recommendations);
75
90
  }
76
- function d() {
77
- const { responseMode: e, recommendationComponent: t } = E(), n = P({});
91
+ function l() {
92
+ const { responseMode: e, recommendationComponent: t } = L(), n = N({});
78
93
  if (e == "HTML")
79
- return { renderCampaigns: z };
94
+ return { renderCampaigns: K };
80
95
  function r(o) {
81
- var i;
82
- const c = ((i = o.campaigns) == null ? void 0 : i.recommendations) ?? {};
83
- for (const a in c) {
84
- const l = c[a], C = "#" + a, p = document.querySelector(C);
85
- p && (n.current[a] || (n.current[a] = O(p)), n.current[a].render(
86
- /* @__PURE__ */ v(
87
- M,
96
+ var s, u;
97
+ T(((s = o.campaigns) == null ? void 0 : s.content) ?? {});
98
+ const c = ((u = o.campaigns) == null ? void 0 : u.recommendations) ?? {};
99
+ for (const i in c) {
100
+ const y = c[i], S = "#" + i, w = document.querySelector(S);
101
+ w && (n.current[i] || (n.current[i] = M(w)), n.current[i].render(
102
+ /* @__PURE__ */ j(
103
+ B,
88
104
  {
89
105
  recommendationComponent: t,
90
- nostoRecommendation: l
106
+ nostoRecommendation: y
91
107
  }
92
108
  )
93
109
  ));
@@ -95,19 +111,19 @@ function d() {
95
111
  }
96
112
  return { renderCampaigns: r };
97
113
  }
98
- function U(e) {
99
- const { renderCampaigns: t } = d();
100
- u(async (n) => {
114
+ function Q(e) {
115
+ const { renderCampaigns: t } = l();
116
+ d(async (n) => {
101
117
  const r = await n.defaultSession().viewNotFound().setPlacements((e == null ? void 0 : e.placements) || n.placements.getPlacements()).load();
102
118
  t(r);
103
119
  });
104
120
  }
105
- function ae(e) {
106
- return U(e), null;
121
+ function fe(e) {
122
+ return Q(e), null;
107
123
  }
108
- function W({ category: e, placements: t }) {
109
- const { renderCampaigns: n } = d();
110
- u(
124
+ function X({ category: e, placements: t }) {
125
+ const { renderCampaigns: n } = l();
126
+ d(
111
127
  async (r) => {
112
128
  const o = await r.defaultSession().viewCategory(e).setPlacements(t || r.placements.getPlacements()).load();
113
129
  n(o);
@@ -115,161 +131,162 @@ function W({ category: e, placements: t }) {
115
131
  [e]
116
132
  );
117
133
  }
118
- function ie(e) {
119
- return W(e), null;
134
+ function pe(e) {
135
+ return X(e), null;
120
136
  }
121
- function B(e) {
122
- const { renderCampaigns: t } = d();
123
- u(async (n) => {
137
+ function Y(e) {
138
+ const { renderCampaigns: t } = l();
139
+ d(async (n) => {
124
140
  const r = await n.defaultSession().viewCart().setPlacements((e == null ? void 0 : e.placements) || n.placements.getPlacements()).load();
125
141
  t(r);
126
142
  });
127
143
  }
128
- function ue(e) {
129
- return B(e), null;
144
+ function Ce(e) {
145
+ return Y(e), null;
130
146
  }
131
- function G(e) {
132
- const { renderCampaigns: t } = d();
133
- u(async (n) => {
147
+ function ee(e) {
148
+ const { renderCampaigns: t } = l();
149
+ d(async (n) => {
134
150
  const r = await n.defaultSession().viewFrontPage().setPlacements((e == null ? void 0 : e.placements) || n.placements.getPlacements()).load();
135
151
  t(r);
136
152
  });
137
153
  }
138
- function de(e) {
139
- return G(e), null;
154
+ function we(e) {
155
+ return ee(e), null;
140
156
  }
141
- function w(e) {
142
- return !e || typeof e != "object" || J(e) || Y(e) ? e : Array.isArray(e) ? e.map(w) : Object.keys(e).reduce((t, n) => {
157
+ function p(e) {
158
+ return !e || typeof e != "object" || te(e) || ne(e) ? e : Array.isArray(e) ? e.map(p) : Object.keys(e).reduce((t, n) => {
143
159
  const r = n[0].toLowerCase() + n.slice(1).replace(/([A-Z]+)/g, (o, c) => "_" + c.toLowerCase());
144
- return t[r] = w(e[n]), t;
160
+ return t[r] = p(e[n]), t;
145
161
  }, {});
146
162
  }
147
- function J(e) {
163
+ function te(e) {
148
164
  return Object.prototype.toString.call(e) === "[object Date]";
149
165
  }
150
- function Y(e) {
166
+ function ne(e) {
151
167
  return Object.prototype.toString.call(e) === "[object RegExp]";
152
168
  }
153
- function Z({ order: e, placements: t }) {
154
- const { renderCampaigns: n } = d();
155
- u(
169
+ function oe({ order: e, placements: t }) {
170
+ const { renderCampaigns: n } = l();
171
+ d(
156
172
  async (r) => {
157
- const o = await r.defaultSession().addOrder(w(e)).setPlacements(t || r.placements.getPlacements()).load();
173
+ const o = await r.defaultSession().addOrder(p(e)).setPlacements(t || r.placements.getPlacements()).load();
158
174
  n(o);
159
175
  },
160
176
  [e],
161
177
  { deep: !0 }
162
178
  );
163
179
  }
164
- function le(e) {
165
- return Z(e), null;
180
+ function he(e) {
181
+ return oe(e), null;
166
182
  }
167
- function K(e) {
168
- const { renderCampaigns: t } = d();
169
- u(async (n) => {
183
+ function re(e) {
184
+ const { renderCampaigns: t } = l();
185
+ d(async (n) => {
170
186
  const r = await n.defaultSession().viewOther().setPlacements((e == null ? void 0 : e.placements) || n.placements.getPlacements()).load();
171
187
  t(r);
172
188
  });
173
189
  }
174
- function me(e) {
175
- return K(e), null;
190
+ function ye(e) {
191
+ return re(e), null;
176
192
  }
177
- function fe({ id: e, pageType: t }) {
178
- return /* @__PURE__ */ v("div", { className: "nosto_element", id: e }, e + (t || ""));
193
+ function Se({ id: e, pageType: t }) {
194
+ return /* @__PURE__ */ j("div", { className: "nosto_element", id: e }, e + (t || ""));
179
195
  }
180
- function Q({ product: e, tagging: t, placements: n }) {
181
- const { renderCampaigns: r } = d();
196
+ function ce({ product: e, tagging: t, placements: n, reference: r }) {
197
+ const { renderCampaigns: o } = l();
182
198
  if (t && !t.product_id)
183
199
  throw new Error("The product object must contain a product_id property");
184
- u(
185
- async (o) => {
186
- const c = await o.defaultSession().viewProduct(t ?? e).setPlacements(n || o.placements.getPlacements()).load();
187
- r(c);
200
+ const c = (t == null ? void 0 : t.product_id) ?? e;
201
+ d(
202
+ async (s) => {
203
+ const u = s.defaultSession().viewProduct(t ?? e).setPlacements(n || s.placements.getPlacements());
204
+ r && u.setRef(c, r);
205
+ const i = await u.load();
206
+ o(i);
188
207
  },
189
- [e, t == null ? void 0 : t.selected_sku_id]
208
+ [c, t == null ? void 0 : t.selected_sku_id]
190
209
  );
191
210
  }
192
- function we(e) {
193
- return Q(e), null;
194
- }
195
- function X() {
196
- return typeof window.nosto < "u";
211
+ function Pe(e) {
212
+ return ce(e), null;
197
213
  }
198
- function ee(e, t) {
214
+ function se(e, t) {
199
215
  return new Promise((n, r) => {
200
216
  const o = document.createElement("script");
201
- o.type = "text/javascript", o.src = e, o.async = !0, o.onload = () => n(), o.onerror = () => r(), Object.entries((t == null ? void 0 : t.attributes) ?? {}).forEach(([c, i]) => o.setAttribute(c, i)), (t == null ? void 0 : t.position) === "head" ? document.head.appendChild(o) : document.body.appendChild(o);
217
+ o.type = "text/javascript", o.src = e, o.async = !0, o.onload = () => n(), o.onerror = () => r(), Object.entries((t == null ? void 0 : t.attributes) ?? {}).forEach(([c, s]) => o.setAttribute(c, s)), (t == null ? void 0 : t.position) === "head" ? document.head.appendChild(o) : document.body.appendChild(o);
202
218
  });
203
219
  }
204
- function te(e) {
220
+ const b = { "nosto-client-script": "" };
221
+ function ae(e) {
205
222
  const {
206
223
  host: t = "connect.nosto.com",
207
- scriptLoader: n = ee,
224
+ scriptLoader: n = se,
208
225
  account: r,
209
226
  shopifyMarkets: o,
210
227
  loadScript: c = !0
211
- } = e, [i, a] = q(!1);
212
- return g(() => {
213
- function l() {
214
- var s;
215
- "nostoReactTest" in window && ((s = window.nosto) == null || s.reload({
216
- site: "localhost"
217
- })), a(!0);
228
+ } = e, [s, u] = q(!1);
229
+ return O(() => {
230
+ function i() {
231
+ u(!0);
218
232
  }
219
- async function C(s, m = {}) {
220
- const f = `//${t}${s}`, S = { "nosto-client-script": "", ...m };
221
- await n(f, { attributes: S }), l();
233
+ async function y(a, m = {}) {
234
+ const f = `//${t}${a}`, P = { ...b, ...m };
235
+ await n(f, { attributes: P }), i();
222
236
  }
223
- function p() {
224
- var R, j;
225
- const s = document.querySelector("[nosto-client-script]"), m = String((o == null ? void 0 : o.marketId) || ""), f = (o == null ? void 0 : o.language) || "", S = (s == null ? void 0 : s.getAttribute("nosto-language")) !== f || (s == null ? void 0 : s.getAttribute("nosto-market-id")) !== m;
226
- if (!s || S) {
227
- i && a(!1);
228
- const N = document.querySelector("#nosto-sandbox");
229
- (R = s == null ? void 0 : s.parentNode) == null || R.removeChild(s), (j = N == null ? void 0 : N.parentNode) == null || j.removeChild(N);
237
+ function S() {
238
+ var R, E;
239
+ const a = document.querySelector("[nosto-client-script]"), m = String((o == null ? void 0 : o.marketId) || ""), f = (o == null ? void 0 : o.language) || "", P = (a == null ? void 0 : a.getAttribute("nosto-language")) !== f || (a == null ? void 0 : a.getAttribute("nosto-market-id")) !== m;
240
+ if (!a || P) {
241
+ s && u(!1);
242
+ const h = document.querySelector("#nosto-sandbox");
243
+ (R = a == null ? void 0 : a.parentNode) == null || R.removeChild(a), (E = h == null ? void 0 : h.parentNode) == null || E.removeChild(h);
230
244
  const $ = `/script/shopify/market/nosto.js?merchant=${r}&market=${m}&locale=${f.toLowerCase()}`;
231
- C($, { "nosto-language": f, "nosto-market-id": m });
245
+ y($, { "nosto-language": f, "nosto-market-id": m });
232
246
  }
233
247
  }
234
- if (window.nostojs || (window.nostojs = (s) => {
235
- (window.nostojs.q = window.nostojs.q || []).push(s);
236
- }, window.nostojs((s) => s.setAutoLoad(!1))), !c) {
237
- window.nosto ? l() : window.nostojs(l);
248
+ if (k(), !c) {
249
+ C(i);
238
250
  return;
239
251
  }
240
- if (!X() && !o) {
241
- const s = `/include/${r}`;
242
- C(s);
252
+ async function w() {
253
+ await W({
254
+ merchantId: r,
255
+ options: {
256
+ attributes: b
257
+ },
258
+ scriptLoader: n
259
+ }), i();
243
260
  }
244
- o && p();
245
- }, [o == null ? void 0 : o.marketId, o == null ? void 0 : o.language]), { clientScriptLoaded: i };
261
+ !G() && !o && w(), o && S();
262
+ }, [o == null ? void 0 : o.marketId, o == null ? void 0 : o.language]), { clientScriptLoaded: s };
246
263
  }
247
- function Ce(e) {
264
+ function Ne(e) {
248
265
  const { account: t, multiCurrency: n = !1, children: r, recommendationComponent: o } = e, c = n ? e.currentVariation : "";
249
266
  if (o && !F(o))
250
267
  throw new Error(
251
268
  "The recommendationComponent prop must be a valid React element. Please provide a valid React element."
252
269
  );
253
- const i = o ? "JSON_ORIGINAL" : "HTML", { clientScriptLoaded: a } = te(e);
254
- return a && window.nostojs((l) => {
255
- l.defaultSession().setVariation(c).setResponseMode(i);
256
- }), /* @__PURE__ */ v(
257
- T.Provider,
270
+ const s = o ? "JSON_ORIGINAL" : "HTML", { clientScriptLoaded: u } = ae(e);
271
+ return u && C((i) => {
272
+ i.defaultSession().setVariation(c).setResponseMode(s);
273
+ }), /* @__PURE__ */ j(
274
+ _.Provider,
258
275
  {
259
276
  value: {
260
277
  account: t,
261
- clientScriptLoaded: a,
278
+ clientScriptLoaded: u,
262
279
  currentVariation: c,
263
- responseMode: i,
280
+ responseMode: s,
264
281
  recommendationComponent: o
265
282
  },
266
283
  children: r
267
284
  }
268
285
  );
269
286
  }
270
- function ne({ query: e, placements: t }) {
271
- const { renderCampaigns: n } = d();
272
- u(
287
+ function ie({ query: e, placements: t }) {
288
+ const { renderCampaigns: n } = l();
289
+ d(
273
290
  async (r) => {
274
291
  const o = await r.defaultSession().viewSearch(e).setPlacements(t || r.placements.getPlacements()).load();
275
292
  n(o);
@@ -277,42 +294,42 @@ function ne({ query: e, placements: t }) {
277
294
  [e]
278
295
  );
279
296
  }
280
- function pe(e) {
281
- return ne(e), null;
297
+ function ve(e) {
298
+ return ie(e), null;
282
299
  }
283
- function oe({ cart: e, customer: t } = {}) {
284
- const { clientScriptLoaded: n } = E();
285
- D(() => {
286
- const r = e ? w(e) : void 0, o = t ? w(t) : void 0;
287
- n && window.nostojs((c) => {
300
+ function ue({ cart: e, customer: t } = {}) {
301
+ const { clientScriptLoaded: n } = L();
302
+ I(() => {
303
+ const r = e ? p(e) : void 0, o = t ? p(t) : void 0;
304
+ n && C((c) => {
288
305
  c.defaultSession().setCart(r).setCustomer(o).viewOther().load({ skipPageViews: !0 });
289
306
  });
290
307
  }, [n, e, t]);
291
308
  }
292
- function Ne(e) {
293
- return oe(e), null;
309
+ function Oe(e) {
310
+ return ue(e), null;
294
311
  }
295
312
  export {
296
- ae as Nosto404,
297
- ie as NostoCategory,
298
- ue as NostoCheckout,
299
- T as NostoContext,
300
- de as NostoHome,
301
- le as NostoOrder,
302
- me as NostoOther,
303
- fe as NostoPlacement,
304
- we as NostoProduct,
305
- Ce as NostoProvider,
306
- pe as NostoSearch,
307
- Ne as NostoSession,
308
- U as useNosto404,
309
- W as useNostoCategory,
310
- B as useNostoCheckout,
311
- E as useNostoContext,
312
- G as useNostoHome,
313
- Z as useNostoOrder,
314
- K as useNostoOther,
315
- Q as useNostoProduct,
316
- ne as useNostoSearch,
317
- oe as useNostoSession
313
+ fe as Nosto404,
314
+ pe as NostoCategory,
315
+ Ce as NostoCheckout,
316
+ _ as NostoContext,
317
+ we as NostoHome,
318
+ he as NostoOrder,
319
+ ye as NostoOther,
320
+ Se as NostoPlacement,
321
+ Pe as NostoProduct,
322
+ Ne as NostoProvider,
323
+ ve as NostoSearch,
324
+ Oe as NostoSession,
325
+ Q as useNosto404,
326
+ X as useNostoCategory,
327
+ Y as useNostoCheckout,
328
+ L as useNostoContext,
329
+ ee as useNostoHome,
330
+ oe as useNostoOrder,
331
+ re as useNostoOther,
332
+ ce as useNostoProduct,
333
+ ie as useNostoSearch,
334
+ ue as useNostoSession
318
335
  };
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")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime","react-dom"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r["@nosto/nosto-react"]={},r.React,r["react/jsx-runtime"],r.ReactDOM))})(this,function(r,a,p,z){"use strict";const j=a.createContext({account:"",currentVariation:"",responseMode:"HTML",clientScriptLoaded:!1});function y(){return a.useContext(j)}const R=e=>String(e)==="[object Object]";function L(e){if(!R(e))return!1;const t=e.constructor;if(t===void 0)return!0;const n=t.prototype;return!(!R(n)||!n.hasOwnProperty("isPrototypeOf"))}function v(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,s)=>v(n,t[s]));if(L(e)&&L(t)){const n=Object.entries(e);return n.length!==Object.keys(t).length?!1:n.every(([s,o])=>v(o,t[s]))}return!1}function _(e,t){return a.useEffect(e,U(t))}function U(e){const t=a.useRef(e),n=a.useRef(0);return v(e,t.current)||(t.current=e,n.current+=1),a.useMemo(()=>t.current,[n.current])}function l(e,t,n){const{clientScriptLoaded:s}=y();(n!=null&&n.deep?_:a.useEffect)(()=>{s&&window.nostojs(e)},[s,...t??[]])}var E,S=z;if(process.env.NODE_ENV==="production")E=S.createRoot,S.hydrateRoot;else{var T=S.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;E=function(e,t){T.usingClientEntryPoint=!0;try{return S.createRoot(e,t)}finally{T.usingClientEntryPoint=!1}}}function W(e){return a.cloneElement(e.recommendationComponent,{nostoRecommendation:e.nostoRecommendation})}function B(e){if(!window.nostojs)throw new Error("Nosto has not yet been initialized");window.nostojs(t=>{t.placements.injectCampaigns(e.recommendations)})}function m(){const{responseMode:e,recommendationComponent:t}=y(),n=a.useRef({});if(e=="HTML")return{renderCampaigns:B};function s(o){var d;const i=((d=o.campaigns)==null?void 0:d.recommendations)??{};for(const u in i){const f=i[u],h="#"+u,P=document.querySelector(h);P&&(n.current[u]||(n.current[u]=E(P)),n.current[u].render(p.jsx(W,{recommendationComponent:t,nostoRecommendation:f})))}}return{renderCampaigns:s}}function A(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewNotFound().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function G(e){return A(e),null}function D({category:e,placements:t}){const{renderCampaigns:n}=m();l(async s=>{const o=await s.defaultSession().viewCategory(e).setPlacements(t||s.placements.getPlacements()).load();n(o)},[e])}function J(e){return D(e),null}function k(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewCart().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function Y(e){return k(e),null}function I(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewFrontPage().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function Z(e){return I(e),null}function w(e){return!e||typeof e!="object"||K(e)||Q(e)?e:Array.isArray(e)?e.map(w):Object.keys(e).reduce((t,n)=>{const s=n[0].toLowerCase()+n.slice(1).replace(/([A-Z]+)/g,(o,i)=>"_"+i.toLowerCase());return t[s]=w(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 q({order:e,placements:t}){const{renderCampaigns:n}=m();l(async s=>{const o=await s.defaultSession().addOrder(w(e)).setPlacements(t||s.placements.getPlacements()).load();n(o)},[e],{deep:!0})}function X(e){return q(e),null}function H(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewOther().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function x(e){return H(e),null}function ee({id:e,pageType:t}){return p.jsx("div",{className:"nosto_element",id:e},e+(t||""))}function V({product:e,tagging:t,placements:n}){const{renderCampaigns:s}=m();if(t&&!t.product_id)throw new Error("The product object must contain a product_id property");l(async o=>{const i=await o.defaultSession().viewProduct(t??e).setPlacements(n||o.placements.getPlacements()).load();s(i)},[e,t==null?void 0:t.selected_sku_id])}function te(e){return V(e),null}function ne(){return typeof window.nosto<"u"}function oe(e,t){return new Promise((n,s)=>{const o=document.createElement("script");o.type="text/javascript",o.src=e,o.async=!0,o.onload=()=>n(),o.onerror=()=>s(),Object.entries((t==null?void 0:t.attributes)??{}).forEach(([i,d])=>o.setAttribute(i,d)),(t==null?void 0:t.position)==="head"?document.head.appendChild(o):document.body.appendChild(o)})}function se(e){const{host:t="connect.nosto.com",scriptLoader:n=oe,account:s,shopifyMarkets:o,loadScript:i=!0}=e,[d,u]=a.useState(!1);return a.useEffect(()=>{function f(){var c;"nostoReactTest"in window&&((c=window.nosto)==null||c.reload({site:"localhost"})),u(!0)}async function h(c,N={}){const C=`//${t}${c}`,g={"nosto-client-script":"",...N};await n(C,{attributes:g}),f()}function P(){var M,F;const c=document.querySelector("[nosto-client-script]"),N=String((o==null?void 0:o.marketId)||""),C=(o==null?void 0:o.language)||"",g=(c==null?void 0:c.getAttribute("nosto-language"))!==C||(c==null?void 0:c.getAttribute("nosto-market-id"))!==N;if(!c||g){d&&u(!1);const O=document.querySelector("#nosto-sandbox");(M=c==null?void 0:c.parentNode)==null||M.removeChild(c),(F=O==null?void 0:O.parentNode)==null||F.removeChild(O);const ie=`/script/shopify/market/nosto.js?merchant=${s}&market=${N}&locale=${C.toLowerCase()}`;h(ie,{"nosto-language":C,"nosto-market-id":N})}}if(window.nostojs||(window.nostojs=c=>{(window.nostojs.q=window.nostojs.q||[]).push(c)},window.nostojs(c=>c.setAutoLoad(!1))),!i){window.nosto?f():window.nostojs(f);return}if(!ne()&&!o){const c=`/include/${s}`;h(c)}o&&P()},[o==null?void 0:o.marketId,o==null?void 0:o.language]),{clientScriptLoaded:d}}function re(e){const{account:t,multiCurrency:n=!1,children:s,recommendationComponent:o}=e,i=n?e.currentVariation:"";if(o&&!a.isValidElement(o))throw new Error("The recommendationComponent prop must be a valid React element. Please provide a valid React element.");const d=o?"JSON_ORIGINAL":"HTML",{clientScriptLoaded:u}=se(e);return u&&window.nostojs(f=>{f.defaultSession().setVariation(i).setResponseMode(d)}),p.jsx(j.Provider,{value:{account:t,clientScriptLoaded:u,currentVariation:i,responseMode:d,recommendationComponent:o},children:s})}function $({query:e,placements:t}){const{renderCampaigns:n}=m();l(async s=>{const o=await s.defaultSession().viewSearch(e).setPlacements(t||s.placements.getPlacements()).load();n(o)},[e])}function ce(e){return $(e),null}function b({cart:e,customer:t}={}){const{clientScriptLoaded:n}=y();_(()=>{const s=e?w(e):void 0,o=t?w(t):void 0;n&&window.nostojs(i=>{i.defaultSession().setCart(s).setCustomer(o).viewOther().load({skipPageViews:!0})})},[n,e,t])}function ae(e){return b(e),null}r.Nosto404=G,r.NostoCategory=J,r.NostoCheckout=Y,r.NostoContext=j,r.NostoHome=Z,r.NostoOrder=X,r.NostoOther=x,r.NostoPlacement=ee,r.NostoProduct=te,r.NostoProvider=re,r.NostoSearch=ce,r.NostoSession=ae,r.useNosto404=A,r.useNostoCategory=D,r.useNostoCheckout=k,r.useNostoContext=y,r.useNostoHome=I,r.useNostoOrder=q,r.useNostoOther=H,r.useNostoProduct=V,r.useNostoSearch=$,r.useNostoSession=b,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,i){typeof exports=="object"&&typeof module<"u"?i(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"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s["@nosto/nosto-react"]={},s.React,s["react/jsx-runtime"],s.client))})(this,function(s,i,P,J){"use strict";const O=i.createContext({account:"",currentVariation:"",responseMode:"HTML",clientScriptLoaded:!1});function h(){return i.useContext(O)}const R=e=>String(e)==="[object Object]";function g(e){if(!R(e))return!1;const t=e.constructor;if(t===void 0)return!0;const n=t.prototype;return!(!R(n)||!n.hasOwnProperty("isPrototypeOf"))}function v(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,c)=>v(n,t[c]));if(g(e)&&g(t)){const n=Object.entries(e);return n.length!==Object.keys(t).length?!1:n.every(([c,o])=>v(o,t[c]))}return!1}function A(e,t){return i.useEffect(e,U(t))}function U(e){const t=i.useRef(e),n=i.useRef(0);return v(e,t.current)||(t.current=e,n.current+=1),i.useMemo(()=>t.current,[n.current])}function b(){window.nostojs=window.nostojs??function(e){(window.nostojs.q=window.nostojs.q??[]).push(e)}}function W(){return typeof window.nosto<"u"}const Z={production:"https://connect.nosto.com/",staging:"https://connect.staging.nosto.com/",local:"https://connect.nosto.com/"};function B(e){return Z[e??"production"]}function K({merchantId:e,env:t,options:n,scriptLoader:c}){const o=new URL(`/include/${e}`,B(t));return(c??Q)(o.toString(),n)}function Q(e,t){return new Promise((n,c)=>{const o=document.createElement("script");o.src=e,o.async=!0,o.type="text/javascript",o.onload=()=>n(),o.onerror=()=>c(),Object.entries((t==null?void 0:t.attributes)??{}).forEach(([r,a])=>o.setAttribute(r,a)),(t==null?void 0:t.position)==="head"?document.head.appendChild(o):document.body.appendChild(o)})}async function C(e){return window.nostojs(e)}b();function m(e,t,n){const{clientScriptLoaded:c}=h();(n!=null&&n.deep?A:i.useEffect)(()=>{c&&C(e)},[c,...t??[]])}function X(e){return i.cloneElement(e.recommendationComponent,{nostoRecommendation:e.nostoRecommendation})}function T(e){C(t=>t.placements.injectCampaigns(e))}function Y(e){if(!window.nostojs)throw new Error("Nosto has not yet been initialized");T(e.recommendations)}function f(){const{responseMode:e,recommendationComponent:t}=h(),n=i.useRef({});if(e=="HTML")return{renderCampaigns:Y};function c(o){var a,l;T(((a=o.campaigns)==null?void 0:a.content)??{});const r=((l=o.campaigns)==null?void 0:l.recommendations)??{};for(const d in r){const j=r[d],L="#"+d,y=document.querySelector(L);y&&(n.current[d]||(n.current[d]=J.createRoot(y)),n.current[d].render(P.jsx(X,{recommendationComponent:t,nostoRecommendation:j})))}}return{renderCampaigns:c}}function k(e){const{renderCampaigns:t}=f();m(async n=>{const c=await n.defaultSession().viewNotFound().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(c)})}function x(e){return k(e),null}function _({category:e,placements:t}){const{renderCampaigns:n}=f();m(async c=>{const o=await c.defaultSession().viewCategory(e).setPlacements(t||c.placements.getPlacements()).load();n(o)},[e])}function ee(e){return _(e),null}function I(e){const{renderCampaigns:t}=f();m(async n=>{const c=await n.defaultSession().viewCart().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(c)})}function te(e){return I(e),null}function q(e){const{renderCampaigns:t}=f();m(async n=>{const c=await n.defaultSession().viewFrontPage().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(c)})}function ne(e){return q(e),null}function N(e){return!e||typeof e!="object"||oe(e)||ce(e)?e:Array.isArray(e)?e.map(N):Object.keys(e).reduce((t,n)=>{const c=n[0].toLowerCase()+n.slice(1).replace(/([A-Z]+)/g,(o,r)=>"_"+r.toLowerCase());return t[c]=N(e[n]),t},{})}function oe(e){return Object.prototype.toString.call(e)==="[object Date]"}function ce(e){return Object.prototype.toString.call(e)==="[object RegExp]"}function H({order:e,placements:t}){const{renderCampaigns:n}=f();m(async c=>{const o=await c.defaultSession().addOrder(N(e)).setPlacements(t||c.placements.getPlacements()).load();n(o)},[e],{deep:!0})}function se(e){return H(e),null}function $(e){const{renderCampaigns:t}=f();m(async n=>{const c=await n.defaultSession().viewOther().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(c)})}function re(e){return $(e),null}function ie({id:e,pageType:t}){return P.jsx("div",{className:"nosto_element",id:e},e+(t||""))}function D({product:e,tagging:t,placements:n,reference:c}){const{renderCampaigns:o}=f();if(t&&!t.product_id)throw new Error("The product object must contain a product_id property");const r=(t==null?void 0:t.product_id)??e;m(async a=>{const l=a.defaultSession().viewProduct(t??e).setPlacements(n||a.placements.getPlacements());c&&l.setRef(r,c);const d=await l.load();o(d)},[r,t==null?void 0:t.selected_sku_id])}function ae(e){return D(e),null}function ue(e,t){return new Promise((n,c)=>{const o=document.createElement("script");o.type="text/javascript",o.src=e,o.async=!0,o.onload=()=>n(),o.onerror=()=>c(),Object.entries((t==null?void 0:t.attributes)??{}).forEach(([r,a])=>o.setAttribute(r,a)),(t==null?void 0:t.position)==="head"?document.head.appendChild(o):document.body.appendChild(o)})}const V={"nosto-client-script":""};function de(e){const{host:t="connect.nosto.com",scriptLoader:n=ue,account:c,shopifyMarkets:o,loadScript:r=!0}=e,[a,l]=i.useState(!1);return i.useEffect(()=>{function d(){l(!0)}async function j(u,p={}){const w=`//${t}${u}`,E={...V,...p};await n(w,{attributes:E}),d()}function L(){var z,G;const u=document.querySelector("[nosto-client-script]"),p=String((o==null?void 0:o.marketId)||""),w=(o==null?void 0:o.language)||"",E=(u==null?void 0:u.getAttribute("nosto-language"))!==w||(u==null?void 0:u.getAttribute("nosto-market-id"))!==p;if(!u||E){a&&l(!1);const S=document.querySelector("#nosto-sandbox");(z=u==null?void 0:u.parentNode)==null||z.removeChild(u),(G=S==null?void 0:S.parentNode)==null||G.removeChild(S);const Ce=`/script/shopify/market/nosto.js?merchant=${c}&market=${p}&locale=${w.toLowerCase()}`;j(Ce,{"nosto-language":w,"nosto-market-id":p})}}if(b(),!r){C(d);return}async function y(){await K({merchantId:c,options:{attributes:V},scriptLoader:n}),d()}!W()&&!o&&y(),o&&L()},[o==null?void 0:o.marketId,o==null?void 0:o.language]),{clientScriptLoaded:a}}function le(e){const{account:t,multiCurrency:n=!1,children:c,recommendationComponent:o}=e,r=n?e.currentVariation:"";if(o&&!i.isValidElement(o))throw new Error("The recommendationComponent prop must be a valid React element. Please provide a valid React element.");const a=o?"JSON_ORIGINAL":"HTML",{clientScriptLoaded:l}=de(e);return l&&C(d=>{d.defaultSession().setVariation(r).setResponseMode(a)}),P.jsx(O.Provider,{value:{account:t,clientScriptLoaded:l,currentVariation:r,responseMode:a,recommendationComponent:o},children:c})}function M({query:e,placements:t}){const{renderCampaigns:n}=f();m(async c=>{const o=await c.defaultSession().viewSearch(e).setPlacements(t||c.placements.getPlacements()).load();n(o)},[e])}function me(e){return M(e),null}function F({cart:e,customer:t}={}){const{clientScriptLoaded:n}=h();A(()=>{const c=e?N(e):void 0,o=t?N(t):void 0;n&&C(r=>{r.defaultSession().setCart(c).setCustomer(o).viewOther().load({skipPageViews:!0})})},[n,e,t])}function fe(e){return F(e),null}s.Nosto404=x,s.NostoCategory=ee,s.NostoCheckout=te,s.NostoContext=O,s.NostoHome=ne,s.NostoOrder=se,s.NostoOther=re,s.NostoPlacement=ie,s.NostoProduct=ae,s.NostoProvider=le,s.NostoSearch=me,s.NostoSession=fe,s.useNosto404=k,s.useNostoCategory=_,s.useNostoCheckout=I,s.useNostoContext=h,s.useNostoHome=q,s.useNostoOrder=H,s.useNostoOther=$,s.useNostoProduct=D,s.useNostoSearch=M,s.useNostoSession=F,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nosto/nosto-react",
3
3
  "description": "Component library to simply implementing Nosto on React.",
4
- "version": "2.2.2",
4
+ "version": "2.4.0",
5
5
  "author": "Mridang Agarwalla, Dominik Gilg",
6
6
  "license": "ISC",
7
7
  "repository": {
@@ -23,35 +23,41 @@
23
23
  "prettier": "prettier '{src,spec}/**' --list-different",
24
24
  "prettier:fix": "prettier '{src,spec}/**' --write",
25
25
  "test": "vitest run --silent",
26
+ "test-loud": "vitest run",
26
27
  "typedoc": "typedoc src/index.ts"
27
28
  },
28
29
  "files": [
29
30
  "dist",
30
31
  "src"
31
32
  ],
33
+ "peerDependencies": {
34
+ "react": "^18.3.1 || ^19.0.0",
35
+ "react-dom": "^18.3.1 || ^19.0.0"
36
+ },
32
37
  "devDependencies": {
38
+ "@nosto/nosto-js": "*",
33
39
  "@testing-library/jest-dom": "^6.4.8",
34
40
  "@testing-library/react": "^16.0.0",
35
41
  "@testing-library/user-event": "^14.4.3",
36
- "@types/react": "^18.0.0",
37
- "@types/react-dom": "^18.0.0",
42
+ "@types/react": "^19.0.2",
43
+ "@types/react-dom": "^19.0.0",
38
44
  "@types/user-event": "^4.1.1",
39
- "@typescript-eslint/eslint-plugin": "^6.11.0",
40
- "@typescript-eslint/parser": "^6.11.0",
41
45
  "@vitejs/plugin-react": "^4.3.1",
42
- "eslint": "^8.53.0",
43
- "eslint-plugin-promise": "^6.1.1",
46
+ "eslint": "^9.13.0",
47
+ "eslint-plugin-promise": "^7.1.0",
44
48
  "eslint-plugin-react": "^7.33.2",
45
- "prettier": "^2.0.5",
46
- "react": "^18.3.1",
47
- "react-dom": "^18.3.1",
48
- "react-router": "^6.25.1",
49
- "react-router-dom": "^6.25.1",
50
- "rimraf": "^3.0.2",
51
- "typedoc": "^0.24.1",
52
- "typescript": "^4.6.3",
53
- "vite": "^5.2.10",
54
- "vite-plugin-dts": "^3.9.1",
49
+ "prettier": "^3.3.3",
50
+ "react": "^19.0.0",
51
+ "react-dom": "^19.0.0",
52
+ "react-router": "^7.0.1",
53
+ "react-router-dom": "^7.0.1",
54
+ "rimraf": "^6.0.1",
55
+ "rollup-plugin-visualizer": "^5.13.1",
56
+ "typedoc": "^0.27.2",
57
+ "typescript": "^5.6.3",
58
+ "typescript-eslint": "^8.13.0",
59
+ "vite": "^6.0.3",
60
+ "vite-plugin-dts": "^4.2.2",
55
61
  "vitest": "^2.0.5"
56
62
  },
57
63
  "main": "./dist/index.umd.js",
@@ -60,9 +66,9 @@
60
66
  "exports": {
61
67
  ".": {
62
68
  "import": "./dist/index.es.js",
63
- "require": "./dist/index.umd.js"
64
- },
65
- "./shopify-hydrogen": "./dist/index.es.js"
69
+ "require": "./dist/index.umd.js",
70
+ "types": "./dist/index.d.ts"
71
+ }
66
72
  },
67
73
  "bugs": {
68
74
  "url": "https://github.com/Nosto/nosto-react/issues"