@c15t/backend 1.5.0 → 1.6.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 (489) hide show
  1. package/.turbo/turbo-build.log +63 -37
  2. package/CHANGELOG.md +4 -209
  3. package/README.md +86 -164
  4. package/dist/contracts/consent/index.d.ts +103 -615
  5. package/dist/contracts/consent/index.d.ts.map +1 -1
  6. package/dist/contracts/consent/post.contract.d.ts +42 -140
  7. package/dist/contracts/consent/post.contract.d.ts.map +1 -1
  8. package/dist/contracts/consent/show-banner.contract.d.ts +28 -376
  9. package/dist/contracts/consent/show-banner.contract.d.ts.map +1 -1
  10. package/dist/contracts/consent/verify.contract.d.ts +33 -99
  11. package/dist/contracts/consent/verify.contract.d.ts.map +1 -1
  12. package/dist/contracts/index.d.ts +222 -1356
  13. package/dist/contracts/index.d.ts.map +1 -1
  14. package/dist/contracts/meta/index.d.ts +8 -63
  15. package/dist/contracts/meta/index.d.ts.map +1 -1
  16. package/dist/contracts/meta/status.contract.d.ts +8 -63
  17. package/dist/contracts/meta/status.contract.d.ts.map +1 -1
  18. package/dist/contracts/shared/jurisdiction.schema.d.ts +21 -9
  19. package/dist/contracts/shared/jurisdiction.schema.d.ts.map +1 -1
  20. package/dist/contracts.cjs +100 -106
  21. package/dist/contracts.js +100 -106
  22. package/dist/core.cjs +681 -681
  23. package/dist/core.d.ts +118 -678
  24. package/dist/core.d.ts.map +1 -1
  25. package/dist/core.js +634 -637
  26. package/dist/handlers/consent/index.d.ts +103 -615
  27. package/dist/handlers/consent/index.d.ts.map +1 -1
  28. package/dist/handlers/consent/post.handler.d.ts +42 -140
  29. package/dist/handlers/consent/post.handler.d.ts.map +1 -1
  30. package/dist/handlers/consent/show-banner/handler.d.ts +28 -376
  31. package/dist/handlers/consent/show-banner/handler.d.ts.map +1 -1
  32. package/dist/handlers/consent/show-banner/translations.d.ts.map +1 -1
  33. package/dist/handlers/consent/verify.handler.d.ts +33 -99
  34. package/dist/handlers/consent/verify.handler.d.ts.map +1 -1
  35. package/dist/handlers/meta/index.d.ts +8 -63
  36. package/dist/handlers/meta/index.d.ts.map +1 -1
  37. package/dist/handlers/meta/status.handler.d.ts +8 -63
  38. package/dist/handlers/meta/status.handler.d.ts.map +1 -1
  39. package/dist/init.d.ts.map +1 -1
  40. package/dist/middleware/openapi/index.d.ts +2 -2
  41. package/dist/middleware/openapi/index.d.ts.map +1 -1
  42. package/dist/pkgs/data-model/fields/index.cjs +14 -26
  43. package/dist/pkgs/data-model/fields/index.d.ts +4 -4
  44. package/dist/pkgs/data-model/fields/index.d.ts.map +1 -1
  45. package/dist/pkgs/data-model/fields/index.js +14 -26
  46. package/dist/pkgs/data-model/fields/zod-fields.d.ts +195 -871
  47. package/dist/pkgs/data-model/fields/zod-fields.d.ts.map +1 -1
  48. package/dist/pkgs/data-model/hooks/index.d.ts +2 -2
  49. package/dist/pkgs/data-model/hooks/index.d.ts.map +1 -1
  50. package/dist/pkgs/data-model/index.cjs +346 -358
  51. package/dist/pkgs/data-model/index.d.ts +1 -1
  52. package/dist/pkgs/data-model/index.d.ts.map +1 -1
  53. package/dist/pkgs/data-model/index.js +345 -357
  54. package/dist/pkgs/data-model/schema/index.cjs +346 -358
  55. package/dist/pkgs/data-model/schema/index.d.ts +1 -1
  56. package/dist/pkgs/data-model/schema/index.d.ts.map +1 -1
  57. package/dist/pkgs/data-model/schema/index.js +345 -357
  58. package/dist/pkgs/data-model/schema/schemas.d.ts +2 -2
  59. package/dist/pkgs/data-model/schema/schemas.d.ts.map +1 -1
  60. package/dist/pkgs/db-adapters/adapters/drizzle-adapter/drizzle-adapter.d.ts +3 -0
  61. package/dist/pkgs/db-adapters/adapters/drizzle-adapter/drizzle-adapter.d.ts.map +1 -1
  62. package/dist/pkgs/db-adapters/adapters/drizzle-adapter/index.cjs +158 -170
  63. package/dist/pkgs/db-adapters/adapters/drizzle-adapter/index.js +157 -169
  64. package/dist/pkgs/db-adapters/adapters/index.d.ts +2 -2
  65. package/dist/pkgs/db-adapters/adapters/index.d.ts.map +1 -1
  66. package/dist/pkgs/db-adapters/adapters/kysely-adapter/index.cjs +215 -227
  67. package/dist/pkgs/db-adapters/adapters/kysely-adapter/index.d.ts +2 -2
  68. package/dist/pkgs/db-adapters/adapters/kysely-adapter/index.d.ts.map +1 -1
  69. package/dist/pkgs/db-adapters/adapters/kysely-adapter/index.js +213 -225
  70. package/dist/pkgs/db-adapters/adapters/kysely-adapter/kysely-adapter.d.ts +2 -0
  71. package/dist/pkgs/db-adapters/adapters/kysely-adapter/kysely-adapter.d.ts.map +1 -1
  72. package/dist/pkgs/db-adapters/adapters/kysely-adapter/tests/test-utils.d.ts +1 -1
  73. package/dist/pkgs/db-adapters/adapters/kysely-adapter/tests/test-utils.d.ts.map +1 -1
  74. package/dist/pkgs/db-adapters/adapters/memory-adapter/index.cjs +158 -170
  75. package/dist/pkgs/db-adapters/adapters/memory-adapter/index.js +157 -169
  76. package/dist/pkgs/db-adapters/adapters/memory-adapter/memory-adapter.d.ts +3 -0
  77. package/dist/pkgs/db-adapters/adapters/memory-adapter/memory-adapter.d.ts.map +1 -1
  78. package/dist/pkgs/db-adapters/adapters/prisma-adapter/index.cjs +243 -255
  79. package/dist/pkgs/db-adapters/adapters/prisma-adapter/index.d.ts +1 -1
  80. package/dist/pkgs/db-adapters/adapters/prisma-adapter/index.d.ts.map +1 -1
  81. package/dist/pkgs/db-adapters/adapters/prisma-adapter/index.js +241 -253
  82. package/dist/pkgs/db-adapters/adapters/prisma-adapter/prisma-adapter.d.ts +3 -0
  83. package/dist/pkgs/db-adapters/adapters/prisma-adapter/prisma-adapter.d.ts.map +1 -1
  84. package/dist/pkgs/db-adapters/index.cjs +714 -726
  85. package/dist/pkgs/db-adapters/index.d.ts +6 -6
  86. package/dist/pkgs/db-adapters/index.d.ts.map +1 -1
  87. package/dist/pkgs/db-adapters/index.js +708 -720
  88. package/dist/pkgs/migrations/get-migration.d.ts.map +1 -1
  89. package/dist/pkgs/migrations/get-schema/get-schema.d.ts.map +1 -1
  90. package/dist/pkgs/migrations/get-schema/process-tables.d.ts.map +1 -1
  91. package/dist/pkgs/migrations/index.cjs +236 -248
  92. package/dist/pkgs/migrations/index.d.ts +4 -4
  93. package/dist/pkgs/migrations/index.d.ts.map +1 -1
  94. package/dist/pkgs/migrations/index.js +235 -247
  95. package/dist/pkgs/results/index.cjs +67 -67
  96. package/dist/pkgs/results/index.d.ts +5 -5
  97. package/dist/pkgs/results/index.d.ts.map +1 -1
  98. package/dist/pkgs/results/index.js +67 -67
  99. package/dist/pkgs/results/orpc-error-handler.d.ts +1 -1
  100. package/dist/pkgs/results/orpc-error-handler.d.ts.map +1 -1
  101. package/dist/pkgs/types/index.d.ts +1 -2
  102. package/dist/pkgs/types/index.d.ts.map +1 -1
  103. package/dist/pkgs/types/options.d.ts +9 -2
  104. package/dist/pkgs/types/options.d.ts.map +1 -1
  105. package/dist/pkgs/utils/index.d.ts +1 -1
  106. package/dist/pkgs/utils/index.d.ts.map +1 -1
  107. package/dist/pkgs/utils/logger.d.ts +1 -1
  108. package/dist/pkgs/utils/logger.d.ts.map +1 -1
  109. package/dist/router.cjs +114 -117
  110. package/dist/router.d.ts +111 -678
  111. package/dist/router.d.ts.map +1 -1
  112. package/dist/router.js +114 -117
  113. package/dist/schema/audit-log/schema.d.ts +2 -24
  114. package/dist/schema/audit-log/schema.d.ts.map +1 -1
  115. package/dist/schema/audit-log/table.d.ts +2 -24
  116. package/dist/schema/audit-log/table.d.ts.map +1 -1
  117. package/dist/schema/consent/registry.d.ts +8 -8
  118. package/dist/schema/consent/schema.d.ts +9 -33
  119. package/dist/schema/consent/schema.d.ts.map +1 -1
  120. package/dist/schema/consent/table.d.ts +9 -33
  121. package/dist/schema/consent/table.d.ts.map +1 -1
  122. package/dist/schema/consent-policy/registry.d.ts +20 -20
  123. package/dist/schema/consent-policy/schema.d.ts +22 -30
  124. package/dist/schema/consent-policy/schema.d.ts.map +1 -1
  125. package/dist/schema/consent-policy/table.d.ts +13 -29
  126. package/dist/schema/consent-policy/table.d.ts.map +1 -1
  127. package/dist/schema/consent-purpose/registry.d.ts +6 -6
  128. package/dist/schema/consent-purpose/schema.d.ts +5 -27
  129. package/dist/schema/consent-purpose/schema.d.ts.map +1 -1
  130. package/dist/schema/consent-purpose/table.d.ts +5 -27
  131. package/dist/schema/consent-purpose/table.d.ts.map +1 -1
  132. package/dist/schema/consent-record/schema.d.ts +3 -19
  133. package/dist/schema/consent-record/schema.d.ts.map +1 -1
  134. package/dist/schema/consent-record/table.d.ts +3 -19
  135. package/dist/schema/consent-record/table.d.ts.map +1 -1
  136. package/dist/schema/create-registry.d.ts +58 -58
  137. package/dist/schema/definition.d.ts +42 -176
  138. package/dist/schema/definition.d.ts.map +1 -1
  139. package/dist/schema/domain/registry.d.ts +20 -20
  140. package/dist/schema/domain/schema.d.ts +6 -24
  141. package/dist/schema/domain/schema.d.ts.map +1 -1
  142. package/dist/schema/domain/table.d.ts +6 -24
  143. package/dist/schema/domain/table.d.ts.map +1 -1
  144. package/dist/schema/index.cjs +426 -438
  145. package/dist/schema/index.d.ts +12 -12
  146. package/dist/schema/index.d.ts.map +1 -1
  147. package/dist/schema/index.js +426 -438
  148. package/dist/schema/schemas.d.ts +42 -176
  149. package/dist/schema/schemas.d.ts.map +1 -1
  150. package/dist/schema/subject/registry.d.ts +4 -4
  151. package/dist/schema/subject/schema.d.ts +4 -20
  152. package/dist/schema/subject/schema.d.ts.map +1 -1
  153. package/dist/schema/subject/table.d.ts +4 -20
  154. package/dist/schema/subject/table.d.ts.map +1 -1
  155. package/dist/schema/types.d.ts +1 -1
  156. package/dist/schema/types.d.ts.map +1 -1
  157. package/dist/testing/contract-testing.d.ts +3 -2
  158. package/dist/testing/contract-testing.d.ts.map +1 -1
  159. package/dist/types/index.d.ts +5 -4
  160. package/dist/types/index.d.ts.map +1 -1
  161. package/dist/types/options.d.ts +2 -2
  162. package/dist/types/options.d.ts.map +1 -1
  163. package/dist/v2/contracts/consent/index.d.ts +260 -0
  164. package/dist/v2/contracts/consent/index.d.ts.map +1 -0
  165. package/dist/v2/contracts/consent/index.test.d.ts +2 -0
  166. package/dist/v2/contracts/consent/index.test.d.ts.map +1 -0
  167. package/dist/v2/contracts/consent/post.contract.d.ts +114 -0
  168. package/dist/v2/contracts/consent/post.contract.d.ts.map +1 -0
  169. package/dist/v2/contracts/consent/post.contract.test.d.ts +2 -0
  170. package/dist/v2/contracts/consent/post.contract.test.d.ts.map +1 -0
  171. package/dist/v2/contracts/consent/show-banner.contract.d.ts +68 -0
  172. package/dist/v2/contracts/consent/show-banner.contract.d.ts.map +1 -0
  173. package/dist/v2/contracts/consent/show-banner.contract.test.d.ts +2 -0
  174. package/dist/v2/contracts/consent/show-banner.contract.test.d.ts.map +1 -0
  175. package/dist/v2/contracts/consent/verify.contract.d.ts +81 -0
  176. package/dist/v2/contracts/consent/verify.contract.d.ts.map +1 -0
  177. package/dist/v2/contracts/consent/verify.contract.test.d.ts +2 -0
  178. package/dist/v2/contracts/consent/verify.contract.test.d.ts.map +1 -0
  179. package/dist/v2/contracts/index.cjs +644 -0
  180. package/dist/v2/contracts/index.d.ts +563 -0
  181. package/dist/v2/contracts/index.d.ts.map +1 -0
  182. package/dist/v2/contracts/index.js +607 -0
  183. package/dist/v2/contracts/meta/index.d.ts +19 -0
  184. package/dist/v2/contracts/meta/index.d.ts.map +1 -0
  185. package/dist/v2/contracts/meta/index.test.d.ts +2 -0
  186. package/dist/v2/contracts/meta/index.test.d.ts.map +1 -0
  187. package/dist/v2/contracts/meta/status.contract.d.ts +18 -0
  188. package/dist/v2/contracts/meta/status.contract.d.ts.map +1 -0
  189. package/dist/v2/contracts/meta/status.contract.test.d.ts +2 -0
  190. package/dist/v2/contracts/meta/status.contract.test.d.ts.map +1 -0
  191. package/dist/v2/contracts/shared/jurisdiction.schema.d.ts +36 -0
  192. package/dist/v2/contracts/shared/jurisdiction.schema.d.ts.map +1 -0
  193. package/dist/v2/contracts/test.utils.d.ts +38 -0
  194. package/dist/v2/contracts/test.utils.d.ts.map +1 -0
  195. package/dist/v2/core.cjs +2181 -0
  196. package/dist/v2/core.d.ts +364 -0
  197. package/dist/v2/core.d.ts.map +1 -0
  198. package/dist/v2/core.js +2130 -0
  199. package/dist/v2/db/adapters/drizzle.cjs +36 -0
  200. package/dist/v2/db/adapters/drizzle.d.ts +2 -0
  201. package/dist/v2/db/adapters/drizzle.d.ts.map +1 -0
  202. package/dist/v2/db/adapters/drizzle.js +3 -0
  203. package/dist/v2/db/adapters/index.cjs +18 -0
  204. package/dist/v2/db/adapters/index.d.ts +2 -0
  205. package/dist/v2/db/adapters/index.d.ts.map +1 -0
  206. package/dist/v2/db/adapters/index.js +0 -0
  207. package/dist/v2/db/adapters/kysely.cjs +36 -0
  208. package/dist/v2/db/adapters/kysely.d.ts +2 -0
  209. package/dist/v2/db/adapters/kysely.d.ts.map +1 -0
  210. package/dist/v2/db/adapters/kysely.js +3 -0
  211. package/dist/v2/db/adapters/mongo.cjs +36 -0
  212. package/dist/v2/db/adapters/mongo.d.ts +2 -0
  213. package/dist/v2/db/adapters/mongo.d.ts.map +1 -0
  214. package/dist/v2/db/adapters/mongo.js +3 -0
  215. package/dist/v2/db/adapters/prisma.cjs +36 -0
  216. package/dist/v2/db/adapters/prisma.d.ts +2 -0
  217. package/dist/v2/db/adapters/prisma.d.ts.map +1 -0
  218. package/dist/v2/db/adapters/prisma.js +3 -0
  219. package/dist/v2/db/adapters/typeorm.cjs +36 -0
  220. package/dist/v2/db/adapters/typeorm.d.ts +2 -0
  221. package/dist/v2/db/adapters/typeorm.d.ts.map +1 -0
  222. package/dist/v2/db/adapters/typeorm.js +3 -0
  223. package/dist/v2/db/migrator/index.cjs +61 -0
  224. package/dist/v2/db/migrator/index.d.ts +29 -0
  225. package/dist/v2/db/migrator/index.d.ts.map +1 -0
  226. package/dist/v2/db/migrator/index.js +27 -0
  227. package/dist/v2/db/registry/audit-log.d.ts +21 -0
  228. package/dist/v2/db/registry/audit-log.d.ts.map +1 -0
  229. package/dist/v2/db/registry/audit-log.test.d.ts +2 -0
  230. package/dist/v2/db/registry/audit-log.test.d.ts.map +1 -0
  231. package/dist/v2/db/registry/consent-policy.d.ts +29 -0
  232. package/dist/v2/db/registry/consent-policy.d.ts.map +1 -0
  233. package/dist/v2/db/registry/consent-policy.test.d.ts +2 -0
  234. package/dist/v2/db/registry/consent-policy.test.d.ts.map +1 -0
  235. package/dist/v2/db/registry/consent-purpose.d.ts +16 -0
  236. package/dist/v2/db/registry/consent-purpose.d.ts.map +1 -0
  237. package/dist/v2/db/registry/consent-purpose.test.d.ts +2 -0
  238. package/dist/v2/db/registry/consent-purpose.test.d.ts.map +1 -0
  239. package/dist/v2/db/registry/consent.d.ts +20 -0
  240. package/dist/v2/db/registry/consent.d.ts.map +1 -0
  241. package/dist/v2/db/registry/consent.test.d.ts +2 -0
  242. package/dist/v2/db/registry/consent.test.d.ts.map +1 -0
  243. package/dist/v2/db/registry/domain.d.ts +24 -0
  244. package/dist/v2/db/registry/domain.d.ts.map +1 -0
  245. package/dist/v2/db/registry/domain.test.d.ts +2 -0
  246. package/dist/v2/db/registry/domain.test.d.ts.map +1 -0
  247. package/dist/v2/db/registry/index.d.ts +102 -0
  248. package/dist/v2/db/registry/index.d.ts.map +1 -0
  249. package/dist/v2/db/registry/subject.d.ts +18 -0
  250. package/dist/v2/db/registry/subject.d.ts.map +1 -0
  251. package/dist/v2/db/registry/subject.test.d.ts +2 -0
  252. package/dist/v2/db/registry/subject.test.d.ts.map +1 -0
  253. package/dist/v2/db/registry/types.d.ts +10 -0
  254. package/dist/v2/db/registry/types.d.ts.map +1 -0
  255. package/dist/v2/db/registry/utils/generate-id.d.ts +25 -0
  256. package/dist/v2/db/registry/utils/generate-id.d.ts.map +1 -0
  257. package/dist/v2/db/registry/utils/generate-id.test.d.ts +2 -0
  258. package/dist/v2/db/registry/utils/generate-id.test.d.ts.map +1 -0
  259. package/dist/v2/db/registry/utils.d.ts +25 -0
  260. package/dist/v2/db/registry/utils.d.ts.map +1 -0
  261. package/dist/v2/db/schema/1.0.0/audit-log.d.ts +29 -0
  262. package/dist/v2/db/schema/1.0.0/audit-log.d.ts.map +1 -0
  263. package/dist/v2/db/schema/1.0.0/consent-policy.d.ts +45 -0
  264. package/dist/v2/db/schema/1.0.0/consent-policy.d.ts.map +1 -0
  265. package/dist/v2/db/schema/1.0.0/consent-purpose.d.ts +27 -0
  266. package/dist/v2/db/schema/1.0.0/consent-purpose.d.ts.map +1 -0
  267. package/dist/v2/db/schema/1.0.0/consent-record.d.ts +19 -0
  268. package/dist/v2/db/schema/1.0.0/consent-record.d.ts.map +1 -0
  269. package/dist/v2/db/schema/1.0.0/consent.d.ts +42 -0
  270. package/dist/v2/db/schema/1.0.0/consent.d.ts.map +1 -0
  271. package/dist/v2/db/schema/1.0.0/domain.d.ts +23 -0
  272. package/dist/v2/db/schema/1.0.0/domain.d.ts.map +1 -0
  273. package/dist/v2/db/schema/1.0.0/index.d.ts +1513 -0
  274. package/dist/v2/db/schema/1.0.0/index.d.ts.map +1 -0
  275. package/dist/v2/db/schema/1.0.0/subject.d.ts +23 -0
  276. package/dist/v2/db/schema/1.0.0/subject.d.ts.map +1 -0
  277. package/dist/v2/db/schema/index.cjs +326 -0
  278. package/dist/v2/db/schema/index.d.ts +1507 -0
  279. package/dist/v2/db/schema/index.d.ts.map +1 -0
  280. package/dist/v2/db/schema/index.js +241 -0
  281. package/dist/v2/define-config.cjs +36 -0
  282. package/dist/v2/define-config.d.ts +5 -0
  283. package/dist/v2/define-config.d.ts.map +1 -0
  284. package/dist/v2/define-config.js +2 -0
  285. package/dist/v2/handlers/consent/index.d.ts +260 -0
  286. package/dist/v2/handlers/consent/index.d.ts.map +1 -0
  287. package/dist/v2/handlers/consent/post.handler.d.ts +136 -0
  288. package/dist/v2/handlers/consent/post.handler.d.ts.map +1 -0
  289. package/dist/v2/handlers/consent/show-banner/geo.d.ts +10 -0
  290. package/dist/v2/handlers/consent/show-banner/geo.d.ts.map +1 -0
  291. package/dist/v2/handlers/consent/show-banner/geo.test.d.ts +2 -0
  292. package/dist/v2/handlers/consent/show-banner/geo.test.d.ts.map +1 -0
  293. package/dist/v2/handlers/consent/show-banner/handler.d.ts +71 -0
  294. package/dist/v2/handlers/consent/show-banner/handler.d.ts.map +1 -0
  295. package/dist/v2/handlers/consent/show-banner/handler.test.d.ts +2 -0
  296. package/dist/v2/handlers/consent/show-banner/handler.test.d.ts.map +1 -0
  297. package/dist/v2/handlers/consent/show-banner/translations.d.ts +13 -0
  298. package/dist/v2/handlers/consent/show-banner/translations.d.ts.map +1 -0
  299. package/dist/v2/handlers/consent/show-banner/translations.test.d.ts +2 -0
  300. package/dist/v2/handlers/consent/show-banner/translations.test.d.ts.map +1 -0
  301. package/dist/v2/handlers/consent/verify.handler.d.ts +103 -0
  302. package/dist/v2/handlers/consent/verify.handler.d.ts.map +1 -0
  303. package/dist/v2/handlers/meta/index.d.ts +19 -0
  304. package/dist/v2/handlers/meta/index.d.ts.map +1 -0
  305. package/dist/v2/handlers/meta/status.handler.d.ts +17 -0
  306. package/dist/v2/handlers/meta/status.handler.d.ts.map +1 -0
  307. package/dist/v2/init.d.ts +3 -0
  308. package/dist/v2/init.d.ts.map +1 -0
  309. package/dist/v2/init.test.d.ts +2 -0
  310. package/dist/v2/init.test.d.ts.map +1 -0
  311. package/dist/v2/middleware/cors/cors.d.ts +37 -0
  312. package/dist/v2/middleware/cors/cors.d.ts.map +1 -0
  313. package/dist/v2/middleware/cors/cors.test.d.ts +2 -0
  314. package/dist/v2/middleware/cors/cors.test.d.ts.map +1 -0
  315. package/dist/v2/middleware/cors/index.d.ts +30 -0
  316. package/dist/v2/middleware/cors/index.d.ts.map +1 -0
  317. package/dist/v2/middleware/cors/is-origin-trusted.d.ts +49 -0
  318. package/dist/v2/middleware/cors/is-origin-trusted.d.ts.map +1 -0
  319. package/dist/v2/middleware/cors/is-origin-trusted.test.d.ts +2 -0
  320. package/dist/v2/middleware/cors/is-origin-trusted.test.d.ts.map +1 -0
  321. package/dist/v2/middleware/cors/process-cors.d.ts +31 -0
  322. package/dist/v2/middleware/cors/process-cors.d.ts.map +1 -0
  323. package/dist/v2/middleware/openapi/config.d.ts +28 -0
  324. package/dist/v2/middleware/openapi/config.d.ts.map +1 -0
  325. package/dist/v2/middleware/openapi/handlers.d.ts +29 -0
  326. package/dist/v2/middleware/openapi/handlers.d.ts.map +1 -0
  327. package/dist/v2/middleware/openapi/index.d.ts +11 -0
  328. package/dist/v2/middleware/openapi/index.d.ts.map +1 -0
  329. package/dist/v2/middleware/process-ip/index.d.ts +3 -0
  330. package/dist/v2/middleware/process-ip/index.d.ts.map +1 -0
  331. package/dist/v2/router.cjs +1275 -0
  332. package/dist/v2/router.d.ts +280 -0
  333. package/dist/v2/router.d.ts.map +1 -0
  334. package/dist/v2/router.js +1231 -0
  335. package/dist/v2/types/api.d.ts +27 -0
  336. package/dist/v2/types/api.d.ts.map +1 -0
  337. package/dist/v2/types/index.cjs +40 -0
  338. package/dist/v2/types/index.d.ts +104 -0
  339. package/dist/v2/types/index.d.ts.map +1 -0
  340. package/dist/v2/types/index.js +6 -0
  341. package/dist/v2/utils/create-telemetry-options.d.ts +28 -0
  342. package/dist/v2/utils/create-telemetry-options.d.ts.map +1 -0
  343. package/dist/v2/utils/env.d.ts +60 -0
  344. package/dist/v2/utils/env.d.ts.map +1 -0
  345. package/dist/v2/utils/index.d.ts +3 -0
  346. package/dist/v2/utils/index.d.ts.map +1 -0
  347. package/dist/v2/utils/logger.d.ts +16 -0
  348. package/dist/v2/utils/logger.d.ts.map +1 -0
  349. package/dist/version.d.ts +1 -1
  350. package/package.json +106 -15
  351. package/readme.json +30 -0
  352. package/rslib.config.ts +13 -14
  353. package/src/__tests__/server.test.ts +1 -1
  354. package/src/contracts/consent/post.contract.test.ts +3 -8
  355. package/src/contracts/consent/post.contract.ts +13 -13
  356. package/src/contracts/consent/show-banner.contract.test.ts +9 -0
  357. package/src/contracts/consent/show-banner.contract.ts +2 -0
  358. package/src/contracts/consent/verify.contract.ts +19 -23
  359. package/src/core.ts +7 -0
  360. package/src/handlers/consent/show-banner/handler.ts +12 -9
  361. package/src/handlers/consent/show-banner/translations.ts +2 -2
  362. package/src/init.ts +9 -6
  363. package/src/middleware/openapi/index.ts +2 -2
  364. package/src/pkgs/api-router/hooks/__tests__/processor.test.ts +1 -1
  365. package/src/pkgs/data-model/fields/index.ts +17 -22
  366. package/src/pkgs/data-model/fields/zod-fields.ts +14 -26
  367. package/src/pkgs/data-model/hooks/index.ts +3 -2
  368. package/src/pkgs/data-model/index.ts +2 -4
  369. package/src/pkgs/data-model/schema/index.ts +6 -7
  370. package/src/pkgs/data-model/schema/schemas.ts +3 -3
  371. package/src/pkgs/db-adapters/adapters/drizzle-adapter/drizzle-adapter.ts +4 -1
  372. package/src/pkgs/db-adapters/adapters/index.ts +2 -2
  373. package/src/pkgs/db-adapters/adapters/kysely-adapter/index.ts +4 -4
  374. package/src/pkgs/db-adapters/adapters/kysely-adapter/kysely-adapter.ts +4 -5
  375. package/src/pkgs/db-adapters/adapters/kysely-adapter/tests/postgres.test.ts +2 -4
  376. package/src/pkgs/db-adapters/adapters/kysely-adapter/tests/sqlite.test.ts +2 -3
  377. package/src/pkgs/db-adapters/adapters/kysely-adapter/tests/test-utils.ts +1 -6
  378. package/src/pkgs/db-adapters/adapters/memory-adapter/memory-adapter.ts +4 -1
  379. package/src/pkgs/db-adapters/adapters/prisma-adapter/index.ts +1 -1
  380. package/src/pkgs/db-adapters/adapters/prisma-adapter/prisma-adapter.ts +5 -2
  381. package/src/pkgs/db-adapters/index.ts +12 -13
  382. package/src/pkgs/migrations/get-migration.ts +4 -2
  383. package/src/pkgs/migrations/get-schema/get-schema.ts +0 -1
  384. package/src/pkgs/migrations/get-schema/process-fields.ts +1 -1
  385. package/src/pkgs/migrations/get-schema/process-tables.ts +0 -2
  386. package/src/pkgs/migrations/index.ts +7 -8
  387. package/src/pkgs/results/__tests__/error-codes.test.ts +2 -2
  388. package/src/pkgs/results/index.ts +22 -27
  389. package/src/pkgs/results/orpc-error-handler.ts +1 -1
  390. package/src/pkgs/results/results/result-helpers.ts +1 -1
  391. package/src/pkgs/types/index.ts +4 -4
  392. package/src/pkgs/types/options.ts +10 -3
  393. package/src/pkgs/utils/index.ts +1 -1
  394. package/src/pkgs/utils/logger.ts +1 -1
  395. package/src/schema/audit-log/schema.ts +3 -3
  396. package/src/schema/consent/schema.ts +4 -4
  397. package/src/schema/consent-policy/schema.ts +3 -3
  398. package/src/schema/consent-purpose/schema.ts +4 -4
  399. package/src/schema/consent-record/schema.ts +3 -3
  400. package/src/schema/definition.ts +1 -1
  401. package/src/schema/domain/schema.ts +5 -5
  402. package/src/schema/index.ts +14 -17
  403. package/src/schema/subject/schema.ts +3 -3
  404. package/src/schema/types.ts +1 -1
  405. package/src/testing/contract-testing.ts +15 -52
  406. package/src/types/index.ts +8 -8
  407. package/src/types/options.ts +2 -3
  408. package/src/v2/contracts/consent/index.test.ts +5 -0
  409. package/src/v2/contracts/consent/index.ts +9 -0
  410. package/src/v2/contracts/consent/post.contract.test.ts +521 -0
  411. package/src/v2/contracts/consent/post.contract.ts +155 -0
  412. package/src/v2/contracts/consent/show-banner.contract.test.ts +252 -0
  413. package/src/v2/contracts/consent/show-banner.contract.ts +73 -0
  414. package/src/v2/contracts/consent/verify.contract.test.ts +185 -0
  415. package/src/v2/contracts/consent/verify.contract.ts +122 -0
  416. package/src/v2/contracts/index.ts +20 -0
  417. package/src/v2/contracts/meta/index.test.ts +5 -0
  418. package/src/v2/contracts/meta/index.ts +5 -0
  419. package/src/v2/contracts/meta/status.contract.test.ts +226 -0
  420. package/src/v2/contracts/meta/status.contract.ts +34 -0
  421. package/src/v2/contracts/shared/jurisdiction.schema.ts +30 -0
  422. package/src/v2/contracts/test.utils.ts +400 -0
  423. package/src/v2/core.ts +379 -0
  424. package/src/v2/db/adapters/drizzle.ts +1 -0
  425. package/src/v2/db/adapters/index.ts +1 -0
  426. package/src/v2/db/adapters/kysely.ts +1 -0
  427. package/src/v2/db/adapters/mongo.ts +1 -0
  428. package/src/v2/db/adapters/prisma.ts +1 -0
  429. package/src/v2/db/adapters/typeorm.ts +1 -0
  430. package/src/v2/db/migrator/index.ts +80 -0
  431. package/src/v2/db/registry/audit-log.test.ts +77 -0
  432. package/src/v2/db/registry/audit-log.ts +46 -0
  433. package/src/v2/db/registry/consent-policy.test.ts +778 -0
  434. package/src/v2/db/registry/consent-policy.ts +74 -0
  435. package/src/v2/db/registry/consent-purpose.test.ts +485 -0
  436. package/src/v2/db/registry/consent-purpose.ts +41 -0
  437. package/src/v2/db/registry/consent.test.ts +843 -0
  438. package/src/v2/db/registry/consent.ts +42 -0
  439. package/src/v2/db/registry/domain.test.ts +463 -0
  440. package/src/v2/db/registry/domain.ts +51 -0
  441. package/src/v2/db/registry/index.ts +18 -0
  442. package/src/v2/db/registry/subject.test.ts +497 -0
  443. package/src/v2/db/registry/subject.ts +101 -0
  444. package/src/v2/db/registry/types.ts +10 -0
  445. package/src/v2/db/registry/utils/generate-id.test.ts +217 -0
  446. package/src/v2/db/registry/utils/generate-id.ts +134 -0
  447. package/src/v2/db/registry/utils.ts +134 -0
  448. package/src/v2/db/schema/1.0.0/audit-log.ts +32 -0
  449. package/src/v2/db/schema/1.0.0/consent-policy.ts +41 -0
  450. package/src/v2/db/schema/1.0.0/consent-purpose.ts +30 -0
  451. package/src/v2/db/schema/1.0.0/consent-record.ts +22 -0
  452. package/src/v2/db/schema/1.0.0/consent.ts +38 -0
  453. package/src/v2/db/schema/1.0.0/domain.ts +26 -0
  454. package/src/v2/db/schema/1.0.0/index.ts +56 -0
  455. package/src/v2/db/schema/1.0.0/subject.ts +26 -0
  456. package/src/v2/db/schema/index.ts +9 -0
  457. package/src/v2/define-config.ts +5 -0
  458. package/src/v2/handlers/consent/index.ts +9 -0
  459. package/src/v2/handlers/consent/post.handler.ts +254 -0
  460. package/src/v2/handlers/consent/show-banner/geo.test.ts +281 -0
  461. package/src/v2/handlers/consent/show-banner/geo.ts +96 -0
  462. package/src/v2/handlers/consent/show-banner/handler.test.ts +374 -0
  463. package/src/v2/handlers/consent/show-banner/handler.ts +123 -0
  464. package/src/v2/handlers/consent/show-banner/translations.test.ts +121 -0
  465. package/src/v2/handlers/consent/show-banner/translations.ts +79 -0
  466. package/src/v2/handlers/consent/verify.handler.ts +288 -0
  467. package/src/v2/handlers/meta/index.ts +5 -0
  468. package/src/v2/handlers/meta/status.handler.ts +43 -0
  469. package/src/v2/init.test.ts +114 -0
  470. package/src/v2/init.ts +126 -0
  471. package/src/v2/middleware/cors/cors.test.ts +111 -0
  472. package/src/v2/middleware/cors/cors.ts +192 -0
  473. package/src/v2/middleware/cors/index.ts +30 -0
  474. package/src/v2/middleware/cors/is-origin-trusted.test.ts +104 -0
  475. package/src/v2/middleware/cors/is-origin-trusted.ts +126 -0
  476. package/src/v2/middleware/cors/process-cors.ts +91 -0
  477. package/src/v2/middleware/openapi/config.ts +27 -0
  478. package/src/v2/middleware/openapi/handlers.ts +132 -0
  479. package/src/v2/middleware/openapi/index.ts +11 -0
  480. package/src/v2/middleware/process-ip/index.ts +39 -0
  481. package/src/v2/router.ts +8 -0
  482. package/src/v2/types/api.ts +32 -0
  483. package/src/v2/types/index.ts +121 -0
  484. package/src/v2/utils/create-telemetry-options.ts +115 -0
  485. package/src/v2/utils/env.ts +84 -0
  486. package/src/v2/utils/index.ts +2 -0
  487. package/src/v2/utils/logger.ts +38 -0
  488. package/src/version.ts +1 -1
  489. package/vitest.config.ts +11 -2
@@ -0,0 +1,497 @@
1
+ import { ORPCError } from '@orpc/server';
2
+ import { afterEach, describe, expect, it, vi } from 'vitest';
3
+ import type { Subject } from '../schema';
4
+ import { subjectRegistry } from './subject';
5
+ import type { Registry } from './types';
6
+
7
+ // Mock generateUniqueId to return a predictable value for assertions
8
+ vi.mock('./utils/generate-id', () => ({
9
+ generateUniqueId: vi.fn().mockResolvedValue('sub_test_123'),
10
+ }));
11
+
12
+ describe('subjectRegistry', () => {
13
+ const mockLogger = {
14
+ debug: vi.fn(),
15
+ error: vi.fn(),
16
+ info: vi.fn(),
17
+ warn: vi.fn(),
18
+ };
19
+
20
+ const createMockSubject = (overrides: Partial<Subject> = {}): Subject => ({
21
+ id: 'sub_test_123',
22
+ isIdentified: false,
23
+ externalId: null,
24
+ identityProvider: 'anonymous',
25
+ lastIpAddress: '192.168.1.1',
26
+ createdAt: new Date('2024-01-01T00:00:00.000Z'),
27
+ updatedAt: new Date('2024-01-01T00:00:00.000Z'),
28
+ ...overrides,
29
+ });
30
+
31
+ afterEach(() => {
32
+ vi.clearAllMocks();
33
+ });
34
+
35
+ describe('findOrCreateSubject', () => {
36
+ describe('when both subjectId and externalSubjectId are provided', () => {
37
+ it('should return the subject when both IDs match an existing subject', async () => {
38
+ const mockSubject = createMockSubject({
39
+ id: 'sub_existing',
40
+ externalId: 'ext_123',
41
+ isIdentified: true,
42
+ });
43
+
44
+ const db = {
45
+ findFirst: vi.fn().mockResolvedValue(mockSubject),
46
+ };
47
+
48
+ const registry = subjectRegistry({
49
+ db,
50
+ ctx: { logger: mockLogger },
51
+ } as unknown as Registry);
52
+
53
+ const result = await registry.findOrCreateSubject({
54
+ subjectId: 'sub_existing',
55
+ externalSubjectId: 'ext_123',
56
+ ipAddress: '192.168.1.1',
57
+ });
58
+
59
+ expect(db.findFirst).toHaveBeenCalledWith('subject', {
60
+ where: expect.any(Function),
61
+ });
62
+
63
+ expect(result).toEqual(mockSubject);
64
+ });
65
+
66
+ it('should throw ORPCError when both IDs are provided but no matching subject exists', async () => {
67
+ const db = {
68
+ findFirst: vi.fn().mockResolvedValue(null),
69
+ };
70
+
71
+ const registry = subjectRegistry({
72
+ db,
73
+ ctx: { logger: mockLogger },
74
+ } as unknown as Registry);
75
+
76
+ const promise = registry.findOrCreateSubject({
77
+ subjectId: 'sub_nonexistent',
78
+ externalSubjectId: 'ext_nonexistent',
79
+ });
80
+
81
+ await expect(promise).rejects.toBeInstanceOf(ORPCError);
82
+ await expect(promise).rejects.toEqual(
83
+ expect.objectContaining({
84
+ code: 'SUBJECT_NOT_FOUND',
85
+ status: 404,
86
+ data: {
87
+ providedSubjectId: 'sub_nonexistent',
88
+ providedExternalId: 'ext_nonexistent',
89
+ },
90
+ })
91
+ );
92
+
93
+ expect(mockLogger.error).toHaveBeenCalledWith('Subject not found', {
94
+ providedSubjectId: 'sub_nonexistent',
95
+ providedExternalId: 'ext_nonexistent',
96
+ });
97
+ });
98
+ });
99
+
100
+ describe('when only subjectId is provided', () => {
101
+ it('should return the subject when found by subjectId', async () => {
102
+ const mockSubject = createMockSubject({
103
+ id: 'sub_existing',
104
+ });
105
+
106
+ const db = {
107
+ findFirst: vi.fn().mockResolvedValue(mockSubject),
108
+ };
109
+
110
+ const registry = subjectRegistry({
111
+ db,
112
+ ctx: { logger: mockLogger },
113
+ } as unknown as Registry);
114
+
115
+ const result = await registry.findOrCreateSubject({
116
+ subjectId: 'sub_existing',
117
+ });
118
+
119
+ expect(db.findFirst).toHaveBeenCalledWith('subject', {
120
+ where: expect.any(Function),
121
+ });
122
+
123
+ expect(result).toEqual(mockSubject);
124
+ });
125
+
126
+ it('should throw ORPCError when subject not found by subjectId', async () => {
127
+ const db = {
128
+ findFirst: vi.fn().mockResolvedValue(null),
129
+ };
130
+
131
+ const registry = subjectRegistry({
132
+ db,
133
+ ctx: { logger: mockLogger },
134
+ } as unknown as Registry);
135
+
136
+ const promise = registry.findOrCreateSubject({
137
+ subjectId: 'sub_nonexistent',
138
+ });
139
+
140
+ await expect(promise).rejects.toBeInstanceOf(ORPCError);
141
+ await expect(promise).rejects.toEqual(
142
+ expect.objectContaining({
143
+ code: 'SUBJECT_NOT_FOUND',
144
+ status: 404,
145
+ data: { subjectId: 'sub_nonexistent' },
146
+ })
147
+ );
148
+ });
149
+ });
150
+
151
+ describe('when only externalSubjectId is provided', () => {
152
+ it('should find existing subject by externalSubjectId', async () => {
153
+ const mockSubject = createMockSubject({
154
+ externalId: 'ext_existing',
155
+ isIdentified: true,
156
+ lastIpAddress: '192.168.1.100',
157
+ });
158
+
159
+ const db = {
160
+ upsert: vi.fn().mockResolvedValue(undefined),
161
+ findFirst: vi.fn().mockResolvedValue(mockSubject),
162
+ };
163
+
164
+ const registry = subjectRegistry({
165
+ db,
166
+ ctx: { logger: mockLogger },
167
+ } as unknown as Registry);
168
+
169
+ const result = await registry.findOrCreateSubject({
170
+ externalSubjectId: 'ext_existing',
171
+ ipAddress: '192.168.1.200',
172
+ });
173
+
174
+ expect(db.upsert).toHaveBeenCalledWith('subject', {
175
+ where: expect.any(Function),
176
+ create: {
177
+ id: 'sub_test_123',
178
+ externalId: 'ext_existing',
179
+ identityProvider: 'anonymous',
180
+ lastIpAddress: '192.168.1.200',
181
+ isIdentified: true,
182
+ },
183
+ update: { lastIpAddress: '192.168.1.200' },
184
+ });
185
+
186
+ expect(db.findFirst).toHaveBeenCalledWith('subject', {
187
+ where: expect.any(Function),
188
+ });
189
+
190
+ expect(result).toEqual(mockSubject);
191
+ expect(mockLogger.debug).toHaveBeenCalledWith(
192
+ 'Finding/Creating subject with external id'
193
+ );
194
+ });
195
+
196
+ it('should create new subject when externalSubjectId does not exist', async () => {
197
+ const mockSubject = createMockSubject({
198
+ externalId: 'ext_new',
199
+ isIdentified: true,
200
+ lastIpAddress: '192.168.1.200',
201
+ });
202
+
203
+ const db = {
204
+ upsert: vi.fn().mockResolvedValue(undefined),
205
+ findFirst: vi.fn().mockResolvedValue(mockSubject),
206
+ };
207
+
208
+ const registry = subjectRegistry({
209
+ db,
210
+ ctx: { logger: mockLogger },
211
+ } as unknown as Registry);
212
+
213
+ const result = await registry.findOrCreateSubject({
214
+ externalSubjectId: 'ext_new',
215
+ ipAddress: '192.168.1.200',
216
+ });
217
+
218
+ expect(db.upsert).toHaveBeenCalledWith('subject', {
219
+ where: expect.any(Function),
220
+ create: {
221
+ id: 'sub_test_123',
222
+ externalId: 'ext_new',
223
+ identityProvider: 'anonymous',
224
+ lastIpAddress: '192.168.1.200',
225
+ isIdentified: true,
226
+ },
227
+ update: { lastIpAddress: '192.168.1.200' },
228
+ });
229
+
230
+ expect(result).toEqual(mockSubject);
231
+ });
232
+
233
+ it('should use default IP address when not provided', async () => {
234
+ const mockSubject = createMockSubject({
235
+ externalId: 'ext_test',
236
+ lastIpAddress: 'unknown',
237
+ });
238
+
239
+ const db = {
240
+ upsert: vi.fn().mockResolvedValue(undefined),
241
+ findFirst: vi.fn().mockResolvedValue(mockSubject),
242
+ };
243
+
244
+ const registry = subjectRegistry({
245
+ db,
246
+ ctx: { logger: mockLogger },
247
+ } as unknown as Registry);
248
+
249
+ await registry.findOrCreateSubject({
250
+ externalSubjectId: 'ext_test',
251
+ });
252
+
253
+ expect(db.upsert).toHaveBeenCalledWith('subject', {
254
+ where: expect.any(Function),
255
+ create: {
256
+ id: 'sub_test_123',
257
+ externalId: 'ext_test',
258
+ identityProvider: 'anonymous',
259
+ lastIpAddress: 'unknown',
260
+ isIdentified: true,
261
+ },
262
+ update: { lastIpAddress: 'unknown' },
263
+ });
264
+ });
265
+ });
266
+
267
+ describe('when no identifiers are provided (anonymous subject)', () => {
268
+ it('should create a new anonymous subject', async () => {
269
+ const mockSubject = createMockSubject({
270
+ externalId: null,
271
+ isIdentified: false,
272
+ lastIpAddress: '10.0.0.1',
273
+ });
274
+
275
+ const db = {
276
+ create: vi.fn().mockResolvedValue(mockSubject),
277
+ };
278
+
279
+ const registry = subjectRegistry({
280
+ db,
281
+ ctx: { logger: mockLogger },
282
+ } as unknown as Registry);
283
+
284
+ const result = await registry.findOrCreateSubject({
285
+ ipAddress: '10.0.0.1',
286
+ });
287
+
288
+ expect(db.create).toHaveBeenCalledWith('subject', {
289
+ id: 'sub_test_123',
290
+ externalId: null,
291
+ identityProvider: 'anonymous',
292
+ lastIpAddress: '10.0.0.1',
293
+ isIdentified: false,
294
+ });
295
+
296
+ expect(result).toEqual(mockSubject);
297
+ expect(mockLogger.debug).toHaveBeenCalledWith(
298
+ 'Creating new anonymous subject'
299
+ );
300
+ });
301
+
302
+ it('should use default IP address for anonymous subject when not provided', async () => {
303
+ const mockSubject = createMockSubject({
304
+ externalId: null,
305
+ isIdentified: false,
306
+ lastIpAddress: 'unknown',
307
+ });
308
+
309
+ const db = {
310
+ create: vi.fn().mockResolvedValue(mockSubject),
311
+ };
312
+
313
+ const registry = subjectRegistry({
314
+ db,
315
+ ctx: { logger: mockLogger },
316
+ } as unknown as Registry);
317
+
318
+ const result = await registry.findOrCreateSubject({});
319
+
320
+ expect(db.create).toHaveBeenCalledWith('subject', {
321
+ id: 'sub_test_123',
322
+ externalId: null,
323
+ identityProvider: 'anonymous',
324
+ lastIpAddress: 'unknown',
325
+ isIdentified: false,
326
+ });
327
+
328
+ expect(result).toEqual(mockSubject);
329
+ });
330
+ });
331
+
332
+ describe('edge cases and error handling', () => {
333
+ it('should handle empty string externalSubjectId as falsy', async () => {
334
+ const mockSubject = createMockSubject();
335
+
336
+ const db = {
337
+ create: vi.fn().mockResolvedValue(mockSubject),
338
+ };
339
+
340
+ const registry = subjectRegistry({
341
+ db,
342
+ ctx: { logger: mockLogger },
343
+ } as unknown as Registry);
344
+
345
+ await registry.findOrCreateSubject({
346
+ externalSubjectId: '',
347
+ });
348
+
349
+ // Should create anonymous subject since empty string is falsy
350
+ expect(db.create).toHaveBeenCalledWith('subject', {
351
+ id: 'sub_test_123',
352
+ externalId: null,
353
+ identityProvider: 'anonymous',
354
+ lastIpAddress: 'unknown',
355
+ isIdentified: false,
356
+ });
357
+ });
358
+
359
+ it('should handle empty string subjectId as falsy', async () => {
360
+ const mockSubject = createMockSubject();
361
+
362
+ const db = {
363
+ create: vi.fn().mockResolvedValue(mockSubject),
364
+ };
365
+
366
+ const registry = subjectRegistry({
367
+ db,
368
+ ctx: { logger: mockLogger },
369
+ } as unknown as Registry);
370
+
371
+ await registry.findOrCreateSubject({
372
+ subjectId: '',
373
+ });
374
+
375
+ // Should create anonymous subject since empty string is falsy
376
+ expect(db.create).toHaveBeenCalledWith('subject', {
377
+ id: 'sub_test_123',
378
+ externalId: null,
379
+ identityProvider: 'anonymous',
380
+ lastIpAddress: 'unknown',
381
+ isIdentified: false,
382
+ });
383
+ });
384
+
385
+ it('should preserve special IP addresses', async () => {
386
+ const specialIPs = ['127.0.0.1', '::1', '0.0.0.0', 'localhost'];
387
+
388
+ for (const ip of specialIPs) {
389
+ const mockSubject = createMockSubject({
390
+ lastIpAddress: ip,
391
+ });
392
+
393
+ const db = {
394
+ create: vi.fn().mockResolvedValue(mockSubject),
395
+ };
396
+
397
+ const registry = subjectRegistry({
398
+ db,
399
+ ctx: { logger: mockLogger },
400
+ } as unknown as Registry);
401
+
402
+ await registry.findOrCreateSubject({
403
+ ipAddress: ip,
404
+ });
405
+
406
+ expect(db.create).toHaveBeenCalledWith('subject', {
407
+ id: 'sub_test_123',
408
+ externalId: null,
409
+ identityProvider: 'anonymous',
410
+ lastIpAddress: ip,
411
+ isIdentified: false,
412
+ });
413
+
414
+ vi.clearAllMocks();
415
+ }
416
+ });
417
+ });
418
+
419
+ describe('database query construction', () => {
420
+ it('should construct correct query for dual identifier lookup', async () => {
421
+ const db = {
422
+ findFirst: vi.fn().mockResolvedValue(null),
423
+ };
424
+
425
+ const registry = subjectRegistry({
426
+ db,
427
+ ctx: { logger: mockLogger },
428
+ } as unknown as Registry);
429
+
430
+ try {
431
+ await registry.findOrCreateSubject({
432
+ subjectId: 'sub_test',
433
+ externalSubjectId: 'ext_test',
434
+ });
435
+ } catch {
436
+ // Expected to throw
437
+ }
438
+
439
+ expect(db.findFirst).toHaveBeenCalledWith('subject', {
440
+ where: expect.any(Function),
441
+ });
442
+ });
443
+
444
+ it('should construct correct query for single subjectId lookup', async () => {
445
+ const db = {
446
+ findFirst: vi.fn().mockResolvedValue(null),
447
+ };
448
+
449
+ const registry = subjectRegistry({
450
+ db,
451
+ ctx: { logger: mockLogger },
452
+ } as unknown as Registry);
453
+
454
+ try {
455
+ await registry.findOrCreateSubject({
456
+ subjectId: 'sub_test',
457
+ });
458
+ } catch {
459
+ // Expected to throw
460
+ }
461
+
462
+ expect(db.findFirst).toHaveBeenCalledWith('subject', {
463
+ where: expect.any(Function),
464
+ });
465
+ });
466
+
467
+ it('should construct correct upsert for externalSubjectId', async () => {
468
+ const mockSubject = createMockSubject();
469
+ const db = {
470
+ upsert: vi.fn().mockResolvedValue(undefined),
471
+ findFirst: vi.fn().mockResolvedValue(mockSubject),
472
+ };
473
+
474
+ const registry = subjectRegistry({
475
+ db,
476
+ ctx: { logger: mockLogger },
477
+ } as unknown as Registry);
478
+
479
+ await registry.findOrCreateSubject({
480
+ externalSubjectId: 'ext_test',
481
+ });
482
+
483
+ expect(db.upsert).toHaveBeenCalledWith('subject', {
484
+ where: expect.any(Function),
485
+ create: expect.objectContaining({
486
+ id: 'sub_test_123',
487
+ externalId: 'ext_test',
488
+ identityProvider: 'anonymous',
489
+ lastIpAddress: 'unknown',
490
+ isIdentified: true,
491
+ }),
492
+ update: { lastIpAddress: 'unknown' },
493
+ });
494
+ });
495
+ });
496
+ });
497
+ });
@@ -0,0 +1,101 @@
1
+ import { ORPCError } from '@orpc/server';
2
+ import type { Registry } from './types';
3
+ import { generateUniqueId } from './utils/generate-id';
4
+
5
+ export function subjectRegistry({ db, ctx }: Registry) {
6
+ const { logger } = ctx;
7
+ return {
8
+ findOrCreateSubject: async ({
9
+ subjectId,
10
+ externalSubjectId,
11
+ ipAddress = 'unknown',
12
+ }: {
13
+ subjectId?: string;
14
+ externalSubjectId?: string;
15
+ ipAddress?: string;
16
+ }) => {
17
+ // If both subjectId and externalSubjectId are provided, validate they match
18
+ if (subjectId && externalSubjectId) {
19
+ const subject = await db.findFirst('subject', {
20
+ where: (b) =>
21
+ b.and(
22
+ b('id', '=', subjectId),
23
+ b('externalId', '=', externalSubjectId)
24
+ ),
25
+ });
26
+
27
+ if (!subject) {
28
+ logger?.error('Subject not found', {
29
+ providedSubjectId: subjectId,
30
+ providedExternalId: externalSubjectId,
31
+ });
32
+
33
+ throw new ORPCError('SUBJECT_NOT_FOUND', {
34
+ message:
35
+ 'The specified subject could not be found. Please verify the subject identifiers and try again.',
36
+ status: 404,
37
+ data: {
38
+ providedSubjectId: subjectId,
39
+ providedExternalId: externalSubjectId,
40
+ },
41
+ });
42
+ }
43
+
44
+ return subject;
45
+ }
46
+
47
+ // Try to find subject by subjectId if provided
48
+ if (subjectId) {
49
+ const subject = await db.findFirst('subject', {
50
+ where: (b) => b('id', '=', subjectId),
51
+ });
52
+
53
+ if (!subject) {
54
+ throw new ORPCError('SUBJECT_NOT_FOUND', {
55
+ message: 'Subject not found by subjectId',
56
+ status: 404,
57
+ data: { subjectId },
58
+ });
59
+ }
60
+
61
+ return subject;
62
+ }
63
+
64
+ // If externalSubjectId provided, try to find or create with upsert
65
+ if (externalSubjectId) {
66
+ logger.debug('Finding/Creating subject with external id');
67
+ await db.upsert('subject', {
68
+ where: (b) => b('externalId', '=', externalSubjectId),
69
+ create: {
70
+ id: await generateUniqueId(db, 'subject', ctx),
71
+ externalId: externalSubjectId,
72
+ identityProvider: 'anonymous',
73
+ lastIpAddress: ipAddress,
74
+ isIdentified: !!externalSubjectId,
75
+ },
76
+ update: { lastIpAddress: ipAddress },
77
+ });
78
+
79
+ const subject = await db.findFirst('subject', {
80
+ where: (b) => b('externalId', '=', externalSubjectId),
81
+ });
82
+
83
+ return subject;
84
+ }
85
+
86
+ // If unknown, create an anonymous subject
87
+ logger?.debug('Creating new anonymous subject');
88
+ const subject = await db.create('subject', {
89
+ id: await generateUniqueId(db, 'subject', ctx),
90
+ externalId: null,
91
+ identityProvider: 'anonymous',
92
+ lastIpAddress: ipAddress,
93
+ isIdentified: false,
94
+ });
95
+
96
+ logger.debug('Created new anonymous subject', { subject });
97
+
98
+ return subject;
99
+ },
100
+ };
101
+ }
@@ -0,0 +1,10 @@
1
+ import type { createLogger } from '@doubletie/logger';
2
+ import type { InferFumaDB } from 'fumadb';
3
+ import type { DB } from '~/v2/db/schema';
4
+
5
+ export interface Registry {
6
+ db: ReturnType<InferFumaDB<typeof DB>['orm']>;
7
+ ctx: {
8
+ logger: ReturnType<typeof createLogger>;
9
+ };
10
+ }