@artstesh/postboy 1.4.6 → 1.5.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.
package/lib/index.d.mts CHANGED
@@ -37,7 +37,6 @@ declare abstract class PostboyMessage {
37
37
  */
38
38
  declare abstract class PostboyGenericMessage extends PostboyMessage {
39
39
  }
40
- declare function checkId(message: new (...args: any[]) => any): string;
41
40
 
42
41
  declare abstract class PostboyExecutor<T> extends PostboyMessage {
43
42
  protected readonly _postboyResultType?: T;
@@ -205,43 +204,52 @@ declare abstract class PostboyAbstractRegistrator {
205
204
  recordSubject<T extends PostboyGenericMessage>(type: MessageType<T>): this;
206
205
  }
207
206
 
208
- interface PostboyMiddleware {
209
- handle(message: PostboyMessage): void;
207
+ declare enum MiddlewareStage {
208
+ Publish = 1,
209
+ Callback = 2,
210
+ Execute = 3
210
211
  }
211
212
 
212
- /**
213
- * A service class that manages middleware functions for processing PostboyMessage objects.
214
- * It provides methods to add, remove, and execute middlewares in sequence.
215
- */
216
- declare class PostboyMiddlewareService {
217
- protected middlewares: PostboyMiddleware[];
218
- /**
219
- * Adds a middleware function to the collection of middlewares.
220
- *
221
- * @param {PostboyMiddleware} middleware - The middleware function to be added.
222
- * @return {void}
223
- */
224
- addMiddleware(middleware: PostboyMiddleware): void;
225
- /**
226
- * Removes a middleware from the list of registered middlewares.
227
- *
228
- * @param {PostboyMiddleware} middleware - The middleware instance to be removed.
229
- * @return {void} No return value.
230
- */
231
- removeMiddleware(middleware: PostboyMiddleware): void;
232
- /**
233
- * Manages the processing of a PostboyMessage by sequentially applying all middlewares.
234
- *
235
- * @param {PostboyMessage} msg - The message object to be processed by the middlewares.
236
- * @return {void} - This method does not return any value.
237
- */
238
- manage(msg: PostboyMessage): void;
239
- /**
240
- * Disposes of the current object by clearing its middlewares array.
241
- *
242
- * @return {void} This method does not return a value.
243
- */
244
- dispose(): void;
213
+ interface PipelineContext<T extends PostboyMessage = PostboyMessage> {
214
+ stage: MiddlewareStage;
215
+ message: T;
216
+ }
217
+
218
+ declare enum MiddlewareDecisionType {
219
+ Continue = 1,
220
+ Interrupt = 2
221
+ }
222
+
223
+ interface MiddlewareDecision {
224
+ type: MiddlewareDecisionType;
225
+ }
226
+
227
+ interface CancelDetails {
228
+ stage: MiddlewareStage;
229
+ reason?: string;
230
+ middleware?: string;
231
+ messageId?: string;
232
+ namespace?: string;
233
+ }
234
+
235
+ declare class CancelError extends Error {
236
+ readonly details: CancelDetails;
237
+ constructor(details: CancelDetails);
238
+ }
239
+
240
+ type PipelineResult = {
241
+ cancelled: boolean;
242
+ cancelledBy?: string;
243
+ reason?: string;
244
+ };
245
+
246
+ interface PostboyMessageContext {
247
+ correlationId: string;
248
+ currentMessageId: string;
249
+ parentMessageId?: string;
250
+ depth: number;
251
+ startedAt: Date;
252
+ tags?: Set<string>;
245
253
  }
246
254
 
247
255
  /**
@@ -282,6 +290,56 @@ declare class PostboySubscription<T> {
282
290
  finish(): void;
283
291
  }
284
292
 
293
+ /**
294
+ * Abstract base class for defining middleware in a pipeline.
295
+ * Middleware acts on various stages of pipeline execution, allowing operations
296
+ * to be intercepted, modified, or monitored.
297
+ */
298
+ declare abstract class PostboyMiddleware {
299
+ readonly name: string;
300
+ /**
301
+ * Creates an instance of a class, optionally assigning a name.
302
+ *
303
+ * @param {string} [name] - An optional name to be assigned. If not provided, defaults to the class name.
304
+ */
305
+ constructor(name?: string);
306
+ /**
307
+ * Optional filter to skip middleware for unrelated messages/stages.
308
+ */
309
+ canHandle(_context: PipelineContext): boolean;
310
+ /**
311
+ * Called before the stage is executed.
312
+ * Return Interrupt to cancel the operation.
313
+ */
314
+ before(_context: PipelineContext): MiddlewareDecision;
315
+ /**
316
+ * Called after the stage has finished successfully.
317
+ * `result` is typically set for execute-stage.
318
+ */
319
+ after(_context: PipelineContext, _result?: unknown): void;
320
+ /**
321
+ * Optional cleanup hook.
322
+ */
323
+ dispose(): void;
324
+ }
325
+
326
+ declare class PostboyMiddlewareService {
327
+ protected middlewares: PostboyMiddleware[];
328
+ addMiddleware(middleware: PostboyMiddleware): void;
329
+ removeMiddleware(middleware: PostboyMiddleware): void;
330
+ dispose(): void;
331
+ before<T extends PostboyMessage>(stage: MiddlewareStage, message: T): void;
332
+ after<T extends PostboyMessage, R = unknown>(stage: MiddlewareStage, message: T, result?: R): void;
333
+ beforePublish(message: PostboyMessage): void;
334
+ afterPublish(message: PostboyMessage): void;
335
+ beforeCallback(message: PostboyMessage): void;
336
+ afterCallback(message: PostboyMessage, result?: unknown): void;
337
+ beforeExecute<T>(message: PostboyExecutor<T>): void;
338
+ afterExecute<T>(message: PostboyExecutor<T>, result: T): void;
339
+ private buildContext;
340
+ private throwIfCancelled;
341
+ }
342
+
285
343
  /**
286
344
  * The PostboyMessageStore is a utility class for managing message subscriptions and executors.
287
345
  * It provides functionality to register, retrieve, and unregister subscription-based messages and executors.
@@ -368,46 +426,6 @@ declare class PostboyService {
368
426
  private dependencyResolver;
369
427
  constructor(resolver?: PostboyDependencyResolver);
370
428
  private registerInfrastructureMessages;
371
- /**
372
- * Locks a specific message type to prevent further modifications or actions.
373
- *
374
- * @deprecated The method should be replaced with firing {@link LockMessage} message.
375
- * @param {MessageType<T>} type - The message type to be locked. It must extend from the `PostboyGenericMessage`.
376
- * @return {void} This method does not return a value.
377
- */
378
- lock<T extends PostboyGenericMessage>(type: MessageType<T>): void;
379
- /**
380
- * Unlocks a previously locked message type by removing its ID from the locked set.
381
- *
382
- * @deprecated The method should be replaced with firing {@link UnlockMessage} message.
383
- * @param {MessageType<T>} type - The message type to unlock, which is a generic type extending PostboyGenericMessage.
384
- * @return {void} This method does not return any value.
385
- */
386
- unlock<T extends PostboyGenericMessage>(type: MessageType<T>): void;
387
- /**
388
- * Adds a middleware to the middleware stack.
389
- *
390
- * @deprecated The method should be replaced with firing {@link AddMiddleware} message.
391
- * @param {PostboyMiddleware} middleware - The middleware instance to be added.
392
- * @return {void}
393
- */
394
- addMiddleware(middleware: PostboyMiddleware): void;
395
- /**
396
- * Removes a middleware from the current middleware stack.
397
- *
398
- * @deprecated The method should be replaced with firing {@link RemoveMiddleware} message.
399
- * @param {PostboyMiddleware} middleware - The middleware instance to be removed.
400
- * @return {void} No return value.
401
- */
402
- removeMiddleware(middleware: PostboyMiddleware): void;
403
- /**
404
- * Unregisters a message identified by the given ID from the store.
405
- *
406
- * @deprecated The method should be replaced with firing {@link DisconnectMessage} message.
407
- * @param {string} id - The unique identifier of the item to unregister.
408
- * @return {void} No value is returned.
409
- */
410
- unregister(id: string): void;
411
429
  /**
412
430
  * Fires a registered event and passes the message to its subscribers.
413
431
  *
@@ -484,22 +502,6 @@ declare class PostboyService {
484
502
  * @return void
485
503
  */
486
504
  recordHandler<E extends PostboyExecutor<R>, R>(executor: new (...args: any[]) => E, handler: PostboyExecutionHandler<R, E>): void;
487
- /**
488
- * Adds a namespace to the namespace store.
489
- *
490
- * @deprecated The method should be replaced with firing {@link AddNamespace} message.
491
- * @param {string} space - The name of the namespace to be added.
492
- * @return {PostboyAbstractRegistrator} The instance of the namespace after adding the specified namespace.
493
- */
494
- addNamespace(space: string): PostboyAbstractRegistrator;
495
- /**
496
- * Removes the specified namespace from the namespace store.
497
- *
498
- * @deprecated The method should be replaced with firing {@link EliminateNamespace} message.
499
- * @param {string} space - The name of the namespace to be removed.
500
- * @return {void} This method does not return a value.
501
- */
502
- eliminateNamespace(space: string): void;
503
505
  /**
504
506
  * Disposes of resources and cleans up any internal components or stores associated with the instance.
505
507
  * This method ensures that all resources are properly released to avoid memory leaks.
@@ -698,4 +700,4 @@ declare class ConnectMessage<T extends PostboyGenericMessage> extends PostboyExe
698
700
  constructor(type: MessageType<T>, sub: Subject<T>, pipe?: ((s: Subject<T>) => Observable<T>) | undefined);
699
701
  }
700
702
 
701
- export { AddMiddleware, AddNamespace, ConnectExecutor, ConnectHandler, ConnectMessage, DisconnectMessage, EliminateNamespace, type IPostboyDependingService, LockMessage, type MessageType, PostboyAbstractRegistrator, PostboyCallbackMessage, PostboyExecutionHandler, PostboyExecutor, PostboyGenericMessage, PostboyMessage, PostboyMessageStore, type PostboyMiddleware, PostboyMiddlewareService, PostboyNamespaceStore, PostboyService, PostboySubscription, RemoveMiddleware, UnlockMessage, checkId };
703
+ export { AddMiddleware, AddNamespace, type CancelDetails, CancelError, ConnectExecutor, ConnectHandler, ConnectMessage, DisconnectMessage, EliminateNamespace, type IPostboyDependingService, LockMessage, type MessageType, type MiddlewareDecision, MiddlewareDecisionType, MiddlewareStage, type PipelineContext, type PipelineResult, PostboyAbstractRegistrator, PostboyCallbackMessage, PostboyExecutionHandler, PostboyExecutor, PostboyGenericMessage, PostboyMessage, type PostboyMessageContext, type PostboyMessageMetadata, PostboyMessageStore, PostboyMiddleware, PostboyMiddlewareService, PostboyNamespaceStore, PostboyService, PostboySubscription, RemoveMiddleware, UnlockMessage };
package/lib/index.d.ts CHANGED
@@ -37,7 +37,6 @@ declare abstract class PostboyMessage {
37
37
  */
38
38
  declare abstract class PostboyGenericMessage extends PostboyMessage {
39
39
  }
40
- declare function checkId(message: new (...args: any[]) => any): string;
41
40
 
42
41
  declare abstract class PostboyExecutor<T> extends PostboyMessage {
43
42
  protected readonly _postboyResultType?: T;
@@ -205,43 +204,52 @@ declare abstract class PostboyAbstractRegistrator {
205
204
  recordSubject<T extends PostboyGenericMessage>(type: MessageType<T>): this;
206
205
  }
207
206
 
208
- interface PostboyMiddleware {
209
- handle(message: PostboyMessage): void;
207
+ declare enum MiddlewareStage {
208
+ Publish = 1,
209
+ Callback = 2,
210
+ Execute = 3
210
211
  }
211
212
 
212
- /**
213
- * A service class that manages middleware functions for processing PostboyMessage objects.
214
- * It provides methods to add, remove, and execute middlewares in sequence.
215
- */
216
- declare class PostboyMiddlewareService {
217
- protected middlewares: PostboyMiddleware[];
218
- /**
219
- * Adds a middleware function to the collection of middlewares.
220
- *
221
- * @param {PostboyMiddleware} middleware - The middleware function to be added.
222
- * @return {void}
223
- */
224
- addMiddleware(middleware: PostboyMiddleware): void;
225
- /**
226
- * Removes a middleware from the list of registered middlewares.
227
- *
228
- * @param {PostboyMiddleware} middleware - The middleware instance to be removed.
229
- * @return {void} No return value.
230
- */
231
- removeMiddleware(middleware: PostboyMiddleware): void;
232
- /**
233
- * Manages the processing of a PostboyMessage by sequentially applying all middlewares.
234
- *
235
- * @param {PostboyMessage} msg - The message object to be processed by the middlewares.
236
- * @return {void} - This method does not return any value.
237
- */
238
- manage(msg: PostboyMessage): void;
239
- /**
240
- * Disposes of the current object by clearing its middlewares array.
241
- *
242
- * @return {void} This method does not return a value.
243
- */
244
- dispose(): void;
213
+ interface PipelineContext<T extends PostboyMessage = PostboyMessage> {
214
+ stage: MiddlewareStage;
215
+ message: T;
216
+ }
217
+
218
+ declare enum MiddlewareDecisionType {
219
+ Continue = 1,
220
+ Interrupt = 2
221
+ }
222
+
223
+ interface MiddlewareDecision {
224
+ type: MiddlewareDecisionType;
225
+ }
226
+
227
+ interface CancelDetails {
228
+ stage: MiddlewareStage;
229
+ reason?: string;
230
+ middleware?: string;
231
+ messageId?: string;
232
+ namespace?: string;
233
+ }
234
+
235
+ declare class CancelError extends Error {
236
+ readonly details: CancelDetails;
237
+ constructor(details: CancelDetails);
238
+ }
239
+
240
+ type PipelineResult = {
241
+ cancelled: boolean;
242
+ cancelledBy?: string;
243
+ reason?: string;
244
+ };
245
+
246
+ interface PostboyMessageContext {
247
+ correlationId: string;
248
+ currentMessageId: string;
249
+ parentMessageId?: string;
250
+ depth: number;
251
+ startedAt: Date;
252
+ tags?: Set<string>;
245
253
  }
246
254
 
247
255
  /**
@@ -282,6 +290,56 @@ declare class PostboySubscription<T> {
282
290
  finish(): void;
283
291
  }
284
292
 
293
+ /**
294
+ * Abstract base class for defining middleware in a pipeline.
295
+ * Middleware acts on various stages of pipeline execution, allowing operations
296
+ * to be intercepted, modified, or monitored.
297
+ */
298
+ declare abstract class PostboyMiddleware {
299
+ readonly name: string;
300
+ /**
301
+ * Creates an instance of a class, optionally assigning a name.
302
+ *
303
+ * @param {string} [name] - An optional name to be assigned. If not provided, defaults to the class name.
304
+ */
305
+ constructor(name?: string);
306
+ /**
307
+ * Optional filter to skip middleware for unrelated messages/stages.
308
+ */
309
+ canHandle(_context: PipelineContext): boolean;
310
+ /**
311
+ * Called before the stage is executed.
312
+ * Return Interrupt to cancel the operation.
313
+ */
314
+ before(_context: PipelineContext): MiddlewareDecision;
315
+ /**
316
+ * Called after the stage has finished successfully.
317
+ * `result` is typically set for execute-stage.
318
+ */
319
+ after(_context: PipelineContext, _result?: unknown): void;
320
+ /**
321
+ * Optional cleanup hook.
322
+ */
323
+ dispose(): void;
324
+ }
325
+
326
+ declare class PostboyMiddlewareService {
327
+ protected middlewares: PostboyMiddleware[];
328
+ addMiddleware(middleware: PostboyMiddleware): void;
329
+ removeMiddleware(middleware: PostboyMiddleware): void;
330
+ dispose(): void;
331
+ before<T extends PostboyMessage>(stage: MiddlewareStage, message: T): void;
332
+ after<T extends PostboyMessage, R = unknown>(stage: MiddlewareStage, message: T, result?: R): void;
333
+ beforePublish(message: PostboyMessage): void;
334
+ afterPublish(message: PostboyMessage): void;
335
+ beforeCallback(message: PostboyMessage): void;
336
+ afterCallback(message: PostboyMessage, result?: unknown): void;
337
+ beforeExecute<T>(message: PostboyExecutor<T>): void;
338
+ afterExecute<T>(message: PostboyExecutor<T>, result: T): void;
339
+ private buildContext;
340
+ private throwIfCancelled;
341
+ }
342
+
285
343
  /**
286
344
  * The PostboyMessageStore is a utility class for managing message subscriptions and executors.
287
345
  * It provides functionality to register, retrieve, and unregister subscription-based messages and executors.
@@ -368,46 +426,6 @@ declare class PostboyService {
368
426
  private dependencyResolver;
369
427
  constructor(resolver?: PostboyDependencyResolver);
370
428
  private registerInfrastructureMessages;
371
- /**
372
- * Locks a specific message type to prevent further modifications or actions.
373
- *
374
- * @deprecated The method should be replaced with firing {@link LockMessage} message.
375
- * @param {MessageType<T>} type - The message type to be locked. It must extend from the `PostboyGenericMessage`.
376
- * @return {void} This method does not return a value.
377
- */
378
- lock<T extends PostboyGenericMessage>(type: MessageType<T>): void;
379
- /**
380
- * Unlocks a previously locked message type by removing its ID from the locked set.
381
- *
382
- * @deprecated The method should be replaced with firing {@link UnlockMessage} message.
383
- * @param {MessageType<T>} type - The message type to unlock, which is a generic type extending PostboyGenericMessage.
384
- * @return {void} This method does not return any value.
385
- */
386
- unlock<T extends PostboyGenericMessage>(type: MessageType<T>): void;
387
- /**
388
- * Adds a middleware to the middleware stack.
389
- *
390
- * @deprecated The method should be replaced with firing {@link AddMiddleware} message.
391
- * @param {PostboyMiddleware} middleware - The middleware instance to be added.
392
- * @return {void}
393
- */
394
- addMiddleware(middleware: PostboyMiddleware): void;
395
- /**
396
- * Removes a middleware from the current middleware stack.
397
- *
398
- * @deprecated The method should be replaced with firing {@link RemoveMiddleware} message.
399
- * @param {PostboyMiddleware} middleware - The middleware instance to be removed.
400
- * @return {void} No return value.
401
- */
402
- removeMiddleware(middleware: PostboyMiddleware): void;
403
- /**
404
- * Unregisters a message identified by the given ID from the store.
405
- *
406
- * @deprecated The method should be replaced with firing {@link DisconnectMessage} message.
407
- * @param {string} id - The unique identifier of the item to unregister.
408
- * @return {void} No value is returned.
409
- */
410
- unregister(id: string): void;
411
429
  /**
412
430
  * Fires a registered event and passes the message to its subscribers.
413
431
  *
@@ -484,22 +502,6 @@ declare class PostboyService {
484
502
  * @return void
485
503
  */
486
504
  recordHandler<E extends PostboyExecutor<R>, R>(executor: new (...args: any[]) => E, handler: PostboyExecutionHandler<R, E>): void;
487
- /**
488
- * Adds a namespace to the namespace store.
489
- *
490
- * @deprecated The method should be replaced with firing {@link AddNamespace} message.
491
- * @param {string} space - The name of the namespace to be added.
492
- * @return {PostboyAbstractRegistrator} The instance of the namespace after adding the specified namespace.
493
- */
494
- addNamespace(space: string): PostboyAbstractRegistrator;
495
- /**
496
- * Removes the specified namespace from the namespace store.
497
- *
498
- * @deprecated The method should be replaced with firing {@link EliminateNamespace} message.
499
- * @param {string} space - The name of the namespace to be removed.
500
- * @return {void} This method does not return a value.
501
- */
502
- eliminateNamespace(space: string): void;
503
505
  /**
504
506
  * Disposes of resources and cleans up any internal components or stores associated with the instance.
505
507
  * This method ensures that all resources are properly released to avoid memory leaks.
@@ -698,4 +700,4 @@ declare class ConnectMessage<T extends PostboyGenericMessage> extends PostboyExe
698
700
  constructor(type: MessageType<T>, sub: Subject<T>, pipe?: ((s: Subject<T>) => Observable<T>) | undefined);
699
701
  }
700
702
 
701
- export { AddMiddleware, AddNamespace, ConnectExecutor, ConnectHandler, ConnectMessage, DisconnectMessage, EliminateNamespace, type IPostboyDependingService, LockMessage, type MessageType, PostboyAbstractRegistrator, PostboyCallbackMessage, PostboyExecutionHandler, PostboyExecutor, PostboyGenericMessage, PostboyMessage, PostboyMessageStore, type PostboyMiddleware, PostboyMiddlewareService, PostboyNamespaceStore, PostboyService, PostboySubscription, RemoveMiddleware, UnlockMessage, checkId };
703
+ export { AddMiddleware, AddNamespace, type CancelDetails, CancelError, ConnectExecutor, ConnectHandler, ConnectMessage, DisconnectMessage, EliminateNamespace, type IPostboyDependingService, LockMessage, type MessageType, type MiddlewareDecision, MiddlewareDecisionType, MiddlewareStage, type PipelineContext, type PipelineResult, PostboyAbstractRegistrator, PostboyCallbackMessage, PostboyExecutionHandler, PostboyExecutor, PostboyGenericMessage, PostboyMessage, type PostboyMessageContext, type PostboyMessageMetadata, PostboyMessageStore, PostboyMiddleware, PostboyMiddlewareService, PostboyNamespaceStore, PostboyService, PostboySubscription, RemoveMiddleware, UnlockMessage };