@builder.io/sdk-react 0.12.7 → 0.13.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.
@@ -0,0 +1,2 @@
1
+ "use strict";const S="react",v="[Builder.io]: ",u={log:(...e)=>console.log(v,...e),error:(...e)=>console.error(v,...e),warn:(...e)=>console.warn(v,...e),debug:(...e)=>console.debug(v,...e)};function a(){return typeof window!="undefined"&&typeof document!="undefined"}function ce(){return a()&&window.self!==window.top}function C(){return ce()&&window.location.search.indexOf("builder.frameEditing=")!==-1}const ue=()=>{if(a()){const e=new URL(location.href);return e.pathname===""&&(e.pathname="/"),e}else return console.warn("Cannot get location for tracking in non-browser environment"),null},de=()=>typeof navigator=="object"&&navigator.userAgent||"",$=()=>{const e=de(),t={Android(){return e.match(/Android/i)},BlackBerry(){return e.match(/BlackBerry/i)},iOS(){return e.match(/iPhone|iPod/i)},Opera(){return e.match(/Opera Mini/i)},Windows(){return e.match(/IEMobile/i)||e.match(/WPDesktop/i)},any(){return t.Android()||t.BlackBerry()||t.iOS()||t.Opera()||t.Windows()||S==="reactNative"}},n=e.match(/Tablet|iPad/i),r=ue();return{urlPath:r==null?void 0:r.pathname,host:(r==null?void 0:r.host)||(r==null?void 0:r.hostname),device:n?"tablet":t.any()?"mobile":"desktop"}},O=e=>JSON.parse(JSON.stringify(e)),h=e=>e!=null,le=e=>{if(e==="localhost"||e==="127.0.0.1")return e;const t=e.split(".");return t.length>2?t.slice(1).join("."):e},j=({name:e,canTrack:t})=>{var n;try{return t?(n=document.cookie.split("; ").find(r=>r.startsWith(`${e}=`)))==null?void 0:n.split("=")[1]:void 0}catch(r){u.warn("[COOKIE] GET error: ",(r==null?void 0:r.message)||r);return}},K=async e=>j(e),fe=e=>e.map(([t,n])=>n?`${t}=${n}`:t).filter(h).join("; "),ge=[["secure",""],["SameSite","None"]],he=({name:e,value:t,expires:n})=>{const o=(a()?location.protocol==="https:":!0)?ge:[[]],s=n?[["expires",n.toUTCString()]]:[[]],i=[[e,t],...s,["path","/"],["domain",le(window.location.hostname)],...o];return fe(i)},_=async({name:e,value:t,expires:n,canTrack:r})=>{try{if(!r)return;const o=he({name:e,value:t,expires:n});document.cookie=o}catch(o){u.warn("[COOKIE] SET error: ",(o==null?void 0:o.message)||o)}},pe="builder.tests",T=e=>`${pe}.${e}`,me=({contentId:e})=>K({name:T(e),canTrack:!0}),ye=({contentId:e})=>j({name:T(e),canTrack:!0}),we=({contentId:e,value:t})=>_({name:T(e),value:t,canTrack:!0}),W=e=>h(e.id)&&h(e.variations)&&Object.keys(e.variations).length>0,be=({id:e,variations:t})=>{var o;let n=0;const r=Math.random();for(const s in t){const i=(o=t[s])==null?void 0:o.testRatio;if(n+=i,r<n)return s}return e},G=e=>{const t=be(e);return we({contentId:e.id,value:t}).catch(n=>{u.error("could not store A/B test variation: ",n)}),t},J=({item:e,testGroupId:t})=>{const n=e.variations[t];return t===e.id||!n?{testVariationId:e.id,testVariationName:"Default"}:{data:n.data,testVariationId:n.id,testVariationName:n.name||(n.id===e.id?"Default":"")}},Se=({item:e,canTrack:t})=>{if(!t)return e;if(!e)return;if(!W(e))return e;const n=ye({contentId:e.id})||G({variations:e.variations,id:e.id}),r=J({item:e,testGroupId:n});return{...e,...r}},Ie=async({item:e,canTrack:t})=>{if(!t||!W(e))return e;const r=await me({contentId:e.id})||G({variations:e.variations,id:e.id}),o=J({item:e,testGroupId:r});return{...e,...o}},z=e=>h(e)?e:!0,ve=[],Ee=e=>({type:"builder.registerComponent",data:H(e)}),ke=e=>{const t=e.toString().trim();return`return (${!t.startsWith("function")&&!t.startsWith("(")?"function ":""}${t}).apply(this, arguments)`},Ce=e=>typeof e=="function"?ke(e):O(e),H=({inputs:e,...t})=>({...O(t),inputs:e==null?void 0:e.map(n=>Object.entries(n).reduce((r,[o,s])=>({...r,[o]:Ce(s)}),{}))});function Oe(){return typeof globalThis!="undefined"?globalThis:typeof window!="undefined"?window:typeof global!="undefined"?global:typeof self!="undefined"?self:globalThis}function Te(){const e=Oe().fetch;if(typeof e=="undefined")throw console.warn(`Builder SDK could not find a global fetch function. Make sure you have a polyfill for fetch in your project.
2
+ For more information, read https://github.com/BuilderIO/this-package-uses-fetch`),new Error("Builder SDK could not find a global `fetch` function");return e}const X=Te();function E(e,t=null,n="."){return Object.keys(e).reduce((r,o)=>{const s=e[o],i=[t,o].filter(Boolean).join(n);return[typeof s=="object",s!==null,!(Array.isArray(s)&&s.length===0)].every(Boolean)?{...r,...E(s,i,n)}:{...r,[i]:s}},{})}const Ae="v3",M="builder.",Pe="options.",xe=e=>{const t={};return e.forEach((n,r)=>{t[r]=n}),t},A=e=>{if(!e)return{};const t=q(e),n={};return Object.keys(t).forEach(r=>{if(r.startsWith(M)){const o=r.replace(M,"").replace(Pe,"");n[o]=t[r]}}),n},Re=()=>{if(!a())return{};const e=new URLSearchParams(window.location.search);return A(e)},q=e=>e instanceof URLSearchParams?xe(e):e,U=e=>typeof e=="number"&&!isNaN(e)&&e>=0,P=e=>{let{noTraverse:t=!1}=e;const{limit:n=30,userAttributes:r,query:o,model:s,apiKey:i,includeRefs:f=!0,enrich:I,locale:p,apiVersion:y=Ae,fields:d,omit:w,offset:g,cacheSeconds:m,staleCacheSeconds:k,sort:B,includeUnpublished:L}=e;if(!i)throw new Error("Missing API key");if(!["v3"].includes(y))throw new Error(`Invalid apiVersion: expected 'v3', received '${y}'`);(e.limit===void 0||e.limit>1)&&!("noTraverse"in e)&&(t=!0);const c=new URL(`https://cdn.builder.io/api/${y}/content/${s}?apiKey=${i}&limit=${n}&noTraverse=${t}&includeRefs=${f}${p?`&locale=${p}`:""}${I?`&enrich=${I}`:""}`);if(c.searchParams.set("omit",w||"meta.componentsUsed"),d&&c.searchParams.set("fields",d),Number.isFinite(g)&&g>-1&&c.searchParams.set("offset",String(Math.floor(g))),typeof L=="boolean"&&c.searchParams.set("includeUnpublished",String(L)),m&&U(m)&&c.searchParams.set("cacheSeconds",String(m)),k&&U(k)&&c.searchParams.set("staleCacheSeconds",String(k)),B){const l=E({sort:B});for(const b in l)c.searchParams.set(b,JSON.stringify(l[b]))}const ae={...Re(),...q(e.options||{})},V=E(ae);for(const l in V)c.searchParams.set(l,String(V[l]));if(r&&c.searchParams.set("userAttributes",JSON.stringify(r)),o){const l=E({query:o});for(const b in l)c.searchParams.set(b,JSON.stringify(l[b]))}return c},Be=e=>"results"in e;async function x(e){const t=await R({...e,limit:1});return t&&t[0]||null}const Le=x,Ve=async e=>{const t=P(e);return await(await X(t.href)).json()},Y=async(e,t,n=P(e))=>{const r=z(e.canTrack);if(n.search.includes("preview="),!r||!(a()||S==="reactNative"))return t.results;try{const o=[];for(const s of t.results)o.push(await Ie({item:s,canTrack:r}));t.results=o}catch(o){u.error("Could not process A/B tests. ",o)}return t.results};async function R(e){try{const t=P(e),n=await Ve(e);return Be(n)?Y(e,n):(u.error("Error fetching data. ",{url:t,content:n,options:e}),null)}catch(t){return u.error("Error fetching data. ",t),null}}const Me=R;function Ue(){return!a()||C()?!1:location.search.indexOf("builder.preview=")!==-1}function Fe(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){const t=Math.random()*16|0;return(e=="x"?t:t&3|8).toString(16)})}function Q(){return Fe().replace(/-/g,"")}const Z="builderSessionId",Ne=async({canTrack:e})=>{if(!e)return;const t=await K({name:Z,canTrack:e});if(h(t))return t;{const n=De();return $e({id:n,canTrack:e}),n}},De=()=>Q(),$e=({id:e,canTrack:t})=>_({name:Z,value:e,canTrack:t}),ee=()=>a()&&typeof localStorage!="undefined"?localStorage:void 0,je=({key:e,canTrack:t})=>{var n;try{return t?(n=ee())==null?void 0:n.getItem(e):void 0}catch(r){console.debug("[LocalStorage] GET error: ",r);return}},Ke=({key:e,canTrack:t,value:n})=>{var r;try{t&&((r=ee())==null||r.setItem(e,n))}catch(o){console.debug("[LocalStorage] SET error: ",o)}},te="builderVisitorId",_e=({canTrack:e})=>{if(!e)return;const t=je({key:te,canTrack:e});if(h(t))return t;{const n=We();return Ge({id:n,canTrack:e}),n}},We=()=>Q(),Ge=({id:e,canTrack:t})=>Ke({key:te,value:e,canTrack:t}),Je=async({canTrack:e})=>{if(!e)return{visitorId:void 0,sessionId:void 0};const t=await Ne({canTrack:e}),n=_e({canTrack:e});return{sessionId:t,visitorId:n}},ze=async({type:e,canTrack:t,apiKey:n,metadata:r,...o})=>({type:e,data:{...o,metadata:{url:location.href,...r},...await Je({canTrack:t}),userAttributes:$(),ownerId:n}});async function ne(e){if(!e.apiKey){u.error("Missing API key for track call. Please provide your API key.");return}if(e.canTrack&&!C()&&(a()||S==="reactNative"))return fetch("https://cdn.builder.io/api/v1/track",{method:"POST",body:JSON.stringify({events:[await ze(e)]}),headers:{"content-type":"application/json"},mode:"cors"}).catch(t=>{console.error("Failed to track: ",t)})}const He=e=>ne({...e,canTrack:!0}),Xe=["*.beta.builder.io","beta.builder.io","builder.io","localhost","qa.builder.io"];function re(e,t){const n=new URL(t.origin),r=n.hostname;return(e||Xe).findIndex(o=>o.startsWith("*.")?r.endsWith(o.slice(1)):o===r)>-1}const qe="0.13.0",F={};function oe(e,t){let n=F[e];if(n||(n=F[e]=[]),n.push(t),a()){const r={type:"builder.register",data:{type:e,info:t}};try{parent.postMessage(r,"*"),parent!==window&&window.postMessage(r,"*")}catch(o){console.debug("Could not postmessage",o)}}}const Ye=()=>{oe("insertMenu",{name:"_default",default:!0,items:[{name:"Box"},{name:"Text"},{name:"Image"},{name:"Columns"},{name:"Core:Section"},{name:"Core:Button"},{name:"Embed"},{name:"Custom Code"}]})};let N=!1;const se=(e={})=>{var t,n;N||(N=!0,a()&&((t=window.parent)==null||t.postMessage({type:"builder.sdkInfo",data:{target:S,version:qe,supportsPatchUpdates:!1,supportsAddBlockScoping:!0,supportsCustomBreakpoints:!0}},"*"),(n=window.parent)==null||n.postMessage({type:"builder.updateContent",data:{options:e}},"*"),window.addEventListener("message",r=>{var s,i;if(!re(e.trustedHosts,r))return;const{data:o}=r;if(o!=null&&o.type)switch(o.type){case"builder.evaluate":{const f=o.data.text,I=o.data.arguments||[],p=o.data.id,y=new Function(f);let d,w=null;try{d=y.apply(null,I)}catch(g){w=g}w?(s=window.parent)==null||s.postMessage({type:"builder.evaluateError",data:{id:p,error:w.message}},"*"):d&&typeof d.then=="function"?d.then(g=>{var m;(m=window.parent)==null||m.postMessage({type:"builder.evaluateResult",data:{id:p,result:g}},"*")}).catch(console.error):(i=window.parent)==null||i.postMessage({type:"builder.evaluateResult",data:{result:d,id:p}},"*");break}}})))},ie=({model:e,trustedHosts:t,callbacks:n})=>r=>{if(!re(t,r))return;const{data:o}=r;if(o)switch(o.type){case"builder.configureSdk":{n.configureSdk(o.data);break}case"builder.triggerAnimation":{n.animation(o.data);break}case"builder.contentUpdate":{const s=o.data,i=s.key||s.alias||s.entry||s.modelName,f=s.data;i===e&&n.contentUpdate(f);break}}},Qe=(e,t,n)=>{if(!a)return u.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server."),()=>{};se();const r=ie({callbacks:{contentUpdate:t,animation:()=>{},configureSdk:()=>{}},model:e,trustedHosts:n==null?void 0:n.trustedHosts});return window.addEventListener("message",r),()=>{window.removeEventListener("message",r)}},D={};function Ze(e){if(a()){Object.assign(D,e);const t={type:"builder.settingsChange",data:D};parent.postMessage(t,"*")}}const et=async e=>{var r,o,s;const t=e.path||((r=e.url)==null?void 0:r.pathname)||((o=e.userAttributes)==null?void 0:o.urlPath),n={...e,apiKey:e.apiKey,model:e.model||"page",userAttributes:{...e.userAttributes,...t?{urlPath:t}:{}},options:A(e.searchParams||((s=e.url)==null?void 0:s.searchParams)||e.options)};return{apiKey:n.apiKey,model:n.model,content:await x(n)}};exports.TARGET=S;exports._processContentResult=Y;exports._track=ne;exports.checkIsDefined=h;exports.components=ve;exports.createEditorListener=ie;exports.createRegisterComponentMessage=Ee;exports.fastClone=O;exports.fetch=X;exports.fetchBuilderProps=et;exports.fetchEntries=R;exports.fetchOneEntry=x;exports.getAllContent=Me;exports.getBuilderSearchParams=A;exports.getContent=Le;exports.getDefaultCanTrack=z;exports.getUserAttributes=$;exports.handleABTestingSync=Se;exports.isBrowser=a;exports.isEditing=C;exports.isPreviewing=Ue;exports.logger=u;exports.register=oe;exports.registerInsertMenu=Ye;exports.serializeComponentInfo=H;exports.setEditorSettings=Ze;exports.setupBrowserForEditing=se;exports.subscribeToEditor=Qe;exports.track=He;
@@ -0,0 +1,690 @@
1
+ const k = "react", v = "[Builder.io]: ", l = {
2
+ log: (...e) => console.log(v, ...e),
3
+ error: (...e) => console.error(v, ...e),
4
+ warn: (...e) => console.warn(v, ...e),
5
+ debug: (...e) => console.debug(v, ...e)
6
+ };
7
+ function i() {
8
+ return typeof window != "undefined" && typeof document != "undefined";
9
+ }
10
+ function Z() {
11
+ return i() && window.self !== window.top;
12
+ }
13
+ function U() {
14
+ return Z() && window.location.search.indexOf("builder.frameEditing=") !== -1;
15
+ }
16
+ const ee = () => {
17
+ if (i()) {
18
+ const e = new URL(location.href);
19
+ return e.pathname === "" && (e.pathname = "/"), e;
20
+ } else
21
+ return console.warn("Cannot get location for tracking in non-browser environment"), null;
22
+ }, te = () => typeof navigator == "object" && navigator.userAgent || "", ne = () => {
23
+ const e = te(), t = {
24
+ Android() {
25
+ return e.match(/Android/i);
26
+ },
27
+ BlackBerry() {
28
+ return e.match(/BlackBerry/i);
29
+ },
30
+ iOS() {
31
+ return e.match(/iPhone|iPod/i);
32
+ },
33
+ Opera() {
34
+ return e.match(/Opera Mini/i);
35
+ },
36
+ Windows() {
37
+ return e.match(/IEMobile/i) || e.match(/WPDesktop/i);
38
+ },
39
+ any() {
40
+ return t.Android() || t.BlackBerry() || t.iOS() || t.Opera() || t.Windows() || k === "reactNative";
41
+ }
42
+ }, n = e.match(/Tablet|iPad/i), r = ee();
43
+ return {
44
+ urlPath: r == null ? void 0 : r.pathname,
45
+ host: (r == null ? void 0 : r.host) || (r == null ? void 0 : r.hostname),
46
+ device: n ? "tablet" : t.any() ? "mobile" : "desktop"
47
+ };
48
+ }, M = (e) => JSON.parse(JSON.stringify(e)), m = (e) => e != null, re = (e) => {
49
+ if (e === "localhost" || e === "127.0.0.1")
50
+ return e;
51
+ const t = e.split(".");
52
+ return t.length > 2 ? t.slice(1).join(".") : e;
53
+ }, N = ({
54
+ name: e,
55
+ canTrack: t
56
+ }) => {
57
+ var n;
58
+ try {
59
+ return t ? (n = document.cookie.split("; ").find((r) => r.startsWith(`${e}=`))) == null ? void 0 : n.split("=")[1] : void 0;
60
+ } catch (r) {
61
+ l.warn("[COOKIE] GET error: ", (r == null ? void 0 : r.message) || r);
62
+ return;
63
+ }
64
+ }, F = async (e) => N(e), oe = (e) => e.map(([t, n]) => n ? `${t}=${n}` : t).filter(m).join("; "), se = [["secure", ""], ["SameSite", "None"]], ae = ({
65
+ name: e,
66
+ value: t,
67
+ expires: n
68
+ }) => {
69
+ const o = (i() ? location.protocol === "https:" : !0) ? se : [[]], s = n ? [["expires", n.toUTCString()]] : [[]], a = [[e, t], ...s, ["path", "/"], ["domain", re(window.location.hostname)], ...o];
70
+ return oe(a);
71
+ }, $ = async ({
72
+ name: e,
73
+ value: t,
74
+ expires: n,
75
+ canTrack: r
76
+ }) => {
77
+ try {
78
+ if (!r)
79
+ return;
80
+ const o = ae({
81
+ name: e,
82
+ value: t,
83
+ expires: n
84
+ });
85
+ document.cookie = o;
86
+ } catch (o) {
87
+ l.warn("[COOKIE] SET error: ", (o == null ? void 0 : o.message) || o);
88
+ }
89
+ }, ie = "builder.tests", C = (e) => `${ie}.${e}`, ce = ({
90
+ contentId: e
91
+ }) => F({
92
+ name: C(e),
93
+ canTrack: !0
94
+ }), ue = ({
95
+ contentId: e
96
+ }) => N({
97
+ name: C(e),
98
+ canTrack: !0
99
+ }), de = ({
100
+ contentId: e,
101
+ value: t
102
+ }) => $({
103
+ name: C(e),
104
+ value: t,
105
+ canTrack: !0
106
+ }), j = (e) => m(e.id) && m(e.variations) && Object.keys(e.variations).length > 0, le = ({
107
+ id: e,
108
+ variations: t
109
+ }) => {
110
+ var o;
111
+ let n = 0;
112
+ const r = Math.random();
113
+ for (const s in t) {
114
+ const a = (o = t[s]) == null ? void 0 : o.testRatio;
115
+ if (n += a, r < n)
116
+ return s;
117
+ }
118
+ return e;
119
+ }, D = (e) => {
120
+ const t = le(e);
121
+ return de({
122
+ contentId: e.id,
123
+ value: t
124
+ }).catch((n) => {
125
+ l.error("could not store A/B test variation: ", n);
126
+ }), t;
127
+ }, K = ({
128
+ item: e,
129
+ testGroupId: t
130
+ }) => {
131
+ const n = e.variations[t];
132
+ return t === e.id || // handle edge-case where `testGroupId` points to non-existing variation
133
+ !n ? {
134
+ testVariationId: e.id,
135
+ testVariationName: "Default"
136
+ } : {
137
+ data: n.data,
138
+ testVariationId: n.id,
139
+ testVariationName: n.name || (n.id === e.id ? "Default" : "")
140
+ };
141
+ }, We = ({
142
+ item: e,
143
+ canTrack: t
144
+ }) => {
145
+ if (!t)
146
+ return e;
147
+ if (!e)
148
+ return;
149
+ if (!j(e))
150
+ return e;
151
+ const n = ue({
152
+ contentId: e.id
153
+ }) || D({
154
+ variations: e.variations,
155
+ id: e.id
156
+ }), r = K({
157
+ item: e,
158
+ testGroupId: n
159
+ });
160
+ return {
161
+ ...e,
162
+ ...r
163
+ };
164
+ }, fe = async ({
165
+ item: e,
166
+ canTrack: t
167
+ }) => {
168
+ if (!t || !j(e))
169
+ return e;
170
+ const r = await ce({
171
+ contentId: e.id
172
+ }) || D({
173
+ variations: e.variations,
174
+ id: e.id
175
+ }), o = K({
176
+ item: e,
177
+ testGroupId: r
178
+ });
179
+ return {
180
+ ...e,
181
+ ...o
182
+ };
183
+ }, ge = (e) => m(e) ? e : !0, Ge = [], Je = (e) => ({
184
+ type: "builder.registerComponent",
185
+ data: me(e)
186
+ }), he = (e) => {
187
+ const t = e.toString().trim();
188
+ return `return (${!t.startsWith("function") && !t.startsWith("(") ? "function " : ""}${t}).apply(this, arguments)`;
189
+ }, pe = (e) => typeof e == "function" ? he(e) : M(e), me = ({
190
+ inputs: e,
191
+ ...t
192
+ }) => ({
193
+ ...M(t),
194
+ inputs: e == null ? void 0 : e.map((n) => Object.entries(n).reduce((r, [o, s]) => ({
195
+ ...r,
196
+ [o]: pe(s)
197
+ }), {}))
198
+ });
199
+ function ye() {
200
+ return typeof globalThis != "undefined" ? globalThis : typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : globalThis;
201
+ }
202
+ function we() {
203
+ const e = ye().fetch;
204
+ if (typeof e == "undefined")
205
+ throw console.warn(`Builder SDK could not find a global fetch function. Make sure you have a polyfill for fetch in your project.
206
+ For more information, read https://github.com/BuilderIO/this-package-uses-fetch`), new Error("Builder SDK could not find a global `fetch` function");
207
+ return e;
208
+ }
209
+ const be = we();
210
+ function I(e, t = null, n = ".") {
211
+ return Object.keys(e).reduce((r, o) => {
212
+ const s = e[o], a = [t, o].filter(Boolean).join(n);
213
+ return [typeof s == "object", s !== null, !(Array.isArray(s) && s.length === 0)].every(Boolean) ? {
214
+ ...r,
215
+ ...I(s, a, n)
216
+ } : {
217
+ ...r,
218
+ [a]: s
219
+ };
220
+ }, {});
221
+ }
222
+ const Se = "v3", T = "builder.", ve = "options.", Ie = (e) => {
223
+ const t = {};
224
+ return e.forEach((n, r) => {
225
+ t[r] = n;
226
+ }), t;
227
+ }, _ = (e) => {
228
+ if (!e)
229
+ return {};
230
+ const t = W(e), n = {};
231
+ return Object.keys(t).forEach((r) => {
232
+ if (r.startsWith(T)) {
233
+ const o = r.replace(T, "").replace(ve, "");
234
+ n[o] = t[r];
235
+ }
236
+ }), n;
237
+ }, ke = () => {
238
+ if (!i())
239
+ return {};
240
+ const e = new URLSearchParams(window.location.search);
241
+ return _(e);
242
+ }, W = (e) => e instanceof URLSearchParams ? Ie(e) : e, R = (e) => typeof e == "number" && !isNaN(e) && e >= 0, O = (e) => {
243
+ let {
244
+ noTraverse: t = !1
245
+ } = e;
246
+ const {
247
+ limit: n = 30,
248
+ userAttributes: r,
249
+ query: o,
250
+ model: s,
251
+ apiKey: a,
252
+ includeRefs: f = !0,
253
+ enrich: S,
254
+ locale: h,
255
+ apiVersion: y = Se,
256
+ fields: u,
257
+ omit: w,
258
+ offset: g,
259
+ cacheSeconds: p,
260
+ staleCacheSeconds: E,
261
+ sort: x,
262
+ includeUnpublished: A
263
+ } = e;
264
+ if (!a)
265
+ throw new Error("Missing API key");
266
+ if (!["v3"].includes(y))
267
+ throw new Error(`Invalid apiVersion: expected 'v3', received '${y}'`);
268
+ (e.limit === void 0 || e.limit > 1) && !("noTraverse" in e) && (t = !0);
269
+ const c = new URL(`https://cdn.builder.io/api/${y}/content/${s}?apiKey=${a}&limit=${n}&noTraverse=${t}&includeRefs=${f}${h ? `&locale=${h}` : ""}${S ? `&enrich=${S}` : ""}`);
270
+ if (c.searchParams.set("omit", w || "meta.componentsUsed"), u && c.searchParams.set("fields", u), Number.isFinite(g) && g > -1 && c.searchParams.set("offset", String(Math.floor(g))), typeof A == "boolean" && c.searchParams.set("includeUnpublished", String(A)), p && R(p) && c.searchParams.set("cacheSeconds", String(p)), E && R(E) && c.searchParams.set("staleCacheSeconds", String(E)), x) {
271
+ const d = I({
272
+ sort: x
273
+ });
274
+ for (const b in d)
275
+ c.searchParams.set(b, JSON.stringify(d[b]));
276
+ }
277
+ const Q = {
278
+ ...ke(),
279
+ ...W(e.options || {})
280
+ }, P = I(Q);
281
+ for (const d in P)
282
+ c.searchParams.set(d, String(P[d]));
283
+ if (r && c.searchParams.set("userAttributes", JSON.stringify(r)), o) {
284
+ const d = I({
285
+ query: o
286
+ });
287
+ for (const b in d)
288
+ c.searchParams.set(b, JSON.stringify(d[b]));
289
+ }
290
+ return c;
291
+ }, Ee = (e) => "results" in e;
292
+ async function G(e) {
293
+ const t = await J({
294
+ ...e,
295
+ limit: 1
296
+ });
297
+ return t && t[0] || null;
298
+ }
299
+ const ze = G, Ce = async (e) => {
300
+ const t = O(e);
301
+ return await (await be(t.href)).json();
302
+ }, Oe = async (e, t, n = O(e)) => {
303
+ const r = ge(e.canTrack);
304
+ if (n.search.includes("preview="), !r || !(i() || k === "reactNative"))
305
+ return t.results;
306
+ try {
307
+ const o = [];
308
+ for (const s of t.results)
309
+ o.push(await fe({
310
+ item: s,
311
+ canTrack: r
312
+ }));
313
+ t.results = o;
314
+ } catch (o) {
315
+ l.error("Could not process A/B tests. ", o);
316
+ }
317
+ return t.results;
318
+ };
319
+ async function J(e) {
320
+ try {
321
+ const t = O(e), n = await Ce(e);
322
+ return Ee(n) ? Oe(e, n) : (l.error("Error fetching data. ", {
323
+ url: t,
324
+ content: n,
325
+ options: e
326
+ }), null);
327
+ } catch (t) {
328
+ return l.error("Error fetching data. ", t), null;
329
+ }
330
+ }
331
+ const He = J;
332
+ function qe() {
333
+ return !i() || U() ? !1 : location.search.indexOf("builder.preview=") !== -1;
334
+ }
335
+ function xe() {
336
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(e) {
337
+ const t = Math.random() * 16 | 0;
338
+ return (e == "x" ? t : t & 3 | 8).toString(16);
339
+ });
340
+ }
341
+ function z() {
342
+ return xe().replace(/-/g, "");
343
+ }
344
+ const H = "builderSessionId", Ae = async ({
345
+ canTrack: e
346
+ }) => {
347
+ if (!e)
348
+ return;
349
+ const t = await F({
350
+ name: H,
351
+ canTrack: e
352
+ });
353
+ if (m(t))
354
+ return t;
355
+ {
356
+ const n = Pe();
357
+ return Te({
358
+ id: n,
359
+ canTrack: e
360
+ }), n;
361
+ }
362
+ }, Pe = () => z(), Te = ({
363
+ id: e,
364
+ canTrack: t
365
+ }) => $({
366
+ name: H,
367
+ value: e,
368
+ canTrack: t
369
+ }), q = () => i() && typeof localStorage != "undefined" ? localStorage : void 0, Re = ({
370
+ key: e,
371
+ canTrack: t
372
+ }) => {
373
+ var n;
374
+ try {
375
+ return t ? (n = q()) == null ? void 0 : n.getItem(e) : void 0;
376
+ } catch (r) {
377
+ console.debug("[LocalStorage] GET error: ", r);
378
+ return;
379
+ }
380
+ }, Be = ({
381
+ key: e,
382
+ canTrack: t,
383
+ value: n
384
+ }) => {
385
+ var r;
386
+ try {
387
+ t && ((r = q()) == null || r.setItem(e, n));
388
+ } catch (o) {
389
+ console.debug("[LocalStorage] SET error: ", o);
390
+ }
391
+ }, X = "builderVisitorId", Ve = ({
392
+ canTrack: e
393
+ }) => {
394
+ if (!e)
395
+ return;
396
+ const t = Re({
397
+ key: X,
398
+ canTrack: e
399
+ });
400
+ if (m(t))
401
+ return t;
402
+ {
403
+ const n = Le();
404
+ return Ue({
405
+ id: n,
406
+ canTrack: e
407
+ }), n;
408
+ }
409
+ }, Le = () => z(), Ue = ({
410
+ id: e,
411
+ canTrack: t
412
+ }) => Be({
413
+ key: X,
414
+ value: e,
415
+ canTrack: t
416
+ }), Me = async ({
417
+ canTrack: e
418
+ }) => {
419
+ if (!e)
420
+ return {
421
+ visitorId: void 0,
422
+ sessionId: void 0
423
+ };
424
+ const t = await Ae({
425
+ canTrack: e
426
+ }), n = Ve({
427
+ canTrack: e
428
+ });
429
+ return {
430
+ sessionId: t,
431
+ visitorId: n
432
+ };
433
+ }, Ne = async ({
434
+ type: e,
435
+ canTrack: t,
436
+ apiKey: n,
437
+ metadata: r,
438
+ ...o
439
+ }) => ({
440
+ type: e,
441
+ data: {
442
+ ...o,
443
+ metadata: {
444
+ url: location.href,
445
+ ...r
446
+ },
447
+ ...await Me({
448
+ canTrack: t
449
+ }),
450
+ userAttributes: ne(),
451
+ ownerId: n
452
+ }
453
+ });
454
+ async function Fe(e) {
455
+ if (!e.apiKey) {
456
+ l.error("Missing API key for track call. Please provide your API key.");
457
+ return;
458
+ }
459
+ if (e.canTrack && !U() && (i() || k === "reactNative"))
460
+ return fetch("https://cdn.builder.io/api/v1/track", {
461
+ method: "POST",
462
+ body: JSON.stringify({
463
+ events: [await Ne(e)]
464
+ }),
465
+ headers: {
466
+ "content-type": "application/json"
467
+ },
468
+ mode: "cors"
469
+ }).catch((t) => {
470
+ console.error("Failed to track: ", t);
471
+ });
472
+ }
473
+ const Xe = (e) => Fe({
474
+ ...e,
475
+ canTrack: !0
476
+ }), $e = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
477
+ function Y(e, t) {
478
+ const n = new URL(t.origin), r = n.hostname;
479
+ return (e || $e).findIndex((o) => o.startsWith("*.") ? r.endsWith(o.slice(1)) : o === r) > -1;
480
+ }
481
+ const je = "0.13.0", B = {};
482
+ function De(e, t) {
483
+ let n = B[e];
484
+ if (n || (n = B[e] = []), n.push(t), i()) {
485
+ const r = {
486
+ type: "builder.register",
487
+ data: {
488
+ type: e,
489
+ info: t
490
+ }
491
+ };
492
+ try {
493
+ parent.postMessage(r, "*"), parent !== window && window.postMessage(r, "*");
494
+ } catch (o) {
495
+ console.debug("Could not postmessage", o);
496
+ }
497
+ }
498
+ }
499
+ const Ye = () => {
500
+ De("insertMenu", {
501
+ name: "_default",
502
+ default: !0,
503
+ items: [{
504
+ name: "Box"
505
+ }, {
506
+ name: "Text"
507
+ }, {
508
+ name: "Image"
509
+ }, {
510
+ name: "Columns"
511
+ }, {
512
+ name: "Core:Section"
513
+ }, {
514
+ name: "Core:Button"
515
+ }, {
516
+ name: "Embed"
517
+ }, {
518
+ name: "Custom Code"
519
+ }]
520
+ });
521
+ };
522
+ let V = !1;
523
+ const Ke = (e = {}) => {
524
+ var t, n;
525
+ V || (V = !0, i() && ((t = window.parent) == null || t.postMessage({
526
+ type: "builder.sdkInfo",
527
+ data: {
528
+ target: k,
529
+ version: je,
530
+ supportsPatchUpdates: !1,
531
+ // Supports builder-model="..." attribute which is needed to
532
+ // scope our '+ add block' button styling
533
+ supportsAddBlockScoping: !0,
534
+ supportsCustomBreakpoints: !0
535
+ }
536
+ }, "*"), (n = window.parent) == null || n.postMessage({
537
+ type: "builder.updateContent",
538
+ data: {
539
+ options: e
540
+ }
541
+ }, "*"), window.addEventListener("message", (r) => {
542
+ var s, a;
543
+ if (!Y(e.trustedHosts, r))
544
+ return;
545
+ const {
546
+ data: o
547
+ } = r;
548
+ if (o != null && o.type)
549
+ switch (o.type) {
550
+ case "builder.evaluate": {
551
+ const f = o.data.text, S = o.data.arguments || [], h = o.data.id, y = new Function(f);
552
+ let u, w = null;
553
+ try {
554
+ u = y.apply(null, S);
555
+ } catch (g) {
556
+ w = g;
557
+ }
558
+ w ? (s = window.parent) == null || s.postMessage({
559
+ type: "builder.evaluateError",
560
+ data: {
561
+ id: h,
562
+ error: w.message
563
+ }
564
+ }, "*") : u && typeof u.then == "function" ? u.then((g) => {
565
+ var p;
566
+ (p = window.parent) == null || p.postMessage({
567
+ type: "builder.evaluateResult",
568
+ data: {
569
+ id: h,
570
+ result: g
571
+ }
572
+ }, "*");
573
+ }).catch(console.error) : (a = window.parent) == null || a.postMessage({
574
+ type: "builder.evaluateResult",
575
+ data: {
576
+ result: u,
577
+ id: h
578
+ }
579
+ }, "*");
580
+ break;
581
+ }
582
+ }
583
+ })));
584
+ }, _e = ({
585
+ model: e,
586
+ trustedHosts: t,
587
+ callbacks: n
588
+ }) => (r) => {
589
+ if (!Y(t, r))
590
+ return;
591
+ const {
592
+ data: o
593
+ } = r;
594
+ if (o)
595
+ switch (o.type) {
596
+ case "builder.configureSdk": {
597
+ n.configureSdk(o.data);
598
+ break;
599
+ }
600
+ case "builder.triggerAnimation": {
601
+ n.animation(o.data);
602
+ break;
603
+ }
604
+ case "builder.contentUpdate": {
605
+ const s = o.data, a = s.key || s.alias || s.entry || s.modelName, f = s.data;
606
+ a === e && n.contentUpdate(f);
607
+ break;
608
+ }
609
+ }
610
+ }, Qe = (e, t, n) => {
611
+ if (!i)
612
+ return l.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server."), () => {
613
+ };
614
+ Ke();
615
+ const r = _e({
616
+ callbacks: {
617
+ contentUpdate: t,
618
+ animation: () => {
619
+ },
620
+ configureSdk: () => {
621
+ }
622
+ },
623
+ model: e,
624
+ trustedHosts: n == null ? void 0 : n.trustedHosts
625
+ });
626
+ return window.addEventListener("message", r), () => {
627
+ window.removeEventListener("message", r);
628
+ };
629
+ }, L = {};
630
+ function Ze(e) {
631
+ if (i()) {
632
+ Object.assign(L, e);
633
+ const t = {
634
+ type: "builder.settingsChange",
635
+ data: L
636
+ };
637
+ parent.postMessage(t, "*");
638
+ }
639
+ }
640
+ const et = async (e) => {
641
+ var r, o, s;
642
+ const t = e.path || ((r = e.url) == null ? void 0 : r.pathname) || ((o = e.userAttributes) == null ? void 0 : o.urlPath), n = {
643
+ ...e,
644
+ apiKey: e.apiKey,
645
+ model: e.model || "page",
646
+ userAttributes: {
647
+ ...e.userAttributes,
648
+ ...t ? {
649
+ urlPath: t
650
+ } : {}
651
+ },
652
+ options: _(e.searchParams || ((s = e.url) == null ? void 0 : s.searchParams) || e.options)
653
+ };
654
+ return {
655
+ apiKey: n.apiKey,
656
+ model: n.model,
657
+ content: await G(n)
658
+ };
659
+ };
660
+ export {
661
+ We as A,
662
+ k as T,
663
+ Oe as _,
664
+ qe as a,
665
+ Qe as b,
666
+ Je as c,
667
+ J as d,
668
+ G as e,
669
+ et as f,
670
+ _ as g,
671
+ He as h,
672
+ U as i,
673
+ ze as j,
674
+ i as k,
675
+ ne as l,
676
+ M as m,
677
+ m as n,
678
+ l as o,
679
+ ge as p,
680
+ Fe as q,
681
+ De as r,
682
+ Ze as s,
683
+ Xe as t,
684
+ _e as u,
685
+ Ye as v,
686
+ Ke as w,
687
+ be as x,
688
+ Ge as y,
689
+ me as z
690
+ };