UNPKG

@query-api/react

Version:
137 lines (136 loc) 3.78 kB
import { jsx as s, jsxs as h } from "react/jsx-runtime"; import { SITE_DETECTION_MODES as w } from "@query-api/js"; const y = { baseUrl: "", authToken: "", contentMapping: { pages: {}, components: {} }, debug: !1, enableEntryTypeMapping: !0, siteDetectionMode: w.PATH, siteMap: [] }; let a = { instance: null }; const p = globalThis; p.__CRAFT_INSTANCE_HOLDER__ || (p.__CRAFT_INSTANCE_HOLDER__ = a); a = p.__CRAFT_INSTANCE_HOLDER__; function N(n = y) { return a.instance = { ...y, ...n }, a.instance; } function C() { if (!a.instance) throw new Error( "Craft CMS instance not found. Ensure `craftInit()` is called in a shared entry point like your root layout. This error can occur in development if the instance is not correctly preserved across module reloads." ); return a.instance; } function M() { const n = C(); if (n.contentMapping === void 0) throw new Error( "You can't use getContentMapping if you're not loading craftInit and provide a valid contentMapping." ); return n.contentMapping; } function _(n, e) { if (!e || !("components" in e)) throw new Error( 'Configuration is missing the "components" property or is invalid. Check your config object' ); if (!("type" in n) || typeof n.type != "string") throw new Error("Provided data has no valid type set. Check your queried data."); const t = e.components[n.type]; return t || console.error(`No mapped component found for component type: ${n.type}`), t; } const I = ({ content: n }) => { const e = M(); if (!n) return; const t = []; for (let o = 0; o < n.length; o++) { const r = n[o], i = _(r, e); i && t.push(/* @__PURE__ */ s(i, { ...r }, o)); } return /* @__PURE__ */ s("div", { children: t }); }; function c(n, e, t) { const o = n.pages, r = o[`page${e}`]; if (r) return r; const i = o.error; if (i) return i; throw new Error(t); } function T(n, e) { var l; const t = n.contentMapping; if (!t) throw new Error( "You can't use getContentMapping if you're not loading craftInit and provide a valid contentMapping." ); const { pages: o } = t; if (!o) throw new Error( 'ContentMapping configuration is missing the "pages" property or is invalid. Check your "contentMapping" object.' ); if (!e) return c( t, "500", "No content was provided in CraftPage. Please check your queried data." ); const r = e.sectionHandle; if (!r) return c( t, "500", "Section handle not found in queried data. Check your query or prevent it by defining an error page." ); const i = ((l = e.metadata) == null ? void 0 : l.entryType) ?? "default", d = n.enableEntryTypeMapping ?? !1; if (d) { const E = `${r}:${i}`, g = o[E]; if (g) return g; } const f = o[r]; if (f) return f; const u = `No mapped page found for section handle "${r}"`, m = `${u} with entry type handle "${i}"`; return c( t, "404", d ? `${m}.` : `${u}.` ); } const k = ({ content: n }) => { const e = T(C(), n); return /* @__PURE__ */ s(e, { ...n }); }, P = (n) => { const { type: e, sectionHandle: t, ...o } = n; return /* @__PURE__ */ h("div", { children: [ /* @__PURE__ */ h("h2", { children: [ "Not Implemented ", e ? "Component" : "Page", ": ", e || t ] }), /* @__PURE__ */ s("pre", { children: JSON.stringify(o, null, 2) }) ] }); }; function A(n) { if (window === void 0) throw new Error("window is undefined, this function can only be used in the browser."); return { pathname: n.pathname, absoluteUrl: window.location.origin + n.pathname + n.search }; } export { I as C, k as a, P as b, N as c, M as d, A as e, y as f, C as g };