groundmail 0.0.0-stage → 0.14.1

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.
@@ -0,0 +1,64 @@
1
+ import { G as Groundmail, S as SendEmailResult } from './client-DdrSVDkA.js';
2
+
3
+ /**
4
+ * Subset of Clerk `email.created` webhook `data` used for delivery.
5
+ * Compatible with Clerk's EmailEventPayload without requiring `@clerk/*` at build time.
6
+ */
7
+ type ClerkEmailCreatedData = {
8
+ to_email_address: string;
9
+ subject: string;
10
+ /** HTML body Clerk would have sent. */
11
+ body: string;
12
+ body_plain?: string | null;
13
+ /** When true, Clerk still delivers — skip unless `force` is set. */
14
+ delivered_by_clerk?: boolean;
15
+ slug?: string | null;
16
+ id?: string;
17
+ };
18
+ type GroundmailClerkOptions = {
19
+ /** Verified sender, e.g. `Auth <noreply@yourdomain.com>`. Required. */
20
+ from: string;
21
+ /** Groundmail API key (`gm_…`). Falls back to `GROUNDMAIL_API_KEY`. */
22
+ apiKey?: string;
23
+ /** Override API base URL (tests / self-hosted). */
24
+ baseUrl?: string;
25
+ /** Injected client (tests). */
26
+ client?: Groundmail;
27
+ /**
28
+ * Skip sends when `delivered_by_clerk` is true (default `true`).
29
+ * Set false only if you intentionally dual-send.
30
+ */
31
+ skipIfDeliveredByClerk?: boolean;
32
+ /** Force send even when `delivered_by_clerk` is true. */
33
+ force?: boolean;
34
+ };
35
+ /**
36
+ * Deliver one Clerk `email.created` payload through Groundmail.
37
+ * Returns `null` when skipped because Clerk still delivers the template.
38
+ */
39
+ declare function deliverClerkEmail(data: ClerkEmailCreatedData, options: GroundmailClerkOptions): Promise<SendEmailResult | null>;
40
+ /**
41
+ * Returns a deliverer bound to your Groundmail options — use after verifying the Clerk webhook.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { verifyWebhook } from '@clerk/nextjs/webhooks'
46
+ * import { createClerkEmailDeliverer } from 'groundmail-clerk'
47
+ *
48
+ * const deliver = createClerkEmailDeliverer({
49
+ * from: 'noreply@yourdomain.com',
50
+ * apiKey: process.env.GROUNDMAIL_API_KEY!,
51
+ * })
52
+ *
53
+ * export async function POST(req: Request) {
54
+ * const evt = await verifyWebhook(req)
55
+ * if (evt.type === 'email.created') {
56
+ * await deliver(evt.data)
57
+ * }
58
+ * return new Response('ok')
59
+ * }
60
+ * ```
61
+ */
62
+ declare function createClerkEmailDeliverer(options: GroundmailClerkOptions): (data: ClerkEmailCreatedData) => Promise<SendEmailResult | null>;
63
+
64
+ export { type ClerkEmailCreatedData, type GroundmailClerkOptions, createClerkEmailDeliverer, deliverClerkEmail };
package/dist/clerk.js ADDED
@@ -0,0 +1,51 @@
1
+ import {
2
+ DEFAULT_API_BASE_URL,
3
+ Groundmail
4
+ } from "./chunk-IZYE7HGT.js";
5
+
6
+ // src/clerk.ts
7
+ function resolveApiKey(options) {
8
+ return options.apiKey || process.env.GROUNDMAIL_API_KEY;
9
+ }
10
+ function resolveClient(options) {
11
+ if (options.client) return options.client;
12
+ const apiKey = resolveApiKey(options);
13
+ if (!apiKey) {
14
+ throw new Error(
15
+ "Groundmail Clerk deliverer: missing apiKey. Pass options.apiKey or set GROUNDMAIL_API_KEY."
16
+ );
17
+ }
18
+ return new Groundmail(apiKey, {
19
+ baseUrl: options.baseUrl ?? DEFAULT_API_BASE_URL
20
+ });
21
+ }
22
+ async function deliverClerkEmail(data, options) {
23
+ if (!options.from) {
24
+ throw new Error("Groundmail Clerk deliverer: options.from is required.");
25
+ }
26
+ if (!data?.to_email_address) {
27
+ throw new Error("Groundmail Clerk deliverer: missing to_email_address on webhook data.");
28
+ }
29
+ const skip = options.skipIfDeliveredByClerk !== false;
30
+ if (skip && data.delivered_by_clerk && !options.force) {
31
+ return null;
32
+ }
33
+ const client = resolveClient(options);
34
+ const tagValue = data.slug ? `clerk-${data.slug}` : "clerk-email";
35
+ return client.emails.send({
36
+ from: options.from,
37
+ to: data.to_email_address,
38
+ subject: data.subject || "Notification",
39
+ html: data.body,
40
+ text: data.body_plain ?? void 0,
41
+ tags: [{ name: "auth", value: tagValue }],
42
+ idempotencyKey: data.id ? `clerk-email-${data.id}` : void 0
43
+ });
44
+ }
45
+ function createClerkEmailDeliverer(options) {
46
+ return (data) => deliverClerkEmail(data, options);
47
+ }
48
+ export {
49
+ createClerkEmailDeliverer,
50
+ deliverClerkEmail
51
+ };