@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/LICENSE +1 -1
- package/dist/index.d.ts +15 -180
- package/dist/index.es.js +197 -180
- package/dist/index.umd.js +1 -1
- package/package.json +26 -20
- package/src/components/NostoProvider.tsx +2 -1
- package/src/context.ts +2 -1
- package/src/hooks/useLoadClientScript.ts +18 -19
- package/src/hooks/useNostoApi.ts +4 -3
- package/src/hooks/useNostoOrder.tsx +1 -1
- package/src/hooks/useNostoProduct.tsx +11 -5
- package/src/hooks/useNostoSession.tsx +3 -2
- package/src/hooks/useRenderCampaigns.tsx +12 -4
- package/src/index.ts +2 -1
- package/src/types.ts +1 -965
- package/src/components/helpers.ts +0 -3
package/dist/index.es.js
CHANGED
|
@@ -1,93 +1,109 @@
|
|
|
1
|
-
import { createContext as
|
|
2
|
-
import { jsx as
|
|
3
|
-
import
|
|
4
|
-
const
|
|
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
|
|
11
|
-
return
|
|
10
|
+
function L() {
|
|
11
|
+
return V(_);
|
|
12
12
|
}
|
|
13
|
-
const
|
|
14
|
-
function
|
|
15
|
-
if (!
|
|
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 !(!
|
|
19
|
+
return !(!g(n) || !n.hasOwnProperty("isPrototypeOf"));
|
|
22
20
|
}
|
|
23
|
-
function
|
|
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) =>
|
|
30
|
-
if (
|
|
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]) =>
|
|
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
|
|
37
|
-
return
|
|
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
|
|
44
|
-
const
|
|
45
|
-
(
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
|
64
|
-
return
|
|
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
|
|
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
|
-
|
|
73
|
-
t.placements.injectCampaigns(e.recommendations);
|
|
74
|
-
});
|
|
89
|
+
T(e.recommendations);
|
|
75
90
|
}
|
|
76
|
-
function
|
|
77
|
-
const { responseMode: e, recommendationComponent: t } =
|
|
91
|
+
function l() {
|
|
92
|
+
const { responseMode: e, recommendationComponent: t } = L(), n = N({});
|
|
78
93
|
if (e == "HTML")
|
|
79
|
-
return { renderCampaigns:
|
|
94
|
+
return { renderCampaigns: K };
|
|
80
95
|
function r(o) {
|
|
81
|
-
var
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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:
|
|
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
|
|
99
|
-
const { renderCampaigns: t } =
|
|
100
|
-
|
|
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
|
|
106
|
-
return
|
|
121
|
+
function fe(e) {
|
|
122
|
+
return Q(e), null;
|
|
107
123
|
}
|
|
108
|
-
function
|
|
109
|
-
const { renderCampaigns: n } =
|
|
110
|
-
|
|
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
|
|
119
|
-
return
|
|
134
|
+
function pe(e) {
|
|
135
|
+
return X(e), null;
|
|
120
136
|
}
|
|
121
|
-
function
|
|
122
|
-
const { renderCampaigns: t } =
|
|
123
|
-
|
|
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
|
|
129
|
-
return
|
|
144
|
+
function Ce(e) {
|
|
145
|
+
return Y(e), null;
|
|
130
146
|
}
|
|
131
|
-
function
|
|
132
|
-
const { renderCampaigns: t } =
|
|
133
|
-
|
|
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
|
|
139
|
-
return
|
|
154
|
+
function we(e) {
|
|
155
|
+
return ee(e), null;
|
|
140
156
|
}
|
|
141
|
-
function
|
|
142
|
-
return !e || typeof e != "object" ||
|
|
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] =
|
|
160
|
+
return t[r] = p(e[n]), t;
|
|
145
161
|
}, {});
|
|
146
162
|
}
|
|
147
|
-
function
|
|
163
|
+
function te(e) {
|
|
148
164
|
return Object.prototype.toString.call(e) === "[object Date]";
|
|
149
165
|
}
|
|
150
|
-
function
|
|
166
|
+
function ne(e) {
|
|
151
167
|
return Object.prototype.toString.call(e) === "[object RegExp]";
|
|
152
168
|
}
|
|
153
|
-
function
|
|
154
|
-
const { renderCampaigns: n } =
|
|
155
|
-
|
|
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(
|
|
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
|
|
165
|
-
return
|
|
180
|
+
function he(e) {
|
|
181
|
+
return oe(e), null;
|
|
166
182
|
}
|
|
167
|
-
function
|
|
168
|
-
const { renderCampaigns: t } =
|
|
169
|
-
|
|
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
|
|
175
|
-
return
|
|
190
|
+
function ye(e) {
|
|
191
|
+
return re(e), null;
|
|
176
192
|
}
|
|
177
|
-
function
|
|
178
|
-
return /* @__PURE__ */
|
|
193
|
+
function Se({ id: e, pageType: t }) {
|
|
194
|
+
return /* @__PURE__ */ j("div", { className: "nosto_element", id: e }, e + (t || ""));
|
|
179
195
|
}
|
|
180
|
-
function
|
|
181
|
-
const { renderCampaigns:
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
[
|
|
208
|
+
[c, t == null ? void 0 : t.selected_sku_id]
|
|
190
209
|
);
|
|
191
210
|
}
|
|
192
|
-
function
|
|
193
|
-
return
|
|
194
|
-
}
|
|
195
|
-
function X() {
|
|
196
|
-
return typeof window.nosto < "u";
|
|
211
|
+
function Pe(e) {
|
|
212
|
+
return ce(e), null;
|
|
197
213
|
}
|
|
198
|
-
function
|
|
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,
|
|
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
|
-
|
|
220
|
+
const b = { "nosto-client-script": "" };
|
|
221
|
+
function ae(e) {
|
|
205
222
|
const {
|
|
206
223
|
host: t = "connect.nosto.com",
|
|
207
|
-
scriptLoader: n =
|
|
224
|
+
scriptLoader: n = se,
|
|
208
225
|
account: r,
|
|
209
226
|
shopifyMarkets: o,
|
|
210
227
|
loadScript: c = !0
|
|
211
|
-
} = e, [
|
|
212
|
-
return
|
|
213
|
-
function
|
|
214
|
-
|
|
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
|
|
220
|
-
const f = `//${t}${
|
|
221
|
-
await n(f, { attributes:
|
|
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
|
|
224
|
-
var R,
|
|
225
|
-
const
|
|
226
|
-
if (!
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
(R =
|
|
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
|
-
|
|
245
|
+
y($, { "nosto-language": f, "nosto-market-id": m });
|
|
232
246
|
}
|
|
233
247
|
}
|
|
234
|
-
if (
|
|
235
|
-
(
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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 &&
|
|
245
|
-
}, [o == null ? void 0 : o.marketId, o == null ? void 0 : o.language]), { clientScriptLoaded:
|
|
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
|
|
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
|
|
254
|
-
return
|
|
255
|
-
|
|
256
|
-
}), /* @__PURE__ */
|
|
257
|
-
|
|
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:
|
|
278
|
+
clientScriptLoaded: u,
|
|
262
279
|
currentVariation: c,
|
|
263
|
-
responseMode:
|
|
280
|
+
responseMode: s,
|
|
264
281
|
recommendationComponent: o
|
|
265
282
|
},
|
|
266
283
|
children: r
|
|
267
284
|
}
|
|
268
285
|
);
|
|
269
286
|
}
|
|
270
|
-
function
|
|
271
|
-
const { renderCampaigns: n } =
|
|
272
|
-
|
|
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
|
|
281
|
-
return
|
|
297
|
+
function ve(e) {
|
|
298
|
+
return ie(e), null;
|
|
282
299
|
}
|
|
283
|
-
function
|
|
284
|
-
const { clientScriptLoaded: n } =
|
|
285
|
-
|
|
286
|
-
const r = e ?
|
|
287
|
-
n &&
|
|
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
|
|
293
|
-
return
|
|
309
|
+
function Oe(e) {
|
|
310
|
+
return ue(e), null;
|
|
294
311
|
}
|
|
295
312
|
export {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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(
|
|
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.
|
|
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": "^
|
|
37
|
-
"@types/react-dom": "^
|
|
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": "^
|
|
43
|
-
"eslint-plugin-promise": "^
|
|
46
|
+
"eslint": "^9.13.0",
|
|
47
|
+
"eslint-plugin-promise": "^7.1.0",
|
|
44
48
|
"eslint-plugin-react": "^7.33.2",
|
|
45
|
-
"prettier": "^
|
|
46
|
-
"react": "^
|
|
47
|
-
"react-dom": "^
|
|
48
|
-
"react-router": "^
|
|
49
|
-
"react-router-dom": "^
|
|
50
|
-
"rimraf": "^
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
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
|
-
|
|
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"
|