DataWeave Script Sharing Made Easy With Anypoint Exchange
Anypoint Exchange is a central part of the Anypoint platform. This is where you look for assets in your organization, such as APIs, async API specifications, API fragments, and custom connectors. Now, you can also create DataWeave library assets and publish them to Anypoint Exchange.
DataWeave Library on Anypoint Exchange
I have previously described how you can encapsulate DataWeave functions within modules. This approach introduced reusability for a single API; however, with the DataWeave library, we can extend reusability to all APIs. Below is a sample health library asset published on Anypoint Exchange. The home page is generated automatically based on the content of the library. As shown below, there are two modules we can use: ErrorHandling and Health. These links will forward you to more detailed documentation on specific modules.
The DataWeave plugin generates user-friendly documentation that we can fill in. Below, you see documentation on the Exchange of the ‘ready’ function. You have a description, input parameters with types, and a detailed description. Moreover, you can find examples with DataWeave code and sample output. This will vary based on what you plan to provide; however, I advise providing all available details to make the function as easy to use as possible.
As you can see, this documentation is self-explanatory and makes the asset easy to use.
DataWeave Plugin
You can create a library using a DataWeave plugin in Visual Studio Code.
Documentation
This plugin generates a template for documentation. You can just fill in the details that are necessary.
As shown below, the template contains:
- Brief one-line description
- Example section with description, DataWeave code in source section, and example output of the execution
You should also include a table with input parameters and provide a name, data type, and brief description for each parameter.
Unit Tests
Your code should be high quality if you decide to share it. This can be tested and enforced by introducing unit tests, giving the developer confidence in the code. The tests are also important to quickly detect errors if you introduce changes into your code.
The DataWeave plugin provides a great tool to test your code. You can do this by writing DataWeave code to make unit tests, using the DataWeave Assertion Library. Here is an example:
As you can see, we are executing only one unit test to check if my ‘alive’ function returns the expected response. If you are used to writing MUnit assertions, you will find the ‘equalTo’ function familiar as it is the same function.
Walkthrough
You can find thorough walkthroughs on the MuleSoft Developer Tutorial page:
- Getting Started With the DataWeave Extension for Visual Studio Code: explains how to install the plugin and create your first DataWeave library project
- Getting Started With DataWeave Libraries in Anypoint Exchange: explains how to publish assets to Anypoint Exchange
Summary
DataWeave Library assets published on Anypoint Exchange can be a valuable source to share reusable DataWeave code. Using the DataWeave plugin, we can easily generate documentation, making our asset very developer-friendly. We can also generate unit tests, allowing us to produce high-quality DataWeave module code.
I recommend using this amazing feature and publishling new assets to Anypoint Exchange. Make your code available for others to use.