@c15t/backend 2.0.0-rc.4 → 2.0.0-rc.5

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 (308) hide show
  1. package/dist/core.cjs +830 -74
  2. package/dist/core.js +807 -75
  3. package/dist/db/schema.cjs +37 -0
  4. package/dist/db/schema.js +33 -2
  5. package/dist/edge.cjs +1106 -0
  6. package/dist/edge.js +1069 -0
  7. package/dist/router.cjs +613 -64
  8. package/dist/router.js +613 -64
  9. package/{dist → dist-types}/cache/adapters/cloudflare-kv.d.ts +0 -1
  10. package/{dist → dist-types}/cache/adapters/index.d.ts +0 -1
  11. package/{dist → dist-types}/cache/adapters/memory.d.ts +0 -1
  12. package/{dist → dist-types}/cache/adapters/upstash-redis.d.ts +0 -1
  13. package/{dist → dist-types}/cache/gvl-resolver.d.ts +1 -2
  14. package/{dist → dist-types}/cache/index.d.ts +0 -1
  15. package/{dist → dist-types}/cache/keys.d.ts +0 -1
  16. package/{dist → dist-types}/cache/types.d.ts +0 -1
  17. package/{dist → dist-types}/core.d.ts +8 -1
  18. package/{dist → dist-types}/db/migrator/index.d.ts +0 -1
  19. package/{dist → dist-types}/db/registry/consent-policy.d.ts +0 -1
  20. package/{dist → dist-types}/db/registry/consent-purpose.d.ts +0 -1
  21. package/{dist → dist-types}/db/registry/domain.d.ts +0 -1
  22. package/{dist → dist-types}/db/registry/index.d.ts +22 -2
  23. package/dist-types/db/registry/runtime-policy-decision.d.ts +60 -0
  24. package/{dist → dist-types}/db/registry/subject.d.ts +0 -1
  25. package/{dist → dist-types}/db/registry/types.d.ts +1 -2
  26. package/{dist → dist-types}/db/registry/utils/generate-id.d.ts +0 -1
  27. package/{dist → dist-types}/db/registry/utils.d.ts +0 -1
  28. package/{dist → dist-types}/db/schema/1.0.0/audit-log.d.ts +0 -1
  29. package/{dist → dist-types}/db/schema/1.0.0/consent-policy.d.ts +0 -1
  30. package/{dist → dist-types}/db/schema/1.0.0/consent-purpose.d.ts +0 -1
  31. package/{dist → dist-types}/db/schema/1.0.0/consent-record.d.ts +0 -1
  32. package/{dist → dist-types}/db/schema/1.0.0/consent.d.ts +1 -2
  33. package/{dist → dist-types}/db/schema/1.0.0/domain.d.ts +0 -1
  34. package/{dist → dist-types}/db/schema/1.0.0/index.d.ts +0 -1
  35. package/{dist → dist-types}/db/schema/1.0.0/subject.d.ts +0 -1
  36. package/{dist → dist-types}/db/schema/2.0.0/audit-log.d.ts +1 -2
  37. package/{dist → dist-types}/db/schema/2.0.0/consent-policy.d.ts +1 -2
  38. package/{dist → dist-types}/db/schema/2.0.0/consent-purpose.d.ts +1 -2
  39. package/{dist → dist-types}/db/schema/2.0.0/consent.d.ts +5 -2
  40. package/{dist → dist-types}/db/schema/2.0.0/domain.d.ts +1 -2
  41. package/{dist → dist-types}/db/schema/2.0.0/index.d.ts +432 -17
  42. package/dist-types/db/schema/2.0.0/runtime-policy-decision.d.ts +23 -0
  43. package/{dist → dist-types}/db/schema/2.0.0/subject.d.ts +1 -2
  44. package/{dist → dist-types}/db/schema/index.d.ts +862 -33
  45. package/{dist → dist-types}/db/tenant-scope.d.ts +0 -1
  46. package/{dist → dist-types}/define-config.d.ts +0 -1
  47. package/dist-types/edge/index.d.ts +5 -0
  48. package/dist-types/edge/init-handler.d.ts +38 -0
  49. package/dist-types/edge/resolve-consent.d.ts +80 -0
  50. package/dist-types/edge/types.d.ts +13 -0
  51. package/{dist → dist-types}/handlers/consent/check.handler.d.ts +0 -1
  52. package/{src/handlers/consent/index.ts → dist-types/handlers/consent/index.d.ts} +0 -1
  53. package/{dist → dist-types}/handlers/init/geo.d.ts +2 -3
  54. package/{dist → dist-types}/handlers/init/index.d.ts +4 -5
  55. package/dist-types/handlers/init/policy.d.ts +26 -0
  56. package/dist-types/handlers/init/resolve-init.d.ts +44 -0
  57. package/dist-types/handlers/init/translations.d.ts +48 -0
  58. package/dist-types/handlers/policy/snapshot.d.ts +99 -0
  59. package/{src/handlers/status/index.ts → dist-types/handlers/status/index.d.ts} +0 -1
  60. package/{dist → dist-types}/handlers/status/status.handler.d.ts +0 -1
  61. package/{dist → dist-types}/handlers/subject/get.handler.d.ts +0 -1
  62. package/{src/handlers/subject/index.ts → dist-types/handlers/subject/index.d.ts} +0 -1
  63. package/{dist → dist-types}/handlers/subject/list.handler.d.ts +0 -1
  64. package/{dist → dist-types}/handlers/subject/patch.handler.d.ts +0 -1
  65. package/{dist → dist-types}/handlers/subject/post.handler.d.ts +12 -1
  66. package/{dist → dist-types}/handlers/utils/consent-enrichment.d.ts +0 -1
  67. package/{dist → dist-types}/init.d.ts +0 -1
  68. package/{dist → dist-types}/middleware/auth/index.d.ts +0 -1
  69. package/{dist → dist-types}/middleware/auth/validate-api-key.d.ts +0 -1
  70. package/{dist → dist-types}/middleware/cors/cors.d.ts +0 -1
  71. package/{src/middleware/cors/index.ts → dist-types/middleware/cors/index.d.ts} +0 -1
  72. package/{dist → dist-types}/middleware/cors/is-origin-trusted.d.ts +1 -2
  73. package/{dist → dist-types}/middleware/cors/process-cors.d.ts +0 -1
  74. package/{dist → dist-types}/middleware/openapi/config.d.ts +0 -1
  75. package/{dist → dist-types}/middleware/openapi/handlers.d.ts +0 -1
  76. package/{src/middleware/openapi/index.ts → dist-types/middleware/openapi/index.d.ts} +0 -1
  77. package/{dist → dist-types}/middleware/process-ip/index.d.ts +0 -1
  78. package/dist-types/policies/builder.d.ts +127 -0
  79. package/dist-types/policies/defaults.d.ts +2 -0
  80. package/dist-types/policies/matchers.d.ts +3 -0
  81. package/{dist → dist-types}/router.d.ts +0 -1
  82. package/{dist → dist-types}/routes/consent.d.ts +0 -1
  83. package/{src/routes/index.ts → dist-types/routes/index.d.ts} +0 -1
  84. package/{dist → dist-types}/routes/init.d.ts +0 -1
  85. package/{dist → dist-types}/routes/status.d.ts +0 -1
  86. package/{dist → dist-types}/routes/subject.d.ts +0 -1
  87. package/{dist → dist-types}/types/api.d.ts +0 -1
  88. package/{dist → dist-types}/types/index.d.ts +110 -6
  89. package/dist-types/utils/background.d.ts +6 -0
  90. package/{dist → dist-types}/utils/create-telemetry-options.d.ts +0 -1
  91. package/{dist → dist-types}/utils/env.d.ts +0 -1
  92. package/{dist → dist-types}/utils/extract-error-message.d.ts +0 -1
  93. package/{dist → dist-types}/utils/instrumentation.d.ts +0 -1
  94. package/{dist → dist-types}/utils/logger.d.ts +1 -2
  95. package/{dist → dist-types}/utils/metrics.d.ts +0 -1
  96. package/dist-types/version.d.ts +1 -0
  97. package/docs/README.md +49 -0
  98. package/docs/api/configuration.md +197 -0
  99. package/docs/api/endpoints.md +211 -0
  100. package/docs/guides/caching.md +85 -0
  101. package/docs/guides/database-setup.md +128 -0
  102. package/docs/guides/edge-deployment.md +248 -0
  103. package/docs/guides/framework-integration.md +142 -0
  104. package/docs/guides/iab-tcf.md +89 -0
  105. package/docs/guides/observability.md +96 -0
  106. package/docs/guides/policy-packs.md +396 -0
  107. package/docs/quickstart.md +129 -0
  108. package/package.json +33 -19
  109. package/.turbo/turbo-build.log +0 -49
  110. package/CHANGELOG.md +0 -123
  111. package/dist/cache/adapters/cloudflare-kv.d.ts.map +0 -1
  112. package/dist/cache/adapters/index.d.ts.map +0 -1
  113. package/dist/cache/adapters/memory.d.ts.map +0 -1
  114. package/dist/cache/adapters/upstash-redis.d.ts.map +0 -1
  115. package/dist/cache/gvl-resolver.d.ts.map +0 -1
  116. package/dist/cache/index.d.ts.map +0 -1
  117. package/dist/cache/keys.d.ts.map +0 -1
  118. package/dist/cache/types.d.ts.map +0 -1
  119. package/dist/core.d.ts.map +0 -1
  120. package/dist/db/adapters/drizzle.d.ts +0 -2
  121. package/dist/db/adapters/drizzle.d.ts.map +0 -1
  122. package/dist/db/adapters/index.d.ts +0 -2
  123. package/dist/db/adapters/index.d.ts.map +0 -1
  124. package/dist/db/adapters/kysely.d.ts +0 -2
  125. package/dist/db/adapters/kysely.d.ts.map +0 -1
  126. package/dist/db/adapters/mongo.d.ts +0 -2
  127. package/dist/db/adapters/mongo.d.ts.map +0 -1
  128. package/dist/db/adapters/prisma.d.ts +0 -2
  129. package/dist/db/adapters/prisma.d.ts.map +0 -1
  130. package/dist/db/adapters/typeorm.d.ts +0 -2
  131. package/dist/db/adapters/typeorm.d.ts.map +0 -1
  132. package/dist/db/migrator/index.d.ts.map +0 -1
  133. package/dist/db/registry/consent-policy.d.ts.map +0 -1
  134. package/dist/db/registry/consent-purpose.d.ts.map +0 -1
  135. package/dist/db/registry/domain.d.ts.map +0 -1
  136. package/dist/db/registry/index.d.ts.map +0 -1
  137. package/dist/db/registry/subject.d.ts.map +0 -1
  138. package/dist/db/registry/types.d.ts.map +0 -1
  139. package/dist/db/registry/utils/generate-id.d.ts.map +0 -1
  140. package/dist/db/registry/utils.d.ts.map +0 -1
  141. package/dist/db/schema/1.0.0/audit-log.d.ts.map +0 -1
  142. package/dist/db/schema/1.0.0/consent-policy.d.ts.map +0 -1
  143. package/dist/db/schema/1.0.0/consent-purpose.d.ts.map +0 -1
  144. package/dist/db/schema/1.0.0/consent-record.d.ts.map +0 -1
  145. package/dist/db/schema/1.0.0/consent.d.ts.map +0 -1
  146. package/dist/db/schema/1.0.0/domain.d.ts.map +0 -1
  147. package/dist/db/schema/1.0.0/index.d.ts.map +0 -1
  148. package/dist/db/schema/1.0.0/subject.d.ts.map +0 -1
  149. package/dist/db/schema/2.0.0/audit-log.d.ts.map +0 -1
  150. package/dist/db/schema/2.0.0/consent-policy.d.ts.map +0 -1
  151. package/dist/db/schema/2.0.0/consent-purpose.d.ts.map +0 -1
  152. package/dist/db/schema/2.0.0/consent.d.ts.map +0 -1
  153. package/dist/db/schema/2.0.0/domain.d.ts.map +0 -1
  154. package/dist/db/schema/2.0.0/index.d.ts.map +0 -1
  155. package/dist/db/schema/2.0.0/subject.d.ts.map +0 -1
  156. package/dist/db/schema/index.d.ts.map +0 -1
  157. package/dist/db/tenant-scope.d.ts.map +0 -1
  158. package/dist/define-config.d.ts.map +0 -1
  159. package/dist/handlers/consent/check.handler.d.ts.map +0 -1
  160. package/dist/handlers/consent/index.d.ts +0 -12
  161. package/dist/handlers/consent/index.d.ts.map +0 -1
  162. package/dist/handlers/init/geo.d.ts.map +0 -1
  163. package/dist/handlers/init/index.d.ts.map +0 -1
  164. package/dist/handlers/init/translations.d.ts +0 -26
  165. package/dist/handlers/init/translations.d.ts.map +0 -1
  166. package/dist/handlers/status/index.d.ts +0 -7
  167. package/dist/handlers/status/index.d.ts.map +0 -1
  168. package/dist/handlers/status/status.handler.d.ts.map +0 -1
  169. package/dist/handlers/subject/get.handler.d.ts.map +0 -1
  170. package/dist/handlers/subject/index.d.ts +0 -10
  171. package/dist/handlers/subject/index.d.ts.map +0 -1
  172. package/dist/handlers/subject/list.handler.d.ts.map +0 -1
  173. package/dist/handlers/subject/patch.handler.d.ts.map +0 -1
  174. package/dist/handlers/subject/post.handler.d.ts.map +0 -1
  175. package/dist/handlers/utils/consent-enrichment.d.ts.map +0 -1
  176. package/dist/init.d.ts.map +0 -1
  177. package/dist/middleware/auth/index.d.ts.map +0 -1
  178. package/dist/middleware/auth/validate-api-key.d.ts.map +0 -1
  179. package/dist/middleware/cors/cors.d.ts.map +0 -1
  180. package/dist/middleware/cors/index.d.ts +0 -30
  181. package/dist/middleware/cors/index.d.ts.map +0 -1
  182. package/dist/middleware/cors/is-origin-trusted.d.ts.map +0 -1
  183. package/dist/middleware/cors/process-cors.d.ts.map +0 -1
  184. package/dist/middleware/openapi/config.d.ts.map +0 -1
  185. package/dist/middleware/openapi/handlers.d.ts.map +0 -1
  186. package/dist/middleware/openapi/index.d.ts +0 -12
  187. package/dist/middleware/openapi/index.d.ts.map +0 -1
  188. package/dist/middleware/process-ip/index.d.ts.map +0 -1
  189. package/dist/router.d.ts.map +0 -1
  190. package/dist/routes/consent.d.ts.map +0 -1
  191. package/dist/routes/index.d.ts +0 -10
  192. package/dist/routes/index.d.ts.map +0 -1
  193. package/dist/routes/init.d.ts.map +0 -1
  194. package/dist/routes/status.d.ts.map +0 -1
  195. package/dist/routes/subject.d.ts.map +0 -1
  196. package/dist/types/api.d.ts.map +0 -1
  197. package/dist/types/index.d.ts.map +0 -1
  198. package/dist/utils/create-telemetry-options.d.ts.map +0 -1
  199. package/dist/utils/env.d.ts.map +0 -1
  200. package/dist/utils/extract-error-message.d.ts.map +0 -1
  201. package/dist/utils/index.d.ts +0 -4
  202. package/dist/utils/index.d.ts.map +0 -1
  203. package/dist/utils/instrumentation.d.ts.map +0 -1
  204. package/dist/utils/logger.d.ts.map +0 -1
  205. package/dist/utils/metrics.d.ts.map +0 -1
  206. package/dist/version.d.ts +0 -2
  207. package/dist/version.d.ts.map +0 -1
  208. package/knip.json +0 -31
  209. package/rslib.config.ts +0 -93
  210. package/src/cache/adapters/cloudflare-kv.ts +0 -71
  211. package/src/cache/adapters/index.ts +0 -22
  212. package/src/cache/adapters/memory.ts +0 -111
  213. package/src/cache/adapters/upstash-redis.ts +0 -113
  214. package/src/cache/gvl-resolver.ts +0 -289
  215. package/src/cache/index.ts +0 -34
  216. package/src/cache/keys.ts +0 -68
  217. package/src/cache/types.ts +0 -66
  218. package/src/core.ts +0 -369
  219. package/src/db/migrator/index.ts +0 -80
  220. package/src/db/registry/consent-policy.test.ts +0 -451
  221. package/src/db/registry/consent-policy.ts +0 -82
  222. package/src/db/registry/consent-purpose.test.ts +0 -428
  223. package/src/db/registry/consent-purpose.ts +0 -61
  224. package/src/db/registry/domain.test.ts +0 -445
  225. package/src/db/registry/domain.ts +0 -91
  226. package/src/db/registry/index.ts +0 -14
  227. package/src/db/registry/subject.test.ts +0 -371
  228. package/src/db/registry/subject.ts +0 -126
  229. package/src/db/registry/types.ts +0 -10
  230. package/src/db/registry/utils/generate-id.test.ts +0 -216
  231. package/src/db/registry/utils/generate-id.ts +0 -133
  232. package/src/db/registry/utils.ts +0 -133
  233. package/src/db/schema/1.0.0/audit-log.ts +0 -15
  234. package/src/db/schema/1.0.0/consent-policy.ts +0 -14
  235. package/src/db/schema/1.0.0/consent-purpose.ts +0 -14
  236. package/src/db/schema/1.0.0/consent-record.ts +0 -10
  237. package/src/db/schema/1.0.0/consent.ts +0 -20
  238. package/src/db/schema/1.0.0/domain.ts +0 -12
  239. package/src/db/schema/1.0.0/index.ts +0 -48
  240. package/src/db/schema/1.0.0/subject.ts +0 -11
  241. package/src/db/schema/2.0.0/audit-log.ts +0 -18
  242. package/src/db/schema/2.0.0/consent-policy.ts +0 -28
  243. package/src/db/schema/2.0.0/consent-purpose.ts +0 -12
  244. package/src/db/schema/2.0.0/consent.ts +0 -28
  245. package/src/db/schema/2.0.0/domain.ts +0 -12
  246. package/src/db/schema/2.0.0/index.ts +0 -47
  247. package/src/db/schema/2.0.0/subject.ts +0 -13
  248. package/src/db/schema/index.ts +0 -15
  249. package/src/db/tenant-scope.test.ts +0 -747
  250. package/src/db/tenant-scope.ts +0 -103
  251. package/src/define-config.ts +0 -19
  252. package/src/handlers/consent/check.handler.ts +0 -126
  253. package/src/handlers/init/geo.test.ts +0 -317
  254. package/src/handlers/init/geo.ts +0 -195
  255. package/src/handlers/init/index.test.ts +0 -205
  256. package/src/handlers/init/index.ts +0 -114
  257. package/src/handlers/init/translations.test.ts +0 -121
  258. package/src/handlers/init/translations.ts +0 -69
  259. package/src/handlers/status/status.handler.test.ts +0 -155
  260. package/src/handlers/status/status.handler.ts +0 -51
  261. package/src/handlers/subject/get.handler.ts +0 -92
  262. package/src/handlers/subject/list.handler.ts +0 -92
  263. package/src/handlers/subject/patch.handler.ts +0 -119
  264. package/src/handlers/subject/post.handler.test.ts +0 -294
  265. package/src/handlers/subject/post.handler.ts +0 -268
  266. package/src/handlers/utils/consent-enrichment.test.ts +0 -380
  267. package/src/handlers/utils/consent-enrichment.ts +0 -218
  268. package/src/init.test.ts +0 -122
  269. package/src/init.ts +0 -88
  270. package/src/middleware/auth/index.ts +0 -11
  271. package/src/middleware/auth/validate-api-key.test.ts +0 -86
  272. package/src/middleware/auth/validate-api-key.ts +0 -107
  273. package/src/middleware/cors/cors.test.ts +0 -135
  274. package/src/middleware/cors/cors.ts +0 -186
  275. package/src/middleware/cors/is-origin-trusted.test.ts +0 -164
  276. package/src/middleware/cors/is-origin-trusted.ts +0 -130
  277. package/src/middleware/cors/process-cors.ts +0 -91
  278. package/src/middleware/openapi/config.ts +0 -29
  279. package/src/middleware/openapi/handlers.ts +0 -34
  280. package/src/middleware/process-ip/index.test.ts +0 -193
  281. package/src/middleware/process-ip/index.ts +0 -199
  282. package/src/router.ts +0 -15
  283. package/src/routes/consent.ts +0 -52
  284. package/src/routes/init.ts +0 -105
  285. package/src/routes/status.ts +0 -46
  286. package/src/routes/subject.ts +0 -152
  287. package/src/types/api.ts +0 -48
  288. package/src/types/index.ts +0 -391
  289. package/src/utils/create-telemetry-options.test.ts +0 -286
  290. package/src/utils/create-telemetry-options.ts +0 -229
  291. package/src/utils/env.ts +0 -84
  292. package/src/utils/extract-error-message.ts +0 -21
  293. package/src/utils/instrumentation.test.ts +0 -183
  294. package/src/utils/instrumentation.ts +0 -194
  295. package/src/utils/logger.ts +0 -41
  296. package/src/utils/metrics.test.ts +0 -311
  297. package/src/utils/metrics.ts +0 -402
  298. package/src/utils/telemetry-pii.test.ts +0 -323
  299. package/src/version.ts +0 -2
  300. package/tsconfig.json +0 -11
  301. package/vitest.config.ts +0 -28
  302. /package/{src/db/adapters/drizzle.ts → dist-types/db/adapters/drizzle.d.ts} +0 -0
  303. /package/{src/db/adapters/index.ts → dist-types/db/adapters/index.d.ts} +0 -0
  304. /package/{src/db/adapters/kysely.ts → dist-types/db/adapters/kysely.d.ts} +0 -0
  305. /package/{src/db/adapters/mongo.ts → dist-types/db/adapters/mongo.d.ts} +0 -0
  306. /package/{src/db/adapters/prisma.ts → dist-types/db/adapters/prisma.d.ts} +0 -0
  307. /package/{src/db/adapters/typeorm.ts → dist-types/db/adapters/typeorm.d.ts} +0 -0
  308. /package/{src/utils/index.ts → dist-types/utils/index.d.ts} +0 -0
package/src/core.ts DELETED
@@ -1,369 +0,0 @@
1
- import { createLogger } from '@c15t/logger';
2
- import { SpanStatusCode } from '@opentelemetry/api';
3
- import { apiReference } from '@scalar/hono-api-reference';
4
- import { Hono } from 'hono';
5
- import { cors } from 'hono/cors';
6
- import { HTTPException } from 'hono/http-exception';
7
- import { openAPIRouteHandler } from 'hono-openapi';
8
- import { validateRequestAuth } from '~/middleware/auth';
9
- import { createCORSOptions } from '~/middleware/cors';
10
- import { createOpenAPIConfig } from '~/middleware/openapi';
11
- import { getIpAddress } from '~/middleware/process-ip';
12
- import type { C15TContext, C15TOptions } from '~/types';
13
- import { init } from './init';
14
- import { createConsentRoutes } from './routes/consent';
15
- // Import route handlers
16
- import { createInitRoute } from './routes/init';
17
- import { createStatusRoute } from './routes/status';
18
- import { createSubjectRoutes } from './routes/subject';
19
- import {
20
- createRequestSpan,
21
- handleSpanError,
22
- withSpanContext,
23
- } from './utils/create-telemetry-options';
24
- import { extractErrorMessage } from './utils/extract-error-message';
25
- import { getMetrics } from './utils/metrics';
26
- import { version } from './version';
27
-
28
- /**
29
- * Type representing an API route
30
- */
31
- export type Route = {
32
- path: string;
33
- method: string;
34
- description?: string;
35
- };
36
-
37
- /**
38
- * Hono app type with c15t context
39
- */
40
- export type C15TApp = Hono<{ Variables: { c15tContext: C15TContext } }>;
41
-
42
- /**
43
- * Interface representing a configured c15t consent management instance.
44
- *
45
- * @remarks
46
- * The C15TInstance provides the main interface for interacting with the consent
47
- * management system. It includes methods for handling requests, accessing API
48
- * endpoints, and managing the system's configuration.
49
- */
50
- export interface C15TInstance {
51
- /**
52
- * Processes incoming HTTP requests and routes them to appropriate handlers.
53
- *
54
- * @param request - The incoming web request
55
- * @returns A Promise containing the HTTP response
56
- *
57
- * @example
58
- * ```typescript
59
- * try {
60
- * const response = await instance.handler(request);
61
- * sendResponse(response);
62
- * } catch (error) {
63
- * handleError(error);
64
- * }
65
- * ```
66
- */
67
- handler: (request: Request) => Promise<Response>;
68
-
69
- /**
70
- * The configuration options used for this instance.
71
- */
72
- options: C15TOptions;
73
-
74
- /**
75
- * Access to the underlying context.
76
- */
77
- $context: C15TContext;
78
-
79
- /**
80
- * Access to the Hono app for direct usage.
81
- */
82
- app: C15TApp;
83
-
84
- /**
85
- * Generates and returns the OpenAPI specification as a JSON object.
86
- *
87
- * @returns A Promise containing the OpenAPI specification
88
- */
89
- getOpenAPISpec: () => Promise<Record<string, unknown>>;
90
-
91
- /**
92
- * Returns an HTML document with the API documentation UI.
93
- *
94
- * @returns An HTML string with the API reference UI
95
- */
96
- getDocsUI: () => string;
97
- }
98
-
99
- /**
100
- * Creates a new c15t consent management instance.
101
- *
102
- * This version uses Hono as the HTTP framework with OpenAPI support.
103
- */
104
- export const c15tInstance = (options: C15TOptions): C15TInstance => {
105
- const context = init(options);
106
- const logger = createLogger(options.logger);
107
-
108
- // Create the Hono app
109
- const app = new Hono<{ Variables: { c15tContext: C15TContext } }>();
110
-
111
- // Set up OpenAPI configuration
112
- const openApiConfig = createOpenAPIConfig(options);
113
- const basePath = options.basePath || '/';
114
-
115
- // CORS middleware
116
- const corsOptions = createCORSOptions(options.trustedOrigins);
117
- app.use('*', cors(corsOptions));
118
-
119
- // Note: Compression disabled - causes issues with response passthrough in wrapper scenarios
120
- // app.use('*', compress());
121
-
122
- // Get metrics instance (null if telemetry is disabled)
123
- const metrics = getMetrics(options);
124
-
125
- // Context middleware - enriches each request with c15t context
126
- app.use('*', async (c, next) => {
127
- const request = c.req.raw;
128
- const startTime = Date.now();
129
-
130
- // Check API key authentication
131
- const apiKeyAuthenticated = validateRequestAuth(
132
- request.headers,
133
- options.apiKeys
134
- );
135
-
136
- const enrichedContext: C15TContext = {
137
- ...context,
138
- ipAddress: getIpAddress(request, options),
139
- userAgent: request.headers.get('user-agent') || undefined,
140
- apiKeyAuthenticated,
141
- path: c.req.path,
142
- method: c.req.method,
143
- headers: request.headers,
144
- };
145
-
146
- c.set('c15tContext', enrichedContext);
147
-
148
- // Create a telemetry span for the request (if enabled)
149
- const span = createRequestSpan(c.req.method, c.req.path, options);
150
-
151
- const runNext = async () => {
152
- await next();
153
- };
154
-
155
- try {
156
- if (span) {
157
- await withSpanContext(span, runNext);
158
- // After routing, update span name with matched route pattern
159
- const matchedRoutes = c.req.matchedRoutes;
160
- const routePattern =
161
- matchedRoutes
162
- .map((r) => r.path)
163
- .filter((p) => p !== '/*')
164
- .pop() ?? c.req.path;
165
- span.updateName(`${c.req.method} ${routePattern}`);
166
- span.setAttribute('http.route', routePattern);
167
- span.setStatus({ code: SpanStatusCode.OK });
168
- } else {
169
- await runNext();
170
- }
171
- } catch (error) {
172
- if (span) {
173
- handleSpanError(span, error);
174
- }
175
- throw error;
176
- } finally {
177
- span?.end();
178
- }
179
-
180
- // Record HTTP metrics after request completes
181
- if (metrics) {
182
- const durationMs = Date.now() - startTime;
183
- // Use route pattern instead of raw path to avoid high cardinality
184
- const matchedRoutes = c.req.matchedRoutes;
185
- const routePattern =
186
- matchedRoutes
187
- .map((r) => r.path)
188
- .filter((p) => p !== '/*')
189
- .pop() ?? c.req.path;
190
- metrics.recordHttpRequest(
191
- {
192
- method: c.req.method,
193
- route: routePattern,
194
- status: c.res.status,
195
- },
196
- durationMs
197
- );
198
- }
199
- });
200
-
201
- // OpenAPI spec endpoint
202
- if (openApiConfig.enabled) {
203
- app.get(
204
- openApiConfig.specPath,
205
- openAPIRouteHandler(app, {
206
- documentation: {
207
- openapi: '3.1.0',
208
- info: {
209
- title: options.appName || 'c15t API',
210
- version,
211
- description: 'API for consent management',
212
- },
213
- servers: [{ url: basePath }],
214
- components: {
215
- securitySchemes: {
216
- bearerAuth: {
217
- type: 'http',
218
- scheme: 'bearer',
219
- },
220
- },
221
- },
222
- },
223
- })
224
- );
225
-
226
- // Docs UI endpoint
227
- // The spec URL needs to include the basePath since it's used by the browser
228
- const publicSpecUrl = `${basePath}${openApiConfig.specPath}`.replace(
229
- /\/+/g,
230
- '/'
231
- );
232
- app.get(
233
- openApiConfig.docsPath,
234
- apiReference({
235
- spec: {
236
- url: publicSpecUrl,
237
- },
238
- pageTitle: `${options.appName || 'c15t API'} Documentation`,
239
- })
240
- );
241
- }
242
-
243
- // Mount routes - using plural nouns for REST conventions
244
- app.route('/init', createInitRoute(options));
245
- app.route('/subjects', createSubjectRoutes());
246
- app.route('/consents', createConsentRoutes());
247
- app.route('/status', createStatusRoute());
248
- app.route('/', createStatusRoute());
249
-
250
- // Global error handler
251
- app.onError((err, c) => {
252
- const ctx = c.get('c15tContext');
253
- const log = ctx?.logger || logger;
254
-
255
- log.error('Request handling error:', extractErrorMessage(err));
256
-
257
- if (err instanceof HTTPException) {
258
- const cause = err.cause as
259
- | { code?: string; message?: string; [key: string]: unknown }
260
- | undefined;
261
- return c.json(
262
- {
263
- code: cause?.code || 'HTTP_ERROR',
264
- message: err.message,
265
- status: err.status,
266
- defined: true,
267
- data: {},
268
- },
269
- err.status
270
- );
271
- }
272
-
273
- return c.json(
274
- {
275
- code: 'INTERNAL_SERVER_ERROR',
276
- message: 'Internal server error',
277
- status: 500,
278
- defined: true,
279
- data: {},
280
- },
281
- 500
282
- );
283
- });
284
-
285
- // 404 handler
286
- app.notFound((c) => {
287
- return c.json(
288
- {
289
- code: 'NOT_FOUND',
290
- message: 'Not Found',
291
- status: 404,
292
- defined: true,
293
- data: {},
294
- },
295
- 404
296
- );
297
- });
298
-
299
- // Create the handler function
300
- const handler = async (request: Request): Promise<Response> => {
301
- logger?.debug?.('Incoming request', {
302
- method: request.method,
303
- url: request.url,
304
- });
305
-
306
- // Strip the basePath from the URL if present
307
- // This allows Hono routes to be defined without the basePath prefix
308
- let modifiedRequest = request;
309
- if (basePath && basePath !== '/') {
310
- const url = new URL(request.url);
311
- const normalizedBasePath = basePath.replace(/\/$/, ''); // Remove trailing slash
312
- if (url.pathname.startsWith(normalizedBasePath)) {
313
- const newPath = url.pathname.slice(normalizedBasePath.length) || '/';
314
- url.pathname = newPath;
315
- modifiedRequest = new Request(url.toString(), {
316
- method: request.method,
317
- headers: request.headers,
318
- body: request.body,
319
- duplex: 'half',
320
- } as RequestInit);
321
- }
322
- }
323
-
324
- return app.fetch(modifiedRequest);
325
- };
326
-
327
- // Create docs UI helper
328
- const getDocsUI = () => {
329
- const specUrl = `${basePath}${openApiConfig.specPath}`.replace(/\/+/g, '/');
330
- return `
331
- <!doctype html>
332
- <html>
333
- <head>
334
- <title>${options.appName || 'c15t API'} Documentation</title>
335
- <meta charset="utf-8" />
336
- <meta name="viewport" content="width=device-width, initial-scale=1" />
337
- <link rel="icon" type="image/svg+xml" href="https://c15t.com/icon.svg" />
338
- </head>
339
- <body>
340
- <script
341
- id="api-reference"
342
- data-url="${encodeURI(specUrl)}">
343
- </script>
344
- <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
345
- </body>
346
- </html>
347
- `;
348
- };
349
-
350
- // Return the instance
351
- return {
352
- options,
353
- $context: context,
354
- app,
355
- handler,
356
- getOpenAPISpec: async () => {
357
- // Generate OpenAPI spec by fetching from our own endpoint
358
- const specResponse = await app.fetch(
359
- new Request(`http://localhost${openApiConfig.specPath}`)
360
- );
361
- return specResponse.json() as Promise<Record<string, unknown>>;
362
- },
363
- getDocsUI,
364
- };
365
- };
366
-
367
- export { defineConfig } from './define-config';
368
- export type { C15TContext, C15TOptions } from './types';
369
- export { version } from './version';
@@ -1,80 +0,0 @@
1
- import type { InferFumaDB } from 'fumadb';
2
- import type { DB } from '~/db/schema';
3
-
4
- type DatabaseInstance = InferFumaDB<typeof DB>;
5
- type MigratorInstance = ReturnType<DatabaseInstance['createMigrator']>;
6
- type VersionTag = ReturnType<(typeof DB)['version']>;
7
-
8
- type MigrateToLatestResult = Awaited<
9
- ReturnType<MigratorInstance['migrateToLatest']>
10
- >;
11
- type MigrateToResult = Awaited<ReturnType<MigratorInstance['migrateTo']>>;
12
- type DownResult = Awaited<ReturnType<MigratorInstance['down']>>;
13
-
14
- export type MigrationResult =
15
- | MigrateToLatestResult
16
- | MigrateToResult
17
- | DownResult;
18
-
19
- export type ORMResult = {
20
- code: string;
21
- path: string;
22
- };
23
-
24
- interface BaseOptions {
25
- db: DatabaseInstance;
26
- schema: VersionTag | 'latest';
27
- }
28
-
29
- /**
30
- * Executes database migrations for supported adapters, or generates ORM schema
31
- * code for ORM-based adapters.
32
- *
33
- * - For 'kysely' and 'mongo', this function runs migrations using the
34
- * underlying migrator returned by `db.createMigrator()`.
35
- * - For 'drizzle', 'prisma', and 'typeorm', this function generates schema
36
- * code via `db.generateSchema()`.
37
- */
38
- export async function migrator(
39
- options: BaseOptions
40
- ): Promise<MigrationResult | ORMResult> {
41
- const { db } = options;
42
-
43
- let version: VersionTag | 'legacy';
44
- try {
45
- version = await db.version();
46
- } catch {
47
- // If FumaDB isn't initalized yet, we're in legacy mode
48
- version = 'legacy';
49
- }
50
-
51
- const migratorInstance = db.adapter?.createMigrationEngine
52
- ? db.createMigrator()
53
- : undefined;
54
-
55
- const schema = db.adapter?.generateSchema
56
- ? db.generateSchema(options.schema)
57
- : undefined;
58
-
59
- if (migratorInstance) {
60
- switch (options.schema) {
61
- case 'latest':
62
- return await migratorInstance.migrateToLatest({
63
- mode: version === 'legacy' ? 'from-database' : 'from-schema',
64
- });
65
- default:
66
- return await migratorInstance.migrateTo(options.schema, {
67
- mode: version === 'legacy' ? 'from-database' : 'from-schema',
68
- });
69
- }
70
- }
71
-
72
- if (schema) {
73
- return {
74
- code: schema.code,
75
- path: schema.path,
76
- };
77
- }
78
-
79
- throw new Error('Adapter does not support migrations or schema generation');
80
- }