UNPKG

@cheqd/sdk

Version:

A TypeScript SDK built with CosmJS to interact with the cheqd network ledger

101 lines 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractCheqdSDKModule = void 0; exports.instantiateCheqdSDKModule = instantiateCheqdSDKModule; exports.instantiateCheqdSDKModuleRegistryTypes = instantiateCheqdSDKModuleRegistryTypes; exports.instantiateCheqdSDKModuleQuerierExtensionSetup = instantiateCheqdSDKModuleQuerierExtensionSetup; exports.applyMixins = applyMixins; /** * Abstract base class for all Cheqd SDK modules. * Provides common functionality for module initialization, registry types, * and querier extensions. All SDK modules must extend this class. */ class AbstractCheqdSDKModule { /** Signing client instance for transaction operations */ _signer; /** Map of methods exposed by this module */ methods = {}; /** Querier instance for blockchain data retrieval */ querier; /** List of method names protected from external access */ _protectedMethods = ['constructor', 'getRegistryTypes', 'getQuerierExtensionSetup']; /** Static registry types for message encoding/decoding */ static registryTypes = []; /** Static querier extension setup function */ static querierExtensionSetup = (base) => ({}); /** * Constructs a new AbstractCheqdSDKModule instance. * * @param signer - Signing client for blockchain transactions * @param querier - Querier client for blockchain data retrieval * @throws Error if signer or querier is not provided */ constructor(signer, querier) { if (!signer) { throw new Error('signer is required'); } if (!querier) { throw new Error('querier is required'); } this._signer = signer; this.querier = querier; } } exports.AbstractCheqdSDKModule = AbstractCheqdSDKModule; /** * Factory function for instantiating Cheqd SDK modules. * Creates a new instance of the specified module class with the provided arguments. * * @template T - Type of the module constructor * @param module - Module constructor class * @param args - Constructor arguments for the module * @returns New instance of the module */ function instantiateCheqdSDKModule(module, ...args) { return new module(...args); } /** * Extracts registry types from a module for message encoding/decoding. * Safely retrieves the registryTypes static property with fallback to empty array. * * @param module - Module instance or class to extract registry types from * @returns Iterable of [typeUrl, GeneratedType] pairs */ function instantiateCheqdSDKModuleRegistryTypes(module) { return module.registryTypes ?? []; } /** * Extracts querier extension setup from a module. * Safely retrieves the querierExtensionSetup static property with fallback to empty object. * * @param module - Module instance or class to extract querier extension setup from * @returns Query extension setup function */ function instantiateCheqdSDKModuleQuerierExtensionSetup(module) { return module.querierExtensionSetup ?? {}; } /** * Applies mixin pattern to combine methods from multiple module constructors. * Extracts public methods from module prototypes while respecting protected method lists. * This enables composition of functionality from multiple modules into a single interface. * * @param derivedCtor - Target constructor to apply mixins to * @param constructors - Array of constructor functions to extract methods from * @returns Map of method names to their implementations */ function applyMixins(derivedCtor, constructors) { let methods = {}; constructors.forEach((baseCtor) => { Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => { const property = baseCtor.prototype[name]; if (typeof property !== 'function' || derivedCtor.hasOwnProperty(name) || derivedCtor?.protectedMethods.includes(name) || baseCtor.prototype?._protectedMethods?.includes(name)) return; methods = { ...methods, [name]: property }; }); }); return methods; } //# sourceMappingURL=_.js.map