@treatwell/moleculer-essentials 1.3.0-beta.1 → 1.3.0

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.
Files changed (2) hide show
  1. package/README.md +121 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -158,6 +158,127 @@ export default wrapService({
158
158
 
159
159
  The documentation isn't done yet, but you can check the [source code](./src/) to see what is available.
160
160
 
161
+ ### ServiceSchema & ActionSchema overriding
162
+
163
+ By using TS declaration [merging feature](https://www.typescriptlang.org/docs/handbook/declaration-merging.html),
164
+ you can augment the default services and action schemas to support additional features:
165
+
166
+ ```ts
167
+ declare module '@treatwell/moleculer-essentials' {
168
+ export interface CustomActionSchema {
169
+ myCustomFeature?: number;
170
+ }
171
+ }
172
+
173
+ export default wrapService({
174
+ name: 'my-service',
175
+ actions: {
176
+ myAction: {
177
+ myCustomFeature: 'not_a_number', // TS2322: Type string is not assignable to type number
178
+ },
179
+ },
180
+ });
181
+ ```
182
+
183
+ #### Moleculer channels example
184
+
185
+ For example, adding support for the [`@moleculer/channels`](https://github.com/moleculerjs/moleculer-channels) package
186
+ can be achieved like this:
187
+
188
+ ```ts
189
+ import { CustomActionSchema, InternalObjectServiceThis } from '@treatwell/moleculer-essentials';
190
+ import { Context } from 'moleculer';
191
+
192
+ declare module '@treatwell/moleculer-essentials' {
193
+ type DeadLetteringOptions = {
194
+ /**
195
+ * Enable dead-letter-queue
196
+ */
197
+ enabled: boolean;
198
+ /**
199
+ * Name of the dead-letter queue
200
+ */
201
+ queueName: string;
202
+ /**
203
+ * Name of the dead-letter exchange (only for AMQP adapter)
204
+ */
205
+ exchangeName: string;
206
+ /**
207
+ * Options for the dead-letter exchange (only for AMQP adapter)
208
+ */
209
+ exchangeOptions: unknown;
210
+ /**
211
+ * Options for the dead-letter queue (only for AMQP adapter)
212
+ */
213
+ queueOptions: unknown;
214
+ };
215
+
216
+ type MoleculerChannel = {
217
+ /**
218
+ * Channel/Queue/Stream name
219
+ * @default record name (with adapter prefix)
220
+ */
221
+ name?: string;
222
+ /**
223
+ * Consumer group
224
+ * @default Service name
225
+ */
226
+ group?: string;
227
+
228
+ /**
229
+ * Use moleculer context instead of direct payload.
230
+ * To have typing enabled, should always be true.
231
+ *
232
+ * @default uses Middleware `context` option (should be set to true)
233
+ */
234
+ context?: boolean;
235
+
236
+ /**
237
+ * Maximum number of messages that can be processed simultaneously
238
+ *
239
+ * @default adapter's maxInFlight
240
+ */
241
+ maxInFlight?: number | null;
242
+
243
+ /**
244
+ * Maximum number of retries before sending the message to dead-letter-queue.
245
+ *
246
+ * @default adapter's maxRetries (default: 3)
247
+ */
248
+ maxRetries?: number | null;
249
+
250
+ /**
251
+ * Dead-letter-queue options
252
+ *
253
+ * @default adapter's deadLettering (default: not enabled)
254
+ */
255
+ deadLettering?: DeadLetteringOptions | null;
256
+
257
+ /**
258
+ * Mandatory handler function (can be provided through mixins)
259
+ */
260
+ handler?: (ctx: Context<never, never>, raw: never) => Promise<unknown> | unknown;
261
+ };
262
+
263
+ export interface CustomServiceSchema<Settings, Methods, Mixins> {
264
+ channels?: Record<string, InternalObjectServiceThis<MoleculerChannel, Settings, Methods, Mixins>>;
265
+ }
266
+ }
267
+
268
+
269
+ export default wrapService({
270
+ name: 'my-service',
271
+ channels: {
272
+ 'payment.processed': {
273
+ group: "other",
274
+ async handler(ctx: Context<{...}>) {
275
+ ctx.logger.info('Processing payment', ctx.params);
276
+ },
277
+ },
278
+ },
279
+ });
280
+ ```
281
+
161
282
  ## License
162
283
 
163
284
  [MIT](https://choosealicense.com/licenses/mit/)
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "type": "git",
7
7
  "url": "https://github.com/treatwell/moleculer-essentials"
8
8
  },
9
- "version": "1.3.0-beta.1",
9
+ "version": "1.3.0",
10
10
  "main": "./dist/index.cjs",
11
11
  "module": "./dist/index.mjs",
12
12
  "types": "./dist/index.d.cts",