@rayondigital/nest-dapr 0.10.2 → 0.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,23 +5,23 @@ Develop NestJs microservices using [Dapr](https://dapr.io/) pubsub, actors and b
5
5
 
6
6
  # Description
7
7
 
8
- Dapr Module for [Nest](https://github.com/nestjs/nest) built on top of the [Dapr JS SDK](https://github.com/dapr/js-sdk).
8
+ Dapr Module for [Nest](https://github.com/nestjs/nest) built on top of the latest [Dapr JS SDK](https://github.com/dapr/js-sdk).
9
9
 
10
10
 
11
11
  # Supported features
12
12
  - [x] [Actors](https://docs.dapr.io/developing-applications/building-blocks/actors/actors-overview/)
13
13
  - [x] [PubSub](https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-overview/)
14
14
  - [x] [Bindings](https://docs.dapr.io/developing-applications/building-blocks/bindings/bindings-overview/)
15
+ - [x] [Workflows](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-overview/)
15
16
  - [ ] [Distributed Lock](https://docs.dapr.io/developing-applications/building-blocks/distributed-lock/)
16
17
  - [ ] [State](https://docs.dapr.io/developing-applications/building-blocks/state-management/state-management-overview/)
17
18
  - [ ] [Service Invocation](https://docs.dapr.io/developing-applications/building-blocks/service-invocation/service-invocation-overview/)
18
- - [ ] [Workflows](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-overview/)
19
19
 
20
20
 
21
21
  # Installation
22
22
 
23
23
  ```bash
24
- npm i --save @rayondigital/nest-dapr
24
+ npm i @rayondigital/nest-dapr
25
25
  ```
26
26
 
27
27
  # Requirements
@@ -35,8 +35,8 @@ dapr --version
35
35
  Output:
36
36
 
37
37
  ```
38
- CLI version: 1.12.0
39
- Runtime version: 1.12.2
38
+ CLI version: 1.15.1
39
+ Runtime version: 1.15.4
40
40
  ```
41
41
 
42
42
  # Quick start
@@ -99,6 +99,7 @@ export class AppController {
99
99
  Create actors and connect them to your NestJS application using the `@DaprActor` decorator.
100
100
  This decorator takes in the interface of the actor, and marks the Actor as transient inside the NestJS
101
101
  dependency injection container.
102
+ Ensure your actor classes are added to the `providers` array of your NestJS module.
102
103
 
103
104
  ```typescript
104
105
  // You must expose your actors interface as an abstract class because Typescript interfaces are not available at runtime (erasure).
@@ -168,6 +169,112 @@ export class CounterController {
168
169
  }
169
170
  ```
170
171
 
172
+ ## Workflows
173
+
174
+ Workflows and Activities are annotated with the `@DaprWorkflow` and `@DaprActivity` decorators respectively.
175
+ When added to the `providers` array of a NestJS module, they are automatically registered with the Dapr server.
176
+
177
+ Note: Take care to ensure that your activities are stateless, and idempotent.
178
+ Be very careful with the state, and services you have inside your workflows.
179
+
180
+
181
+ ```typescript
182
+ @DaprActivity()
183
+ export class HelloActivity implements WorkflowActivity<string, string> {
184
+ async run(context: WorkflowActivityContext, name: string): Promise<string> {
185
+ return `Hello ${name}!`;
186
+ }
187
+ }
188
+
189
+ @DaprActivity()
190
+ export class CreateEntityActivity implements WorkflowActivity<string, Entity> {
191
+ @Inject()
192
+ private readonly entityService: EntityService;
193
+
194
+ constructor(private readonly cacheService: CacheService) {}
195
+
196
+ async run(context: WorkflowActivityContext, id: string): Promise<Entity> {
197
+ const entity: Entity = { id: id, createdAt: new Date(), lastUpdatedAt: new Date(), status: 'created', data: {} };
198
+ await this.entityService.update(entity);
199
+ console.log('entity', entity);
200
+ return entity;
201
+ }
202
+ }
203
+
204
+ @DaprActivity()
205
+ export class GetEntityActivity implements WorkflowActivity<string, Entity> {
206
+ @Inject()
207
+ private readonly entityService: EntityService;
208
+
209
+ constructor(private readonly cacheService: CacheService) {}
210
+
211
+ async run(context: WorkflowActivityContext, id: string): Promise<Entity> {
212
+ const entity = await this.entityService.get(id);
213
+ console.log('entity', entity);
214
+ return entity;
215
+ }
216
+ }
217
+
218
+ @DaprWorkflow()
219
+ export class HelloWorkflow implements Workflow<string[], string> {
220
+ async *run(ctx: WorkflowContext, input: string): AsyncGenerator<unknown, string[]> {
221
+ const cities: string[] = [];
222
+
223
+ let entity = expect<Entity>(yield ctx.callActivity(CreateEntityActivity, '12345'));
224
+ ctx.setCustomStatus('Entity');
225
+
226
+ entity = expect<Entity>(yield ctx.callActivity(GetEntityActivity, '12345'));
227
+ console.log('entity', entity);
228
+ ctx.setCustomStatus('Entity');
229
+
230
+ const result1 = expect<string>(yield ctx.callActivity(HelloActivity, 'Tokyo'));
231
+ ctx.setCustomStatus('Tokyo');
232
+
233
+ const event = yield ctx.waitForExternalEvent('next');
234
+ console.log('event', event);
235
+
236
+ const result2 = expect<string>(yield ctx.callActivity(HelloActivity, 'Seattle'));
237
+ ctx.setCustomStatus('Seattle');
238
+
239
+ const result3 = expect<string>(yield ctx.callActivity(HelloActivity, 'London'));
240
+ ctx.setCustomStatus('London');
241
+
242
+ return cities;
243
+ }
244
+ }
245
+ ```
246
+
247
+ ### Workflow Client
248
+
249
+ ```typescript
250
+ @Controller()
251
+ export class WorkflowController {
252
+ constructor(
253
+ private readonly workflowClient: DaprWorkflowClient,
254
+ ) {}
255
+
256
+ @Get(":id")
257
+ async start(@Param("id") uuid: string): Promise<string> {
258
+ const id = await workflowClient.scheduleNewWorkflow(HelloWorkflow, 'Hello', uuid);
259
+ // Workflow is started, and the id is returned.
260
+ // You can wait for the workflow to start and get the initial state.
261
+ const initialState = await workflowClient.waitForWorkflowStart(id, undefined, 15);
262
+
263
+ // You can raise events
264
+ // await workflowClient.raiseEvent(id, 'next', { input: 'next' });
265
+
266
+ // Optionally you can also wait for it to complete.
267
+ // const state = await workflowClient.waitForWorkflowCompletion(id, undefined, 15);
268
+ // Use the workflowOutput helper to get typed variables
269
+ // const value = workflowOutput(HelloWorkflow, state);
270
+ return {
271
+ id: id,
272
+ state: initialState
273
+ }
274
+ }
275
+ }
276
+ ```
277
+
171
278
  ## PubSub
172
279
 
173
280
  Create pubsub & topic names used for pubsub operations and message interface
@@ -7,14 +7,9 @@ export declare class DaprActorClient {
7
7
  private actorProxyBuilders;
8
8
  private interfaces;
9
9
  private interfaceToActorTypeNames;
10
- private prefix;
11
- private delimiter;
12
- private typeNamePrefix;
13
10
  private allowInternalCalls;
14
11
  constructor(moduleRef: ModuleRef, daprClient: DaprClient);
15
12
  setAllowInternalCalls(allowInternalCalls: boolean): void;
16
- setPrefix(prefix: string, delimiter?: string): void;
17
- setTypeNamePrefix(prefix: string): void;
18
13
  register<T>(actorTypeName: string, actorType: Type<T> | Function, daprClient?: DaprClient): void;
19
14
  registerInterface<T>(actorType: Type<T> | Function, interfaceType: Type<T> | Function, daprClient?: DaprClient): void;
20
15
  getActorId(actorId: string): ActorId;
@@ -21,21 +21,11 @@ let DaprActorClient = class DaprActorClient {
21
21
  this.actorProxyBuilders = new Map();
22
22
  this.interfaces = new Map();
23
23
  this.interfaceToActorTypeNames = new Map();
24
- this.prefix = '';
25
- this.delimiter = '-';
26
- this.typeNamePrefix = '';
27
24
  this.allowInternalCalls = process.env.DAPR_ACTOR_ALLOW_INTERNAL_CALLS === 'true';
28
25
  }
29
26
  setAllowInternalCalls(allowInternalCalls) {
30
27
  this.allowInternalCalls = allowInternalCalls;
31
28
  }
32
- setPrefix(prefix, delimiter = '-') {
33
- this.prefix = prefix;
34
- this.delimiter = delimiter;
35
- }
36
- setTypeNamePrefix(prefix) {
37
- this.typeNamePrefix = prefix;
38
- }
39
29
  register(actorTypeName, actorType, daprClient) {
40
30
  this.interfaces.set(this.formatActorTypeName(actorTypeName), actorType);
41
31
  const proxyBuilder = new actor_proxy_builder_1.ActorProxyBuilder(this.moduleRef, actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient);
@@ -53,13 +43,9 @@ let DaprActorClient = class DaprActorClient {
53
43
  this.actorProxyBuilders.set(this.formatActorTypeName(interfaceTypeName), proxyBuilder);
54
44
  }
55
45
  getActorId(actorId) {
56
- var _a;
57
46
  if (!actorId) {
58
47
  throw new Error('Actor id must be provided');
59
48
  }
60
- if (this.prefix) {
61
- return new dapr_1.ActorId(`${this.prefix}${(_a = this.delimiter) !== null && _a !== void 0 ? _a : '-'}${actorId}`);
62
- }
63
49
  return new dapr_1.ActorId(actorId);
64
50
  }
65
51
  getActor(actorType, actorId) {
@@ -98,16 +84,7 @@ let DaprActorClient = class DaprActorClient {
98
84
  }
99
85
  getActorTypeName(typeName) {
100
86
  if (this.interfaceToActorTypeNames.has(typeName)) {
101
- const actorTypeName = this.interfaceToActorTypeNames.get(typeName);
102
- if (this.actorProxyBuilders.has(actorTypeName)) {
103
- return actorTypeName;
104
- }
105
- else {
106
- return `${this.typeNamePrefix}${actorTypeName}`;
107
- }
108
- }
109
- if (this.typeNamePrefix) {
110
- return `${this.typeNamePrefix}${typeName}`;
87
+ return this.interfaceToActorTypeNames.get(typeName);
111
88
  }
112
89
  return typeName;
113
90
  }
@@ -8,6 +8,8 @@ export declare class NestActorManager {
8
8
  setupCSLWrapper(options: DaprModuleOptions, contextService: DaprContextService, invokeWrapperFn?: (actorId: ActorId, methodName: string, data: any, method: (actorId: ActorId, methodName: string, data: any) => Promise<any>) => Promise<any>): void;
9
9
  private catchAndLogUnhandledExceptions;
10
10
  private patchToSupportSerializableError;
11
+ private patchFireTimer;
12
+ private patchFireReminder;
11
13
  private patchDeactivate;
12
14
  private resolveDependencies;
13
15
  private static extractContext;
@@ -37,14 +37,11 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
37
37
  const originalCreateActor = ActorManager_1.default.prototype.createActor;
38
38
  const resolveDependencies = this.resolveDependencies;
39
39
  ActorManager_1.default.prototype.createActor = function (actorId) {
40
- var _a, _b;
40
+ var _a;
41
41
  return __awaiter(this, void 0, void 0, function* () {
42
42
  const instance = (yield originalCreateActor.bind(this)(actorId));
43
- if ((_a = options === null || options === void 0 ? void 0 : options.actorOptions) === null || _a === void 0 ? void 0 : _a.typeNamePrefix) {
44
- instance['actorType'] = `${options.typeNamePrefix}${instance.actorType}`;
45
- }
46
43
  if (isLoggingEnabled) {
47
- const actorTypeName = (_b = this.actorCls.name) !== null && _b !== void 0 ? _b : instance.constructor.name;
44
+ const actorTypeName = (_a = this.actorCls.name) !== null && _a !== void 0 ? _a : instance.constructor.name;
48
45
  common_1.Logger.verbose(`Activating actor ${actorId}`, actorTypeName);
49
46
  }
50
47
  try {
@@ -65,9 +62,11 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
65
62
  };
66
63
  this.patchDeactivate(options);
67
64
  this.patchToSupportSerializableError(options);
68
- const isErrorHandlerEnabled = (_c = options === null || options === void 0 ? void 0 : options.catchErrors) !== null && _c !== void 0 ? _c : false;
65
+ const isErrorHandlerEnabled = (_c = options === null || options === void 0 ? void 0 : options.catchErrors) !== null && _c !== void 0 ? _c : true;
69
66
  if (isErrorHandlerEnabled) {
70
67
  this.catchAndLogUnhandledExceptions();
68
+ this.patchFireTimer();
69
+ this.patchFireReminder();
71
70
  }
72
71
  }
73
72
  setupReentrancy(options) {
@@ -97,6 +96,7 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
97
96
  const isLoggingEnabled = (_b = (_a = options === null || options === void 0 ? void 0 : options.logging) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : true;
98
97
  const originalCallActor = ActorManager_1.default.prototype.callActorMethod;
99
98
  ActorManager_1.default.prototype.callActorMethod = function (actorId, methodName, data) {
99
+ var _a;
100
100
  return __awaiter(this, void 0, void 0, function* () {
101
101
  try {
102
102
  if (isLoggingEnabled) {
@@ -113,12 +113,12 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
113
113
  }
114
114
  else {
115
115
  return yield clsService.run(() => __awaiter(this, void 0, void 0, function* () {
116
- var _a;
116
+ var _b;
117
117
  contextService.setIdIfNotDefined();
118
118
  const context = NestActorManager_1.extractContext(data);
119
119
  if (context) {
120
120
  contextService.set(context);
121
- const correlationId = (_a = context[dapr_context_service_1.DAPR_CORRELATION_ID_KEY]) !== null && _a !== void 0 ? _a : (0, crypto_1.randomUUID)();
121
+ const correlationId = (_b = context[dapr_context_service_1.DAPR_CORRELATION_ID_KEY]) !== null && _b !== void 0 ? _b : (0, crypto_1.randomUUID)();
122
122
  if (correlationId) {
123
123
  contextService.setCorrelationId(correlationId);
124
124
  }
@@ -142,6 +142,10 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
142
142
  if (error.stack) {
143
143
  common_1.Logger.error(error.stack);
144
144
  }
145
+ if (serializable_error_1.SerializableError.isSerializableError(error)) {
146
+ error.statusCode = (_a = error.statusCode) !== null && _a !== void 0 ? _a : HttpStatusCode_enum_1.default.BAD_REQUEST;
147
+ return error;
148
+ }
145
149
  throw error;
146
150
  }
147
151
  });
@@ -182,6 +186,38 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
182
186
  });
183
187
  };
184
188
  }
189
+ patchFireTimer() {
190
+ const originalFireTimer = ActorManager_1.default.prototype.fireTimer;
191
+ if (!originalFireTimer)
192
+ return;
193
+ ActorManager_1.default.prototype.fireTimer = function (actorId, timerName, requestBody) {
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ try {
196
+ return yield originalFireTimer.bind(this)(actorId, timerName, requestBody);
197
+ }
198
+ catch (error) {
199
+ common_1.Logger.error(`Error firing timer ${timerName} for actor ${actorId}`);
200
+ common_1.Logger.error(error);
201
+ }
202
+ });
203
+ };
204
+ }
205
+ patchFireReminder() {
206
+ const originalFireReminder = ActorManager_1.default.prototype.fireReminder;
207
+ if (!originalFireReminder)
208
+ return;
209
+ ActorManager_1.default.prototype.fireReminder = function (actorId, reminderName, requestBody) {
210
+ return __awaiter(this, void 0, void 0, function* () {
211
+ try {
212
+ return yield originalFireReminder.bind(this)(actorId, reminderName, requestBody);
213
+ }
214
+ catch (error) {
215
+ common_1.Logger.error(`Error firing reminder ${reminderName} for actor ${actorId}`);
216
+ common_1.Logger.error(error);
217
+ }
218
+ });
219
+ };
220
+ }
185
221
  patchDeactivate(options) {
186
222
  var _a, _b;
187
223
  const isLoggingEnabled = (_b = (_a = options === null || options === void 0 ? void 0 : options.logging) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : true;
@@ -25,9 +25,10 @@ export declare class DaprLoader implements OnApplicationBootstrap, OnApplication
25
25
  constructor(discoveryService: DiscoveryService, metadataScanner: MetadataScanner, daprServer: DaprServer, daprMetadataAccessor: DaprMetadataAccessor, options: DaprModuleOptions, daprActorClient: DaprActorClient, moduleRef: ModuleRef, contextService: DaprContextService, pubSubClient: DaprPubSubClient, workflowClient: DaprWorkflowClient, actorManager: NestActorManager);
26
26
  onApplicationBootstrap(): Promise<void>;
27
27
  onApplicationShutdown(): Promise<void>;
28
- loadDaprHandlers(isActorsEnabled: boolean, isWorkflowEnabled: boolean): void;
28
+ loadDaprHandlers(isActorsEnabled: boolean, isWorkflowEnabled: boolean): Promise<void>;
29
29
  private subscribeToDaprPubSubEventIfListener;
30
30
  private subscribeToDaprBindingEventIfListener;
31
+ private registerHandlers;
31
32
  private registerActivity;
32
33
  private registerWorkflow;
33
34
  private registerActor;
@@ -20,15 +20,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
20
20
  step((generator = generator.apply(thisArg, _arguments || [])).next());
21
21
  });
22
22
  };
23
- var __importDefault = (this && this.__importDefault) || function (mod) {
24
- return (mod && mod.__esModule) ? mod : { "default": mod };
25
- };
26
23
  var DaprLoader_1;
27
24
  Object.defineProperty(exports, "__esModule", { value: true });
28
25
  exports.DaprLoader = void 0;
29
26
  const dapr_1 = require("@dapr/dapr");
30
- const ActorManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorManager"));
31
- const ActorRuntime_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorRuntime"));
32
27
  const common_1 = require("@nestjs/common");
33
28
  const core_1 = require("@nestjs/core");
34
29
  const dapr_actor_client_service_1 = require("./actors/dapr-actor-client.service");
@@ -56,45 +51,38 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
56
51
  this.logger = new common_1.Logger(DaprLoader_1.name);
57
52
  }
58
53
  onApplicationBootstrap() {
59
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
54
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
60
55
  return __awaiter(this, void 0, void 0, function* () {
61
56
  this.logger.log('Dapr initializing');
62
57
  const isEnabled = !this.options.disabled;
63
58
  const isActorsEnabled = (_b = (_a = this.options.actorOptions) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : true;
64
59
  const isWorkflowEnabled = (_d = (_c = this.options.workflowOptions) === null || _c === void 0 ? void 0 : _c.enabled) !== null && _d !== void 0 ? _d : false;
60
+ if (isActorsEnabled && isEnabled) {
61
+ this.logger.log('Registering Dapr actors');
62
+ yield this.daprServer.actor.init();
63
+ }
65
64
  if (isWorkflowEnabled) {
66
65
  this.workflowRuntime = new dapr_1.WorkflowRuntime({
67
66
  daprHost: this.options.serverHost,
68
67
  daprPort: (_f = (_e = this.options.workflowOptions) === null || _e === void 0 ? void 0 : _e.daprPort) !== null && _f !== void 0 ? _f : '3501',
69
68
  });
70
69
  }
70
+ if ((_g = this.options.pubsubOptions) === null || _g === void 0 ? void 0 : _g.defaultName) {
71
+ this.pubSubClient.setDefaultName(this.options.pubsubOptions.defaultName);
72
+ }
73
+ yield this.loadDaprHandlers(isActorsEnabled, isWorkflowEnabled);
71
74
  if (isActorsEnabled) {
72
75
  this.actorManager.setup(this.moduleRef, this.options);
73
76
  if (this.options.contextProvider !== dapr_module_1.DaprContextProvider.None) {
74
77
  this.actorManager.setupCSLWrapper(this.options, this.contextService);
75
78
  }
76
- if ((_j = (_h = (_g = this.options.clientOptions) === null || _g === void 0 ? void 0 : _g.actor) === null || _h === void 0 ? void 0 : _h.reentrancy) === null || _j === void 0 ? void 0 : _j.enabled) {
79
+ if ((_k = (_j = (_h = this.options.clientOptions) === null || _h === void 0 ? void 0 : _h.actor) === null || _j === void 0 ? void 0 : _j.reentrancy) === null || _k === void 0 ? void 0 : _k.enabled) {
77
80
  this.actorManager.setupReentrancy(this.options);
78
81
  }
79
- }
80
- if ((_k = this.options.pubsubOptions) === null || _k === void 0 ? void 0 : _k.defaultName) {
81
- this.pubSubClient.setDefaultName(this.options.pubsubOptions.defaultName);
82
- }
83
- if (isActorsEnabled) {
84
82
  if (this.options.actorOptions) {
85
83
  this.daprActorClient.setAllowInternalCalls((_m = (_l = this.options.actorOptions) === null || _l === void 0 ? void 0 : _l.allowInternalCalls) !== null && _m !== void 0 ? _m : false);
86
- this.daprActorClient.setPrefix((_p = (_o = this.options.actorOptions) === null || _o === void 0 ? void 0 : _o.prefix) !== null && _p !== void 0 ? _p : '', (_r = (_q = this.options.actorOptions) === null || _q === void 0 ? void 0 : _q.delimiter) !== null && _r !== void 0 ? _r : '-');
87
- this.daprActorClient.setTypeNamePrefix((_t = (_s = this.options.actorOptions) === null || _s === void 0 ? void 0 : _s.typeNamePrefix) !== null && _t !== void 0 ? _t : '');
88
- if ((_u = this.options.actorOptions) === null || _u === void 0 ? void 0 : _u.prefix) {
89
- this.logger.log(`Actors will be prefixed with ${(_w = (_v = this.options.actorOptions) === null || _v === void 0 ? void 0 : _v.prefix) !== null && _w !== void 0 ? _w : ''} and delimited with ${(_y = (_x = this.options.actorOptions) === null || _x === void 0 ? void 0 : _x.delimiter) !== null && _y !== void 0 ? _y : '-'}`);
90
- }
91
- }
92
- if (isEnabled) {
93
- this.logger.log('Registering Dapr actors');
94
- yield this.daprServer.actor.init();
95
84
  }
96
85
  }
97
- this.loadDaprHandlers(isActorsEnabled, isWorkflowEnabled);
98
86
  if (isEnabled && this.options.serverPort !== '0') {
99
87
  this.logger.log('Starting Dapr server');
100
88
  if (this.options.catchErrors) {
@@ -111,12 +99,12 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
111
99
  yield this.daprServer.start();
112
100
  this.logger.log('Dapr server started');
113
101
  }
114
- if (isEnabled && this.options.workflowOptions.enabled) {
102
+ if (isEnabled && isWorkflowEnabled) {
115
103
  this.logger.log('Starting Dapr workflow runtime');
116
104
  yield this.workflowRuntime.start();
117
105
  yield this.workflowClient.start({
118
106
  daprHost: this.options.serverHost,
119
- daprPort: (_z = this.options.workflowOptions.daprPort) !== null && _z !== void 0 ? _z : '3501',
107
+ daprPort: (_o = this.options.workflowOptions.daprPort) !== null && _o !== void 0 ? _o : '3501',
120
108
  });
121
109
  }
122
110
  if (!isActorsEnabled || !isEnabled)
@@ -155,44 +143,34 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
155
143
  });
156
144
  }
157
145
  loadDaprHandlers(isActorsEnabled, isWorkflowEnabled) {
158
- const providers = this.discoveryService.getProviders();
159
- if (isActorsEnabled) {
160
- providers
161
- .filter((wrapper) => wrapper.isDependencyTreeStatic() &&
162
- wrapper.metatype &&
163
- this.daprMetadataAccessor.getDaprActorMetadata(wrapper.metatype))
164
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
165
- yield this.registerActor(wrapper.metatype);
166
- }));
167
- }
168
- const controllers = this.discoveryService.getControllers();
169
- [...providers, ...controllers]
170
- .filter((wrapper) => wrapper.isDependencyTreeStatic())
171
- .filter((wrapper) => wrapper.instance)
172
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
173
- const { instance } = wrapper;
174
- const prototype = Object.getPrototypeOf(instance) || {};
175
- this.metadataScanner.scanFromPrototype(instance, prototype, (methodKey) => __awaiter(this, void 0, void 0, function* () {
176
- yield this.subscribeToDaprPubSubEventIfListener(instance, methodKey);
177
- yield this.subscribeToDaprBindingEventIfListener(instance, methodKey);
178
- }));
179
- }));
180
- if (isWorkflowEnabled) {
181
- providers
182
- .filter((wrapper) => wrapper.isDependencyTreeStatic() &&
183
- wrapper.metatype &&
184
- this.daprMetadataAccessor.getDaprActivityMetadata(wrapper.metatype))
185
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
186
- yield this.registerActivity(wrapper.metatype);
187
- }));
188
- providers
189
- .filter((wrapper) => wrapper.isDependencyTreeStatic() &&
190
- wrapper.metatype &&
191
- this.daprMetadataAccessor.getDaprWorkflowMetadata(wrapper.metatype))
192
- .forEach((wrapper) => __awaiter(this, void 0, void 0, function* () {
193
- yield this.registerWorkflow(wrapper.metatype);
194
- }));
195
- }
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ const providers = this.discoveryService.getProviders();
148
+ if (isActorsEnabled) {
149
+ for (const instanceWrapper of providers.filter((wrapper) => wrapper.isDependencyTreeStatic() &&
150
+ wrapper.metatype &&
151
+ this.daprMetadataAccessor.getDaprActorMetadata(wrapper.metatype))) {
152
+ yield this.registerActor(instanceWrapper.metatype);
153
+ }
154
+ }
155
+ const controllers = this.discoveryService.getControllers();
156
+ for (const instanceWrapper of [...providers, ...controllers]
157
+ .filter((wrapper) => wrapper.isDependencyTreeStatic())
158
+ .filter((wrapper) => wrapper.instance)) {
159
+ yield this.registerHandlers(instanceWrapper);
160
+ }
161
+ if (isWorkflowEnabled) {
162
+ for (const instanceWrapper of providers.filter((wrapper) => wrapper.isDependencyTreeStatic() &&
163
+ wrapper.metatype &&
164
+ this.daprMetadataAccessor.getDaprActivityMetadata(wrapper.metatype))) {
165
+ yield this.registerActivity(instanceWrapper.metatype);
166
+ }
167
+ for (const instanceWrapper of providers.filter((wrapper) => wrapper.isDependencyTreeStatic() &&
168
+ wrapper.metatype &&
169
+ this.daprMetadataAccessor.getDaprWorkflowMetadata(wrapper.metatype))) {
170
+ yield this.registerWorkflow(instanceWrapper.metatype);
171
+ }
172
+ }
173
+ });
196
174
  }
197
175
  subscribeToDaprPubSubEventIfListener(instance, methodKey) {
198
176
  var _a, _b;
@@ -244,6 +222,16 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
244
222
  }));
245
223
  });
246
224
  }
225
+ registerHandlers(instanceWrapper) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ const instance = instanceWrapper.instance;
228
+ const prototype = Object.getPrototypeOf(instance) || {};
229
+ this.metadataScanner.scanFromPrototype(instance, prototype, (methodKey) => __awaiter(this, void 0, void 0, function* () {
230
+ yield this.subscribeToDaprPubSubEventIfListener(instance, methodKey);
231
+ yield this.subscribeToDaprBindingEventIfListener(instance, methodKey);
232
+ }));
233
+ });
234
+ }
247
235
  registerActivity(activityType) {
248
236
  var _a, _b;
249
237
  return __awaiter(this, void 0, void 0, function* () {
@@ -275,29 +263,14 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
275
263
  });
276
264
  }
277
265
  registerActor(actorType) {
278
- var _a, _b, _c, _d, _e;
266
+ var _a, _b, _c, _d;
279
267
  return __awaiter(this, void 0, void 0, function* () {
280
268
  if (!actorType)
281
269
  return;
282
- let actorTypeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
283
270
  const daprActorMetadata = this.daprMetadataAccessor.getDaprActorMetadata(actorType);
271
+ const actorTypeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
284
272
  const interfaceTypeName = (_c = (_b = daprActorMetadata === null || daprActorMetadata === void 0 ? void 0 : daprActorMetadata.interfaceType) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : (_d = daprActorMetadata === null || daprActorMetadata === void 0 ? void 0 : daprActorMetadata.interfaceType) === null || _d === void 0 ? void 0 : _d.constructor.name;
285
- if ((_e = this.options.actorOptions) === null || _e === void 0 ? void 0 : _e.typeNamePrefix) {
286
- actorTypeName = this.options.actorOptions.typeNamePrefix + actorTypeName;
287
- try {
288
- const actorManager = ActorRuntime_1.default.getInstanceByDaprClient(this.daprServer.client);
289
- const managers = actorManager['actorManagers'];
290
- if (!managers.has(actorTypeName)) {
291
- managers.set(actorTypeName, new ActorManager_1.default(actorType, this.daprServer.client));
292
- }
293
- }
294
- catch (err) {
295
- yield this.daprServer.actor.registerActor(actorType);
296
- }
297
- }
298
- else {
299
- yield this.daprServer.actor.registerActor(actorType);
300
- }
273
+ yield this.daprServer.actor.registerActor(actorType);
301
274
  this.logger.log(`Registering Dapr Actor: ${actorTypeName} of type ${interfaceTypeName !== null && interfaceTypeName !== void 0 ? interfaceTypeName : 'unknown'}`);
302
275
  this.daprActorClient.register(actorTypeName, actorType, this.daprServer.client);
303
276
  if (daprActorMetadata.interfaceType) {
@@ -22,9 +22,6 @@ export interface DaprModuleLoggingOptions {
22
22
  }
23
23
  export interface DaprModuleActorOptions {
24
24
  enabled: boolean;
25
- prefix?: string;
26
- delimiter?: string;
27
- typeNamePrefix?: string;
28
25
  allowInternalCalls?: boolean;
29
26
  }
30
27
  export interface DaprModuleWorkflowOptions {
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.10.2",
4
- "description": "Develop NestJs microservices using Dapr pubsub, actors and other bindings",
3
+ "version": "0.10.5",
4
+ "description": "Develop NestJs microservices using Dapr pubsub, actors, workflows and other bindings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "rimraf -rf dist && tsc -p tsconfig.json",
9
9
  "lint": "eslint \"{lib,tests}/**/*.ts\" --fix",
10
10
  "test": "jest --runInBand",
11
- "test:in-memory": "dapr run --app-id testing --app-protocol http --app-port 3001 --dapr-http-port 3500 --resources-path ./tests/components npm run test",
11
+ "test:in-memory": "dapr run --app-id testing --app-protocol http --app-port 3001 --dapr-http-port 3500 --dapr-grpc-port 3501 --resources-path ./tests/components npm run test",
12
12
  "start:test": "nodemon tests/e2e/main.ts",
13
13
  "push:version": "git push --follow-tags"
14
14
  },
@@ -1 +0,0 @@
1
- export declare function fromJson(json: any): any;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fromJson = void 0;
4
- function fromJson(json) {
5
- try {
6
- if (typeof json === 'string') {
7
- const isJson = json.includes('{') || json.includes('[');
8
- if (isJson)
9
- return JSON.parse(json);
10
- }
11
- return json;
12
- }
13
- catch (error) {
14
- return json;
15
- }
16
- }
17
- exports.fromJson = fromJson;