Giter Site home page Giter Site logo

moleculer-service-decorators's Introduction

Moleculer logo

Moleculer Service Decorators

Powered by moleculer npm

ES7/Typescript Decorators for Moleculer

Example

import { Service } from "moleculer";
import {
    action,
    event,
    param,
    service,
    string
} from "moleculer-service-decorators";

@service()
class Example extends Service {
    @action()
    public help(@param({ type: "string" }) text: string,
                @param({ type: "number", optional: true }) page: number) {}
    @action()
    public test(@string({ optional: true }) test: string) {}

    @event()
    public "test.started"(payload: any, sender: string, eventName: string) {}

    @event({ name: "test.ended", group: "test" })
    public testEnded(payload: any, sender: string, eventName: string) {}
}

Service

The Service class must be used as the base of any decorated service. Most options can be added either by defining them in the class itself or by adding them to the decorator options.

@service()
class Example extends Service {
    version = 1;
    settings = {};
    metadata = {
        test: "This is a test"
    };
    mixins = [];
}

class Base extends Service {}

@service({
    name: "Tester",
    version: 1,
    settings: {},
    metadata: {
        test: "This is a test"
    },
    mixins: []
})
class Example2 extends Base {
}

Parameters

Param decorators for Fastest Validator are provided and creating custom param decorators is easy.

This example assumes using the Joi Validator example

import * as Joi from "joi";
import { Service } from "moleculer";

import {
    action,
    param,
    service
} from "moleculer-service-decorators";

function joiString() {
    return param(Joi.string().max(255).required());
}

@service()
class Example extends Service {
    @action()
    public help(@joiString() text: string) {}
}

Actions

Actions can have options set in the same format as the ServiceSchema. The handler can be defined with the parameters of the context or you can set the parameters in the action options and use a Context as the only parameter to the handler.

@service()
class Example extends Service {
    @action({
        name: "getHelp",
        cache: true,
        metrics: { params: false, meta: true }
    })
    public help(@string() text: string) {}
}

@service()
class Example2 extends Service {
    @action({
        name: "getHelp",
        cache: true,
        metrics: { params: false, meta: true },
        params: {
            text: "string"
        }
    })
    public help(ctx: Context) {}
}

Events

Event handlers are added easily with options available to make it more flexible.

@service()
class Example extends Service {
    @event()
    public "test.started"(payload: any, sender: string, eventName: string) {}

    @event({ name: "test.ended", group: "test" })
    public testEnded(payload: any, sender: string, eventName: string) {}
}

License

Moleculer Service Decorators is available under the MIT license.

moleculer-service-decorators's People

Contributors

rmccallum81 avatar dependabot-preview[bot] avatar anthrax63 avatar ryanm-pm avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.