@discoveryjs/discovery
Version:
Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
84 lines (83 loc) • 2.56 kB
JavaScript
import modelCommonJoraMethods from "./model-common-jora-methods.js";
import modelCommonJoraAssertions from "./model-common-jora-assertions.js";
import jora from "jora";
export function createExtensionApi(host, options) {
return {
before() {
host.objectMarkers.reset();
},
contextApi: {
markers: host.objectMarkers.markerMap(),
setWorkTitle: options?.setPrepareWorkTitle || (() => Promise.resolve()),
rejectData(message, renderContent) {
throw Object.assign(new Error(message), { renderContent });
}
}
};
}
export function setupModel(host, setup) {
const objectMarkers = host.objectMarkers;
const methods = {
setPrepare: host.setPrepare.bind(host),
defineObjectMarker,
addQueryHelpers(methods2) {
host.logger.warn("addQueryHelpers() is deprecated, use addQueryMethods() instead");
return addQueryMethods(methods2);
},
addQueryMethods,
addQueryAssertions
};
let queryCustomMethods = {
...modelCommonJoraMethods,
query: host.query.bind(host),
pageLink: (pageRef, pageId, pageParams, pageAnchor) => host.encodePageHash(pageId, pageRef, pageParams, pageAnchor),
marker: objectMarkers.lookup.bind(objectMarkers),
markerAll: objectMarkers.lookupAll.bind(objectMarkers),
callAction,
actionHandler: (actionName, ...args) => host.action.has(actionName) ? () => callAction(actionName, ...args) : void 0
};
let queryCustomAssertions = {
...modelCommonJoraAssertions
};
if (typeof setup === "function") {
setup(methods);
}
objectMarkers.lock();
host.queryFnFromString = jora.setup({
methods: queryCustomMethods,
assertions: queryCustomAssertions
});
function addQueryMethods(methods2) {
queryCustomMethods = {
...queryCustomMethods,
...methods2
};
}
function addQueryAssertions(assertions) {
queryCustomAssertions = {
...queryCustomAssertions,
...assertions
};
}
function defineObjectMarker(name, options) {
const { mark, lookup } = objectMarkers.define(name, options);
if (!lookup) {
return () => {
};
}
return mark;
}
function callAction(actionName, ...args) {
const lastArg = args[args.length - 1];
let callback = null;
if (typeof lastArg === "function") {
callback = lastArg;
args.pop();
}
const ret = host.action.call(actionName, ...args);
if (ret && callback && typeof ret.then === "function") {
return ret.then(callback);
}
return callback ? callback(ret) : ret;
}
}