DataWeave Script Sharing Made Easy With Anypoint Exchange

Patryk Bandurski
Another Integration Blog
3 min readSep 23, 2022

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.

DataWeave Library asset published on Anypoint Exchange — Home page

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.

DataWeave function documentation details on Anypoint Exchange

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
Default documentation generated for DataWeave function by DataWeave plugin

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:

DataWeave unit test

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:

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.

--

--