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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eilichev/moleculer-service-types

v0.0.3

Published

Type-safe Moleculer service definitions with automatic type extraction

Downloads

59

Readme

moleculer-service-types

Type-safe Moleculer service definitions with automatic type extraction. Single Source of Truth for service types - define once, use everywhere!

Installation

npm install @eilichev/moleculer-service-types

Usage

1. Define Services

import { createService, Context } from '@eilichev/moleculer-service-types';

// Define your types
interface UserId { user_id: string; }
interface UserType { id: string; email: string; name: string; }
interface ServiceResponse<T> { response: T | null; error: string | null; }

// Create a service with type-safe actions
export const UserService = createService('users', {
  getById: async (ctx: Context<UserId>): Promise<ServiceResponse<UserType>> => {
    // Your implementation
    return { response: null, error: null };
  },

  // With validator
  create: {
    validator: { email: 'email', name: 'string' },
    handler: async (ctx: Context<{ email: string; name: string }>): Promise<ServiceResponse<UserType>> => {
      return { response: null, error: null };
    },
  },
});

2. Create Service Registry

import { InferMethods } from '@eilichev/moleculer-service-types';
import { UserService } from './user.service';
import { ProductService } from './product.service';

export const services = {
  users: UserService,
  product: ProductService,
} as const;

// ServiceTypes is automatically generated!
export type ServiceTypes = {
  [K in keyof typeof services]: InferMethods<(typeof services)[K]>;
};

export function getAllServiceSchemas() {
  return Object.values(services).map((s) => s.schema);
}

3. Use BrokerWrapper

import { BrokerWrapper } from '@eilichev/moleculer-service-types';
import { ServiceTypes, getAllServiceSchemas } from './services';

const broker = new BrokerWrapper<ServiceTypes>({
  nodeID: 'my-service',
  transporter: 'NATS',
});

// Register all services
for (const schema of getAllServiceSchemas()) {
  broker.createService(schema);
}

await broker.start();

// Type-safe calls!
const result = await broker.service.users.getById({ user_id: '123' });
//                         ^-- autocomplete works here!

if (result.response) {
  console.log(result.response.email); // typed!
}

API

createService(name, actions, settings?)

Creates a type-safe service definition.

  • name: Service name (string)
  • actions: Object with action handlers
  • settings: Optional Moleculer service settings

Returns an object with:

  • name: Service name
  • actions: Typed actions
  • schema: Moleculer ServiceSchema for broker.createService()

InferMethods<T>

Type utility that extracts method signatures from a service.

type Methods = InferMethods<typeof UserService>;
// { getById: (params: UserId) => Promise<ServiceResponse<UserType>>; ... }

BrokerWrapper<T>

Extended Moleculer ServiceBroker with type-safe service proxy.

const broker = new BrokerWrapper<ServiceTypes>(options);
broker.service.users.getById({ user_id: '123' }); // fully typed!

Context<T>

Re-exported from Moleculer for convenience.

Benefits

  • Single Source of Truth: Define types once in service handlers
  • Auto-generated Types: ServiceTypes is derived from actual code
  • Full IDE Support: Autocomplete, hover types, go-to-definition
  • No Manual Sync: No separate type files to maintain
  • Type-safe Calls: Compile-time errors for wrong parameters

License

MIT