Mapping Function Params to Context #995
Replies: 1 comment 1 reply
-
The following is an extremely simple example of what I believe you have described. import { GenericObject, Service, ServiceSchema } from 'moleculer'
export type RegisterableFunction<P = GenericObject, M = GenericObject> = (request: P, metaData: M) => any
const innerService: ServiceSchema = {
name: 'inner',
actions: {}
}
export default function registerFunction(fn: RegisterableFunction) {
if(!innerService.actions) innerService.actions = {};
innerService.actions[fn.name] = {
name: fn.name,
handler: (ctx) => fn(ctx.params, ctx.meta)
}
return fn;
} There is, of course, much that could be done here but this is basically the answer to how you would map the Moleculer Context to your example signature with a little context. That said, I'm not sure what benefit this provides. Perhaps you want to provide a sort of "Function as a Service" API so that you can wrap actions with custom functionality and limit the developers need to learn and implement the Moleculer framework? Not necessarily the approach I would take in that case given the simplicity of the framework and the ability to easily distribute mixins for repeatability, and middleware for highly customizable internal functionality. However, if you were building sort of custom, open source, version of AWS Lambda (or similar CSP type FaaS) where one would declare these functions using a web-API of sorts, I could see the allure. In any event, I hope this helps! Happy coding! |
Beta Was this translation helpful? Give feedback.
-
Hello,
I am trying to create a wrap-around package for Moleculer that allows for other devs to use Moleculer without having to worry about the Moleculer service schema.
I.E Developer external Project -> installs my package. Dev project communicates with my package that will form and handle all Moleculer functionalities.
My package would accept an action structure that is a function similar to:
My package would accepts this function and map it to a service as the action for such service. So if the service action is called it would call and execute the helloWorld function.
I was wondering how would I be able to map the
(request: unknown, metaData: unknown, service?: unknown)
to the context for Moleculer ?(Hopefully I am explain this properly)
Beta Was this translation helpful? Give feedback.
All reactions