Introduction #
Creating and integrating a custom Xpell-Node XModule involves a few essential steps.
This guide will walk you through the process to ensure your custom module works seamlessly within the Xpell environment. By the end of this guide, you will have a working example of a custom module. Let’s get started by extending the XModule base class and implementing your custom logic.
Finally, we will register the module with a unique name in the environment.
Extending the XModule Base Class #
The first step in creating your custom module is to extend the XModule base class.
This is crucial as it allows your module to inherit core functionalities necessary for integration with the Xpell environment. Below is a TypeScript example showing how to extend the base class. This example includes the constructor and metadata required for initialization.
Make sure to import the necessary classes from ‘xpell-node’.
Implementing Custom Logic #
After extending the base class, the next step is to implement your custom logic. In this example, we will create two functions: _info and _add.
The _info function will return a simple greeting message, while the _add function will add two numbers provided as parameters.
These functions are prefixed with an underscore to indicate they are exposed to the Xpell runtime. Implementing these functions will demonstrate how to process inputs and return results.
Exposing Functions to the Xpell Interpreter #
For the Xpell Interpreter to access your functions, they need to be exposed correctly.
Only methods prefixed with an underscore are accessible. The xcmd parameter in these functions provides the context for the command, including parameters and metadata.
This allows you to handle input data properly and ensure your functions execute within the given context.
Registering the Module #
The final step is to register your module in the Xpell environment with a unique name.
This involves creating an instance of your class and passing the required metadata. The metadata should include at least the _name field, which uniquely identifies the module. In the provided example, we register the module with the name ‘test-module’.
This step ensures your module is recognized and can be utilized within the Xpell environment.
Example #
import { Xpell,XCommand, XModule, XModuleData } from "xpell-node"
export class _TestModuleClass extends XModule {
constructor(data:XModuleData){
super(data)
}
// only functions that start with _ are exposed to the Xpell
async _info(xcmd:XCommand){
return "Hello from Test Module"
}
/**
* Add two numbers
* @param xcmd params should have x and y
* @returns sum of x and y`
*/
async _add(xcmd:XCommand){
return <any>xcmd._params.x + xcmd._params.y;
}
}
//The module is a Singleton object
export const TestModule = new _TestModuleClass({_name:"test-module"})
//Register the Module
Xpell.loadModule(TestModule);
//Call The module ops:
const xcommand = {
_module: "test-module",
_op: "info"
}
const res = await Xpell.execute(xcommand);
console.log( res)
#
Sample Project #
We invite you to visit our GitHub repository at https://github.com/AimeVerse/xpell-node-module.
We have created a guide on how to create and integrate a custom Xpell-Node XModule, along with a working example.
This is a fantastic opportunity for you to showcase your coding skills and contribute to the project.
Feel free to explore the code, write your own XModule, and share your improvements with the community.
We look forward to seeing your creativity and contributions. Happy coding! 💻✨