@rayondigital/nest-dapr 0.9.34 → 0.9.36

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.
@@ -6,6 +6,7 @@ import { NestActorManager } from './actors/nest-actor-manager';
6
6
  import { DaprContextService } from './dapr-context-service';
7
7
  import { DaprMetadataAccessor } from './dapr-metadata.accessor';
8
8
  import { DaprModuleOptions } from './dapr.module';
9
+ import { DaprPubSubClient } from './pubsub/dapr-pubsub-client.service';
9
10
  export declare class DaprLoader implements OnApplicationBootstrap, OnApplicationShutdown {
10
11
  private readonly discoveryService;
11
12
  private readonly metadataScanner;
@@ -15,9 +16,10 @@ export declare class DaprLoader implements OnApplicationBootstrap, OnApplication
15
16
  private readonly daprActorClient;
16
17
  private readonly moduleRef;
17
18
  private readonly contextService;
19
+ private readonly pubSubClient;
18
20
  private readonly actorManager;
19
21
  private readonly logger;
20
- constructor(discoveryService: DiscoveryService, metadataScanner: MetadataScanner, daprServer: DaprServer, daprMetadataAccessor: DaprMetadataAccessor, options: DaprModuleOptions, daprActorClient: DaprActorClient, moduleRef: ModuleRef, contextService: DaprContextService, actorManager: NestActorManager);
22
+ constructor(discoveryService: DiscoveryService, metadataScanner: MetadataScanner, daprServer: DaprServer, daprMetadataAccessor: DaprMetadataAccessor, options: DaprModuleOptions, daprActorClient: DaprActorClient, moduleRef: ModuleRef, contextService: DaprContextService, pubSubClient: DaprPubSubClient, actorManager: NestActorManager);
21
23
  onApplicationBootstrap(): Promise<void>;
22
24
  onApplicationShutdown(): Promise<void>;
23
25
  loadDaprHandlers(): void;
@@ -36,8 +36,9 @@ const nest_actor_manager_1 = require("./actors/nest-actor-manager");
36
36
  const dapr_context_service_1 = require("./dapr-context-service");
37
37
  const dapr_metadata_accessor_1 = require("./dapr-metadata.accessor");
38
38
  const dapr_module_1 = require("./dapr.module");
39
+ const dapr_pubsub_client_service_1 = require("./pubsub/dapr-pubsub-client.service");
39
40
  let DaprLoader = DaprLoader_1 = class DaprLoader {
40
- constructor(discoveryService, metadataScanner, daprServer, daprMetadataAccessor, options, daprActorClient, moduleRef, contextService, actorManager) {
41
+ constructor(discoveryService, metadataScanner, daprServer, daprMetadataAccessor, options, daprActorClient, moduleRef, contextService, pubSubClient, actorManager) {
41
42
  this.discoveryService = discoveryService;
42
43
  this.metadataScanner = metadataScanner;
43
44
  this.daprServer = daprServer;
@@ -46,11 +47,12 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
46
47
  this.daprActorClient = daprActorClient;
47
48
  this.moduleRef = moduleRef;
48
49
  this.contextService = contextService;
50
+ this.pubSubClient = pubSubClient;
49
51
  this.actorManager = actorManager;
50
52
  this.logger = new common_1.Logger(DaprLoader_1.name);
51
53
  }
52
54
  onApplicationBootstrap() {
53
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
55
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
54
56
  return __awaiter(this, void 0, void 0, function* () {
55
57
  if (this.options.disabled) {
56
58
  this.logger.log('Dapr server is disabled');
@@ -63,12 +65,15 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
63
65
  if ((_c = (_b = (_a = this.options.clientOptions) === null || _a === void 0 ? void 0 : _a.actor) === null || _b === void 0 ? void 0 : _b.reentrancy) === null || _c === void 0 ? void 0 : _c.enabled) {
64
66
  this.actorManager.setupReentrancy();
65
67
  }
68
+ if ((_d = this.options.pubsubOptions) === null || _d === void 0 ? void 0 : _d.defaultName) {
69
+ this.pubSubClient.setDefaultName(this.options.pubsubOptions.defaultName);
70
+ }
66
71
  if (this.options.actorOptions) {
67
- this.daprActorClient.setAllowInternalCalls((_e = (_d = this.options.actorOptions) === null || _d === void 0 ? void 0 : _d.allowInternalCalls) !== null && _e !== void 0 ? _e : false);
68
- this.daprActorClient.setPrefix((_g = (_f = this.options.actorOptions) === null || _f === void 0 ? void 0 : _f.prefix) !== null && _g !== void 0 ? _g : '', (_j = (_h = this.options.actorOptions) === null || _h === void 0 ? void 0 : _h.delimiter) !== null && _j !== void 0 ? _j : '-');
69
- this.daprActorClient.setTypeNamePrefix((_l = (_k = this.options.actorOptions) === null || _k === void 0 ? void 0 : _k.typeNamePrefix) !== null && _l !== void 0 ? _l : '');
70
- if ((_m = this.options.actorOptions) === null || _m === void 0 ? void 0 : _m.prefix) {
71
- this.logger.log(`Actors will be prefixed with ${(_p = (_o = this.options.actorOptions) === null || _o === void 0 ? void 0 : _o.prefix) !== null && _p !== void 0 ? _p : ''} and delimited with ${(_r = (_q = this.options.actorOptions) === null || _q === void 0 ? void 0 : _q.delimiter) !== null && _r !== void 0 ? _r : '-'}`);
72
+ this.daprActorClient.setAllowInternalCalls((_f = (_e = this.options.actorOptions) === null || _e === void 0 ? void 0 : _e.allowInternalCalls) !== null && _f !== void 0 ? _f : false);
73
+ this.daprActorClient.setPrefix((_h = (_g = this.options.actorOptions) === null || _g === void 0 ? void 0 : _g.prefix) !== null && _h !== void 0 ? _h : '', (_k = (_j = this.options.actorOptions) === null || _j === void 0 ? void 0 : _j.delimiter) !== null && _k !== void 0 ? _k : '-');
74
+ this.daprActorClient.setTypeNamePrefix((_m = (_l = this.options.actorOptions) === null || _l === void 0 ? void 0 : _l.typeNamePrefix) !== null && _m !== void 0 ? _m : '');
75
+ if ((_o = this.options.actorOptions) === null || _o === void 0 ? void 0 : _o.prefix) {
76
+ this.logger.log(`Actors will be prefixed with ${(_q = (_p = this.options.actorOptions) === null || _p === void 0 ? void 0 : _p.prefix) !== null && _q !== void 0 ? _q : ''} and delimited with ${(_s = (_r = this.options.actorOptions) === null || _r === void 0 ? void 0 : _r.delimiter) !== null && _s !== void 0 ? _s : '-'}`);
72
77
  }
73
78
  }
74
79
  yield this.daprServer.actor.init();
@@ -218,6 +223,7 @@ DaprLoader = DaprLoader_1 = __decorate([
218
223
  dapr_metadata_accessor_1.DaprMetadataAccessor, Object, dapr_actor_client_service_1.DaprActorClient,
219
224
  core_1.ModuleRef,
220
225
  dapr_context_service_1.DaprContextService,
226
+ dapr_pubsub_client_service_1.DaprPubSubClient,
221
227
  nest_actor_manager_1.NestActorManager])
222
228
  ], DaprLoader);
223
229
  exports.DaprLoader = DaprLoader;
@@ -77,17 +77,10 @@ let DaprModule = DaprModule_1 = class DaprModule {
77
77
  dapr_metadata_accessor_1.DaprMetadataAccessor,
78
78
  dapr_context_service_1.DaprContextService,
79
79
  dapr_actor_client_service_1.DaprActorClient,
80
- {
81
- provide: dapr_pubsub_client_service_1.DaprPubSubClient,
82
- scope: common_1.Scope.DEFAULT,
83
- inject: [exports.DAPR_MODULE_OPTIONS_TOKEN, dapr_1.DaprClient],
84
- useFactory: (options, daprClient) => {
85
- return new dapr_pubsub_client_service_1.DaprPubSubClient(options, daprClient);
86
- },
87
- },
80
+ dapr_pubsub_client_service_1.DaprPubSubClient,
88
81
  core_1.Reflector,
89
82
  ],
90
- exports: [dapr_1.DaprClient, dapr_pubsub_client_service_1.DaprPubSubClient, exports.DAPR_MODULE_OPTIONS_TOKEN],
83
+ exports: [dapr_1.DaprClient, dapr_pubsub_client_service_1.DaprPubSubClient, dapr_context_service_1.DaprContextService, actor_runtime_service_1.ActorRuntimeService, dapr_actor_client_service_1.DaprActorClient],
91
84
  };
92
85
  }
93
86
  static registerAsync(options) {
@@ -118,18 +111,11 @@ let DaprModule = DaprModule_1 = class DaprModule {
118
111
  dapr_metadata_accessor_1.DaprMetadataAccessor,
119
112
  dapr_context_service_1.DaprContextService,
120
113
  dapr_actor_client_service_1.DaprActorClient,
121
- {
122
- provide: dapr_pubsub_client_service_1.DaprPubSubClient,
123
- scope: common_1.Scope.DEFAULT,
124
- inject: [exports.DAPR_MODULE_OPTIONS_TOKEN, dapr_1.DaprClient],
125
- useFactory: (options, daprClient) => {
126
- return new dapr_pubsub_client_service_1.DaprPubSubClient(options, daprClient);
127
- },
128
- },
114
+ dapr_pubsub_client_service_1.DaprPubSubClient,
129
115
  core_1.Reflector,
130
116
  ...(options.extraProviders || []),
131
117
  ],
132
- exports: [dapr_1.DaprClient, dapr_pubsub_client_service_1.DaprPubSubClient, exports.DAPR_MODULE_OPTIONS_TOKEN],
118
+ exports: [dapr_1.DaprClient, dapr_pubsub_client_service_1.DaprPubSubClient, dapr_context_service_1.DaprContextService, actor_runtime_service_1.ActorRuntimeService, dapr_actor_client_service_1.DaprActorClient],
133
119
  };
134
120
  }
135
121
  static createAsyncProviders(options) {
@@ -1,28 +1,27 @@
1
1
  import { DaprClient } from '@dapr/dapr';
2
2
  import { OnApplicationShutdown } from '@nestjs/common';
3
- import { DaprModuleOptions } from '../dapr.module';
4
3
  import { PublishMessage } from './publish.message';
5
4
  export declare class DaprPubSubClient implements OnApplicationShutdown {
6
- private readonly options;
7
5
  private readonly daprClient;
8
6
  private readonly logger;
9
- private readonly defaultName;
10
- private readonly buffer;
7
+ private defaultName;
11
8
  private subscription;
9
+ private readonly buffer;
12
10
  private readonly bufferSize;
13
11
  private readonly bufferTimeSpan;
14
12
  private onError;
15
- constructor(options: DaprModuleOptions, daprClient: DaprClient);
13
+ constructor(daprClient: DaprClient);
14
+ setDefaultName(name: string): void;
16
15
  registerErrorHandler(handler: (messages: PublishMessage[], error: Error) => void): void;
17
16
  protected handleError(messages: PublishMessage[], error: Error): Promise<void>;
18
17
  onApplicationShutdown(signal?: string): Promise<void>;
19
18
  protected setupBufferSubscription(): void;
20
19
  protected publishBulkDirectly(messages: PublishMessage[]): Promise<void>;
21
- protected publishDirectly(name: string, topic: string, payload: any, producerId?: string, metadata?: any, fireAndForget?: boolean): Promise<void>;
22
- publish(name: string, producerId: string, topic: string, payload: any, buffer: boolean, metadata?: any): Promise<void>;
23
- publish(producerId: string, topic: string, payload: any, buffer: boolean, metadata?: any): Promise<void>;
20
+ protected publishDirectly(name: string, topic: string, payload: any, producerId?: string, metadata?: any, fireAndForget?: boolean, contentType?: string): Promise<void>;
21
+ publish(name: string, producerId: string, topic: string, payload: any, buffer: boolean, metadata?: any, contentType?: string): Promise<void>;
22
+ publish(producerId: string, topic: string, payload: any, buffer: boolean, metadata?: any, contentType?: string): Promise<void>;
24
23
  publish(producerId: string, topic: string, payload: any): Promise<void>;
25
- publishBulk(name: string, producerId: string, topic: string, payloads: any[], metadata?: any): Promise<void>;
26
- publishBulk(producerId: string, topic: string, payloads: any[], metadata?: any): Promise<void>;
24
+ publishBulk(name: string, producerId: string, topic: string, payloads: any[], metadata?: any, contentType?: string): Promise<void>;
25
+ publishBulk(producerId: string, topic: string, payloads: any[], metadata?: any, contentType?: string): Promise<void>;
27
26
  publishBulk(producerId: string, topic: string, payloads: any[]): Promise<void>;
28
27
  }
@@ -8,9 +8,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
11
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
12
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
13
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -27,19 +24,19 @@ const dapr_1 = require("@dapr/dapr");
27
24
  const common_1 = require("@nestjs/common");
28
25
  const rxjs_1 = require("rxjs");
29
26
  const operators_1 = require("rxjs/operators");
30
- const dapr_module_1 = require("../dapr.module");
31
27
  let DaprPubSubClient = DaprPubSubClient_1 = class DaprPubSubClient {
32
- constructor(options, daprClient) {
33
- var _a, _b;
34
- this.options = options;
28
+ constructor(daprClient) {
35
29
  this.daprClient = daprClient;
36
30
  this.logger = new common_1.Logger(DaprPubSubClient_1.name);
31
+ this.defaultName = 'pubsub';
37
32
  this.buffer = new rxjs_1.Subject();
38
33
  this.bufferSize = 10;
39
34
  this.bufferTimeSpan = 1000;
40
- this.defaultName = (_b = (_a = this.options.pubsubOptions) === null || _a === void 0 ? void 0 : _a.defaultName) !== null && _b !== void 0 ? _b : 'pubsub';
41
35
  this.setupBufferSubscription();
42
36
  }
37
+ setDefaultName(name) {
38
+ this.defaultName = name;
39
+ }
43
40
  registerErrorHandler(handler) {
44
41
  this.onError = handler;
45
42
  }
@@ -87,10 +84,11 @@ let DaprPubSubClient = DaprPubSubClient_1 = class DaprPubSubClient {
87
84
  }));
88
85
  }
89
86
  publishBulkDirectly(messages) {
87
+ var _a;
90
88
  return __awaiter(this, void 0, void 0, function* () {
91
89
  if (messages.length === 1) {
92
90
  const message = messages[0];
93
- yield this.publishDirectly(message.name, message.topic, message.payload, message.producerId, message.metadata);
91
+ yield this.publishDirectly(message.name, message.topic, message.payload, message.producerId, message.metadata, false, message.contentType);
94
92
  return;
95
93
  }
96
94
  const grouped = messages.reduce((acc, message) => {
@@ -105,11 +103,12 @@ let DaprPubSubClient = DaprPubSubClient_1 = class DaprPubSubClient {
105
103
  const [name, topic, producerId] = key.split(':');
106
104
  const messages = grouped[key];
107
105
  if (messages.length === 1) {
108
- yield this.publishDirectly(name, topic, messages[0].payload, producerId, messages[0].metadata);
106
+ yield this.publishDirectly(name, topic, messages[0].payload, producerId, messages[0].metadata, false, messages[0].contentType);
109
107
  continue;
110
108
  }
111
109
  try {
112
- yield this.daprClient.pubsub.publishBulk(name, topic, messages.map((m) => m.payload), producerId ? { metadata: { partitionKey: producerId } } : undefined);
110
+ const contentType = (_a = messages[0].contentType) !== null && _a !== void 0 ? _a : 'application/json';
111
+ yield this.daprClient.pubsub.publishBulk(name, topic, messages.map((m) => m.payload), producerId ? { metadata: { partitionKey: producerId }, contentType } : undefined);
113
112
  }
114
113
  catch (error) {
115
114
  yield this.handleError(messages, error);
@@ -117,12 +116,16 @@ let DaprPubSubClient = DaprPubSubClient_1 = class DaprPubSubClient {
117
116
  }
118
117
  });
119
118
  }
120
- publishDirectly(name, topic, payload, producerId, metadata, fireAndForget = false) {
119
+ publishDirectly(name, topic, payload, producerId, metadata, fireAndForget = false, contentType) {
121
120
  return __awaiter(this, void 0, void 0, function* () {
122
121
  try {
123
122
  if (!name)
124
123
  name = this.defaultName;
125
- const options = {};
124
+ if (!contentType)
125
+ contentType = 'application/json';
126
+ const options = {
127
+ contentType,
128
+ };
126
129
  if (metadata) {
127
130
  options['metadata'] = metadata;
128
131
  }
@@ -150,20 +153,23 @@ let DaprPubSubClient = DaprPubSubClient_1 = class DaprPubSubClient {
150
153
  let payload;
151
154
  let buffer;
152
155
  let metadata;
153
- if (args.length === 6) {
154
- [name, producerId, topic, payload, buffer, metadata] = args;
156
+ let contentType;
157
+ if (args.length === 7) {
158
+ [name, producerId, topic, payload, buffer, metadata, contentType] = args;
155
159
  }
156
160
  else {
157
- [producerId, topic, payload, buffer, metadata] = args;
161
+ [producerId, topic, payload, buffer, metadata, contentType] = args;
158
162
  name = this.defaultName;
159
163
  }
160
164
  if (!name)
161
165
  name = this.defaultName;
166
+ if (!contentType)
167
+ contentType = 'application/json';
162
168
  if (buffer === undefined || buffer) {
163
- this.buffer.next({ name, producerId, topic, payload, metadata });
169
+ this.buffer.next({ name, producerId, topic, payload, metadata, contentType });
164
170
  return;
165
171
  }
166
- yield this.publishDirectly(name, topic, payload, producerId, true);
172
+ yield this.publishDirectly(name, topic, payload, producerId, metadata, true, contentType);
167
173
  });
168
174
  }
169
175
  publishBulk(...args) {
@@ -173,32 +179,34 @@ let DaprPubSubClient = DaprPubSubClient_1 = class DaprPubSubClient {
173
179
  let topic;
174
180
  let payloads;
175
181
  let metadata;
176
- if (args.length === 5) {
177
- [name, producerId, topic, payloads, metadata] = args;
182
+ let contentType;
183
+ if (args.length === 6) {
184
+ [name, producerId, topic, payloads, metadata, contentType] = args;
178
185
  }
179
- else if (args.length === 4) {
180
- [producerId, topic, payloads, metadata] = args;
186
+ else if (args.length === 5) {
187
+ [producerId, topic, payloads, metadata, contentType] = args;
181
188
  name = this.defaultName;
182
189
  }
183
- else if (args.length === 3) {
190
+ else {
184
191
  [producerId, topic, payloads] = args;
185
192
  name = this.defaultName;
186
193
  }
187
194
  if (!name)
188
195
  name = this.defaultName;
196
+ if (!contentType)
197
+ contentType = 'application/json';
189
198
  if (payloads.length === 1) {
190
- yield this.publishDirectly(name, topic, payloads[0], producerId, metadata);
199
+ yield this.publishDirectly(name, topic, payloads[0], producerId, metadata, true, contentType);
191
200
  return;
192
201
  }
193
202
  for (const payload of payloads) {
194
- this.buffer.next({ name, producerId, topic, payload, metadata });
203
+ this.buffer.next({ name, producerId, topic, payload, metadata, contentType });
195
204
  }
196
205
  });
197
206
  }
198
207
  };
199
208
  DaprPubSubClient = DaprPubSubClient_1 = __decorate([
200
209
  (0, common_1.Injectable)(),
201
- __param(0, (0, common_1.Inject)(dapr_module_1.DAPR_MODULE_OPTIONS_TOKEN)),
202
- __metadata("design:paramtypes", [Object, dapr_1.DaprClient])
210
+ __metadata("design:paramtypes", [dapr_1.DaprClient])
203
211
  ], DaprPubSubClient);
204
212
  exports.DaprPubSubClient = DaprPubSubClient;
@@ -4,4 +4,5 @@ export interface PublishMessage {
4
4
  topic: string;
5
5
  payload: any;
6
6
  metadata?: any;
7
+ contentType?: string;
7
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.34",
3
+ "version": "0.9.36",
4
4
  "description": "Develop NestJs microservices using Dapr pubsub, actors and other bindings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",