Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfrtala committed Dec 20, 2019
1 parent 42d14c9 commit 7b7dc41
Show file tree
Hide file tree
Showing 22 changed files with 3,870 additions and 83 deletions.
104 changes: 98 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@

## Description

moleculer-ts is a tool which generate [moleculer](https://github.com/moleculerjs/moleculer) types for your sevices actions & events.
moleculer-ts is a tool which generates [moleculer](https://github.com/moleculerjs/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 - generateBrokerWatch
- Automatic regeneration of types on files change - `generateBrokerWatch`
- Using ts-patch & ts-transformer-enumerate - please follow installation instructions

## Installation

```
npm i moleculer moleculer-decorators moleculer-ts --save
Follow these steps to install `moleculer-ts`

```bash
# 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`

```json
{
"compilerOptions": {
"plugins": [
Expand All @@ -33,8 +40,93 @@ node_modules/.bin/ts-patch install
}
```

### Example
You should be good to go

### How to generate


#### *.service.types module structure

Define your service types interface

```typescript
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 dev.watch.ts script

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

```typescript
import { generateBrokerWatch } from 'moleculer-ts';

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

const watcher = await generateBrokerWatch({
serviceTypesPattern: `${brokerRootDir}/**/*.service.types.ts`,
outputDir: `${brokerRootDir}/types`,
});

watcher.on('change', filename => {
console.log(`Generate broker, file changed: `, filename);
});
})();
```

Run this script from `package.json`

```json
{
"scripts": {
"dev:watch": "ts-node src/dev.watch.ts",
}
}
```

### How to use

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

```typescript
import { Context } from 'moleculer';
import { UserServiceTypes } from 'path/to/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

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

Head to [examples](/examples#readme) to find out more
48 changes: 24 additions & 24 deletions examples/multiple-brokers/src/first.broker/types/services.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,50 @@ import { OwnActions as Service2Action0 } from '../services/user/user.service.typ
import { OwnEvents as Service2Event0 } from '../services/user/user.service.types';

declare module '../services/api/api.service.types' {
type Actions = [Service0Action0[0], Service0Action0[1]];
type Events = [];
type DerivedActions = [Service0Action0[0], Service0Action0[1]];
type DerivedEvents = [];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/api/v1.api.service.types' {
type Actions = [Service1Action0[0], Service1Action0[1]];
type Events = [];
type DerivedActions = [Service1Action0[0], Service1Action0[1]];
type DerivedEvents = [];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/user/user.service.types' {
type Actions = [Service2Action0[0], Service2Action0[1], Service2Action0[2]];
type Events = [Service2Event0[0]];
type DerivedActions = [Service2Action0[0], Service2Action0[1], Service2Action0[2]];
type DerivedEvents = [Service2Event0[0]];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

Expand Down
96 changes: 48 additions & 48 deletions examples/multiple-brokers/src/second.broker/types/services.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,98 +13,98 @@ import { OwnActions as Service5Action0 } from '../services/test/test.service.typ
import { OwnEvents as Service5Event0 } from '../services/test/test.service.types';

declare module '../services/internal/broker.service.types' {
type Actions = [];
type Events = [Service0Event0[0]];
type DerivedActions = [];
type DerivedEvents = [Service0Event0[0]];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/internal/circuit-breaker.service.types' {
type Actions = [];
type Events = [Service1Event0[0], Service1Event0[1], Service1Event0[2]];
type DerivedActions = [];
type DerivedEvents = [Service1Event0[0], Service1Event0[1], Service1Event0[2]];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/internal/node.service.types' {
type Actions = [];
type Events = [Service2Event0[0], Service2Event0[1], Service2Event0[2]];
type DerivedActions = [];
type DerivedEvents = [Service2Event0[0], Service2Event0[1], Service2Event0[2]];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/internal/services.service.types' {
type Actions = [];
type Events = [Service3Event0[0]];
type DerivedActions = [];
type DerivedEvents = [Service3Event0[0]];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/internal/transporter.service.types' {
type Actions = [];
type Events = [Service4Event0[0], Service4Event0[1]];
type DerivedActions = [];
type DerivedEvents = [Service4Event0[0], Service4Event0[1]];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

declare module '../services/test/test.service.types' {
type Actions = [];
type Events = [];
type DerivedActions = [];
type DerivedEvents = [];

type ActionParams<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetParamsStrict<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetParamsStrict<DerivedActions, T>;
type ActionReturn<
T extends MoleculerTs.GetNames<Actions>
> = MoleculerTs.GetReturn<Actions, T>;
T extends MoleculerTs.GetNames<DerivedActions>
> = MoleculerTs.GetReturn<DerivedActions, T>;
type EventParams<
T extends MoleculerTs.GetNames<Events>
> = MoleculerTs.GetParamsStrict<Events, T>;
T extends MoleculerTs.GetNames<DerivedEvents>
> = MoleculerTs.GetParamsStrict<DerivedEvents, T>;
type ServiceOwnActions = MoleculerTs.GetServiceOwnActions<OwnActions>;
}

Expand Down
14 changes: 14 additions & 0 deletions examples/single-broker/dev.watch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { generateBrokerWatch } from 'moleculer-ts';

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

const watcher = await generateBrokerWatch({
serviceTypesPattern: `${brokerRootDir}/**/*.service.types.ts`,
outputDir: `${brokerRootDir}/types`,
});

watcher.on('change', filename => {
console.log(`Generate broker, file changed: `, filename);
});
})();
Loading

0 comments on commit 7b7dc41

Please sign in to comment.