@ea-lab/reactive-json
Version:
A REACT-based lib that transforms JSON (or YAML) into interactive HTML markup.
96 lines (95 loc) • 2.85 kB
JavaScript
import { dataLocationToPath as V, evaluateTemplateValueCollection as v } from "../../engine/TemplateSystem.js";
function T({ config: h, globalDataContext: a, templateContext: i, responseData: m }) {
const { stringMap: M = {}, onErrorMap: p } = h, { updateData: g } = a;
try {
Object.entries(M).map(([r, t]) => l({
destinationDataLocation: r,
mappingConfig: t,
sourceDataRetriever: ({ location: n }) => s({ location: n, data: m })
})).filter(Boolean).forEach(u);
} catch (e) {
if (!p)
throw e;
Object.entries(p).map(([t, n]) => l({
destinationDataLocation: t,
mappingConfig: n,
sourceDataRetriever: w
})).filter(Boolean).forEach(u);
}
function u({ destinationPath: e, value: r, updateMode: t }) {
g(r, e, t);
}
function s({ location: e, data: r }) {
if (typeof r != "object")
throw new Error("simpleMapping: Could not find location in response data: " + e + ".");
if (!e || e === "")
throw new Error("simpleMapping: Location is empty.");
const t = e.split(".")[0];
if (t in r) {
const n = e.split(".").slice(1).join(".");
return n === "" ? r[t] : s({ location: n, data: r[t] });
}
throw new Error(
"simpleMapping: Could not find location in response data: " + e + " (location not found in data)."
);
}
function w({ location: e }) {
return v({
valueToEvaluate: e,
globalDataContext: a,
templateContext: i,
evaluationDepth: -1
});
}
function l({ destinationDataLocation: e, mappingConfig: r, sourceDataRetriever: t }) {
const { value: n, required: E = !0, defaultValue: c } = r;
let f = "";
if (!n) {
console.warn("simpleMapping: 'value' is missing for", e);
return;
}
const o = V({
dataLocation: e,
// TODO: This is incorrect because simpleMapping could be called from a fetchData reaction that is not located at the root of the data.
currentPath: "data",
globalDataContext: a,
templateContext: i
});
if (typeof o != "string" || !o.startsWith("data")) {
console.warn(
"simpleMapping: the given destination path is invalid:",
e,
"->",
o
);
return;
}
try {
const d = t({ location: n });
return {
destinationPath: o,
value: d,
updateMode: f
};
} catch {
if (E)
throw new Error("simpleMapping: Required value not found in response data: " + n + ".");
if (c !== void 0) {
const y = v({
valueToEvaluate: c,
globalDataContext: a,
templateContext: i,
evaluationDepth: -1
});
return {
destinationPath: o,
value: y,
updateMode: f
};
}
}
}
}
export {
T as simpleMapping
};