transform input
In makeModule, there is a transformInput option that allows you to automatically transform or augment the module’s dependencies (inputs) before they reach the module’s main function. This is useful if you need to: • Add extra information (e.g., the module’s name). • Modify input fields. • Validate or check incoming data before processing.
How It Works
Logging Input Data with the Module Name
If your implementation (or a custom signature) supports a second argument for the module name in transformInput, you can log both the incoming data and the name of the module:
• The input is augmented by the field { Module: { name: 'A' } }
.
• Inside the module function, you can retrieve the module’s name, which is useful for logging, debugging, or dynamic logic that depends on the module name.
Dependency Validation
If the user field is missing or the user object lacks an id, an error is thrown before the module’s main logic even runs.
Transforming Input with Higher-Kinded Types (HKT)
Higher-Kinded Types (HKT) in TypeScript allow you to introduce a “placeholder” type that can later be instantiated with specific logic—something standard infer can’t do, as infer only extracts types when matching a known shape. Since TypeScript doesn’t provide true HKTs out of the box, we use a hack to emulate them, much like passing a class into a function and creating an instance of it inside. By doing so, you can dynamically reshape your module’s dependencies and still maintain strict compile-time safety.
Example: Converting an Object to an Array
Suppose you want to pass an object to the module but work with an array of values inside the module:
Here, the incoming object obj: Record<string, number>
is automatically converted to a flattened array of key/value pairs, giving you more convenient handling of data in the ExampleModule
.