Replies: 2 comments 4 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.
-
|
FYI: I got green light from NLNet :) As a first step, I created a draft PR in the fastest-validator repo icebob/fastest-validator#355 for inferring TS types from schemas. |
Beta Was this translation helpful? Give feedback.
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 testService1is merged into theAllServicesinterface.testService1needs to be typed usingsatisfies ServiceSchema.That's the only modification necessary.
With type-mapping, you can convert the fastest-validator
paramsschema objects into typescript types.Also using type-mapping, you can create a type from
AllServicesthat contains all actions in the format{ [<serviceVersion>.<serviceName>.<actionName>]: ActionSchema | ActionHandler }.From there, you can construct a
ctx.callsignature 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.paramsin an actionhandlerifself, you need to wrap the action in adefineActionfunction that does nothing but to inform typescript thatctx.paramsshould be inferred from theparamsschema. (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