UNPKG

jsm-core

Version:
142 lines (141 loc) 5.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRegistry = getRegistry; const jsm_sdk_1 = require("jsm-sdk"); const node_process_1 = require("node:process"); const config_1 = __importDefault(require("../app/config")); const object_utilities_1 = require("../app/utilities/object.utilities"); class Registry { constructor() { this.context = new Map(); this.set('cwd', (0, node_process_1.cwd)()); this.set('config', config_1.default); this.setSdkInitiator(() => { const sdk = (0, jsm_sdk_1.initSdk)('backend', { baseURL: config_1.default.http.sdk.baseURL, appId: config_1.default.http.sdk.appId, appSecret: config_1.default.http.sdk.appSecret, debug: config_1.default.http.sdk.debug, headersInjector: () => ({ Authorization: 'Bearer example-token', 'X-App-Id': 'example-app-id', 'X-Service-Name': 'example-service', 'X-Service-Secret': 'example service secret' }) }); return (0, jsm_sdk_1.extendSdk)(sdk, { // TODO: Add your service containers here }); }); } // Register a service or function in the registry set(key, value) { this.context.set(key, value); return this; } // Retrieve a service or function from the registry get(key) { return this.context.get(key); } // Check if a service exists in the registry has(key) { return this.context.has(key); } get cwd() { return this.get('cwd') || process.cwd(); } // Get the SDK object from the registry get sdk() { if (!this._sdk) { this._sdk = this._sdk_initiator(); } return this._sdk; } // Set the SDK Initiator function in the registry setSdkInitiator(initiator) { this._sdk_initiator = initiator; return this; } // Get the SDK object from the registry get cache() { if (!this._cache) { this._cache = this._cache_initiator(); } return this._cache; } setCache(cache) { this._cache = cache; } // Set the SDK Initiator function in the registry setCacheInitiator(initiator) { this._cache_initiator = initiator; return this; } config(_config) { const flattened = (0, object_utilities_1.flattenConfig)(_config); for (const key in flattened) { this.setConfig(key, flattened[key]); } return this; } /** * * @param path wether to get the whole config object or a specific path * @returns {object} the config object or a specific path */ getConfig(path, defaultValue) { const configObject = this.get('config') || config_1.default; if (path !== true) { let result = configObject; const pathArray = path.split('.'); for (let i = 0; i < pathArray.length; i++) { if (result[pathArray[i]]) { result = result[pathArray[i]]; } else { return defaultValue; } } return result; } return configObject; } setConfig(path, value) { try { const pathArray = path.split('.'); // Split the path into components. const _config = Object.assign({}, (this.get('config') || config_1.default)); // Get the current config or fallback to the global config. let current = _config; // Start at the root of the config object. for (let i = 0; i < pathArray.length; i++) { if (i === pathArray.length - 1) { // Check if the final property can accept the new value. if (current[pathArray[i]] == null || typeof current[pathArray[i]] === typeof value) { current[pathArray[i]] = value; // Set the value on the property. } else { throw new Error(`Cannot set value of type ${typeof value} to ${path}`); } } else { // Traverse deeper into the object. if (current[pathArray[i]] == null) { current[pathArray[i]] = {}; // Create intermediate objects if they don't exist. } current = current[pathArray[i]]; } } this.set('config', _config); // Assuming `set` updates the configuration after modification. return this; } catch (error) { console.error("Error setting config value:", error); // Log the error for debugging. return this; } } } const registry = new Registry(); function getRegistry() { return registry; }