npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anthrax63/moleculer-service-decorators

v1.0.9

Published

Typescript decorators for Moleculer

Downloads

232

Readme

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.