@minima-global/mds
Version:
Official MDS Typescript Library for Minima. Used for creating minidapps that interact with the Minima Blockchain.
78 lines • 2.96 kB
JavaScript
import { httpPostAsync } from './mds.js';
/**
* Helper function to handle command arguments and return the command string, callback, and payload.
* @param command - The command to be executed.
* @param args - The arguments to be passed to the command.
* @returns An object containing the command string and callback.
*/
export function commandHandler(command, args) {
let commandString = command;
let callback;
let payload;
// Handle different argument patterns
if (args.length > 0) {
// If last argument is a function, it's a callback
if (typeof args[args.length - 1] === 'function') {
callback = args.pop();
if (args.length > 0) {
payload = args[0];
}
}
else {
// No callback, just parameters
payload = args[0];
}
}
// Build command string with parameters
if (payload) {
if (typeof payload === 'string') {
// Handle direct string parameters
commandString += ` ${payload}`;
}
else if (typeof payload === 'object') {
// Handle object parameters
if (payload.params) {
const payloadString = Object.entries(payload.params)
.filter(([_, value]) => value !== undefined)
.map(([key, value]) => {
// Stringify all objects (including arrays)
if (typeof value === 'object' && value !== null) {
return `${key}:${JSON.stringify(value)}`;
}
return `${key}:${value}`;
})
.join(' ');
commandString += payloadString ? ` ${payloadString}` : '';
}
else {
// Handle direct object parameters
const payloadString = Object.entries(payload)
.filter(([_, value]) => value !== undefined)
.map(([key, value]) => {
// Stringify all objects (including arrays)
if (typeof value === 'object' && value !== null) {
return `${key}:${JSON.stringify(value)}`;
}
return `${key}:${value}`;
})
.join(' ');
commandString += payloadString ? ` ${payloadString}` : '';
}
}
}
return { commandString, callback };
}
export function createCommandFunction(command) {
return (...args) => {
const { commandString, callback } = commandHandler(command, args);
return new Promise((resolve) => {
httpPostAsync('cmd', commandString, (data) => {
resolve(data);
if (callback && typeof callback === 'function') {
callback(data);
}
});
});
};
}
//# sourceMappingURL=helpers.js.map