feat: add message handler middleware and fallback #1894
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds two things:
You could quite easily solve 2 (fallback message handler) with 1 (middleware), but it's meant to be a convenience method so you can easily handle incoming messages without a handler (i suspect for a lot of use case it should suffice). But the middleware gives that extra power to e.g. modify the incoming message.
It would be nice to also start adopting the middleware in more places, e.g. the message sender (to modify messages before they are sent).
Our use case for adding this at the moment is that we want to handle messages that have no message class supported. I haven't added any top-level APIs yet (only on the dependency manager) as it's good to fiddle around for a bit maybe first. One thing I think that might be worth considering is adding a top level method where you can register a handler for incoming messages. So you can 'extend' the framework without having to write custom classes. E.g. a didcommCustomMessaging module, where you can send and receive messages.
Then this work would be more the internal API to hook into the receiving / sending process but for most cases we can just expose it as an easy to use API.
The API can be used as follows at the moment:
And for the fallback handler
No breaking changes
Fixes #97
cc @Tommylans