@grupodiariodaregiao/bunstone 0.5.3 → 0.5.4

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/dist/index.js CHANGED
@@ -43116,6 +43116,7 @@ function mapProvidersWithType(providers = [], type) {
43116
43116
  result.set(provider, []);
43117
43117
  }
43118
43118
  result.get(provider)?.push({
43119
+ name: method.name,
43119
43120
  expression: method.expression,
43120
43121
  delay: method.delay,
43121
43122
  methodName: method.methodName
@@ -100673,7 +100674,6 @@ function toPublicBucketPath(key) {
100673
100674
  return `/${normalizeS3Key(key)}`;
100674
100675
  }
100675
100676
  // lib/app-startup.ts
100676
- import { mkdir as mkdir2 } from "fs/promises";
100677
100677
  import { cors } from "@elysiajs/cors";
100678
100678
  import { html } from "@elysiajs/html";
100679
100679
  import jwt2 from "@elysiajs/jwt";
@@ -116663,6 +116663,7 @@ var ClientType;
116663
116663
  var import_node_cron = __toESM(require_node_cron(), 1);
116664
116664
  var import_reflect_metadata15 = __toESM(require_Reflect(), 1);
116665
116665
  import Elysia from "elysia";
116666
+ import { mkdir as mkdir2 } from "fs/promises";
116666
116667
  import React15 from "react";
116667
116668
  import { renderToReadableStream } from "react-dom/server";
116668
116669
 
@@ -116903,12 +116904,28 @@ class CommandBus {
116903
116904
  }
116904
116905
  async execute(command) {
116905
116906
  const commandType = command.constructor;
116906
- const handler = this.handlers.get(commandType);
116907
+ const handler = this.getHandler(commandType);
116907
116908
  if (!handler) {
116908
116909
  throw CqrsError.noCommandHandler(commandType.name);
116909
116910
  }
116910
116911
  return handler.execute(command);
116911
116912
  }
116913
+ getHandler(commandType) {
116914
+ const exactHandler = this.handlers.get(commandType);
116915
+ if (exactHandler) {
116916
+ return exactHandler;
116917
+ }
116918
+ if (typeof commandType !== "function" || !commandType.name) {
116919
+ return;
116920
+ }
116921
+ let matchedHandler;
116922
+ for (const [registeredType, handler] of this.handlers.entries()) {
116923
+ if (typeof registeredType === "function" && registeredType.name === commandType.name) {
116924
+ matchedHandler = handler;
116925
+ }
116926
+ }
116927
+ return matchedHandler;
116928
+ }
116912
116929
  }
116913
116930
  CommandBus = __legacyDecorateClassTS([
116914
116931
  Injectable()
@@ -116989,7 +117006,7 @@ class EventBus {
116989
117006
  }
116990
117007
  publish(event) {
116991
117008
  const eventType = event.constructor;
116992
- const handlers = this.handlers.get(eventType) || [];
117009
+ const handlers = this.getHandlers(eventType);
116993
117010
  handlers.forEach((handler) => {
116994
117011
  handler.handle(event);
116995
117012
  });
@@ -117002,6 +117019,26 @@ class EventBus {
117002
117019
  this.listeners.push(callback);
117003
117020
  });
117004
117021
  }
117022
+ getHandlers(eventType) {
117023
+ const exactHandlers = this.handlers.get(eventType);
117024
+ if (exactHandlers) {
117025
+ return exactHandlers;
117026
+ }
117027
+ if (typeof eventType !== "function" || !eventType.name) {
117028
+ return [];
117029
+ }
117030
+ const matchedHandlers = [];
117031
+ for (const [registeredType, handlers] of this.handlers.entries()) {
117032
+ if (typeof registeredType === "function" && registeredType.name === eventType.name) {
117033
+ for (const handler of handlers) {
117034
+ if (!matchedHandlers.includes(handler)) {
117035
+ matchedHandlers.push(handler);
117036
+ }
117037
+ }
117038
+ }
117039
+ }
117040
+ return matchedHandlers;
117041
+ }
117005
117042
  }
117006
117043
  EventBus = __legacyDecorateClassTS([
117007
117044
  Injectable()
@@ -117035,12 +117072,28 @@ class QueryBus {
117035
117072
  }
117036
117073
  async execute(query) {
117037
117074
  const queryType = query.constructor;
117038
- const handler = this.handlers.get(queryType);
117075
+ const handler = this.getHandler(queryType);
117039
117076
  if (!handler) {
117040
117077
  throw CqrsError.noQueryHandler(queryType.name);
117041
117078
  }
117042
117079
  return handler.execute(query);
117043
117080
  }
117081
+ getHandler(queryType) {
117082
+ const exactHandler = this.handlers.get(queryType);
117083
+ if (exactHandler) {
117084
+ return exactHandler;
117085
+ }
117086
+ if (typeof queryType !== "function" || !queryType.name) {
117087
+ return;
117088
+ }
117089
+ let matchedHandler;
117090
+ for (const [registeredType, handler] of this.handlers.entries()) {
117091
+ if (typeof registeredType === "function" && registeredType.name === queryType.name) {
117092
+ matchedHandler = handler;
117093
+ }
117094
+ }
117095
+ return matchedHandler;
117096
+ }
117044
117097
  }
117045
117098
  QueryBus = __legacyDecorateClassTS([
117046
117099
  Injectable()
@@ -117896,9 +117949,20 @@ class AppStartup {
117896
117949
  AppStartup.registerTimeouts(module);
117897
117950
  AppStartup.registerCronJobs(module);
117898
117951
  AppStartup.registerBullMqWorkers(module);
117899
- AppStartup.registerCqrsHandlers(module);
117900
117952
  AppStartup.registerRabbitMQConsumers(module);
117901
117953
  const modules = Reflect.getMetadata("dip:modules", module) || [];
117954
+ for (const mod of modules) {
117955
+ const modIsGlobal = Reflect.getMetadata("dip:module:global", mod);
117956
+ if (modIsGlobal) {
117957
+ const modInjectables = Reflect.getMetadata("dip:injectables", mod);
117958
+ if (modInjectables) {
117959
+ for (const [key, value] of modInjectables.entries()) {
117960
+ GlobalRegistry.register(key, value);
117961
+ }
117962
+ }
117963
+ }
117964
+ }
117965
+ AppStartup.registerCqrsHandlers(module);
117902
117966
  for (const mod of modules) {
117903
117967
  await AppStartup.registerModules(mod);
117904
117968
  }
@@ -118346,9 +118410,9 @@ class AppStartup {
118346
118410
  const injectables = Reflect.getMetadata("dip:injectables", module);
118347
118411
  if (!injectables)
118348
118412
  return;
118349
- const commandBus = injectables.get(CommandBus);
118350
- const queryBus = injectables.get(QueryBus);
118351
- const eventBus = injectables.get(EventBus);
118413
+ const commandBus = injectables.get(CommandBus) || GlobalRegistry.get(CommandBus);
118414
+ const queryBus = injectables.get(QueryBus) || GlobalRegistry.get(QueryBus);
118415
+ const eventBus = injectables.get(EventBus) || GlobalRegistry.get(EventBus);
118352
118416
  const commandHandlers = [];
118353
118417
  const queryHandlers = [];
118354
118418
  const eventHandlers = [];
@@ -16,4 +16,5 @@ export declare class CommandBus {
16
16
  * @throws CqrsError if no handler is found for the command.
17
17
  */
18
18
  execute<T extends ICommand, R = any>(command: T): Promise<R>;
19
+ private getHandler;
19
20
  }
@@ -25,6 +25,7 @@ export declare class EventBus {
25
25
  * Used primarily by Sagas.
26
26
  */
27
27
  get stream(): IEventStream;
28
+ private getHandlers;
28
29
  }
29
30
  /**
30
31
  * Filters events by type in a Saga stream.
@@ -16,4 +16,5 @@ export declare class QueryBus {
16
16
  * @throws CqrsError if no handler is found for the query.
17
17
  */
18
18
  execute<T extends IQuery, R = any>(query: T): Promise<R>;
19
+ private getHandler;
19
20
  }
@@ -1,6 +1,3 @@
1
- import { statSync } from "node:fs";
2
- import { mkdir, readdir } from "node:fs/promises";
3
- import { basename, extname, join, resolve } from "node:path";
4
1
  import { cors } from "@elysiajs/cors";
5
2
  import { html } from "@elysiajs/html";
6
3
  import jwt from "@elysiajs/jwt";
@@ -9,6 +6,7 @@ import { swagger } from "@elysiajs/swagger";
9
6
  import { type Job, Worker } from "bullmq";
10
7
  import Elysia from "elysia";
11
8
  import scheduler from "node-cron";
9
+ import { mkdir } from "node:fs/promises";
12
10
  import React from "react";
13
11
  import { renderToReadableStream } from "react-dom/server";
14
12
  import "reflect-metadata";
@@ -44,7 +42,6 @@ import { RateLimitService } from "./ratelimit/ratelimit.service";
44
42
  import { RENDER_METADATA } from "./render";
45
43
  import type { Options, RateLimitGlobalConfig } from "./types/options";
46
44
  import { Bundler } from "./utils/bundler";
47
- import { cwd } from "./utils/cwd";
48
45
  import {
49
46
  GlobalRegistry,
50
47
  resolveDependencies,
@@ -373,11 +370,29 @@ export class AppStartup {
373
370
  AppStartup.registerTimeouts(module);
374
371
  AppStartup.registerCronJobs(module);
375
372
  AppStartup.registerBullMqWorkers(module);
376
- AppStartup.registerCqrsHandlers(module);
377
373
  AppStartup.registerRabbitMQConsumers(module);
378
374
 
379
375
  const modules = Reflect.getMetadata("dip:modules", module) || [];
380
376
 
377
+ // Pre-register global sub-modules so their injectables (e.g. CqrsModule buses)
378
+ // are available in GlobalRegistry before CQRS handlers are registered
379
+ for (const mod of modules) {
380
+ const modIsGlobal = Reflect.getMetadata("dip:module:global", mod);
381
+ if (modIsGlobal) {
382
+ const modInjectables: Map<any, any> | undefined = Reflect.getMetadata(
383
+ "dip:injectables",
384
+ mod,
385
+ );
386
+ if (modInjectables) {
387
+ for (const [key, value] of modInjectables.entries()) {
388
+ GlobalRegistry.register(key, value);
389
+ }
390
+ }
391
+ }
392
+ }
393
+
394
+ AppStartup.registerCqrsHandlers(module);
395
+
381
396
  for (const mod of modules) {
382
397
  await AppStartup.registerModules(mod);
383
398
  }
@@ -1135,9 +1150,10 @@ export class AppStartup {
1135
1150
 
1136
1151
  if (!injectables) return;
1137
1152
 
1138
- const commandBus = injectables.get(CommandBus);
1139
- const queryBus = injectables.get(QueryBus);
1140
- const eventBus = injectables.get(EventBus);
1153
+ const commandBus =
1154
+ injectables.get(CommandBus) || GlobalRegistry.get(CommandBus);
1155
+ const queryBus = injectables.get(QueryBus) || GlobalRegistry.get(QueryBus);
1156
+ const eventBus = injectables.get(EventBus) || GlobalRegistry.get(EventBus);
1141
1157
 
1142
1158
  const commandHandlers: any[] = [];
1143
1159
  const queryHandlers: any[] = [];
@@ -34,10 +34,33 @@ export class CommandBus {
34
34
  */
35
35
  async execute<T extends ICommand, R = any>(command: T): Promise<R> {
36
36
  const commandType = command.constructor;
37
- const handler = this.handlers.get(commandType);
37
+ const handler = this.getHandler(commandType);
38
38
  if (!handler) {
39
39
  throw CqrsError.noCommandHandler(commandType.name);
40
40
  }
41
41
  return handler.execute(command);
42
42
  }
43
+
44
+ private getHandler(commandType: any): ICommandHandler | undefined {
45
+ const exactHandler = this.handlers.get(commandType);
46
+ if (exactHandler) {
47
+ return exactHandler;
48
+ }
49
+
50
+ if (typeof commandType !== "function" || !commandType.name) {
51
+ return undefined;
52
+ }
53
+
54
+ let matchedHandler: ICommandHandler | undefined;
55
+ for (const [registeredType, handler] of this.handlers.entries()) {
56
+ if (
57
+ typeof registeredType === "function" &&
58
+ registeredType.name === commandType.name
59
+ ) {
60
+ matchedHandler = handler;
61
+ }
62
+ }
63
+
64
+ return matchedHandler;
65
+ }
43
66
  }
@@ -69,7 +69,7 @@ export class EventBus {
69
69
  */
70
70
  publish<T extends IEvent>(event: T): void {
71
71
  const eventType = event.constructor;
72
- const handlers = this.handlers.get(eventType) || [];
72
+ const handlers = this.getHandlers(eventType);
73
73
  handlers.forEach((handler) => {
74
74
  handler.handle(event);
75
75
  });
@@ -89,6 +89,33 @@ export class EventBus {
89
89
  this.listeners.push(callback);
90
90
  });
91
91
  }
92
+
93
+ private getHandlers(eventType: any): IEventHandler[] {
94
+ const exactHandlers = this.handlers.get(eventType);
95
+ if (exactHandlers) {
96
+ return exactHandlers;
97
+ }
98
+
99
+ if (typeof eventType !== "function" || !eventType.name) {
100
+ return [];
101
+ }
102
+
103
+ const matchedHandlers: IEventHandler[] = [];
104
+ for (const [registeredType, handlers] of this.handlers.entries()) {
105
+ if (
106
+ typeof registeredType === "function" &&
107
+ registeredType.name === eventType.name
108
+ ) {
109
+ for (const handler of handlers) {
110
+ if (!matchedHandlers.includes(handler)) {
111
+ matchedHandlers.push(handler);
112
+ }
113
+ }
114
+ }
115
+ }
116
+
117
+ return matchedHandlers;
118
+ }
92
119
  }
93
120
 
94
121
  /**
@@ -34,10 +34,33 @@ export class QueryBus {
34
34
  */
35
35
  async execute<T extends IQuery, R = any>(query: T): Promise<R> {
36
36
  const queryType = query.constructor;
37
- const handler = this.handlers.get(queryType);
37
+ const handler = this.getHandler(queryType);
38
38
  if (!handler) {
39
39
  throw CqrsError.noQueryHandler(queryType.name);
40
40
  }
41
41
  return handler.execute(query);
42
42
  }
43
+
44
+ private getHandler(queryType: any): IQueryHandler | undefined {
45
+ const exactHandler = this.handlers.get(queryType);
46
+ if (exactHandler) {
47
+ return exactHandler;
48
+ }
49
+
50
+ if (typeof queryType !== "function" || !queryType.name) {
51
+ return undefined;
52
+ }
53
+
54
+ let matchedHandler: IQueryHandler | undefined;
55
+ for (const [registeredType, handler] of this.handlers.entries()) {
56
+ if (
57
+ typeof registeredType === "function" &&
58
+ registeredType.name === queryType.name
59
+ ) {
60
+ matchedHandler = handler;
61
+ }
62
+ }
63
+
64
+ return matchedHandler;
65
+ }
43
66
  }
@@ -14,7 +14,7 @@ export function mapProvidersWithType(
14
14
  ): Map<any, { expression?: string; delay?: number; methodName: string }[]> {
15
15
  const result = new Map<
16
16
  any,
17
- { expression?: string; delay?: number; methodName: string }[]
17
+ { name?: string; expression?: string; delay?: number; methodName: string }[]
18
18
  >();
19
19
 
20
20
  for (const provider of providers) {
@@ -30,6 +30,7 @@ export function mapProvidersWithType(
30
30
  }
31
31
 
32
32
  result.get(provider)?.push({
33
+ name: method.name,
33
34
  expression: method.expression,
34
35
  delay: method.delay,
35
36
  methodName: method.methodName,
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "types": "./dist/*.d.ts"
14
14
  }
15
15
  },
16
- "version": "0.5.3",
16
+ "version": "0.5.4",
17
17
  "homepage": "https://bunstone.diario.one/",
18
18
  "repository": {
19
19
  "url": "https://github.com/diariodaregiao/bunstone.git",