Modules (m)
The Module Artifact (m)
Filetype:{filename}.m.lua
A module is a lightweight way to share reusable code across your TENUM® application. You can require
a module in other artifacts such as UI elements, entities and message handlers to avoid duplicating logic in multiple places. Modules are not directly accessible via HTTP and never execute in the browser unless a UI artifact explicitly imports them.
Usage Sample
Modules are a way to share common functionality in your frontend code and backend code.
Let's say you want to validate an email. You probably want to validate an email in your frontend so that clients don't send requests to your backend unnecessarily. But you also want to validate your email on the backend, since you can't really trust that the clients will send you valid data.
This is a use case for a module Artefact. You can create a validateEmail function, and then you can import this module script both in you .uie Artefact and .mh Artefact, and use the same code to validate the email in both places.
Sample App: TENUM ToDo App
Sample Module: Util.m.lua
Best Practices
Keep Modules Focused and Reusable
Design each module to handle a single, well-defined responsibility (e.g., string manipulation, validation) to promote reuse across your application.
Use Clear and Consistent APIs
Export well-named functions with clear, consistent input and output structures to make your modules easy to use and integrate.
Avoid Side Effects
Ensure your module functions are pure wherever possible, meaning they do not modify external state and return consistent outputs for the same inputs.
Document Your Functions
Provide inline documentation for each function to explain its purpose, parameters, and return values, making it easy for other developers to understand.
Share Logic Across Artifacts
Use modules to share common logic between UI elements, entities, and message handlers, reducing duplication and ensuring consistency.
Version and Maintain
Regularly review and version your modules as your project evolves, ensuring backward compatibility and clear version management within your
package.json
.