@hamur/notifications 0.0.0-stage → 0.0.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 (45) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +14 -2
  3. package/dist/src/index.js +235 -0
  4. package/dist/src/server/index.js +282 -0
  5. package/dist/src/test.js +32 -0
  6. package/dist/types/src/build/scan.d.ts +27 -0
  7. package/dist/types/src/build/setup.d.ts +4 -0
  8. package/dist/types/src/index.d.ts +11 -0
  9. package/dist/types/src/module.d.ts +3 -0
  10. package/dist/types/src/options.d.ts +9 -0
  11. package/dist/types/src/server/broadcast.d.ts +6 -0
  12. package/dist/types/src/server/deliver.d.ts +27 -0
  13. package/dist/types/src/server/inbox.d.ts +23 -0
  14. package/dist/types/src/server/index.d.ts +17 -0
  15. package/dist/types/src/server/notify.d.ts +20 -0
  16. package/dist/types/src/server/queue.d.ts +7 -0
  17. package/dist/types/src/server/recipient.d.ts +2 -0
  18. package/dist/types/src/server/record.d.ts +3 -0
  19. package/dist/types/src/server/runtime.d.ts +6 -0
  20. package/dist/types/src/server/task.d.ts +4 -0
  21. package/dist/types/src/server/topic.d.ts +3 -0
  22. package/dist/types/src/server/webhook.d.ts +10 -0
  23. package/dist/types/src/test.d.ts +5 -0
  24. package/dist/types/src/types.d.ts +87 -0
  25. package/package.json +51 -4
  26. package/src/build/scan.ts +119 -0
  27. package/src/build/setup.ts +111 -0
  28. package/src/env.d.ts +26 -0
  29. package/src/index.ts +13 -0
  30. package/src/module.ts +11 -0
  31. package/src/options.ts +52 -0
  32. package/src/server/broadcast.ts +9 -0
  33. package/src/server/deliver.ts +57 -0
  34. package/src/server/inbox.ts +121 -0
  35. package/src/server/index.ts +74 -0
  36. package/src/server/notify.ts +127 -0
  37. package/src/server/queue.ts +22 -0
  38. package/src/server/recipient.ts +10 -0
  39. package/src/server/record.ts +16 -0
  40. package/src/server/runtime.ts +42 -0
  41. package/src/server/task.ts +14 -0
  42. package/src/server/topic.ts +18 -0
  43. package/src/server/webhook.ts +71 -0
  44. package/src/test.ts +35 -0
  45. package/src/types.ts +93 -0
package/src/types.ts ADDED
@@ -0,0 +1,93 @@
1
+ import type { MailAddress, MailMessage } from "@hamur/mail"
2
+ import type { EnvRef } from "hamur"
3
+ import type { StandardSchemaV1 } from "hamur/server"
4
+ import type { HamurNotifications } from "./index.ts"
5
+
6
+ /** In the order a notification's deliveries are made. */
7
+ export const CHANNELS = ["mail", "database", "broadcast", "webhook"] as const
8
+
9
+ export type NotificationChannel = (typeof CHANNELS)[number]
10
+
11
+ export interface WebhookTarget {
12
+ url: string
13
+ /** A Standard Webhooks secret, `whsec_…`. */
14
+ secret: EnvRef | string
15
+ }
16
+
17
+ export interface Recipient {
18
+ /** An entity type name, such as `User`. */
19
+ type: string
20
+ id: string
21
+ email?: MailAddress
22
+ /** The recipient's own endpoint; default `notifications.webhook`. */
23
+ webhook?: WebhookTarget
24
+ /** Read by `via()`. */
25
+ prefs?: Record<string, unknown>
26
+ }
27
+
28
+ export interface NotificationsConfig {
29
+ /** Default true: `notify` queues one task per delivery. */
30
+ queue?: boolean
31
+ /** The endpoint of a recipient without one. */
32
+ webhook?: { url: string; secret: EnvRef }
33
+ /** The table of the database channel; default `"notifications"`. */
34
+ table?: string
35
+ /** The connection of `db.connections` that has the table; default `"default"`. */
36
+ connection?: string
37
+ }
38
+
39
+ export type NotificationName = keyof HamurNotifications extends never ? string : keyof HamurNotifications & string
40
+
41
+ export type NotificationInput<N> = N extends keyof HamurNotifications
42
+ ? HamurNotifications[N] extends { input: infer I }
43
+ ? I
44
+ : unknown
45
+ : unknown
46
+
47
+ /** What `defineNotification` takes; a channel's function gets the validated input and the recipient without its `webhook`. */
48
+ export interface NotificationOptions<In, Out> {
49
+ input: StandardSchemaV1<In, Out>
50
+ mail?: (input: Out, to: Recipient) => Omit<MailMessage, "to">
51
+ database?: (input: Out, to: Recipient) => unknown
52
+ broadcast?: (input: Out, to: Recipient) => unknown
53
+ webhook?: (input: Out, to: Recipient) => unknown
54
+ via?: (to: Recipient, input: Out) => readonly NotificationChannel[]
55
+ }
56
+
57
+ /** A notification; `In` is what `notify` takes. */
58
+ export interface Notification<In = unknown> {
59
+ /** Types only: never set. */
60
+ readonly "~types"?: { input: In }
61
+ }
62
+
63
+ /** A notification of the database channel, as `listNotifications` returns it. */
64
+ export interface NotificationRow {
65
+ id: string
66
+ notification: NotificationName
67
+ /** What the notification's `database` function returned. */
68
+ data: unknown
69
+ readAt: Date | null
70
+ createdAt: Date
71
+ }
72
+
73
+ /** One (recipient, channel) of a `notify` call. */
74
+ export interface Delivery {
75
+ /** A UUID v7, the same on every attempt: the `webhook-id`, the row's `id`, and the left side of a mail's Message-ID. */
76
+ id: string
77
+ name: string
78
+ channel: NotificationChannel
79
+ /** Without `webhook`. */
80
+ to: Recipient
81
+ input: unknown
82
+ webhook?: WebhookTarget
83
+ /** The `site.url` of the request `notify` was called in, a tenant's own with `site.tenants`; a mail links to it. */
84
+ site?: string
85
+ }
86
+
87
+ /** A line of `notifications.jsonl` in a test build. */
88
+ export interface DeliveryRecord {
89
+ name: string
90
+ channel: NotificationChannel
91
+ recipient: { type: string; id: string }
92
+ payload: unknown
93
+ }