@flusys/nestjs-shared 7.1.0 → 8.0.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 (338) hide show
  1. package/classes/api-controller.class.d.ts +4 -4
  2. package/classes/api-service.class.d.ts +7 -7
  3. package/classes/disabled-event-bus.class.d.ts +1 -1
  4. package/classes/domain-event.publisher.d.ts +1 -1
  5. package/classes/event-bus.registry.d.ts +1 -1
  6. package/classes/event-transports/event-transport.registry.d.ts +1 -1
  7. package/classes/event-transports/index.d.ts +2 -2
  8. package/classes/event-transports/kafka-transport.optional.d.ts +1 -1
  9. package/classes/event-transports/memory-transport.d.ts +1 -1
  10. package/classes/event-transports/rabbitmq-transport.optional.d.ts +1 -1
  11. package/classes/hybrid-event-bus.class.d.ts +1 -1
  12. package/classes/index.d.ts +11 -11
  13. package/constants/index.d.ts +2 -2
  14. package/decorators/api-response.decorator.d.ts +1 -1
  15. package/decorators/current-user.decorator.d.ts +1 -1
  16. package/decorators/index.d.ts +6 -6
  17. package/decorators/require-permission.decorator.d.ts +1 -1
  18. package/dtos/filter-and-pagination.dto.d.ts +1 -1
  19. package/dtos/index.d.ts +6 -6
  20. package/entities/index.d.ts +3 -3
  21. package/enums/index.d.ts +3 -3
  22. package/exceptions/index.d.ts +2 -2
  23. package/fesm/470.js +2713 -0
  24. package/fesm/509.js +793 -0
  25. package/fesm/667.js +606 -0
  26. package/fesm/67.js +808 -0
  27. package/fesm/720.js +914 -0
  28. package/{cjs/classes/event-transports/rabbitmq-transport.optional.js → fesm/chunks/123.js} +78 -55
  29. package/fesm/{classes/event-transports/kafka-transport.optional.js → chunks/37.js} +74 -41
  30. package/fesm/classes/index.js +62 -12
  31. package/fesm/constants/index.js +445 -17
  32. package/fesm/decorators/index.js +341 -6
  33. package/fesm/dtos/index.js +23 -6
  34. package/fesm/entities/index.js +336 -3
  35. package/fesm/enums/index.js +57 -3
  36. package/fesm/exceptions/index.js +280 -2
  37. package/fesm/guards/index.js +1003 -3
  38. package/fesm/index.js +1524 -14
  39. package/fesm/interceptors/index.js +924 -5
  40. package/fesm/interfaces/index.js +57 -11
  41. package/fesm/middlewares/index.js +33 -1
  42. package/fesm/modules/index.js +432 -9
  43. package/fesm/runtime.js +134 -0
  44. package/fesm/utils/index.js +1045 -8
  45. package/filters/global-exception.filter.d.ts +1 -1
  46. package/filters/index.d.ts +1 -1
  47. package/guards/index.d.ts +3 -3
  48. package/guards/jwt-auth.guard.d.ts +1 -1
  49. package/guards/optional-jwt-auth.guard.d.ts +1 -1
  50. package/guards/permission.guard.d.ts +2 -2
  51. package/index.d.ts +14 -14
  52. package/interceptors/delete-empty-id-from-body.interceptor.d.ts +1 -1
  53. package/interceptors/idempotency.interceptor.d.ts +2 -2
  54. package/interceptors/index.d.ts +5 -5
  55. package/interceptors/response-meta.interceptor.d.ts +1 -1
  56. package/interceptors/set-user-field-on-body.interceptor.d.ts +1 -1
  57. package/interceptors/slug.interceptor.d.ts +2 -2
  58. package/interfaces/ai-assistant-adapter.interface.d.ts +1 -1
  59. package/interfaces/api.interface.d.ts +2 -2
  60. package/interfaces/datasource.interface.d.ts +1 -1
  61. package/interfaces/event-manager-adapter.interface.d.ts +2 -2
  62. package/interfaces/index.d.ts +11 -11
  63. package/interfaces/module-config.interface.d.ts +1 -1
  64. package/interfaces/notification-adapter.interface.d.ts +1 -1
  65. package/middlewares/index.d.ts +1 -1
  66. package/middlewares/logger.middleware.d.ts +2 -2
  67. package/modules/cache/cache.module.d.ts +1 -1
  68. package/modules/datasource/index.d.ts +1 -1
  69. package/modules/datasource/multi-tenant-datasource.service.d.ts +4 -4
  70. package/modules/event-bus/event-bus.explorer.d.ts +2 -2
  71. package/modules/event-bus/event-bus.module.d.ts +2 -2
  72. package/modules/event-bus/module-events.provider.d.ts +1 -1
  73. package/modules/index.d.ts +9 -9
  74. package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -3
  75. package/modules/utils/utils.service.d.ts +1 -1
  76. package/package.json +30 -44
  77. package/utils/index.d.ts +8 -8
  78. package/utils/permission-cache-key.util.d.ts +1 -1
  79. package/utils/query-helpers.util.d.ts +2 -2
  80. package/utils/request.util.d.ts +1 -1
  81. package/cjs/classes/api-controller.class.js +0 -728
  82. package/cjs/classes/api-controller.class.spec.js +0 -684
  83. package/cjs/classes/api-service.class.js +0 -631
  84. package/cjs/classes/api-service.class.spec.js +0 -1137
  85. package/cjs/classes/disabled-event-bus.class.js +0 -15
  86. package/cjs/classes/disabled-event-bus.class.spec.js +0 -40
  87. package/cjs/classes/domain-event.publisher.js +0 -76
  88. package/cjs/classes/domain-event.publisher.spec.js +0 -234
  89. package/cjs/classes/event-bus.registry.js +0 -68
  90. package/cjs/classes/event-bus.registry.spec.js +0 -141
  91. package/cjs/classes/event-transports/event-transport.registry.js +0 -41
  92. package/cjs/classes/event-transports/event-transport.registry.spec.js +0 -31
  93. package/cjs/classes/event-transports/index.js +0 -23
  94. package/cjs/classes/event-transports/kafka-transport.optional.js +0 -151
  95. package/cjs/classes/event-transports/kafka-transport.optional.spec.js +0 -198
  96. package/cjs/classes/event-transports/memory-transport.js +0 -53
  97. package/cjs/classes/event-transports/memory-transport.spec.js +0 -57
  98. package/cjs/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -255
  99. package/cjs/classes/handle-custome-db.js +0 -37
  100. package/cjs/classes/handle-custome-db.spec.js +0 -82
  101. package/cjs/classes/hybrid-cache.class.js +0 -96
  102. package/cjs/classes/hybrid-cache.class.spec.js +0 -168
  103. package/cjs/classes/hybrid-event-bus.class.js +0 -281
  104. package/cjs/classes/hybrid-event-bus.class.spec.js +0 -218
  105. package/cjs/classes/index.js +0 -28
  106. package/cjs/classes/winston.logger.class.js +0 -274
  107. package/cjs/classes/winston.logger.class.spec.js +0 -330
  108. package/cjs/constants/index.js +0 -88
  109. package/cjs/constants/message-keys.js +0 -68
  110. package/cjs/constants/permissions.js +0 -307
  111. package/cjs/decorators/api-response.decorator.js +0 -52
  112. package/cjs/decorators/api-response.decorator.spec.js +0 -126
  113. package/cjs/decorators/current-user.decorator.js +0 -16
  114. package/cjs/decorators/current-user.decorator.spec.js +0 -74
  115. package/cjs/decorators/index.js +0 -23
  116. package/cjs/decorators/log-action.decorator.js +0 -149
  117. package/cjs/decorators/log-action.decorator.spec.js +0 -353
  118. package/cjs/decorators/on-domain-event.decorator.js +0 -13
  119. package/cjs/decorators/on-domain-event.decorator.spec.js +0 -50
  120. package/cjs/decorators/public.decorator.js +0 -13
  121. package/cjs/decorators/public.decorator.spec.js +0 -50
  122. package/cjs/decorators/require-permission.decorator.js +0 -32
  123. package/cjs/decorators/require-permission.decorator.spec.js +0 -147
  124. package/cjs/dtos/delete.dto.js +0 -92
  125. package/cjs/dtos/filter-and-pagination.dto.js +0 -164
  126. package/cjs/dtos/get-by-ids.dto.js +0 -65
  127. package/cjs/dtos/identity-response.dto.js +0 -108
  128. package/cjs/dtos/index.js +0 -23
  129. package/cjs/dtos/pagination.dto.js +0 -67
  130. package/cjs/dtos/response-payload.dto.js +0 -369
  131. package/cjs/entities/identity.js +0 -100
  132. package/cjs/entities/index.js +0 -20
  133. package/cjs/entities/raw-type.js +0 -4
  134. package/cjs/entities/user-root.js +0 -189
  135. package/cjs/enums/index.js +0 -20
  136. package/cjs/enums/notification-type.enum.js +0 -17
  137. package/cjs/enums/participant-status.enum.js +0 -17
  138. package/cjs/enums/recurrence-type.enum.js +0 -18
  139. package/cjs/exceptions/base-app.exception.js +0 -151
  140. package/cjs/exceptions/base-app.exception.spec.js +0 -157
  141. package/cjs/exceptions/index.js +0 -19
  142. package/cjs/exceptions/permission.exception.js +0 -56
  143. package/cjs/exceptions/permission.exception.spec.js +0 -82
  144. package/cjs/filters/global-exception.filter.js +0 -279
  145. package/cjs/filters/global-exception.filter.spec.js +0 -440
  146. package/cjs/filters/index.js +0 -18
  147. package/cjs/guards/index.js +0 -20
  148. package/cjs/guards/jwt-auth.guard.js +0 -75
  149. package/cjs/guards/jwt-auth.guard.spec.js +0 -75
  150. package/cjs/guards/optional-jwt-auth.guard.js +0 -67
  151. package/cjs/guards/optional-jwt-auth.guard.spec.js +0 -55
  152. package/cjs/guards/permission.guard.js +0 -179
  153. package/cjs/guards/permission.guard.spec.js +0 -373
  154. package/cjs/index.js +0 -31
  155. package/cjs/interceptors/delete-empty-id-from-body.interceptor.js +0 -40
  156. package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -107
  157. package/cjs/interceptors/idempotency.interceptor.js +0 -96
  158. package/cjs/interceptors/idempotency.interceptor.spec.js +0 -116
  159. package/cjs/interceptors/index.js +0 -22
  160. package/cjs/interceptors/response-meta.interceptor.js +0 -47
  161. package/cjs/interceptors/response-meta.interceptor.spec.js +0 -89
  162. package/cjs/interceptors/set-user-field-on-body.interceptor.js +0 -60
  163. package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +0 -128
  164. package/cjs/interceptors/slug.interceptor.js +0 -75
  165. package/cjs/interceptors/slug.interceptor.spec.js +0 -110
  166. package/cjs/interfaces/ai-assistant-adapter.interface.js +0 -24
  167. package/cjs/interfaces/api.interface.js +0 -4
  168. package/cjs/interfaces/datasource.interface.js +0 -4
  169. package/cjs/interfaces/event-bus.interface.js +0 -4
  170. package/cjs/interfaces/event-manager-adapter.interface.js +0 -11
  171. package/cjs/interfaces/identity.interface.js +0 -4
  172. package/cjs/interfaces/index.js +0 -28
  173. package/cjs/interfaces/logged-user-info.interface.js +0 -6
  174. package/cjs/interfaces/module-config.interface.js +0 -4
  175. package/cjs/interfaces/notification-adapter.interface.js +0 -11
  176. package/cjs/interfaces/permission-sync-adapter.interface.js +0 -11
  177. package/cjs/interfaces/permission.interface.js +0 -4
  178. package/cjs/middlewares/index.js +0 -18
  179. package/cjs/middlewares/logger.middleware.js +0 -264
  180. package/cjs/middlewares/logger.middleware.spec.js +0 -462
  181. package/cjs/modules/cache/cache.module.js +0 -39
  182. package/cjs/modules/cache/cache.module.spec.js +0 -48
  183. package/cjs/modules/datasource/index.js +0 -18
  184. package/cjs/modules/datasource/multi-tenant-datasource.service.js +0 -222
  185. package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +0 -381
  186. package/cjs/modules/event-bus/event-bus.explorer.js +0 -124
  187. package/cjs/modules/event-bus/event-bus.explorer.spec.js +0 -189
  188. package/cjs/modules/event-bus/event-bus.integration.spec.js +0 -253
  189. package/cjs/modules/event-bus/event-bus.module.js +0 -94
  190. package/cjs/modules/event-bus/event-bus.module.spec.js +0 -149
  191. package/cjs/modules/event-bus/module-events.provider.js +0 -39
  192. package/cjs/modules/event-bus/module-events.provider.spec.js +0 -56
  193. package/cjs/modules/index.js +0 -26
  194. package/cjs/modules/shared-permission-cache/shared-permission-cache.module.js +0 -32
  195. package/cjs/modules/shared-permission-cache/shared-permission-cache.service.js +0 -101
  196. package/cjs/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -165
  197. package/cjs/modules/utils/utils.module.js +0 -32
  198. package/cjs/modules/utils/utils.service.js +0 -76
  199. package/cjs/modules/utils/utils.service.spec.js +0 -169
  200. package/cjs/utils/date-time.util.js +0 -94
  201. package/cjs/utils/date-time.util.spec.js +0 -84
  202. package/cjs/utils/event-pattern.util.js +0 -103
  203. package/cjs/utils/event-pattern.util.spec.js +0 -251
  204. package/cjs/utils/html-sanitizer.util.js +0 -64
  205. package/cjs/utils/html-sanitizer.util.spec.js +0 -76
  206. package/cjs/utils/index.js +0 -25
  207. package/cjs/utils/optional-module.util.js +0 -35
  208. package/cjs/utils/optional-module.util.spec.js +0 -14
  209. package/cjs/utils/permission-cache-key.util.js +0 -31
  210. package/cjs/utils/permission-cache-key.util.spec.js +0 -88
  211. package/cjs/utils/query-helpers.util.js +0 -60
  212. package/cjs/utils/query-helpers.util.spec.js +0 -209
  213. package/cjs/utils/request.util.js +0 -96
  214. package/cjs/utils/request.util.spec.js +0 -249
  215. package/cjs/utils/string.util.js +0 -63
  216. package/cjs/utils/string.util.spec.js +0 -63
  217. package/fesm/classes/api-controller.class.js +0 -718
  218. package/fesm/classes/api-controller.class.spec.js +0 -680
  219. package/fesm/classes/api-service.class.js +0 -621
  220. package/fesm/classes/api-service.class.spec.js +0 -1133
  221. package/fesm/classes/disabled-event-bus.class.js +0 -11
  222. package/fesm/classes/disabled-event-bus.class.spec.js +0 -36
  223. package/fesm/classes/domain-event.publisher.js +0 -63
  224. package/fesm/classes/domain-event.publisher.spec.js +0 -230
  225. package/fesm/classes/event-bus.registry.js +0 -64
  226. package/fesm/classes/event-bus.registry.spec.js +0 -137
  227. package/fesm/classes/event-transports/event-transport.registry.js +0 -35
  228. package/fesm/classes/event-transports/event-transport.registry.spec.js +0 -27
  229. package/fesm/classes/event-transports/index.js +0 -6
  230. package/fesm/classes/event-transports/kafka-transport.optional.spec.js +0 -194
  231. package/fesm/classes/event-transports/memory-transport.js +0 -49
  232. package/fesm/classes/event-transports/memory-transport.spec.js +0 -53
  233. package/fesm/classes/event-transports/rabbitmq-transport.optional.js +0 -174
  234. package/fesm/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -251
  235. package/fesm/classes/handle-custome-db.js +0 -19
  236. package/fesm/classes/handle-custome-db.spec.js +0 -78
  237. package/fesm/classes/hybrid-cache.class.js +0 -81
  238. package/fesm/classes/hybrid-cache.class.spec.js +0 -164
  239. package/fesm/classes/hybrid-event-bus.class.js +0 -237
  240. package/fesm/classes/hybrid-event-bus.class.spec.js +0 -214
  241. package/fesm/classes/winston.logger.class.js +0 -228
  242. package/fesm/classes/winston.logger.class.spec.js +0 -327
  243. package/fesm/constants/message-keys.js +0 -49
  244. package/fesm/constants/permissions.js +0 -215
  245. package/fesm/decorators/api-response.decorator.js +0 -42
  246. package/fesm/decorators/api-response.decorator.spec.js +0 -122
  247. package/fesm/decorators/current-user.decorator.js +0 -18
  248. package/fesm/decorators/current-user.decorator.spec.js +0 -73
  249. package/fesm/decorators/log-action.decorator.js +0 -139
  250. package/fesm/decorators/log-action.decorator.spec.js +0 -349
  251. package/fesm/decorators/on-domain-event.decorator.js +0 -10
  252. package/fesm/decorators/on-domain-event.decorator.spec.js +0 -46
  253. package/fesm/decorators/public.decorator.js +0 -20
  254. package/fesm/decorators/public.decorator.spec.js +0 -46
  255. package/fesm/decorators/require-permission.decorator.js +0 -27
  256. package/fesm/decorators/require-permission.decorator.spec.js +0 -143
  257. package/fesm/dtos/delete.dto.js +0 -82
  258. package/fesm/dtos/filter-and-pagination.dto.js +0 -148
  259. package/fesm/dtos/get-by-ids.dto.js +0 -55
  260. package/fesm/dtos/identity-response.dto.js +0 -98
  261. package/fesm/dtos/pagination.dto.js +0 -57
  262. package/fesm/dtos/response-payload.dto.js +0 -336
  263. package/fesm/entities/identity.js +0 -90
  264. package/fesm/entities/raw-type.js +0 -1
  265. package/fesm/entities/user-root.js +0 -179
  266. package/fesm/enums/notification-type.enum.js +0 -7
  267. package/fesm/enums/participant-status.enum.js +0 -7
  268. package/fesm/enums/recurrence-type.enum.js +0 -8
  269. package/fesm/exceptions/base-app.exception.js +0 -115
  270. package/fesm/exceptions/base-app.exception.spec.js +0 -153
  271. package/fesm/exceptions/permission.exception.js +0 -35
  272. package/fesm/exceptions/permission.exception.spec.js +0 -78
  273. package/fesm/filters/global-exception.filter.js +0 -269
  274. package/fesm/filters/global-exception.filter.spec.js +0 -436
  275. package/fesm/filters/index.js +0 -1
  276. package/fesm/guards/jwt-auth.guard.js +0 -65
  277. package/fesm/guards/jwt-auth.guard.spec.js +0 -71
  278. package/fesm/guards/optional-jwt-auth.guard.js +0 -57
  279. package/fesm/guards/optional-jwt-auth.guard.spec.js +0 -51
  280. package/fesm/guards/permission.guard.js +0 -169
  281. package/fesm/guards/permission.guard.spec.js +0 -369
  282. package/fesm/interceptors/delete-empty-id-from-body.interceptor.js +0 -30
  283. package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -103
  284. package/fesm/interceptors/idempotency.interceptor.js +0 -86
  285. package/fesm/interceptors/idempotency.interceptor.spec.js +0 -112
  286. package/fesm/interceptors/response-meta.interceptor.js +0 -37
  287. package/fesm/interceptors/response-meta.interceptor.spec.js +0 -85
  288. package/fesm/interceptors/set-user-field-on-body.interceptor.js +0 -39
  289. package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +0 -124
  290. package/fesm/interceptors/slug.interceptor.js +0 -65
  291. package/fesm/interceptors/slug.interceptor.spec.js +0 -106
  292. package/fesm/interfaces/ai-assistant-adapter.interface.js +0 -3
  293. package/fesm/interfaces/api.interface.js +0 -1
  294. package/fesm/interfaces/datasource.interface.js +0 -1
  295. package/fesm/interfaces/event-bus.interface.js +0 -1
  296. package/fesm/interfaces/event-manager-adapter.interface.js +0 -1
  297. package/fesm/interfaces/identity.interface.js +0 -1
  298. package/fesm/interfaces/logged-user-info.interface.js +0 -3
  299. package/fesm/interfaces/module-config.interface.js +0 -5
  300. package/fesm/interfaces/notification-adapter.interface.js +0 -1
  301. package/fesm/interfaces/permission-sync-adapter.interface.js +0 -1
  302. package/fesm/interfaces/permission.interface.js +0 -1
  303. package/fesm/middlewares/logger.middleware.js +0 -235
  304. package/fesm/middlewares/logger.middleware.spec.js +0 -459
  305. package/fesm/modules/cache/cache.module.js +0 -29
  306. package/fesm/modules/cache/cache.module.spec.js +0 -44
  307. package/fesm/modules/datasource/index.js +0 -1
  308. package/fesm/modules/datasource/multi-tenant-datasource.service.js +0 -212
  309. package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +0 -377
  310. package/fesm/modules/event-bus/event-bus.explorer.js +0 -114
  311. package/fesm/modules/event-bus/event-bus.explorer.spec.js +0 -185
  312. package/fesm/modules/event-bus/event-bus.integration.spec.js +0 -249
  313. package/fesm/modules/event-bus/event-bus.module.js +0 -84
  314. package/fesm/modules/event-bus/event-bus.module.spec.js +0 -145
  315. package/fesm/modules/event-bus/module-events.provider.js +0 -26
  316. package/fesm/modules/event-bus/module-events.provider.spec.js +0 -52
  317. package/fesm/modules/shared-permission-cache/shared-permission-cache.module.js +0 -22
  318. package/fesm/modules/shared-permission-cache/shared-permission-cache.service.js +0 -91
  319. package/fesm/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -161
  320. package/fesm/modules/utils/utils.module.js +0 -22
  321. package/fesm/modules/utils/utils.service.js +0 -66
  322. package/fesm/modules/utils/utils.service.spec.js +0 -165
  323. package/fesm/utils/date-time.util.js +0 -70
  324. package/fesm/utils/date-time.util.spec.js +0 -80
  325. package/fesm/utils/event-pattern.util.js +0 -91
  326. package/fesm/utils/event-pattern.util.spec.js +0 -247
  327. package/fesm/utils/html-sanitizer.util.js +0 -69
  328. package/fesm/utils/html-sanitizer.util.spec.js +0 -72
  329. package/fesm/utils/optional-module.util.js +0 -25
  330. package/fesm/utils/optional-module.util.spec.js +0 -10
  331. package/fesm/utils/permission-cache-key.util.js +0 -10
  332. package/fesm/utils/permission-cache-key.util.spec.js +0 -84
  333. package/fesm/utils/query-helpers.util.js +0 -85
  334. package/fesm/utils/query-helpers.util.spec.js +0 -205
  335. package/fesm/utils/request.util.js +0 -81
  336. package/fesm/utils/request.util.spec.js +0 -245
  337. package/fesm/utils/string.util.js +0 -71
  338. package/fesm/utils/string.util.spec.js +0 -59
@@ -1,251 +0,0 @@
1
- const mockLoadOptionalModule = jest.fn();
2
- jest.mock('../../utils/optional-module.util', ()=>({
3
- loadOptionalModule: (...args)=>mockLoadOptionalModule(...args)
4
- }));
5
- import { InternalServerErrorException, Logger } from '@nestjs/common';
6
- import { RabbitMqTransport } from './rabbitmq-transport.optional';
7
- function buildEvent() {
8
- return {
9
- id: 'event-1',
10
- name: 'storage.folder.created',
11
- module: 'storage',
12
- entity: 'folder',
13
- action: 'created',
14
- payload: {
15
- ids: [
16
- '1'
17
- ]
18
- },
19
- actor: {},
20
- occurredAt: '2026-01-01T00:00:00.000Z',
21
- version: 1
22
- };
23
- }
24
- function createChannelMock() {
25
- return {
26
- assertExchange: jest.fn().mockResolvedValue(undefined),
27
- assertQueue: jest.fn().mockResolvedValue(undefined),
28
- prefetch: jest.fn().mockResolvedValue(undefined),
29
- bindQueue: jest.fn().mockResolvedValue(undefined),
30
- consume: jest.fn().mockResolvedValue(undefined),
31
- publish: jest.fn(),
32
- ack: jest.fn(),
33
- nack: jest.fn(),
34
- on: jest.fn(),
35
- removeAllListeners: jest.fn(),
36
- close: jest.fn().mockResolvedValue(undefined)
37
- };
38
- }
39
- /** A reconnect must land on a fresh channel, so every createChannel yields one */ function createAmqpMock() {
40
- const channels = [];
41
- const connection = {
42
- createChannel: jest.fn(async ()=>{
43
- const channel = createChannelMock();
44
- channels.push(channel);
45
- return channel;
46
- }),
47
- on: jest.fn(),
48
- removeAllListeners: jest.fn(),
49
- close: jest.fn().mockResolvedValue(undefined)
50
- };
51
- return {
52
- channels,
53
- connection,
54
- connect: jest.fn().mockResolvedValue(connection),
55
- get channel () {
56
- return channels[channels.length - 1];
57
- }
58
- };
59
- }
60
- function listenerFor(target, event) {
61
- const registered = target.on.mock.calls.find(([name])=>name === event);
62
- if (!registered) throw new Error(`No '${event}' listener was registered`);
63
- return registered[1];
64
- }
65
- describe('RabbitMqTransport', ()=>{
66
- let amqp;
67
- let warnSpy;
68
- let errorSpy;
69
- beforeEach(()=>{
70
- amqp = createAmqpMock();
71
- mockLoadOptionalModule.mockResolvedValue({
72
- connect: amqp.connect
73
- });
74
- warnSpy = jest.spyOn(Logger.prototype, 'warn').mockImplementation(()=>undefined);
75
- errorSpy = jest.spyOn(Logger.prototype, 'error').mockImplementation(()=>undefined);
76
- });
77
- afterEach(()=>{
78
- warnSpy.mockRestore();
79
- errorSpy.mockRestore();
80
- jest.clearAllMocks();
81
- });
82
- it('should throw a descriptive error when amqplib is not installed', async ()=>{
83
- mockLoadOptionalModule.mockRejectedValue(new Error('Cannot find module'));
84
- await expect(new RabbitMqTransport().connect()).rejects.toThrow(InternalServerErrorException);
85
- });
86
- it('should assert a topic exchange and a durable queue on connect', async ()=>{
87
- const transport = new RabbitMqTransport({
88
- exchange: 'ex',
89
- queue: 'svc.events'
90
- });
91
- await transport.connect();
92
- expect(amqp.channel.assertExchange).toHaveBeenCalledWith('ex', 'topic', {
93
- durable: true
94
- });
95
- expect(amqp.channel.assertQueue).toHaveBeenCalledWith('svc.events', {
96
- durable: true
97
- });
98
- expect(transport.isReady()).toBe(true);
99
- });
100
- it('should publish with the event name as the routing key', async ()=>{
101
- const transport = new RabbitMqTransport({
102
- exchange: 'ex'
103
- });
104
- await transport.connect();
105
- const event = buildEvent();
106
- await transport.publish(event);
107
- expect(amqp.channel.publish).toHaveBeenCalledWith('ex', 'storage.folder.created', expect.any(Buffer), expect.objectContaining({
108
- messageId: 'event-1',
109
- contentType: 'application/json'
110
- }));
111
- });
112
- it('should ignore publish before connect instead of throwing', async ()=>{
113
- await expect(new RabbitMqTransport().publish(buildEvent())).resolves.toBeUndefined();
114
- });
115
- it('should translate the ** wildcard to the AMQP # wildcard when binding', async ()=>{
116
- const transport = new RabbitMqTransport({
117
- exchange: 'ex',
118
- queue: 'q'
119
- });
120
- await transport.connect();
121
- await transport.bind('storage.**');
122
- expect(amqp.channel.bindQueue).toHaveBeenCalledWith('q', 'ex', 'storage.#');
123
- });
124
- it('should start consuming only once across several binds', async ()=>{
125
- const transport = new RabbitMqTransport();
126
- await transport.connect();
127
- await transport.bind('storage.#');
128
- await transport.bind('auth.#');
129
- expect(amqp.channel.consume).toHaveBeenCalledTimes(1);
130
- });
131
- it('should hand a consumed message to its handlers and ack it', async ()=>{
132
- const transport = new RabbitMqTransport();
133
- const handler = jest.fn();
134
- transport.onMessage(handler);
135
- await transport.connect();
136
- await transport.bind('storage.#');
137
- const event = buildEvent();
138
- const message = {
139
- content: Buffer.from(JSON.stringify(event))
140
- };
141
- await amqp.channel.consume.mock.calls[0][1](message);
142
- expect(handler).toHaveBeenCalledWith(event);
143
- expect(amqp.channel.ack).toHaveBeenCalledWith(message);
144
- });
145
- it('should nack a message it cannot parse', async ()=>{
146
- const transport = new RabbitMqTransport();
147
- await transport.connect();
148
- await transport.bind('storage.#');
149
- const message = {
150
- content: Buffer.from('not json')
151
- };
152
- await amqp.channel.consume.mock.calls[0][1](message);
153
- expect(amqp.channel.nack).toHaveBeenCalledWith(message, false, false);
154
- });
155
- it('should close the channel and the connection', async ()=>{
156
- const transport = new RabbitMqTransport();
157
- await transport.connect();
158
- await transport.close();
159
- expect(amqp.channel.close).toHaveBeenCalled();
160
- expect(amqp.connection.close).toHaveBeenCalled();
161
- expect(transport.isReady()).toBe(false);
162
- });
163
- it('should reconnect when the channel dies while the connection is still up', async ()=>{
164
- jest.useFakeTimers();
165
- const transport = new RabbitMqTransport({
166
- reconnectDelay: 10
167
- });
168
- await transport.connect();
169
- listenerFor(amqp.channels[0], 'close')();
170
- expect(transport.isReady()).toBe(false);
171
- await jest.advanceTimersByTimeAsync(10);
172
- expect(amqp.connect).toHaveBeenCalledTimes(2);
173
- expect(transport.isReady()).toBe(true);
174
- jest.useRealTimers();
175
- });
176
- it('should close the surviving connection rather than leak it on a channel failure', async ()=>{
177
- jest.useFakeTimers();
178
- const transport = new RabbitMqTransport({
179
- reconnectDelay: 10
180
- });
181
- await transport.connect();
182
- listenerFor(amqp.channels[0], 'close')();
183
- await jest.advanceTimersByTimeAsync(10);
184
- expect(amqp.connection.close).toHaveBeenCalledTimes(1);
185
- jest.useRealTimers();
186
- });
187
- it('should schedule a single reconnect when the channel and the connection both close', async ()=>{
188
- jest.useFakeTimers();
189
- const transport = new RabbitMqTransport({
190
- reconnectDelay: 10
191
- });
192
- await transport.connect();
193
- listenerFor(amqp.channels[0], 'close')();
194
- listenerFor(amqp.connection, 'close')();
195
- await jest.advanceTimersByTimeAsync(10);
196
- expect(amqp.connect).toHaveBeenCalledTimes(2);
197
- jest.useRealTimers();
198
- });
199
- it('should re-bind every pattern onto the new channel after a reconnect', async ()=>{
200
- jest.useFakeTimers();
201
- const transport = new RabbitMqTransport({
202
- exchange: 'ex',
203
- queue: 'q',
204
- reconnectDelay: 10
205
- });
206
- await transport.connect();
207
- await transport.bind('storage.**');
208
- listenerFor(amqp.channels[0], 'close')();
209
- await jest.advanceTimersByTimeAsync(10);
210
- expect(amqp.channels[1].bindQueue).toHaveBeenCalledWith('q', 'ex', 'storage.#');
211
- expect(amqp.channels[1].consume).toHaveBeenCalledTimes(1);
212
- jest.useRealTimers();
213
- });
214
- it('should ack an in-flight delivery on the channel it arrived on', async ()=>{
215
- jest.useFakeTimers();
216
- const transport = new RabbitMqTransport({
217
- reconnectDelay: 10
218
- });
219
- await transport.connect();
220
- await transport.bind('storage.#');
221
- const deliver = amqp.channels[0].consume.mock.calls[0][1];
222
- listenerFor(amqp.channels[0], 'close')();
223
- await jest.advanceTimersByTimeAsync(10);
224
- await deliver({
225
- content: Buffer.from(JSON.stringify(buildEvent()))
226
- });
227
- expect(amqp.channels[0].ack).toHaveBeenCalled();
228
- expect(amqp.channels[1].ack).not.toHaveBeenCalled();
229
- jest.useRealTimers();
230
- });
231
- it('should let a later bind retry consuming after the broker rejects consume', async ()=>{
232
- const transport = new RabbitMqTransport();
233
- await transport.connect();
234
- amqp.channel.consume.mockRejectedValueOnce(new Error('CHANNEL_ERROR'));
235
- await expect(transport.bind('storage.#')).rejects.toThrow('CHANNEL_ERROR');
236
- await transport.bind('auth.#');
237
- expect(amqp.channel.consume).toHaveBeenCalledTimes(2);
238
- });
239
- it('should stop reconnecting once it has been closed', async ()=>{
240
- jest.useFakeTimers();
241
- const transport = new RabbitMqTransport({
242
- reconnectDelay: 10
243
- });
244
- await transport.connect();
245
- await transport.close();
246
- listenerFor(amqp.channels[0], 'close')();
247
- await jest.advanceTimersByTimeAsync(50);
248
- expect(amqp.connect).toHaveBeenCalledTimes(1);
249
- jest.useRealTimers();
250
- });
251
- });
@@ -1,19 +0,0 @@
1
- import { envConfig } from '@flusys/nestjs-core';
2
- export function getJsonColumnType() {
3
- const driver = envConfig.getTypeOrmConfig().type;
4
- if (driver === 'postgres') return 'jsonb';
5
- if (driver === 'mysql') return 'json';
6
- return 'simple-json';
7
- }
8
- export function getCastIntType() {
9
- const driver = envConfig.getTypeOrmConfig().type;
10
- return driver === 'postgres' ? 'INTEGER' : 'UNSIGNED';
11
- }
12
- /**
13
- * Converts MySQL-style `?` placeholders to PostgreSQL `$1, $2, ...` placeholders.
14
- * No-op for MySQL/MariaDB.
15
- */ export function buildQuery(sql, dbType) {
16
- if (dbType !== 'postgres') return sql;
17
- let index = 0;
18
- return sql.replace(/\?/g, ()=>`$${++index}`);
19
- }
@@ -1,78 +0,0 @@
1
- const mockGetTypeOrmConfig = jest.fn();
2
- jest.mock('@flusys/nestjs-core', ()=>({
3
- envConfig: {
4
- getTypeOrmConfig: (...args)=>mockGetTypeOrmConfig(...args)
5
- }
6
- }));
7
- import { getJsonColumnType, getCastIntType, buildQuery } from './handle-custome-db';
8
- describe('handle-custome-db', ()=>{
9
- beforeEach(()=>{
10
- jest.clearAllMocks();
11
- });
12
- describe('getJsonColumnType', ()=>{
13
- it('should return jsonb for postgres', ()=>{
14
- mockGetTypeOrmConfig.mockReturnValue({
15
- type: 'postgres'
16
- });
17
- expect(getJsonColumnType()).toBe('jsonb');
18
- });
19
- it('should return json for mysql', ()=>{
20
- mockGetTypeOrmConfig.mockReturnValue({
21
- type: 'mysql'
22
- });
23
- expect(getJsonColumnType()).toBe('json');
24
- });
25
- it('should return simple-json for mariadb', ()=>{
26
- mockGetTypeOrmConfig.mockReturnValue({
27
- type: 'mariadb'
28
- });
29
- expect(getJsonColumnType()).toBe('simple-json');
30
- });
31
- it('should return simple-json for sqlite', ()=>{
32
- mockGetTypeOrmConfig.mockReturnValue({
33
- type: 'sqlite'
34
- });
35
- expect(getJsonColumnType()).toBe('simple-json');
36
- });
37
- });
38
- describe('getCastIntType', ()=>{
39
- it('should return INTEGER for postgres', ()=>{
40
- mockGetTypeOrmConfig.mockReturnValue({
41
- type: 'postgres'
42
- });
43
- expect(getCastIntType()).toBe('INTEGER');
44
- });
45
- it('should return UNSIGNED for mysql', ()=>{
46
- mockGetTypeOrmConfig.mockReturnValue({
47
- type: 'mysql'
48
- });
49
- expect(getCastIntType()).toBe('UNSIGNED');
50
- });
51
- it('should return UNSIGNED for mariadb', ()=>{
52
- mockGetTypeOrmConfig.mockReturnValue({
53
- type: 'mariadb'
54
- });
55
- expect(getCastIntType()).toBe('UNSIGNED');
56
- });
57
- });
58
- describe('buildQuery', ()=>{
59
- it('should leave the sql untouched for mysql', ()=>{
60
- expect(buildQuery('SELECT * FROM t WHERE id = ?', 'mysql')).toBe('SELECT * FROM t WHERE id = ?');
61
- });
62
- it('should leave the sql untouched for mariadb', ()=>{
63
- expect(buildQuery('SELECT * FROM t WHERE id = ?', 'mariadb')).toBe('SELECT * FROM t WHERE id = ?');
64
- });
65
- it('should convert a single ? placeholder to $1 for postgres', ()=>{
66
- expect(buildQuery('SELECT * FROM t WHERE id = ?', 'postgres')).toBe('SELECT * FROM t WHERE id = $1');
67
- });
68
- it('should convert multiple ? placeholders in order for postgres', ()=>{
69
- expect(buildQuery('WHERE a = ? AND b = ? AND c = ?', 'postgres')).toBe('WHERE a = $1 AND b = $2 AND c = $3');
70
- });
71
- it('should return the sql unchanged when there are no placeholders', ()=>{
72
- expect(buildQuery('SELECT 1', 'postgres')).toBe('SELECT 1');
73
- });
74
- it('should handle an empty sql string', ()=>{
75
- expect(buildQuery('', 'postgres')).toBe('');
76
- });
77
- });
78
- });
@@ -1,81 +0,0 @@
1
- function _define_property(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
13
- }
14
- import { envConfig } from '@flusys/nestjs-core/config';
15
- import KeyvRedis from '@keyv/redis';
16
- import { CacheableMemory } from 'cacheable';
17
- import Keyv from 'keyv';
18
- export class HybridCache {
19
- async get(key) {
20
- // Prefer memory first if available
21
- if (this.memory) {
22
- const value = await this.memory.get(key);
23
- if (value !== undefined) return value;
24
- }
25
- // Fallback to redis
26
- if (this.redis) {
27
- const value = await this.redis.get(key);
28
- if (value !== undefined && this.memory) {
29
- // backfill L1 memory cache
30
- await this.memory.set(key, value);
31
- }
32
- return value;
33
- }
34
- return undefined;
35
- }
36
- async set(key, value, ttl) {
37
- if (this.memory) {
38
- await this.memory.set(key, value, ttl);
39
- }
40
- if (this.redis) {
41
- await this.redis.set(key, value, ttl);
42
- }
43
- }
44
- async del(key) {
45
- if (this.memory) {
46
- await this.memory.delete(key);
47
- }
48
- if (this.redis) {
49
- await this.redis.delete(key);
50
- }
51
- }
52
- async reset() {
53
- if (this.memory) {
54
- await this.memory.clear();
55
- }
56
- }
57
- async resetL2() {
58
- if (this.redis) {
59
- await this.redis.clear();
60
- }
61
- }
62
- constructor(memoryTtl = 60 * 1000, memorySize = 5000){
63
- _define_property(this, "memory", void 0);
64
- _define_property(this, "redis", void 0);
65
- // Default to 'memory' if not specified
66
- const useCacheLabel = envConfig.tryGetValue('USE_CACHE_LABEL', false) || 'memory';
67
- if (useCacheLabel === 'memory' || useCacheLabel === 'hybrid') {
68
- this.memory = new Keyv({
69
- store: new CacheableMemory({
70
- ttl: memoryTtl,
71
- lruSize: memorySize
72
- })
73
- });
74
- }
75
- if (useCacheLabel === 'redis' || useCacheLabel === 'hybrid') {
76
- this.redis = new Keyv({
77
- store: new KeyvRedis(envConfig.getRedisUrl())
78
- });
79
- }
80
- }
81
- }
@@ -1,164 +0,0 @@
1
- const mockTryGetValue = jest.fn();
2
- const mockGetRedisUrl = jest.fn();
3
- jest.mock('@flusys/nestjs-core/config', ()=>({
4
- envConfig: {
5
- tryGetValue: (...args)=>mockTryGetValue(...args),
6
- getRedisUrl: (...args)=>mockGetRedisUrl(...args)
7
- }
8
- }));
9
- const mockKeyvGet = jest.fn();
10
- const mockKeyvSet = jest.fn();
11
- const mockKeyvDelete = jest.fn();
12
- const mockKeyvClear = jest.fn();
13
- const KeyvCtor = jest.fn().mockImplementation(()=>({
14
- get: mockKeyvGet,
15
- set: mockKeyvSet,
16
- delete: mockKeyvDelete,
17
- clear: mockKeyvClear
18
- }));
19
- jest.mock('keyv', ()=>({
20
- __esModule: true,
21
- default: KeyvCtor
22
- }));
23
- jest.mock('@keyv/redis', ()=>({
24
- __esModule: true,
25
- default: jest.fn().mockImplementation(()=>({}))
26
- }));
27
- jest.mock('cacheable', ()=>({
28
- CacheableMemory: jest.fn().mockImplementation(()=>({}))
29
- }));
30
- import { HybridCache } from './hybrid-cache.class';
31
- describe('HybridCache', ()=>{
32
- beforeEach(()=>{
33
- jest.clearAllMocks();
34
- mockGetRedisUrl.mockReturnValue('redis://localhost:6379');
35
- });
36
- describe('construction', ()=>{
37
- it('should default to memory-only cache when USE_CACHE_LABEL is unset', ()=>{
38
- mockTryGetValue.mockReturnValue('');
39
- new HybridCache();
40
- expect(KeyvCtor).toHaveBeenCalledTimes(1);
41
- });
42
- it('should create only a memory store for "memory" mode', ()=>{
43
- mockTryGetValue.mockReturnValue('memory');
44
- new HybridCache();
45
- expect(KeyvCtor).toHaveBeenCalledTimes(1);
46
- });
47
- it('should create only a redis store for "redis" mode', async ()=>{
48
- mockTryGetValue.mockReturnValue('redis');
49
- const cache = new HybridCache();
50
- mockKeyvGet.mockResolvedValue('redis-value');
51
- const result = await cache.get('key');
52
- expect(KeyvCtor).toHaveBeenCalledTimes(1);
53
- expect(result).toBe('redis-value');
54
- });
55
- it('should create both stores for "hybrid" mode', ()=>{
56
- mockTryGetValue.mockReturnValue('hybrid');
57
- new HybridCache();
58
- expect(KeyvCtor).toHaveBeenCalledTimes(2);
59
- });
60
- it('should pass custom ttl and size through to the memory store options', ()=>{
61
- mockTryGetValue.mockReturnValue('memory');
62
- const { CacheableMemory } = require('cacheable');
63
- new HybridCache(30_000, 100);
64
- expect(CacheableMemory).toHaveBeenCalledWith({
65
- ttl: 30_000,
66
- lruSize: 100
67
- });
68
- });
69
- });
70
- describe('get (hybrid mode)', ()=>{
71
- let cache;
72
- beforeEach(()=>{
73
- mockTryGetValue.mockReturnValue('hybrid');
74
- cache = new HybridCache();
75
- });
76
- it('should return the memory value without touching redis when present', async ()=>{
77
- mockKeyvGet.mockResolvedValueOnce('memory-value');
78
- const result = await cache.get('key');
79
- expect(result).toBe('memory-value');
80
- expect(mockKeyvGet).toHaveBeenCalledTimes(1);
81
- });
82
- it('should fall back to redis and backfill memory on L1 miss', async ()=>{
83
- mockKeyvGet.mockResolvedValueOnce(undefined).mockResolvedValueOnce('redis-value');
84
- const result = await cache.get('key');
85
- expect(result).toBe('redis-value');
86
- expect(mockKeyvSet).toHaveBeenCalledWith('key', 'redis-value');
87
- });
88
- it('should return undefined without backfilling when redis also misses', async ()=>{
89
- mockKeyvGet.mockResolvedValue(undefined);
90
- const result = await cache.get('key');
91
- expect(result).toBeUndefined();
92
- expect(mockKeyvSet).not.toHaveBeenCalled();
93
- });
94
- it('should treat falsy-but-defined values (0, "", false) as cache hits', async ()=>{
95
- mockKeyvGet.mockResolvedValueOnce(0);
96
- const result = await cache.get('key');
97
- expect(result).toBe(0);
98
- });
99
- });
100
- describe('get (memory-only mode)', ()=>{
101
- it('should return undefined when there is no redis store to fall back to', async ()=>{
102
- mockTryGetValue.mockReturnValue('memory');
103
- const cache = new HybridCache();
104
- mockKeyvGet.mockResolvedValue(undefined);
105
- const result = await cache.get('key');
106
- expect(result).toBeUndefined();
107
- });
108
- });
109
- describe('set', ()=>{
110
- it('should write to both stores in hybrid mode with the given ttl', async ()=>{
111
- mockTryGetValue.mockReturnValue('hybrid');
112
- const cache = new HybridCache();
113
- await cache.set('key', {
114
- a: 1
115
- }, 5000);
116
- expect(mockKeyvSet).toHaveBeenNthCalledWith(1, 'key', {
117
- a: 1
118
- }, 5000);
119
- expect(mockKeyvSet).toHaveBeenNthCalledWith(2, 'key', {
120
- a: 1
121
- }, 5000);
122
- });
123
- it('should write only to memory when redis is not configured', async ()=>{
124
- mockTryGetValue.mockReturnValue('memory');
125
- const cache = new HybridCache();
126
- await cache.set('key', 'value');
127
- expect(mockKeyvSet).toHaveBeenCalledTimes(1);
128
- });
129
- });
130
- describe('del', ()=>{
131
- it('should delete from both stores in hybrid mode', async ()=>{
132
- mockTryGetValue.mockReturnValue('hybrid');
133
- const cache = new HybridCache();
134
- await cache.del('key');
135
- expect(mockKeyvDelete).toHaveBeenCalledTimes(2);
136
- });
137
- it('should be a no-op safe call when no stores are configured', async ()=>{
138
- mockTryGetValue.mockReturnValue('unknown-mode');
139
- const cache = new HybridCache();
140
- await expect(cache.del('key')).resolves.toBeUndefined();
141
- expect(mockKeyvDelete).not.toHaveBeenCalled();
142
- });
143
- });
144
- describe('reset / resetL2', ()=>{
145
- it('reset should clear only the memory store', async ()=>{
146
- mockTryGetValue.mockReturnValue('hybrid');
147
- const cache = new HybridCache();
148
- await cache.reset();
149
- expect(mockKeyvClear).toHaveBeenCalledTimes(1);
150
- });
151
- it('resetL2 should clear only the redis store', async ()=>{
152
- mockTryGetValue.mockReturnValue('hybrid');
153
- const cache = new HybridCache();
154
- await cache.resetL2();
155
- expect(mockKeyvClear).toHaveBeenCalledTimes(1);
156
- });
157
- it('reset should be a no-op when memory is not configured', async ()=>{
158
- mockTryGetValue.mockReturnValue('redis');
159
- const cache = new HybridCache();
160
- await expect(cache.reset()).resolves.toBeUndefined();
161
- expect(mockKeyvClear).not.toHaveBeenCalled();
162
- });
163
- });
164
- });