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