UNPKG

@memberjunction/ng-react

Version:

Angular components for hosting React components in MemberJunction applications

113 lines 3.93 kB
/** * @fileoverview Runtime utilities for React components providing access to MemberJunction core functionality * @module @memberjunction/ng-react/utilities */ import { __decorate } from "tslib"; import { Metadata, RunView, RunQuery, LogError } from '@memberjunction/core'; import { MJGlobal, RegisterClass } from '@memberjunction/global'; /** * Base class for providing runtime utilities to React components in Angular. * This class can be extended and registered with MJ's ClassFactory * to provide custom implementations of data access methods. */ let RuntimeUtilities = class RuntimeUtilities { /** * Builds the complete utilities object for React components * This is the main method that components will use */ buildUtilities() { const md = new Metadata(); return this.SetupUtilities(md); } /** * Sets up the utilities object - copied from skip-chat implementation */ SetupUtilities(md) { const rv = new RunView(); const rq = new RunQuery(); const u = { md: this.CreateSimpleMetadata(md), rv: this.CreateSimpleRunView(rv), rq: this.CreateSimpleRunQuery(rq) }; return u; } CreateSimpleMetadata(md) { return { Entities: md.Entities, GetEntityObject: (entityName) => { return md.GetEntityObject(entityName); } }; } CreateSimpleRunQuery(rq) { return { RunQuery: async (params) => { // Run a single query and return the results try { const result = await rq.RunQuery(params); return result; } catch (error) { LogError(error); throw error; // Re-throw to handle it in the caller } } }; } CreateSimpleRunView(rv) { return { RunView: async (params) => { // Run a single view and return the results try { const result = await rv.RunView(params); return result; } catch (error) { LogError(error); throw error; // Re-throw to handle it in the caller } }, RunViews: async (params) => { // Runs multiple views and returns the results try { const results = await rv.RunViews(params); return results; } catch (error) { LogError(error); throw error; // Re-throw to handle it in the caller } } }; } }; RuntimeUtilities = __decorate([ RegisterClass(RuntimeUtilities, 'RuntimeUtilities') ], RuntimeUtilities); export { RuntimeUtilities }; /** * Factory function to create RuntimeUtilities * In a Node.js environment, this will use MJ's ClassFactory for runtime substitution * In a browser environment, it will use the base class directly */ export function createRuntimeUtilities() { // Check if we're in a Node.js environment with MJGlobal available if (typeof window === 'undefined') { try { // Use ClassFactory to get the registered class, defaulting to base RuntimeUtilities const obj = MJGlobal.Instance.ClassFactory.CreateInstance(RuntimeUtilities); if (!obj) { throw new Error('Failed to create RuntimeUtilities instance'); } // Ensure the object is an instance of RuntimeUtilities return obj; } catch (e) { // Fall through to default } } // Default: just use the base class return new RuntimeUtilities(); } //# sourceMappingURL=runtime-utilities.js.map