Skip to content
Definitive TypeScript support for Moleculer service actions interface
TypeScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
src
.DS_Store beta to master Dec 9, 2019
.gitignore beta to master Dec 9, 2019
.npmignore
.prettierrc first commit Nov 4, 2019
README.md update README.md & examples package.json for version 2.0.4 Jan 18, 2020
copy-templates.ts
package-lock.json 2.0.5 Jan 18, 2020
package.json
tsconfig.json

README.md

Moleculer logo

npm npm GitHub issues Powered by moleculer

Moleculer TypeScript generator for services types

Description

moleculer-ts is a tool which generates moleculer types for your sevices actions & events.

Features

  • Generate types for call, emit, broadcast, broadcastLocal functions
  • Customizable Broker & Service interface
  • Automatic regeneration of types on files change
  • Using ts-patch & ts-transformer-enumerate - please follow installation instructions

Installation

Follow these steps to install moleculer-ts

# install moleculer and moleculer-ts
npm i moleculer moleculer-ts --save

# install typescript and few other tools
npm i typescript ts-patch ts-transformer-enumerate prettier @types/node -D

# localy patch typescript in order to work properly
node_modules/.bin/ts-patch install

Add to your tsconfig.json

Add ts-transformer-enumerate plugin to your compilerOptions in your tsconfig

{
  "compilerOptions": {
    "plugins": [{ "transform": "ts-transformer-enumerate/transformer" }]
  }
}

You should be good to go

How to generate

*.service.types module structure

Define your service types interface

import { Action, Event, ConcatMultiple } from 'moleculer-ts';

// required to specify your service
export const name: 'serviceName' = 'serviceName';

// export list of own service actions
export type OwnActions = [];

// export list of own service events
export type OwnEvents = [];

// concat service's own actions/events with mixins inherited types
export type Actions = ConcatMultiple<[OwnActions]>;
export type Events = ConcatMultiple<[OwnEvents]>;

Write your gen.broker.types.ts script

Write your generator module. Use generateBroker to scan types and prints it in outputDir

import { generateBroker } from 'moleculer-ts';

(async () => {
  const brokerRootDir = `${process.cwd()}/src`;

  await generateBroker({
    serviceTypesPattern: `${brokerRootDir}/**/*.service.types.ts`,
    outputDir: `${brokerRootDir}/types`,
  });
})();

Run this script from package.json

{
  "scripts": {
    "gen:broker:types": "ts-node src/gen.broker.types.ts"
  }
}

How to use

import your generated types and use them as input/output definition in service actions

import { Context } from 'moleculer';
import { UserServiceTypes } from '{brokerRootDir}/types';

export default {
  name: UserServiceTypes.name,
  actions: {
    async get(
      ctx: Context<UserServiceTypes.ActionParams<'get'>>,
    ): Promise<UserServiceTypes.ActionReturn<'get'>> {
      // fully typed params
      const { params } = ctx;

      // Return matching output
      return {
        id: 'a',
        email: 'a',
        name: 'a',
      };
    },
  },
};

Advanced usage

Moleculer TypeScript

Want to see more advanced usage? You can enable realtime typescript checking in your IDE

Head to examples to find out more

You can’t perform that action at this time.