@voyantjs/notifications 0.1.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.
- package/LICENSE +109 -0
- package/README.md +100 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/provider-resolution.d.ts +14 -0
- package/dist/provider-resolution.d.ts.map +1 -0
- package/dist/provider-resolution.js +30 -0
- package/dist/providers/local.d.ts +22 -0
- package/dist/providers/local.d.ts.map +1 -0
- package/dist/providers/local.js +24 -0
- package/dist/providers/resend.d.ts +52 -0
- package/dist/providers/resend.d.ts.map +1 -0
- package/dist/providers/resend.js +50 -0
- package/dist/routes.d.ts +655 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +108 -0
- package/dist/schema.d.ts +1324 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +215 -0
- package/dist/service.d.ts +370 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +781 -0
- package/dist/tasks/index.d.ts +2 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +1 -0
- package/dist/tasks/send-due-reminders.d.ts +18 -0
- package/dist/tasks/send-due-reminders.d.ts.map +1 -0
- package/dist/tasks/send-due-reminders.js +6 -0
- package/dist/types.d.ts +58 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/validation.d.ts +275 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +138 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 PixelMakers Studio SRL
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly
|
|
28
|
+
display and redistribute the Software for any Permitted Purpose identified
|
|
29
|
+
below.
|
|
30
|
+
|
|
31
|
+
### Permitted Purpose
|
|
32
|
+
|
|
33
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing
|
|
34
|
+
Use means making the Software available to others in a commercial product or
|
|
35
|
+
service that:
|
|
36
|
+
|
|
37
|
+
1. substitutes for the Software;
|
|
38
|
+
|
|
39
|
+
2. substitutes for any other product or service we offer using the Software
|
|
40
|
+
that exists as of the date we make the Software available; or
|
|
41
|
+
|
|
42
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
43
|
+
|
|
44
|
+
Permitted Purposes specifically include using the Software:
|
|
45
|
+
|
|
46
|
+
1. for your internal use and access;
|
|
47
|
+
|
|
48
|
+
2. for non-commercial education;
|
|
49
|
+
|
|
50
|
+
3. for non-commercial research; and
|
|
51
|
+
|
|
52
|
+
4. in connection with professional services that you provide to a licensee
|
|
53
|
+
using the Software in accordance with these Terms and Conditions.
|
|
54
|
+
|
|
55
|
+
### Patents
|
|
56
|
+
|
|
57
|
+
To the extent your use for a Permitted Purpose would necessarily infringe
|
|
58
|
+
our patents, the license grant above includes a license under our patents.
|
|
59
|
+
If you make a claim against any party that the Software infringes or
|
|
60
|
+
contributes to the infringement of any patent, then your patent license to
|
|
61
|
+
the Software ends immediately.
|
|
62
|
+
|
|
63
|
+
### Redistribution
|
|
64
|
+
|
|
65
|
+
The Terms and Conditions apply to all copies, modifications and derivatives
|
|
66
|
+
of the Software.
|
|
67
|
+
|
|
68
|
+
If you redistribute any copies, modifications or derivatives of the
|
|
69
|
+
Software, you must include a copy of or a link to these Terms and Conditions
|
|
70
|
+
and not remove any copyright notices provided in or with the Software.
|
|
71
|
+
|
|
72
|
+
### Disclaimer
|
|
73
|
+
|
|
74
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS
|
|
75
|
+
OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A
|
|
76
|
+
PARTICULAR PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
77
|
+
|
|
78
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO
|
|
79
|
+
THE SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
|
|
80
|
+
DAMAGES, EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
81
|
+
|
|
82
|
+
### Trademarks
|
|
83
|
+
|
|
84
|
+
Except for displaying the License Details and identifying us as the origin
|
|
85
|
+
of the Software, you have no right under these Terms and Conditions to use
|
|
86
|
+
our trademarks, trade names, service marks or product names.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Grant of Future License
|
|
91
|
+
|
|
92
|
+
We hereby irrevocably grant you an additional license to use the Software
|
|
93
|
+
under the Apache License, Version 2.0 that is effective on the second
|
|
94
|
+
anniversary of the date we make the Software available. On or after that
|
|
95
|
+
date, you may use the Software under the Apache License, Version 2.0, in
|
|
96
|
+
which case the following will apply:
|
|
97
|
+
|
|
98
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
99
|
+
use this file except in compliance with the License.
|
|
100
|
+
|
|
101
|
+
You may obtain a copy of the License at
|
|
102
|
+
|
|
103
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
104
|
+
|
|
105
|
+
Unless required by applicable law or agreed to in writing, software
|
|
106
|
+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
107
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
108
|
+
License for the specific language governing permissions and limitations
|
|
109
|
+
under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @voyantjs/notifications
|
|
2
|
+
|
|
3
|
+
Notifications module for Voyant. It includes:
|
|
4
|
+
|
|
5
|
+
- provider abstraction via `NotificationProvider`
|
|
6
|
+
- first-party providers for local development and optional Resend (email)
|
|
7
|
+
- database-backed notification templates
|
|
8
|
+
- database-backed delivery logs
|
|
9
|
+
- notification reminder rules and reminder runs
|
|
10
|
+
- finance-aware send flows for invoices and payment sessions
|
|
11
|
+
- routes for template management, delivery listing, reminder management, and sending
|
|
12
|
+
|
|
13
|
+
CRM communication history should remain a business-facing log. This module owns transport templates, delivery attempts, provider message ids, and reminder-oriented operational sends.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @voyantjs/notifications
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { createNotificationService } from "@voyantjs/notifications"
|
|
25
|
+
import { createLocalProvider } from "@voyantjs/notifications/providers/local"
|
|
26
|
+
import { createResendProvider } from "@voyantjs/notifications/providers/resend"
|
|
27
|
+
|
|
28
|
+
const notifications = createNotificationService([
|
|
29
|
+
createLocalProvider(),
|
|
30
|
+
createResendProvider({ apiKey: env.RESEND_API_KEY, from: "noreply@example.com" }),
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
await notifications.sendWith("resend", {
|
|
34
|
+
to: "user@example.com",
|
|
35
|
+
channel: "email",
|
|
36
|
+
template: "welcome",
|
|
37
|
+
subject: "Hello",
|
|
38
|
+
html: "<p>Welcome</p>",
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Later providers override earlier ones on channel conflict; `sendWith(name, payload)` dispatches by provider name.
|
|
43
|
+
|
|
44
|
+
The bring-your-own path is first-class: any project can register its own `NotificationProvider`
|
|
45
|
+
without using Resend at all.
|
|
46
|
+
|
|
47
|
+
For the Hono module:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import {
|
|
51
|
+
createDefaultNotificationProviders,
|
|
52
|
+
createNotificationsHonoModule,
|
|
53
|
+
} from "@voyantjs/notifications"
|
|
54
|
+
|
|
55
|
+
const notificationsModule = createNotificationsHonoModule({
|
|
56
|
+
resolveProviders: (env) =>
|
|
57
|
+
createDefaultNotificationProviders(env, {
|
|
58
|
+
emailProvider: "resend",
|
|
59
|
+
}),
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`createDefaultNotificationProviders(env)` is intentionally local-only by default. Built-in
|
|
64
|
+
email providers should be opted into explicitly at the app boundary.
|
|
65
|
+
|
|
66
|
+
For scheduled reminder sweeps:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { sendDueNotificationReminders } from "@voyantjs/notifications/tasks"
|
|
70
|
+
|
|
71
|
+
await sendDueNotificationReminders(db, process.env, {
|
|
72
|
+
now: "2026-04-08T09:00:00.000Z",
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
For finance-aware collection sends, the routes also support:
|
|
77
|
+
|
|
78
|
+
- `POST /payment-sessions/:id/send`
|
|
79
|
+
- `POST /invoices/:id/send`
|
|
80
|
+
|
|
81
|
+
Those routes resolve recipients from the payment session, invoice, and linked booking participants, then render the selected notification template with finance context such as payment links, invoice balances, and booking references.
|
|
82
|
+
|
|
83
|
+
## Exports
|
|
84
|
+
|
|
85
|
+
| Entry | Description |
|
|
86
|
+
| --- | --- |
|
|
87
|
+
| `.` | Barrel re-exports |
|
|
88
|
+
| `./schema` | Drizzle tables and module definitions |
|
|
89
|
+
| `./types` | `NotificationProvider`, payload types |
|
|
90
|
+
| `./validation` | Zod schemas for templates, deliveries, reminders, send input |
|
|
91
|
+
| `./routes` | Hono route factory |
|
|
92
|
+
| `./service` | Dispatcher + database-backed notifications service |
|
|
93
|
+
| `./tasks` | Reminder sweep task helpers |
|
|
94
|
+
| `./provider-resolution` | Default local/Resend provider wiring |
|
|
95
|
+
| `./providers/local` | Console sink for dev |
|
|
96
|
+
| `./providers/resend` | Resend email provider |
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
FSL-1.1-Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { HonoModule } from "@voyantjs/hono/module";
|
|
2
|
+
import { createNotificationsRoutes } from "./routes.js";
|
|
3
|
+
export type { LocalProviderOptions } from "./providers/local.js";
|
|
4
|
+
export { createLocalProvider } from "./providers/local.js";
|
|
5
|
+
export { createDefaultNotificationProviders, createResendProviderFromEnv, } from "./provider-resolution.js";
|
|
6
|
+
export type { DefaultNotificationProviderOptions } from "./provider-resolution.js";
|
|
7
|
+
export type { ResendFetch, ResendProviderOptions, ResendRenderedEmail, } from "./providers/resend.js";
|
|
8
|
+
export { createResendProvider } from "./providers/resend.js";
|
|
9
|
+
export type { NotificationService } from "./service.js";
|
|
10
|
+
export { createNotificationService, notificationsService, NotificationError, renderNotificationTemplate, } from "./service.js";
|
|
11
|
+
export { notificationTemplates, notificationDeliveries, notificationReminderRules, notificationReminderRuns, notificationChannelEnum, notificationTemplateStatusEnum, notificationDeliveryStatusEnum, notificationTargetTypeEnum, notificationReminderStatusEnum, notificationReminderTargetTypeEnum, notificationReminderRunStatusEnum, notificationsModule, } from "./schema.js";
|
|
12
|
+
export type { NotificationTemplate, NewNotificationTemplate, NotificationDelivery, NewNotificationDelivery, NotificationReminderRule, NewNotificationReminderRule, NotificationReminderRun, NewNotificationReminderRun, NotificationsHonoModule, } from "./schema.js";
|
|
13
|
+
export { notificationChannelSchema, notificationTemplateStatusSchema, notificationDeliveryStatusSchema, notificationTargetTypeSchema, notificationReminderStatusSchema, notificationReminderTargetTypeSchema, notificationReminderRunStatusSchema, insertNotificationTemplateSchema, updateNotificationTemplateSchema, notificationTemplateListQuerySchema, notificationDeliveryListQuerySchema, insertNotificationReminderRuleSchema, updateNotificationReminderRuleSchema, notificationReminderRuleListQuerySchema, notificationReminderRunListQuerySchema, runDueRemindersSchema, sendPaymentSessionNotificationSchema, sendInvoiceNotificationSchema, sendNotificationSchema, } from "./validation.js";
|
|
14
|
+
export { createNotificationsRoutes } from "./routes.js";
|
|
15
|
+
export { sendDueNotificationReminders } from "./tasks/index.js";
|
|
16
|
+
export type { NotificationChannel, NotificationPayload, NotificationProvider, NotificationResult, } from "./types.js";
|
|
17
|
+
export declare function createNotificationsHonoModule(options?: Parameters<typeof createNotificationsRoutes>[0]): HonoModule;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAEvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAA;AAGvD,YAAY,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,EACL,kCAAkC,EAClC,2BAA2B,GAC5B,MAAM,0BAA0B,CAAA;AACjC,YAAY,EAAE,kCAAkC,EAAE,MAAM,0BAA0B,CAAA;AAClF,YAAY,EACV,WAAW,EACX,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAA;AAE5D,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AACvD,OAAO,EACL,yBAAyB,EACzB,oBAAoB,EACpB,iBAAiB,EACjB,0BAA0B,GAC3B,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,yBAAyB,EACzB,wBAAwB,EACxB,uBAAuB,EACvB,8BAA8B,EAC9B,8BAA8B,EAC9B,0BAA0B,EAC1B,8BAA8B,EAC9B,kCAAkC,EAClC,iCAAiC,EACjC,mBAAmB,GACpB,MAAM,aAAa,CAAA;AACpB,YAAY,EACV,oBAAoB,EACpB,uBAAuB,EACvB,oBAAoB,EACpB,uBAAuB,EACvB,wBAAwB,EACxB,2BAA2B,EAC3B,uBAAuB,EACvB,0BAA0B,EAC1B,uBAAuB,GACxB,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,yBAAyB,EACzB,gCAAgC,EAChC,gCAAgC,EAChC,4BAA4B,EAC5B,gCAAgC,EAChC,oCAAoC,EACpC,mCAAmC,EACnC,gCAAgC,EAChC,gCAAgC,EAChC,mCAAmC,EACnC,mCAAmC,EACnC,oCAAoC,EACpC,oCAAoC,EACpC,uCAAuC,EACvC,sCAAsC,EACtC,qBAAqB,EACrB,oCAAoC,EACpC,6BAA6B,EAC7B,sBAAsB,GACvB,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAA;AACvD,OAAO,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAA;AAC/D,YAAY,EACV,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,YAAY,CAAA;AAEnB,wBAAgB,6BAA6B,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC,CAAC,CAAC,GAAG,UAAU,CAKnH"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createNotificationsRoutes } from "./routes.js";
|
|
2
|
+
import { notificationsModule } from "./schema.js";
|
|
3
|
+
export { createLocalProvider } from "./providers/local.js";
|
|
4
|
+
export { createDefaultNotificationProviders, createResendProviderFromEnv, } from "./provider-resolution.js";
|
|
5
|
+
export { createResendProvider } from "./providers/resend.js";
|
|
6
|
+
export { createNotificationService, notificationsService, NotificationError, renderNotificationTemplate, } from "./service.js";
|
|
7
|
+
export { notificationTemplates, notificationDeliveries, notificationReminderRules, notificationReminderRuns, notificationChannelEnum, notificationTemplateStatusEnum, notificationDeliveryStatusEnum, notificationTargetTypeEnum, notificationReminderStatusEnum, notificationReminderTargetTypeEnum, notificationReminderRunStatusEnum, notificationsModule, } from "./schema.js";
|
|
8
|
+
export { notificationChannelSchema, notificationTemplateStatusSchema, notificationDeliveryStatusSchema, notificationTargetTypeSchema, notificationReminderStatusSchema, notificationReminderTargetTypeSchema, notificationReminderRunStatusSchema, insertNotificationTemplateSchema, updateNotificationTemplateSchema, notificationTemplateListQuerySchema, notificationDeliveryListQuerySchema, insertNotificationReminderRuleSchema, updateNotificationReminderRuleSchema, notificationReminderRuleListQuerySchema, notificationReminderRunListQuerySchema, runDueRemindersSchema, sendPaymentSessionNotificationSchema, sendInvoiceNotificationSchema, sendNotificationSchema, } from "./validation.js";
|
|
9
|
+
export { createNotificationsRoutes } from "./routes.js";
|
|
10
|
+
export { sendDueNotificationReminders } from "./tasks/index.js";
|
|
11
|
+
export function createNotificationsHonoModule(options) {
|
|
12
|
+
return {
|
|
13
|
+
module: notificationsModule,
|
|
14
|
+
routes: createNotificationsRoutes(options),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { NotificationProvider } from "./types.js";
|
|
2
|
+
type NotificationProviderEnv = {
|
|
3
|
+
RESEND_API_KEY?: unknown;
|
|
4
|
+
EMAIL_FROM?: unknown;
|
|
5
|
+
};
|
|
6
|
+
export interface DefaultNotificationProviderOptions {
|
|
7
|
+
includeLocal?: boolean;
|
|
8
|
+
emailProvider?: "resend" | null;
|
|
9
|
+
customProviders?: ReadonlyArray<NotificationProvider>;
|
|
10
|
+
}
|
|
11
|
+
export declare function createResendProviderFromEnv(env: NotificationProviderEnv): NotificationProvider | null;
|
|
12
|
+
export declare function createDefaultNotificationProviders(env: NotificationProviderEnv, options?: DefaultNotificationProviderOptions): NotificationProvider[];
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=provider-resolution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-resolution.d.ts","sourceRoot":"","sources":["../src/provider-resolution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAItD,KAAK,uBAAuB,GAAG;IAC7B,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AAED,MAAM,WAAW,kCAAkC;IACjD,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,aAAa,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAA;IAC/B,eAAe,CAAC,EAAE,aAAa,CAAC,oBAAoB,CAAC,CAAA;CACtD;AAED,wBAAgB,2BAA2B,CAAC,GAAG,EAAE,uBAAuB,+BAcvE;AAED,wBAAgB,kCAAkC,CAChD,GAAG,EAAE,uBAAuB,EAC5B,OAAO,GAAE,kCAAuC,0BAoBjD"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createLocalProvider } from "./providers/local.js";
|
|
2
|
+
import { createResendProvider } from "./providers/resend.js";
|
|
3
|
+
export function createResendProviderFromEnv(env) {
|
|
4
|
+
if (typeof env.RESEND_API_KEY === "string" &&
|
|
5
|
+
env.RESEND_API_KEY &&
|
|
6
|
+
typeof env.EMAIL_FROM === "string" &&
|
|
7
|
+
env.EMAIL_FROM) {
|
|
8
|
+
return createResendProvider({
|
|
9
|
+
apiKey: env.RESEND_API_KEY,
|
|
10
|
+
from: env.EMAIL_FROM,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
export function createDefaultNotificationProviders(env, options = {}) {
|
|
16
|
+
const providers = [];
|
|
17
|
+
if (options.includeLocal !== false) {
|
|
18
|
+
providers.push(createLocalProvider());
|
|
19
|
+
}
|
|
20
|
+
if (options.emailProvider === "resend") {
|
|
21
|
+
const resend = createResendProviderFromEnv(env);
|
|
22
|
+
if (resend) {
|
|
23
|
+
providers.push(resend);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (options.customProviders?.length) {
|
|
27
|
+
providers.push(...options.customProviders);
|
|
28
|
+
}
|
|
29
|
+
return providers;
|
|
30
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { NotificationChannel, NotificationPayload, NotificationProvider } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link createLocalProvider}.
|
|
4
|
+
*/
|
|
5
|
+
export interface LocalProviderOptions {
|
|
6
|
+
/** Channels this provider advertises. Defaults to `["email", "sms"]`. */
|
|
7
|
+
channels?: ReadonlyArray<NotificationChannel>;
|
|
8
|
+
/**
|
|
9
|
+
* Optional sink for captured payloads. Defaults to `console.log`.
|
|
10
|
+
* Tests can pass a vi.fn() to capture notifications without console noise.
|
|
11
|
+
*/
|
|
12
|
+
sink?: (payload: NotificationPayload) => void;
|
|
13
|
+
/** Provider name (defaults to `"local"`). Useful for stacking multiple locals. */
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a development/test notification provider that captures sends to a
|
|
18
|
+
* sink (console by default) and returns synthetic message ids. Never makes
|
|
19
|
+
* network calls.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createLocalProvider(options?: LocalProviderOptions): NotificationProvider;
|
|
22
|
+
//# sourceMappingURL=local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../src/providers/local.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EAErB,MAAM,aAAa,CAAA;AAEpB;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,yEAAyE;IACzE,QAAQ,CAAC,EAAE,aAAa,CAAC,mBAAmB,CAAC,CAAA;IAC7C;;;OAGG;IACH,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,IAAI,CAAA;IAC7C,kFAAkF;IAClF,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,oBAAyB,GAAG,oBAAoB,CAoB5F"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a development/test notification provider that captures sends to a
|
|
3
|
+
* sink (console by default) and returns synthetic message ids. Never makes
|
|
4
|
+
* network calls.
|
|
5
|
+
*/
|
|
6
|
+
export function createLocalProvider(options = {}) {
|
|
7
|
+
const name = options.name ?? "local";
|
|
8
|
+
const channels = options.channels ?? ["email", "sms"];
|
|
9
|
+
const sink = options.sink ??
|
|
10
|
+
((payload) => {
|
|
11
|
+
// biome-ignore lint/suspicious/noConsole: local provider is console-based by design
|
|
12
|
+
console.log(`[notifications:${name}]`, payload);
|
|
13
|
+
});
|
|
14
|
+
let counter = 0;
|
|
15
|
+
return {
|
|
16
|
+
name,
|
|
17
|
+
channels,
|
|
18
|
+
async send(payload) {
|
|
19
|
+
counter += 1;
|
|
20
|
+
sink(payload);
|
|
21
|
+
return { id: `${name}_${counter}`, provider: name };
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { NotificationProvider } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal `fetch` shape the Resend provider depends on. Works with the
|
|
4
|
+
* global `fetch` in Node 18+ / workers / browsers, and can be stubbed in
|
|
5
|
+
* tests.
|
|
6
|
+
*/
|
|
7
|
+
export type ResendFetch = (input: string, init: {
|
|
8
|
+
method: string;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
body: string;
|
|
11
|
+
}) => Promise<{
|
|
12
|
+
ok: boolean;
|
|
13
|
+
status: number;
|
|
14
|
+
json: () => Promise<unknown>;
|
|
15
|
+
text: () => Promise<string>;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Rendered email template. At least one of `html` or `text` should be set.
|
|
19
|
+
*/
|
|
20
|
+
export interface ResendRenderedEmail {
|
|
21
|
+
subject: string;
|
|
22
|
+
html?: string;
|
|
23
|
+
text?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Options for {@link createResendProvider}.
|
|
27
|
+
*/
|
|
28
|
+
export interface ResendProviderOptions {
|
|
29
|
+
/** Resend API key. */
|
|
30
|
+
apiKey: string;
|
|
31
|
+
/** Default sender address. Payload `from` overrides. */
|
|
32
|
+
from: string;
|
|
33
|
+
/** Override the Resend API base URL. Defaults to `https://api.resend.com`. */
|
|
34
|
+
baseUrl?: string;
|
|
35
|
+
/** Override `fetch` (e.g. in tests). Defaults to global `fetch`. */
|
|
36
|
+
fetch?: ResendFetch;
|
|
37
|
+
/**
|
|
38
|
+
* Render a template id + data tuple into an email body. When omitted,
|
|
39
|
+
* the payload's `template` is used as the subject and `data` is
|
|
40
|
+
* JSON-stringified into the text body.
|
|
41
|
+
*/
|
|
42
|
+
renderTemplate?: (template: string, data: unknown) => Promise<ResendRenderedEmail> | ResendRenderedEmail;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a notification provider that delivers email through the Resend HTTP
|
|
46
|
+
* API (https://resend.com/docs/api-reference/emails/send-email).
|
|
47
|
+
*
|
|
48
|
+
* Only the `"email"` channel is supported; attempting to send to any other
|
|
49
|
+
* channel throws.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createResendProvider(options: ResendProviderOptions): NotificationProvider;
|
|
52
|
+
//# sourceMappingURL=resend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resend.d.ts","sourceRoot":"","sources":["../../src/providers/resend.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAsB,MAAM,aAAa,CAAA;AAE3E;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,CACxB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE;IACJ,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B,IAAI,EAAE,MAAM,CAAA;CACb,KACE,OAAO,CAAC;IACX,EAAE,EAAE,OAAO,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;IAC5B,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;CAC5B,CAAC,CAAA;AAEF;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,sBAAsB;IACtB,MAAM,EAAE,MAAM,CAAA;IACd,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAA;IACZ,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,oEAAoE;IACpE,KAAK,CAAC,EAAE,WAAW,CAAA;IACnB;;;;OAIG;IACH,cAAc,CAAC,EAAE,CACf,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,KACV,OAAO,CAAC,mBAAmB,CAAC,GAAG,mBAAmB,CAAA;CACxD;AAMD;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,oBAAoB,CA6CzF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a notification provider that delivers email through the Resend HTTP
|
|
3
|
+
* API (https://resend.com/docs/api-reference/emails/send-email).
|
|
4
|
+
*
|
|
5
|
+
* Only the `"email"` channel is supported; attempting to send to any other
|
|
6
|
+
* channel throws.
|
|
7
|
+
*/
|
|
8
|
+
export function createResendProvider(options) {
|
|
9
|
+
const baseUrl = options.baseUrl ?? "https://api.resend.com";
|
|
10
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
11
|
+
return {
|
|
12
|
+
name: "resend",
|
|
13
|
+
channels: ["email"],
|
|
14
|
+
async send(payload) {
|
|
15
|
+
if (payload.channel !== "email") {
|
|
16
|
+
throw new Error(`Resend provider only supports the "email" channel, got "${payload.channel}"`);
|
|
17
|
+
}
|
|
18
|
+
if (!fetchImpl) {
|
|
19
|
+
throw new Error("Resend provider requires a fetch implementation");
|
|
20
|
+
}
|
|
21
|
+
const rendered = options.renderTemplate
|
|
22
|
+
? await options.renderTemplate(payload.template, payload.data)
|
|
23
|
+
: {
|
|
24
|
+
subject: payload.subject ?? payload.template,
|
|
25
|
+
text: JSON.stringify(payload.data ?? {}),
|
|
26
|
+
};
|
|
27
|
+
const body = {
|
|
28
|
+
from: payload.from ?? options.from,
|
|
29
|
+
to: payload.to,
|
|
30
|
+
subject: payload.subject ?? rendered.subject,
|
|
31
|
+
html: payload.html ?? rendered.html,
|
|
32
|
+
text: payload.text ?? rendered.text,
|
|
33
|
+
};
|
|
34
|
+
const response = await fetchImpl(`${baseUrl}/emails`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: {
|
|
37
|
+
Authorization: `Bearer ${options.apiKey}`,
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
},
|
|
40
|
+
body: JSON.stringify(body),
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const text = await response.text().catch(() => "");
|
|
44
|
+
throw new Error(`Resend send failed (${response.status}): ${text}`);
|
|
45
|
+
}
|
|
46
|
+
const data = (await response.json());
|
|
47
|
+
return { id: data.id, provider: "resend" };
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|