Replies: 1 comment 2 replies
-
Good idea, I was thinking about a similar solution for version 1.0 of Moleculer where there is a global interface to store the action definitions with augmenting. But in my mind, I thought just you should extend it in each services, and just get the param types for Please create a PR with your idea but for the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I found a way to create a decentralized global 'index' of all service actions so that TypeScript provides you with a list of all actions, their parameters, and their return types (thanks to @Telokis for the inspiration).
Implementation Idea
The idea is to make the moleculer namespace global and in each service definition file to add the service to the global namespace (using interface merging). With that, the required changes are quite small and I can provide a codemod for applying them.
In the following example of a service definition file, you can see how
typeof testService1
is merged into theAllServices
interface.testService1
needs to be typed usingsatisfies ServiceSchema
.That's the only modification necessary.
With type-mapping, you can convert the fastest-validator
params
schema objects into typescript types.Also using type-mapping, you can create a type from
AllServices
that contains all actions in the format{ [<serviceVersion>.<serviceName>.<actionName>]: ActionSchema | ActionHandler }
.From there, you can construct a
ctx.call
signature that provides all actions and knows their parameter and return types. Adding parameter doc strings is possible, too.To enable parameter type inference of
ctx.params
in an actionhandler
ifself, you need to wrap the action in adefineAction
function that does nothing but to inform typescript thatctx.params
should be inferred from theparams
schema. (If you have advanced TypeScript experience, I'm running into issues here, and I'd appreciate your help.)For our project, ActivityPods, we are going to use this feature since it is a big pain to find service actions, their return types, and their required parameters.
I would like to share this work with the community and wonder how to do so. Should I create a separate npm library (perhaps the most straight forward option?) or make a PR to this repository?
Best,
Laurin
Cross-linking:
Beta Was this translation helpful? Give feedback.
All reactions