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

Commit

Permalink
update examples for upcomming version
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-sabo committed Jan 18, 2020
1 parent 0d06951 commit 8a47c5a
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/empty-broker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"dev:watch": "ts-node src/bin/dev.watch.ts",
"dev:broker:gen": "ts-node src/bin/dev.broker.gen.ts",
"gen:broker:types": "ts-node src/bin/gen.broker.types.ts",
"postinstall": "ts-patch install"
},
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion examples/empty-broker/src/bin/dev.watch.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./dev.broker.gen');
import './gen.broker.types';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateBroker } from 'moleculer-ts';
import { generateBroker, GenerateBrokerOptions } from 'moleculer-ts';
import watch from 'glob-watcher';

const brokers = ['empty-broker'];
Expand All @@ -9,7 +9,7 @@ export function isWatchMode() {

brokers.forEach(async broker => {
let brokerRootDir = `${process.cwd()}/src/${broker}`;
const generateBrokerOptions = {
const generateBrokerOptions: GenerateBrokerOptions = {
serviceTypesPattern: `${brokerRootDir}/**/*.service.types.ts`,
outputDir: `${brokerRootDir}/types`,
};
Expand Down
6 changes: 3 additions & 3 deletions examples/empty-broker/src/empty-broker/types/broker.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export interface ServiceBroker {
broadcastLocal: ServiceBroker['emit'];
}

type GetCallParams = {};
export type GetCallParams = {};

type GetCallReturn = {};
export type GetCallReturn = {};

type GetEmitParams = {};
export type GetEmitParams = {};

export type ServiceNames = Exclude<never, never>;
export type ServiceEventNames = Exclude<never, never>;
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple-brokers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"dev:watch": "ts-node src/bin/dev.watch.ts",
"dev:broker:gen": "ts-node src/bin/dev.broker.gen.ts",
"gen:broker:types": "ts-node src/bin/gen.broker.types.ts",
"postinstall": "ts-patch install && patch-package"
},
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple-brokers/src/bin/dev.watch.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./dev.broker.gen');
import './gen.broker.types';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateBroker } from 'moleculer-ts';
import { generateBroker, GenerateBrokerOptions } from 'moleculer-ts';
import watch from 'glob-watcher';

const brokers = ['first.broker', 'second.broker'];
Expand All @@ -9,11 +9,13 @@ export function isWatchMode() {

brokers.forEach(async broker => {
let brokerRootDir = `${process.cwd()}/src/${broker}`;
const generateBrokerOptions = {
const generateBrokerOptions: GenerateBrokerOptions = {
serviceTypesPattern: `${brokerRootDir}/**/*.service.types.ts`,
outputDir: `${brokerRootDir}/types`,
generateParamsSchema: true,
generateParamsAssert: true,
generateActionsParamsAssert: true,
generateActionsParamsSchema: true,
generateEventsParamsAssert: true,
generateEventsParamsSchema: true,
};

if (isWatchMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ServiceBroker {
broadcastLocal: ServiceBroker['emit'];
}

type GetCallParams = {
export type GetCallParams = {
'api.request': Services.ApiServiceTypes.Actions[0]['in'];
'api.context': Services.ApiServiceTypes.Actions[1]['in'];
'v1.api.request': Services.V1ApiServiceTypes.Actions[0]['in'];
Expand All @@ -29,7 +29,7 @@ type GetCallParams = {
'user.delete': Services.UserServiceTypes.Actions[2]['in'];
};

type GetCallReturn = {
export type GetCallReturn = {
'api.request': Services.ApiServiceTypes.Actions[0]['out'];
'api.context': Services.ApiServiceTypes.Actions[1]['out'];
'v1.api.request': Services.V1ApiServiceTypes.Actions[0]['out'];
Expand All @@ -39,7 +39,7 @@ type GetCallReturn = {
'user.delete': Services.UserServiceTypes.Actions[2]['out'];
};

type GetEmitParams = {
export type GetEmitParams = {
'user.nodeChange': MoleculerTs.Union.Strict<
Services.UserServiceTypes.Events[0]['in']
>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Services from './services.types';
import { createAssertEquals, createAssertType } from 'typescript-is';

export default {
'user.nodeChange': {
equals: createAssertEquals<
Services.UserServiceTypes.EventParams<'nodeChange'>
>(),
type: createAssertType<
Services.UserServiceTypes.EventParams<'nodeChange'>
>(),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Services from './services.types';
import { schema } from 'ts-transformer-json-schema';
type Trim<T> = { [K in keyof T]: any };

export default {
'user.nodeChange': schema<
Trim<Services.UserServiceTypes.EventParams<'nodeChange'>>
>(),
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export interface ServiceBroker {
broadcastLocal: ServiceBroker['emit'];
}

type GetCallParams = {};
export type GetCallParams = {};

type GetCallReturn = {};
export type GetCallReturn = {};

type GetEmitParams = {
export type GetEmitParams = {
'$broker.started': MoleculerTs.Union.Strict<
Services.BrokerServiceTypes.Events[0]['in']
>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import * as Services from './services.types';
import { createAssertEquals, createAssertType } from 'typescript-is';

export default {
'$broker.started': {
equals: createAssertEquals<
Services.BrokerServiceTypes.EventParams<'started'>
>(),
type: createAssertType<
Services.BrokerServiceTypes.EventParams<'started'>
>(),
},
'$circuit-breaker.opened': {
equals: createAssertEquals<
Services.CircuitBreakerServiceTypes.EventParams<'opened'>
>(),
type: createAssertType<
Services.CircuitBreakerServiceTypes.EventParams<'opened'>
>(),
},
'$circuit-breaker.half-opened': {
equals: createAssertEquals<
Services.CircuitBreakerServiceTypes.EventParams<'half-opened'>
>(),
type: createAssertType<
Services.CircuitBreakerServiceTypes.EventParams<'half-opened'>
>(),
},
'$circuit-breaker.closed': {
equals: createAssertEquals<
Services.CircuitBreakerServiceTypes.EventParams<'closed'>
>(),
type: createAssertType<
Services.CircuitBreakerServiceTypes.EventParams<'closed'>
>(),
},
'$node.connected': {
equals: createAssertEquals<
Services.NodeServiceTypes.EventParams<'connected'>
>(),
type: createAssertType<
Services.NodeServiceTypes.EventParams<'connected'>
>(),
},
'$node.updated': {
equals: createAssertEquals<
Services.NodeServiceTypes.EventParams<'updated'>
>(),
type: createAssertType<Services.NodeServiceTypes.EventParams<'updated'>>(),
},
'$node.disconnected': {
equals: createAssertEquals<
Services.NodeServiceTypes.EventParams<'disconnected'>
>(),
type: createAssertType<
Services.NodeServiceTypes.EventParams<'disconnected'>
>(),
},
'$services.changed': {
equals: createAssertEquals<
Services.ServicesServiceTypes.EventParams<'changed'>
>(),
type: createAssertType<
Services.ServicesServiceTypes.EventParams<'changed'>
>(),
},
'$transporter.connected': {
equals: createAssertEquals<
Services.TransporterServiceTypes.EventParams<'connected'>
>(),
type: createAssertType<
Services.TransporterServiceTypes.EventParams<'connected'>
>(),
},
'$transporter.disconnected': {
equals: createAssertEquals<
Services.TransporterServiceTypes.EventParams<'disconnected'>
>(),
type: createAssertType<
Services.TransporterServiceTypes.EventParams<'disconnected'>
>(),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as Services from './services.types';
import { schema } from 'ts-transformer-json-schema';
type Trim<T> = { [K in keyof T]: any };

export default {
'$broker.started': schema<
Trim<Services.BrokerServiceTypes.EventParams<'started'>>
>(),
'$circuit-breaker.opened': schema<
Trim<Services.CircuitBreakerServiceTypes.EventParams<'opened'>>
>(),
'$circuit-breaker.half-opened': schema<
Trim<Services.CircuitBreakerServiceTypes.EventParams<'half-opened'>>
>(),
'$circuit-breaker.closed': schema<
Trim<Services.CircuitBreakerServiceTypes.EventParams<'closed'>>
>(),
'$node.connected': schema<
Trim<Services.NodeServiceTypes.EventParams<'connected'>>
>(),
'$node.updated': schema<
Trim<Services.NodeServiceTypes.EventParams<'updated'>>
>(),
'$node.disconnected': schema<
Trim<Services.NodeServiceTypes.EventParams<'disconnected'>>
>(),
'$services.changed': schema<
Trim<Services.ServicesServiceTypes.EventParams<'changed'>>
>(),
'$transporter.connected': schema<
Trim<Services.TransporterServiceTypes.EventParams<'connected'>>
>(),
'$transporter.disconnected': schema<
Trim<Services.TransporterServiceTypes.EventParams<'disconnected'>>
>(),
};
2 changes: 1 addition & 1 deletion examples/single-broker/bin/dev.watch.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./dev.broker.gen');
import './gen.broker.types';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateBroker } from 'moleculer-ts';
import { generateBroker, GenerateBrokerOptions } from 'moleculer-ts';
import watch from 'glob-watcher';

const brokers = ['single-broker'];
Expand All @@ -9,7 +9,7 @@ export function isWatchMode() {

brokers.forEach(async broker => {
let brokerRootDir = `${process.cwd()}/src`;
const generateBrokerOptions = {
const generateBrokerOptions: GenerateBrokerOptions = {
serviceTypesPattern: `${brokerRootDir}/**/*.service.types.ts`,
outputDir: `${brokerRootDir}/types`,
};
Expand Down
2 changes: 1 addition & 1 deletion examples/single-broker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"dev:watch": "ts-node bin/dev.watch.ts --watch",
"dev:broker:gen": "ts-node bin/dev.broker.gen.ts",
"gen:broker:types": "ts-node bin/gen.broker.types.ts",
"postinstall": "ts-patch install"
},
"author": "",
Expand Down
13 changes: 6 additions & 7 deletions examples/single-broker/src/services/api/v1.api.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import types from custom Moleculer typedef
import { Service, Context, eventName } from '../../typed-moleculer';
// import other services types
import { UserServiceTypes, V1apiServiceTypes } from '../../types';
import { UserServiceTypes, V1ApiServiceTypes } from '../../types';
// use moleculer-decorators
import { Service as DService, Action, Event } from 'moleculer-decorators';

Expand All @@ -14,7 +14,7 @@ interface V1ApiService {
mixins: [],
})
class V1ApiService extends Service<{}>
implements V1apiServiceTypes.ServiceOwnActions {
implements V1ApiServiceTypes.ServiceOwnActions {
settings: any;
@Event({
name: eventName('user.nodeChange'),
Expand All @@ -30,22 +30,21 @@ class V1ApiService extends Service<{}>

@Action()
async request(
ctx: Context<V1apiServiceTypes.ActionParams<'request'>>,
): Promise<V1apiServiceTypes.ActionReturn<'request'>> {
ctx: Context<V1ApiServiceTypes.ActionParams<'request'>>,
): Promise<V1ApiServiceTypes.ActionReturn<'request'>> {
const params = ctx.params;
params.req;
params.res;


ctx.call('user.delete', { id: 'a' });

return 'string';
}

@Action()
async context(
ctx: Context<V1apiServiceTypes.ActionParams<'context'>>,
): Promise<V1apiServiceTypes.ActionReturn<'context'>> {
ctx: Context<V1ApiServiceTypes.ActionParams<'context'>>,
): Promise<V1ApiServiceTypes.ActionReturn<'context'>> {
const params = ctx.params;
params.account.id;
params.user.id;
Expand Down
14 changes: 7 additions & 7 deletions examples/single-broker/src/types/broker.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ export interface ServiceBroker {
broadcastLocal: ServiceBroker['emit'];
}

type GetCallParams = {
export type GetCallParams = {
'api.request': Services.ApiServiceTypes.Actions[0]['in'];
'api.context': Services.ApiServiceTypes.Actions[1]['in'];
'v1.api.request': Services.V1apiServiceTypes.Actions[0]['in'];
'v1.api.context': Services.V1apiServiceTypes.Actions[1]['in'];
'v1.api.request': Services.V1ApiServiceTypes.Actions[0]['in'];
'v1.api.context': Services.V1ApiServiceTypes.Actions[1]['in'];
'user.create': Services.UserServiceTypes.Actions[0]['in'];
'user.get': Services.UserServiceTypes.Actions[1]['in'];
'user.delete': Services.UserServiceTypes.Actions[2]['in'];
};

type GetCallReturn = {
export type GetCallReturn = {
'api.request': Services.ApiServiceTypes.Actions[0]['out'];
'api.context': Services.ApiServiceTypes.Actions[1]['out'];
'v1.api.request': Services.V1apiServiceTypes.Actions[0]['out'];
'v1.api.context': Services.V1apiServiceTypes.Actions[1]['out'];
'v1.api.request': Services.V1ApiServiceTypes.Actions[0]['out'];
'v1.api.context': Services.V1ApiServiceTypes.Actions[1]['out'];
'user.create': Services.UserServiceTypes.Actions[0]['out'];
'user.get': Services.UserServiceTypes.Actions[1]['out'];
'user.delete': Services.UserServiceTypes.Actions[2]['out'];
};

type GetEmitParams = {
export type GetEmitParams = {
'user.nodeChange': MoleculerTs.Union.Strict<
Services.UserServiceTypes.Events[0]['in']
>;
Expand Down
4 changes: 2 additions & 2 deletions examples/single-broker/src/types/services.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ declare module '../services/user/user.service.types' {
}

import * as ApiServiceTypes from '../services/api/api.service.types';
import * as V1apiServiceTypes from '../services/api/v1.api.service.types';
import * as V1ApiServiceTypes from '../services/api/v1.api.service.types';
import * as UserServiceTypes from '../services/user/user.service.types';

export { ApiServiceTypes, V1apiServiceTypes, UserServiceTypes };
export { ApiServiceTypes, V1ApiServiceTypes, UserServiceTypes };

0 comments on commit 8a47c5a

Please sign in to comment.