@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,69 +0,0 @@
1
- /**
2
- * HTML Sanitizer Utilities
3
- *
4
- * Provides functions for escaping HTML content to prevent XSS attacks.
5
- * Use these utilities when interpolating user-provided variables into HTML content.
6
- */ /**
7
- * HTML entity mapping for escaping special characters
8
- */ const HTML_ESCAPE_MAP = {
9
- '&': '&',
10
- '<': '&lt;',
11
- '>': '&gt;',
12
- '"': '&quot;',
13
- "'": '&#x27;',
14
- '/': '&#x2F;',
15
- '`': '&#x60;',
16
- '=': '&#x3D;'
17
- };
18
- /**
19
- * Regex pattern matching characters that need HTML escaping
20
- */ const HTML_ESCAPE_REGEX = /[&<>"'`=/]/g;
21
- /**
22
- * Escapes HTML special characters to prevent XSS attacks.
23
- *
24
- * @param str - The string to escape
25
- * @returns The escaped string safe for HTML insertion
26
- *
27
- * @example
28
- * ```typescript
29
- * escapeHtml('<script>alert("xss")</script>')
30
- * // Returns: '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;'
31
- * ```
32
- */ export function escapeHtml(str) {
33
- if (!str || typeof str !== 'string') {
34
- return str ?? '';
35
- }
36
- return str.replace(HTML_ESCAPE_REGEX, (char)=>HTML_ESCAPE_MAP[char]);
37
- }
38
- /**
39
- * Escapes all string values in a variables object for safe HTML interpolation.
40
- * Non-string values are converted to strings and escaped.
41
- *
42
- * @param variables - Object containing template variables
43
- * @returns New object with all string values HTML-escaped
44
- *
45
- * @example
46
- * ```typescript
47
- * escapeHtmlVariables({ name: '<script>xss</script>', count: 5 })
48
- * // Returns: { name: '&lt;script&gt;xss&lt;/script&gt;', count: '5' }
49
- * ```
50
- */ export function escapeHtmlVariables(variables) {
51
- if (!variables || typeof variables !== 'object') {
52
- return {};
53
- }
54
- const escaped = {};
55
- for (const [key, value] of Object.entries(variables)){
56
- if (value === null || value === undefined) {
57
- escaped[key] = '';
58
- } else if (typeof value === 'string') {
59
- escaped[key] = escapeHtml(value);
60
- } else if (typeof value === 'object') {
61
- // For objects/arrays, stringify and escape
62
- escaped[key] = escapeHtml(JSON.stringify(value));
63
- } else {
64
- // For numbers, booleans, etc., convert to string (no escaping needed)
65
- escaped[key] = String(value);
66
- }
67
- }
68
- return escaped;
69
- }
@@ -1,72 +0,0 @@
1
- import { escapeHtml, escapeHtmlVariables } from './html-sanitizer.util';
2
- describe('html-sanitizer.util', ()=>{
3
- describe('escapeHtml', ()=>{
4
- it('should escape all special HTML characters', ()=>{
5
- expect(escapeHtml('<script>alert("xss")</script>')).toBe('&lt;script&gt;alert(&quot;xss&quot;)&lt;&#x2F;script&gt;');
6
- });
7
- it('should escape ampersands', ()=>{
8
- expect(escapeHtml('a & b')).toBe('a &amp; b');
9
- });
10
- it('should escape single quotes, backticks, and equals signs', ()=>{
11
- expect(escapeHtml(`a='b' c=\`d\``)).toBe('a&#x3D;&#x27;b&#x27; c&#x3D;&#x60;d&#x60;');
12
- });
13
- it('should return an empty string for empty input', ()=>{
14
- expect(escapeHtml('')).toBe('');
15
- });
16
- it('should return input unchanged when it has no special characters', ()=>{
17
- expect(escapeHtml('Hello World 123')).toBe('Hello World 123');
18
- });
19
- it('should return non-string input coerced via nullish fallback', ()=>{
20
- expect(escapeHtml(null)).toBe('');
21
- expect(escapeHtml(undefined)).toBe('');
22
- });
23
- it('should return non-string, truthy input unchanged (not throw)', ()=>{
24
- // e.g. a number passed at runtime despite the TS signature
25
- expect(escapeHtml(42)).toBe(42);
26
- });
27
- });
28
- describe('escapeHtmlVariables', ()=>{
29
- it('should escape string values', ()=>{
30
- expect(escapeHtmlVariables({
31
- name: '<b>bold</b>'
32
- })).toEqual({
33
- name: '&lt;b&gt;bold&lt;&#x2F;b&gt;'
34
- });
35
- });
36
- it('should convert numbers and booleans to strings without escaping', ()=>{
37
- expect(escapeHtmlVariables({
38
- count: 5,
39
- active: true
40
- })).toEqual({
41
- count: '5',
42
- active: 'true'
43
- });
44
- });
45
- it('should stringify and escape object/array values', ()=>{
46
- const result = escapeHtmlVariables({
47
- meta: {
48
- a: '<x>'
49
- }
50
- });
51
- expect(result.meta).toBe(escapeHtml(JSON.stringify({
52
- a: '<x>'
53
- })));
54
- });
55
- it('should convert null/undefined values to empty string', ()=>{
56
- expect(escapeHtmlVariables({
57
- a: null,
58
- b: undefined
59
- })).toEqual({
60
- a: '',
61
- b: ''
62
- });
63
- });
64
- it('should return an empty object for null/non-object input', ()=>{
65
- expect(escapeHtmlVariables(null)).toEqual({});
66
- expect(escapeHtmlVariables(undefined)).toEqual({});
67
- });
68
- it('should return an empty object for an empty input object', ()=>{
69
- expect(escapeHtmlVariables({})).toEqual({});
70
- });
71
- });
72
- });
@@ -1,25 +0,0 @@
1
- /**
2
- * Loads an optional peer dependency at runtime.
3
- *
4
- * The specifier has to stay invisible to the bundler. A plain `import(name)`
5
- * looks dynamic in source but webpack rewrites it into a context module rooted
6
- * at this directory - one that can only ever resolve files sitting next to this
7
- * one - so `amqplib` and `kafkajs` fail to load in the bundled app no matter how
8
- * correctly they are installed. Both lookups below are assembled at runtime,
9
- * where no bundler can rewrite them.
10
- *
11
- * `require` is used first because it is what the CommonJS builds actually run
12
- * on; native ESM has no `require`, and there `import()` is the only way in.
13
- */ export async function loadOptionalModule(name) {
14
- const runtimeRequire = resolveRuntimeRequire();
15
- if (runtimeRequire) return runtimeRequire(name);
16
- const runtimeImport = new Function('specifier', 'return import(specifier);');
17
- return runtimeImport(name);
18
- }
19
- /** Undefined under native ESM and inside a bundle that shadows nothing back */ function resolveRuntimeRequire() {
20
- try {
21
- return eval('typeof require === "function" ? require : undefined');
22
- } catch {
23
- return undefined;
24
- }
25
- }
@@ -1,10 +0,0 @@
1
- import { loadOptionalModule } from './optional-module.util';
2
- describe('loadOptionalModule', ()=>{
3
- it('should resolve an installed module', async ()=>{
4
- const events = await loadOptionalModule('events');
5
- expect(typeof events.EventEmitter).toBe('function');
6
- });
7
- it('should reject for a module that is not installed', async ()=>{
8
- await expect(loadOptionalModule('definitely-not-installed-pkg')).rejects.toThrow();
9
- });
10
- });
@@ -1,10 +0,0 @@
1
- import { PERMISSIONS_CACHE_PREFIX } from '../constants';
2
- export const DEFAULT_USER_PERMISSION_KEY_FORMAT = `${PERMISSIONS_CACHE_PREFIX}:user:{userId}`;
3
- export const DEFAULT_COMPANY_PERMISSION_KEY_FORMAT = `${PERMISSIONS_CACHE_PREFIX}:company:{companyId}:branch:{branchId}:user:{userId}`;
4
- /** Single source of truth for permission cache keys, shared by the guard (reader) and cache service (writer) so a config override can't desync them; company scope requires enableCompanyFeature, not just a companyId */ export function buildPermissionCacheKey(options, config) {
5
- const isCompanyScope = Boolean(config?.enableCompanyFeature && options.companyId);
6
- if (isCompanyScope) {
7
- return (config?.companyPermissionKeyFormat || DEFAULT_COMPANY_PERMISSION_KEY_FORMAT).replace('{userId}', options.userId).replace('{companyId}', options.companyId).replace('{branchId}', options.branchId || 'null');
8
- }
9
- return (config?.userPermissionKeyFormat || DEFAULT_USER_PERMISSION_KEY_FORMAT).replace('{userId}', options.userId);
10
- }
@@ -1,84 +0,0 @@
1
- import { buildPermissionCacheKey, DEFAULT_COMPANY_PERMISSION_KEY_FORMAT, DEFAULT_USER_PERMISSION_KEY_FORMAT } from './permission-cache-key.util';
2
- describe('permission-cache-key.util', ()=>{
3
- describe('buildPermissionCacheKey', ()=>{
4
- it('should build a user-scoped key when no config is provided', ()=>{
5
- const key = buildPermissionCacheKey({
6
- userId: 'user-1'
7
- });
8
- expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
9
- });
10
- it('should build a user-scoped key when company feature is disabled', ()=>{
11
- const key = buildPermissionCacheKey({
12
- userId: 'user-1',
13
- companyId: 'company-1'
14
- }, {
15
- enableCompanyFeature: false
16
- });
17
- expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
18
- });
19
- it('should build a user-scoped key when company feature enabled but no companyId given', ()=>{
20
- const key = buildPermissionCacheKey({
21
- userId: 'user-1'
22
- }, {
23
- enableCompanyFeature: true
24
- });
25
- expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
26
- });
27
- it('should build a company-scoped key when company feature is enabled and companyId is present', ()=>{
28
- const key = buildPermissionCacheKey({
29
- userId: 'user-1',
30
- companyId: 'company-1',
31
- branchId: 'branch-1'
32
- }, {
33
- enableCompanyFeature: true
34
- });
35
- expect(key).toBe(DEFAULT_COMPANY_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1').replace('{companyId}', 'company-1').replace('{branchId}', 'branch-1'));
36
- });
37
- it('should default branchId placeholder to "null" when branchId is missing', ()=>{
38
- const key = buildPermissionCacheKey({
39
- userId: 'user-1',
40
- companyId: 'company-1'
41
- }, {
42
- enableCompanyFeature: true
43
- });
44
- expect(key).toContain(':branch:null:');
45
- });
46
- it('should default branchId placeholder to "null" when branchId is explicitly null', ()=>{
47
- const key = buildPermissionCacheKey({
48
- userId: 'user-1',
49
- companyId: 'company-1',
50
- branchId: null
51
- }, {
52
- enableCompanyFeature: true
53
- });
54
- expect(key).toContain(':branch:null:');
55
- });
56
- it('should use a custom userPermissionKeyFormat when provided', ()=>{
57
- const key = buildPermissionCacheKey({
58
- userId: 'user-1'
59
- }, {
60
- userPermissionKeyFormat: 'custom:{userId}'
61
- });
62
- expect(key).toBe('custom:user-1');
63
- });
64
- it('should use a custom companyPermissionKeyFormat when provided', ()=>{
65
- const key = buildPermissionCacheKey({
66
- userId: 'user-1',
67
- companyId: 'company-1'
68
- }, {
69
- enableCompanyFeature: true,
70
- companyPermissionKeyFormat: 'custom:{companyId}:{userId}:{branchId}'
71
- });
72
- expect(key).toBe('custom:company-1:user-1:null');
73
- });
74
- it('should treat an empty-string companyId as no company scope', ()=>{
75
- const key = buildPermissionCacheKey({
76
- userId: 'user-1',
77
- companyId: ''
78
- }, {
79
- enableCompanyFeature: true
80
- });
81
- expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
82
- });
83
- });
84
- });
@@ -1,85 +0,0 @@
1
- import { BadRequestException } from '@nestjs/common';
2
- import { AUTH_MESSAGES } from '../constants';
3
- /**
4
- * Apply company filter to a query builder if company feature is enabled.
5
- * Centralizes the common pattern of filtering by user's company.
6
- *
7
- * @param query - TypeORM SelectQueryBuilder
8
- * @param config - Company filter configuration
9
- * @param user - Current logged user (may be null)
10
- * @returns The modified query builder
11
- *
12
- * @example
13
- * ```typescript
14
- * applyCompanyFilter(query, {
15
- * isCompanyFeatureEnabled: this.config.isCompanyFeatureEnabled(),
16
- * entityAlias: 'file_manager',
17
- * }, user);
18
- * ```
19
- */ export function applyCompanyFilter(query, config, user) {
20
- const columnName = config.columnName ?? 'companyId';
21
- if (config.isCompanyFeatureEnabled && user?.companyId) {
22
- query.andWhere(`${config.entityAlias}.${columnName} = :companyId`, {
23
- companyId: user.companyId
24
- });
25
- }
26
- return query;
27
- }
28
- /**
29
- * Build a where condition object with optional company filter.
30
- *
31
- * @param baseWhere - Base where condition object
32
- * @param isCompanyFeatureEnabled - Whether company feature is enabled
33
- * @param user - Current logged user
34
- * @returns Where condition with companyId added if applicable
35
- *
36
- * @example
37
- * ```typescript
38
- * const where = buildCompanyWhereCondition(
39
- * { id: dto.id },
40
- * this.config.isCompanyFeatureEnabled(),
41
- * user
42
- * );
43
- * // Returns { id: dto.id, companyId: user.companyId } if company feature enabled
44
- * ```
45
- */ export function buildCompanyWhereCondition(baseWhere, isCompanyFeatureEnabled, user) {
46
- if (isCompanyFeatureEnabled && user?.companyId) {
47
- return {
48
- ...baseWhere,
49
- companyId: user.companyId
50
- };
51
- }
52
- return baseWhere;
53
- }
54
- /**
55
- * Type guard to check if entity has companyId property
56
- */ export function hasCompanyId(entity) {
57
- return entity !== null && typeof entity === 'object' && 'companyId' in entity;
58
- }
59
- /**
60
- * Validates that an entity with companyId belongs to the user's company.
61
- * Throws BadRequestException if the entity belongs to another company.
62
- *
63
- * @param entity - Entity to validate
64
- * @param user - Current logged user
65
- * @param isCompanyFeatureEnabled - Whether company feature is enabled
66
- * @param entityName - Name of entity for error message (e.g., 'Email configuration')
67
- * @throws BadRequestException if entity belongs to another company
68
- *
69
- * @example
70
- * ```typescript
71
- * validateCompanyOwnership(config, user, this.config.isCompanyFeatureEnabled(), 'Email configuration');
72
- * ```
73
- */ export function validateCompanyOwnership(entity, user, isCompanyFeatureEnabled, entityName) {
74
- if (isCompanyFeatureEnabled && user?.companyId && hasCompanyId(entity)) {
75
- if (entity.companyId && entity.companyId !== user.companyId) {
76
- throw new BadRequestException({
77
- message: `${entityName} belongs to another company`,
78
- messageKey: AUTH_MESSAGES.ENTITY_BELOG_ANOTHER_COMPANY,
79
- messageVariables: {
80
- entity: entityName
81
- }
82
- });
83
- }
84
- }
85
- }
@@ -1,205 +0,0 @@
1
- import { BadRequestException } from '@nestjs/common';
2
- import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
3
- import { applyCompanyFilter, buildCompanyWhereCondition, hasCompanyId, validateCompanyOwnership } from './query-helpers.util';
4
- function createMockQueryBuilder() {
5
- return {
6
- andWhere: jest.fn().mockReturnThis()
7
- };
8
- }
9
- describe('query-helpers.util', ()=>{
10
- describe('applyCompanyFilter', ()=>{
11
- it('should apply the company filter when feature enabled and user has companyId', ()=>{
12
- const query = createMockQueryBuilder();
13
- const user = buildMockUser({
14
- companyId: 'company-1'
15
- });
16
- applyCompanyFilter(query, {
17
- isCompanyFeatureEnabled: true,
18
- entityAlias: 'file'
19
- }, user);
20
- expect(query.andWhere).toHaveBeenCalledWith('file.companyId = :companyId', {
21
- companyId: 'company-1'
22
- });
23
- });
24
- it('should use a custom column name when provided', ()=>{
25
- const query = createMockQueryBuilder();
26
- const user = buildMockUser({
27
- companyId: 'company-1'
28
- });
29
- applyCompanyFilter(query, {
30
- isCompanyFeatureEnabled: true,
31
- entityAlias: 'file',
32
- columnName: 'company_id'
33
- }, user);
34
- expect(query.andWhere).toHaveBeenCalledWith('file.company_id = :companyId', {
35
- companyId: 'company-1'
36
- });
37
- });
38
- it('should not apply filter when feature disabled', ()=>{
39
- const query = createMockQueryBuilder();
40
- const user = buildMockUser({
41
- companyId: 'company-1'
42
- });
43
- applyCompanyFilter(query, {
44
- isCompanyFeatureEnabled: false,
45
- entityAlias: 'file'
46
- }, user);
47
- expect(query.andWhere).not.toHaveBeenCalled();
48
- });
49
- it('should not apply filter when user is null', ()=>{
50
- const query = createMockQueryBuilder();
51
- applyCompanyFilter(query, {
52
- isCompanyFeatureEnabled: true,
53
- entityAlias: 'file'
54
- }, null);
55
- expect(query.andWhere).not.toHaveBeenCalled();
56
- });
57
- it('should not apply filter when user has no companyId', ()=>{
58
- const query = createMockQueryBuilder();
59
- const user = buildMockUser({
60
- companyId: undefined
61
- });
62
- applyCompanyFilter(query, {
63
- isCompanyFeatureEnabled: true,
64
- entityAlias: 'file'
65
- }, user);
66
- expect(query.andWhere).not.toHaveBeenCalled();
67
- });
68
- it('should return the same query builder instance', ()=>{
69
- const query = createMockQueryBuilder();
70
- const result = applyCompanyFilter(query, {
71
- isCompanyFeatureEnabled: false,
72
- entityAlias: 'file'
73
- }, null);
74
- expect(result).toBe(query);
75
- });
76
- });
77
- describe('buildCompanyWhereCondition', ()=>{
78
- it('should add companyId when feature enabled and user has one', ()=>{
79
- const user = buildMockUser({
80
- companyId: 'company-1'
81
- });
82
- const result = buildCompanyWhereCondition({
83
- id: '1'
84
- }, true, user);
85
- expect(result).toEqual({
86
- id: '1',
87
- companyId: 'company-1'
88
- });
89
- });
90
- it('should return base where unchanged when feature disabled', ()=>{
91
- const user = buildMockUser({
92
- companyId: 'company-1'
93
- });
94
- const result = buildCompanyWhereCondition({
95
- id: '1'
96
- }, false, user);
97
- expect(result).toEqual({
98
- id: '1'
99
- });
100
- });
101
- it('should return base where unchanged when user is null', ()=>{
102
- const result = buildCompanyWhereCondition({
103
- id: '1'
104
- }, true, null);
105
- expect(result).toEqual({
106
- id: '1'
107
- });
108
- });
109
- it('should return base where unchanged when user has no companyId', ()=>{
110
- const user = buildMockUser({
111
- companyId: undefined
112
- });
113
- const result = buildCompanyWhereCondition({
114
- id: '1'
115
- }, true, user);
116
- expect(result).toEqual({
117
- id: '1'
118
- });
119
- });
120
- });
121
- describe('hasCompanyId', ()=>{
122
- it('should return true for an object with a companyId property', ()=>{
123
- expect(hasCompanyId({
124
- companyId: 'company-1'
125
- })).toBe(true);
126
- });
127
- it('should return true even when companyId is explicitly undefined but present', ()=>{
128
- expect(hasCompanyId({
129
- companyId: undefined
130
- })).toBe(true);
131
- });
132
- it('should return false for an object without a companyId property', ()=>{
133
- expect(hasCompanyId({
134
- id: '1'
135
- })).toBe(false);
136
- });
137
- it('should return false for null', ()=>{
138
- expect(hasCompanyId(null)).toBe(false);
139
- });
140
- it('should return false for non-object values', ()=>{
141
- expect(hasCompanyId('string')).toBe(false);
142
- expect(hasCompanyId(42)).toBe(false);
143
- });
144
- });
145
- describe('validateCompanyOwnership', ()=>{
146
- it('should not throw when feature disabled', ()=>{
147
- const user = buildMockUser({
148
- companyId: 'company-1'
149
- });
150
- expect(()=>validateCompanyOwnership({
151
- companyId: 'company-2'
152
- }, user, false, 'Entity')).not.toThrow();
153
- });
154
- it('should not throw when user is null', ()=>{
155
- expect(()=>validateCompanyOwnership({
156
- companyId: 'company-2'
157
- }, null, true, 'Entity')).not.toThrow();
158
- });
159
- it('should not throw when entity has no companyId', ()=>{
160
- const user = buildMockUser({
161
- companyId: 'company-1'
162
- });
163
- expect(()=>validateCompanyOwnership({
164
- id: '1'
165
- }, user, true, 'Entity')).not.toThrow();
166
- });
167
- it('should not throw when entity companyId is falsy', ()=>{
168
- const user = buildMockUser({
169
- companyId: 'company-1'
170
- });
171
- expect(()=>validateCompanyOwnership({
172
- companyId: null
173
- }, user, true, 'Entity')).not.toThrow();
174
- });
175
- it('should not throw when entity companyId matches user companyId', ()=>{
176
- const user = buildMockUser({
177
- companyId: 'company-1'
178
- });
179
- expect(()=>validateCompanyOwnership({
180
- companyId: 'company-1'
181
- }, user, true, 'Entity')).not.toThrow();
182
- });
183
- it('should throw BadRequestException when entity belongs to a different company', ()=>{
184
- const user = buildMockUser({
185
- companyId: 'company-1'
186
- });
187
- expect(()=>validateCompanyOwnership({
188
- companyId: 'company-2'
189
- }, user, true, 'Email configuration')).toThrow(BadRequestException);
190
- });
191
- it('should include the entity name in the thrown exception message', ()=>{
192
- const user = buildMockUser({
193
- companyId: 'company-1'
194
- });
195
- try {
196
- validateCompanyOwnership({
197
- companyId: 'company-2'
198
- }, user, true, 'Email configuration');
199
- fail('expected to throw');
200
- } catch (error) {
201
- expect(error.getResponse().message).toContain('Email configuration');
202
- }
203
- });
204
- });
205
- });
@@ -1,81 +0,0 @@
1
- import { envConfig } from '@flusys/nestjs-core/config';
2
- import { CLIENT_TYPE_HEADER } from '../constants';
3
- /** Time unit multipliers in milliseconds */ const TIME_UNIT_MS = {
4
- s: 1000,
5
- m: 60 * 1000,
6
- h: 60 * 60 * 1000,
7
- d: 24 * 60 * 60 * 1000,
8
- w: 7 * 24 * 60 * 60 * 1000
9
- };
10
- /** Get normalized client type from request header */ function getClientType(req) {
11
- const clientType = req.headers[CLIENT_TYPE_HEADER];
12
- return clientType ? clientType.toLowerCase() : null;
13
- }
14
- /**
15
- * Detect if request is from a browser client.
16
- * Uses x-client-type header first, then falls back to user-agent detection.
17
- */ export function isBrowserRequest(req) {
18
- const clientType = getClientType(req);
19
- if (clientType) {
20
- return clientType === 'browser' || clientType === 'web';
21
- }
22
- const accept = req.headers['accept'] || '';
23
- if (accept.includes('text/html')) return true;
24
- const userAgent = req.headers['user-agent'] || '';
25
- const browserPatterns = /mozilla|chrome|safari|firefox|edge|opera|msie/i;
26
- return browserPatterns.test(userAgent) && !userAgent.includes('Postman');
27
- }
28
- /**
29
- * Build secure cookie options based on request context.
30
- * Handles HTTPS detection for TLS-terminating proxies.
31
- */ export function buildCookieOptions(req) {
32
- const hostname = req.hostname || '';
33
- const origin = req.headers.origin || '';
34
- const isProduction = envConfig.isProduction();
35
- const forwardedProto = req.headers['x-forwarded-proto'];
36
- const isHttps = isProduction || forwardedProto === 'https' || origin.startsWith('https://') || req.secure;
37
- let domain;
38
- const domainParts = hostname.split('.');
39
- if (domainParts.length > 2 && !hostname.includes('localhost')) {
40
- domain = '.' + domainParts.slice(-2).join('.');
41
- }
42
- return {
43
- secure: isHttps,
44
- sameSite: isHttps ? 'strict' : 'lax',
45
- ...domain && {
46
- domain
47
- }
48
- };
49
- }
50
- /**
51
- * Parse duration string to milliseconds.
52
- * @example '7d' → 604800000, '24h' → 86400000, '30m' → 1800000
53
- */ export function parseDurationToMs(duration, defaultMs = TIME_UNIT_MS.w) {
54
- const match = duration.match(/^(\d+)(s|m|h|d|w)$/);
55
- if (!match) return defaultMs;
56
- const value = parseInt(match[1], 10);
57
- const unit = match[2];
58
- return value * (TIME_UNIT_MS[unit] ?? TIME_UNIT_MS.d);
59
- }
60
- /**
61
- * Determine the frontend base URL for auth-related links.
62
- * Uses allowed origins from envConfig first, then falls back to request host.
63
- */ export function extractFrontendUrl(req) {
64
- const allowedOrigins = envConfig.getOrigins();
65
- const origin = (req.get('origin') ?? req.get('referer') ?? '').trim();
66
- if (origin && allowedOrigins.length) {
67
- const matched = allowedOrigins.find((u)=>origin.startsWith(u));
68
- if (matched) {
69
- return matched;
70
- }
71
- }
72
- const host = req.get('host');
73
- if (!host) {
74
- throw new Error('Unable to determine frontend URL from request');
75
- }
76
- return `${req.protocol}://${host}`;
77
- }
78
- export function buildFrontendUrl(req, path) {
79
- const baseUrl = extractFrontendUrl(req);
80
- return `${baseUrl}${path}`;
81
- }