@beechcms/api 0.4.0 → 0.4.2

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 (138) hide show
  1. package/assets/dashboard/assets/index-BKWnlvnV.css +1 -0
  2. package/assets/dashboard/assets/index-C1P9BXnU.js +629 -0
  3. package/assets/dashboard/index.html +2 -2
  4. package/migrations/0000_v040_base.sql +25 -0
  5. package/migrations/0029_automations.sql +14 -0
  6. package/package.json +4 -3
  7. package/src/auth/bcrypt-hash-provider.ts +20 -0
  8. package/src/auth/constants.ts +3 -3
  9. package/src/auth/generate-refresh-token.test.ts +19 -0
  10. package/src/auth/hash-provider.test.ts +46 -0
  11. package/src/auth/in-memory-hash-provider.ts +13 -0
  12. package/src/auth/jose-token-service.ts +55 -0
  13. package/src/auth/login.test.ts +92 -0
  14. package/src/auth/login.ts +15 -32
  15. package/src/auth/refresh.ts +0 -122
  16. package/src/auth/static-token-service.ts +18 -0
  17. package/src/auth/token-service.test.ts +82 -0
  18. package/src/factory.ts +80 -80
  19. package/src/features/automations/__tests__/action-executors.test.ts +268 -0
  20. package/src/features/automations/__tests__/automation-runner.test.ts +192 -0
  21. package/src/features/automations/__tests__/automation-runner.utils.test.ts +56 -0
  22. package/src/features/automations/__tests__/automations.handler.test.ts +260 -0
  23. package/src/features/automations/__tests__/automations.repository.test.ts +159 -0
  24. package/src/features/automations/__tests__/automations.schema.test.ts +134 -0
  25. package/src/features/automations/__tests__/context-resolver.test.ts +122 -0
  26. package/src/features/automations/__tests__/cron-runner.test.ts +263 -0
  27. package/src/features/automations/__tests__/cron-runner.utils.test.ts +140 -0
  28. package/src/features/automations/__tests__/set-variable.executor.test.ts +306 -0
  29. package/src/features/automations/__tests__/template-grammar.test.ts +304 -0
  30. package/src/features/automations/__tests__/when-evaluator.test.ts +270 -0
  31. package/src/features/automations/__tests__/when-pushdown.test.ts +277 -0
  32. package/src/features/automations/action-executors/create-entry.executor.ts +23 -0
  33. package/src/features/automations/action-executors/edit-field.executor.ts +22 -0
  34. package/src/features/automations/action-executors/index.ts +33 -0
  35. package/src/features/automations/action-executors/send-mail.executor.ts +42 -0
  36. package/src/features/automations/action-executors/set-variable.executor.ts +146 -0
  37. package/src/features/automations/action-executors/webhook.executor.ts +25 -0
  38. package/src/features/automations/automation-runner.ts +81 -0
  39. package/src/features/automations/automation-runner.utils.ts +43 -0
  40. package/src/features/automations/automations.handler.ts +193 -0
  41. package/src/features/automations/automations.schema.ts +160 -0
  42. package/src/features/automations/context-resolver.ts +148 -0
  43. package/src/features/automations/cron-runner.ts +136 -0
  44. package/src/features/automations/cron-runner.utils.ts +40 -0
  45. package/src/features/automations/filter-translation.ts +42 -0
  46. package/src/features/automations/index.ts +12 -0
  47. package/src/features/automations/template-grammar.ts +241 -0
  48. package/src/features/automations/var-access-resolver.ts +136 -0
  49. package/src/features/automations/when-evaluator.ts +83 -0
  50. package/src/features/automations/when-pushdown.ts +53 -0
  51. package/src/features/content/handlers/create.ts +22 -10
  52. package/src/features/content/handlers/delete.ts +21 -10
  53. package/src/features/content/handlers/update.ts +21 -9
  54. package/src/features/draft/draft.handler.ts +51 -153
  55. package/src/features/draft/draft.middleware.ts +62 -0
  56. package/src/features/email/email.service.ts +13 -0
  57. package/src/features/email/email.types.ts +10 -0
  58. package/src/features/email/index.ts +2 -1
  59. package/src/features/email/templates/automation-mail.ts +15 -0
  60. package/src/features/notifications/notifications.handler.ts +25 -54
  61. package/src/features/password-reset/request.ts +17 -41
  62. package/src/features/password-reset/reset.ts +18 -54
  63. package/src/features/rotate-field/rotate-field.handler.ts +15 -19
  64. package/src/features/schema/schema.handler.ts +1 -1
  65. package/src/features/settings/settings.handler.ts +64 -176
  66. package/src/features/setup/index.ts +12 -17
  67. package/src/features/stats/stats.handler.ts +110 -138
  68. package/src/index.ts +40 -8
  69. package/src/middleware/auth-providers.middleware.ts +32 -0
  70. package/src/middleware/observability.middleware.ts +52 -0
  71. package/src/middleware/rate-limit.middleware.ts +41 -0
  72. package/src/middleware/repository.middleware.ts +72 -5
  73. package/src/middleware.ts +15 -35
  74. package/src/public/cache-utils.ts +34 -0
  75. package/src/public/entry-projection.ts +42 -0
  76. package/src/public/idempotency.ts +19 -0
  77. package/src/public/problem-details.ts +5 -0
  78. package/src/public/public-add.ts +110 -166
  79. package/src/public/public-edit.ts +4 -3
  80. package/src/public/public-read.ts +59 -216
  81. package/src/public/public-routes.ts +2 -2
  82. package/src/public/query-builder.test.ts +220 -0
  83. package/src/public/rate-limit-middleware.ts +7 -19
  84. package/src/public/read-list.ts +50 -0
  85. package/src/public/read-single.ts +44 -0
  86. package/src/rate-limit/cloudflare-rate-limiter.test.ts +26 -0
  87. package/src/rate-limit/cloudflare-rate-limiter.ts +11 -0
  88. package/src/rate-limit/in-memory-rate-limiter.test.ts +33 -0
  89. package/src/rate-limit/in-memory-rate-limiter.ts +13 -0
  90. package/src/rate-limit/no-op-rate-limiter.test.ts +18 -0
  91. package/src/rate-limit/no-op-rate-limiter.ts +7 -0
  92. package/src/search-utils.test.ts +207 -0
  93. package/src/search-utils.ts +18 -1
  94. package/src/search.ts +24 -35
  95. package/src/shared/apply-policies.test.ts +77 -0
  96. package/src/shared/automations.repository.d1.ts +146 -0
  97. package/src/shared/background-notification-service.test.ts +58 -0
  98. package/src/shared/background-notification-service.ts +48 -0
  99. package/src/shared/content-utils.test.ts +161 -0
  100. package/src/shared/content.repository.d1.test.ts +312 -0
  101. package/src/shared/d1-activity-log.repository.test.ts +136 -0
  102. package/src/shared/d1-activity-log.repository.ts +101 -0
  103. package/src/shared/d1-activity-logger.test.ts +82 -0
  104. package/src/shared/d1-activity-logger.ts +63 -0
  105. package/src/shared/d1-analytics.repository.test.ts +74 -0
  106. package/src/shared/d1-analytics.repository.ts +81 -0
  107. package/src/shared/d1-content-scan.repository.test.ts +76 -0
  108. package/src/shared/d1-content-scan.repository.ts +29 -0
  109. package/src/shared/d1-notification.repository.test.ts +124 -0
  110. package/src/shared/d1-notification.repository.ts +114 -0
  111. package/src/shared/d1-password-reset-token.repository.test.ts +77 -0
  112. package/src/shared/d1-password-reset-token.repository.ts +52 -0
  113. package/src/shared/d1-search.repository.test.ts +83 -0
  114. package/src/shared/d1-search.repository.ts +84 -0
  115. package/src/shared/d1-session.repository.test.ts +121 -0
  116. package/src/shared/d1-session.repository.ts +98 -0
  117. package/src/shared/d1-user.repository.test.ts +147 -0
  118. package/src/shared/d1-user.repository.ts +109 -0
  119. package/src/shared/d1-widget.repository.test.ts +217 -0
  120. package/src/shared/d1-widget.repository.ts +337 -0
  121. package/src/shared/execution-context-scheduler.ts +9 -0
  122. package/src/shared/fixed-clock.ts +21 -0
  123. package/src/shared/idempotency.repository.d1.test.ts +79 -0
  124. package/src/shared/in-memory-activity-logger.ts +15 -0
  125. package/src/shared/in-memory-notification-service.ts +15 -0
  126. package/src/shared/media.repository.d1.test.ts +103 -0
  127. package/src/shared/media.repository.d1.ts +1 -1
  128. package/src/shared/request-utils.ts +22 -0
  129. package/src/shared/sequential-id-generator.ts +22 -0
  130. package/src/shared/storage-utils.ts +3 -3
  131. package/src/shared/system-stats.repository.d1.test.ts +54 -0
  132. package/src/types.ts +24 -3
  133. package/src/upload.ts +17 -9
  134. package/src/widget.ts +112 -253
  135. package/assets/dashboard/assets/index-CewtCjom.css +0 -1
  136. package/assets/dashboard/assets/index-FQ6JhvRH.js +0 -554
  137. package/src/shared/activity-logger.ts +0 -79
  138. package/src/shared/notification-service.ts +0 -56
@@ -1,79 +0,0 @@
1
- import { Context } from 'hono'
2
-
3
- export type ActivityAction = 'create' | 'update' | 'delete' | 'upload'
4
- export type EntityType = 'content' | 'media'
5
-
6
- export interface ActivityLogParams {
7
- action: ActivityAction
8
- entityType: EntityType
9
- entityId: string
10
- entitySlug?: string
11
- details?: Record<string, any>
12
- }
13
-
14
- /**
15
- * Logga un'azione utente asincronamente nel database D1.
16
- * Estrae le info dell'utente dal JWT payload presente nel contesto Hono.
17
- */
18
- export function logActivity(
19
- c: Context<any>,
20
- params: ActivityLogParams
21
- ): void {
22
- const db = c.env.DB as D1Database
23
- const user = c.get('jwtPayload') as { sub: string; email: string; name?: string } | undefined
24
-
25
- if (!db || !user) return
26
-
27
- const { action, entityType, entityId, entitySlug, details } = params
28
- const id = crypto.randomUUID()
29
-
30
- // Usa waitUntil per non bloccare la risposta al client
31
- let executionCtx: { waitUntil: (p: Promise<any>) => void } | undefined
32
- try {
33
- executionCtx = c.executionCtx
34
- } catch {
35
- // In ambiente di test Hono lancia se non presente
36
- }
37
-
38
- if (executionCtx) {
39
- executionCtx.waitUntil((async () => {
40
- try {
41
- await db.prepare(
42
- `INSERT INTO activity_logs (id, user_id, user_email, user_name, action, entity_type, entity_id, entity_slug, details)
43
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
44
- ).bind(
45
- id,
46
- user.sub,
47
- user.email || 'unknown',
48
- user.name || null,
49
- action,
50
- entityType,
51
- entityId,
52
- entitySlug || null,
53
- details ? JSON.stringify(details) : null
54
- ).run()
55
- } catch (err) {
56
- console.error('Failed to log activity:', err)
57
- }
58
- })())
59
- } else {
60
- try {
61
- db.prepare(
62
- `INSERT INTO activity_logs (id, user_id, user_email, user_name, action, entity_type, entity_id, entity_slug, details)
63
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
64
- ).bind(
65
- id,
66
- user.sub,
67
- user.email || 'unknown',
68
- user.name || null,
69
- action,
70
- entityType,
71
- entityId,
72
- entitySlug || null,
73
- details ? JSON.stringify(details) : null
74
- ).run().catch(err => console.error('Failed to log activity (sync fallback):', err))
75
- } catch (err) {
76
- console.error('Failed to log activity (sync fallback):', err)
77
- }
78
- }
79
- }
@@ -1,56 +0,0 @@
1
- import { Context } from 'hono'
2
-
3
- export interface NotificationParams {
4
- title: string
5
- message: string
6
- type?: 'info' | 'success' | 'warning' | 'error'
7
- }
8
-
9
- /**
10
- * Crea una notifica persistente nel database D1.
11
- * Usata principalmente per segnalare azioni provenienti dalle Public API.
12
- */
13
- export async function createNotification(
14
- c: Context<any>,
15
- params: NotificationParams
16
- ): Promise<void> {
17
- const db = c.env.DB as D1Database
18
- if (!db) return
19
-
20
- const { title, message, type = 'info' } = params
21
- const id = crypto.randomUUID()
22
-
23
- // Usiamo waitUntil per non bloccare la richiesta dell'utente esterno
24
- let executionCtx: { waitUntil: (p: Promise<any>) => void } | undefined
25
- try {
26
- executionCtx = c.executionCtx
27
- } catch {
28
- // In ambiente di test Hono lancia se non presente
29
- }
30
-
31
- if (executionCtx) {
32
- executionCtx.waitUntil((async () => {
33
- try {
34
- await db.prepare(
35
- `INSERT INTO notifications (id, title, message, type)
36
- VALUES (?, ?, ?, ?)`
37
- ).bind(id, title, message, type).run()
38
- } catch (err) {
39
- console.error('Failed to create notification:', err)
40
- }
41
- })())
42
- } else {
43
- // Fallback sync per test o ambienti senza executionCtx (se vogliamo che le notifiche siano create)
44
- // Oppure semplicemente ignoriamo. In questo caso, per i test, meglio tentare di crearle
45
- // ma dato che è un'azione opzionale "background", in test possiamo saltarla o farla sync.
46
- // Facciamola sync se non c'è executionCtx per garantire che i test che verificano le notifiche (se ce ne sono) passino.
47
- try {
48
- await db.prepare(
49
- `INSERT INTO notifications (id, title, message, type)
50
- VALUES (?, ?, ?, ?)`
51
- ).bind(id, title, message, type).run()
52
- } catch (err) {
53
- console.error('Failed to create notification (sync fallback):', err)
54
- }
55
- }
56
- }