@eilichev/moleculer-service-types
v0.0.3
Published
Type-safe Moleculer service definitions with automatic type extraction
Downloads
59
Maintainers
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-typesUsage
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 handlerssettings: Optional Moleculer service settings
Returns an object with:
name: Service nameactions: Typed actionsschema: Moleculer ServiceSchema forbroker.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:
ServiceTypesis 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
