moleculer-ts
is a tool which generates moleculer types for your sevices actions & events.
- 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
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 ts-transformer-enumerate
plugin to your compilerOptions
in your tsconfig
{
"compilerOptions": {
"plugins": [{ "transform": "ts-transformer-enumerate/transformer" }]
}
}
You should be good to go
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 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"
}
}
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',
};
},
},
};
Want to see more advanced usage? You can enable realtime typescript checking in your IDE
Head to examples to find out more