create-feedbax 0.2.0 → 0.2.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.
- package/README.md +17 -5
- package/dist/cli.js +2610 -291
- package/dist/index.js +2375 -200
- package/package.json +7 -7
- package/template/.feedbax-template.sha256 +1 -1
- package/template/.gitignore.template +1 -0
- package/template/.npmignore.template +1 -0
- package/template/.nvmrc +1 -0
- package/template/AGENTS.md +13 -0
- package/template/CONNECTED.md +418 -0
- package/template/README.md +97 -0
- package/template/components.json +1 -1
- package/template/eslint.config.mjs +23 -0
- package/template/feedbax-vite.ts +79 -0
- package/template/migrations/0001_connected.sql +6 -0
- package/template/migrations/0002_participation.sql +8 -0
- package/template/migrations/0003_images.sql +3 -0
- package/template/migrations/0004_followed_status.sql +9 -0
- package/template/migrations/0005_github.sql +4 -0
- package/template/migrations/0006_integration.sql +3 -0
- package/template/migrations/0007_ownership.sql +2 -0
- package/template/migrations/0008_integration_recovery.sql +5 -0
- package/template/migrations/0009_observed_history.sql +3 -0
- package/template/package.json +39 -34
- package/template/src/authorized-draft-post.tsx +1 -0
- package/template/src/changelog-page.tsx +63 -54
- package/template/src/changelog-runtime.ts +2 -1
- package/template/src/cloudflare-workers.d.ts +9 -0
- package/template/src/comment-creation.test.tsx +95 -7
- package/template/src/comment-thread-page.ts +11 -1
- package/template/src/components/command-palette.tsx +59 -30
- package/template/src/components/connected-identity-provider.tsx +135 -0
- package/template/src/components/device-profile-provider.tsx +41 -26
- package/template/src/components/follow-post-button.tsx +78 -0
- package/template/src/components/header.tsx +2 -0
- package/template/src/components/image-attachments.tsx +187 -0
- package/template/src/components/loader.tsx +48 -5
- package/template/src/components/notification-inbox.tsx +232 -0
- package/template/src/components/portal-footer.tsx +14 -0
- package/template/src/components/post-history.tsx +101 -0
- package/template/src/components/profile-menu.tsx +37 -0
- package/template/src/components/ui/components/command.tsx +30 -29
- package/template/src/components/ui/components/message-scroller.tsx +2 -2
- package/template/src/components/ui/components/scroll-area.tsx +0 -1
- package/template/src/components/ui/components/switch.tsx +29 -0
- package/template/src/components/ui/styles/globals.css +1 -1
- package/template/src/configured-feedback.test.ts +82 -0
- package/template/src/configured-feedback.ts +61 -0
- package/template/src/connected-identity.test.ts +148 -0
- package/template/src/connected-identity.ts +127 -0
- package/template/src/connected-participation.test.tsx +67 -0
- package/template/src/connected-runtime.test.ts +244 -0
- package/template/src/connected-runtime.ts +176 -0
- package/template/src/connected-social.test.tsx +158 -0
- package/template/src/feedback-cache-invalidation.ts +3 -0
- package/template/src/feedback-runtime.ts +123 -13
- package/template/src/feedbax/changelog/index.test.ts +35 -0
- package/template/src/feedbax/changelog/index.ts +5 -1
- package/template/src/feedbax/changelog/notion-changelog.test.ts +1 -1
- package/template/src/feedbax/changelog/notion-changelog.ts +1 -1
- package/template/src/feedbax/config/index.test.ts +73 -1
- package/template/src/feedbax/config/index.ts +205 -9
- package/template/src/feedbax/feedback/comment-storage.ts +38 -0
- package/template/src/feedbax/feedback/connected-feedback.ts +235 -0
- package/template/src/feedbax/feedback/connected.test.ts +272 -0
- package/template/src/feedbax/feedback/connected.ts +225 -0
- package/template/src/feedbax/feedback/email-delivery.test.ts +65 -0
- package/template/src/feedbax/feedback/email-delivery.ts +66 -0
- package/template/src/feedbax/feedback/feedback-storage.ts +1 -0
- package/template/src/feedbax/feedback/github-comments.ts +390 -0
- package/template/src/feedbax/feedback/github-feedback.test.ts +1138 -0
- package/template/src/feedbax/feedback/github-feedback.ts +581 -0
- package/template/src/feedbax/feedback/github-request.ts +120 -0
- package/template/src/feedbax/feedback/github-service-client.ts +56 -0
- package/template/src/feedbax/feedback/identity-handoff.test.ts +81 -0
- package/template/src/feedbax/feedback/identity-handoff.ts +119 -0
- package/template/src/feedbax/feedback/image-feedback.ts +114 -0
- package/template/src/feedbax/feedback/images.test.ts +223 -0
- package/template/src/feedbax/feedback/images.ts +350 -0
- package/template/src/feedbax/feedback/index.ts +226 -16
- package/template/src/feedbax/feedback/integration-auth.ts +138 -0
- package/template/src/feedbax/feedback/notion-comments.ts +2 -35
- package/template/src/feedbax/feedback/notion-image-review.ts +55 -0
- package/template/src/feedbax/feedback/observed-history.test.ts +150 -0
- package/template/src/feedbax/feedback/observed-history.ts +219 -0
- package/template/src/feedbax/feedback/ownership.ts +170 -0
- package/template/src/feedbax/feedback/participation.test.ts +653 -0
- package/template/src/feedbax/feedback/participation.ts +502 -0
- package/template/src/history-runtime.test.ts +80 -0
- package/template/src/history-runtime.ts +37 -0
- package/template/src/images-runtime.test.ts +133 -0
- package/template/src/images-runtime.ts +125 -0
- package/template/src/index.css +103 -440
- package/template/src/integration-runtime.test.ts +365 -0
- package/template/src/integration-runtime.ts +289 -0
- package/template/src/lazy-post-detail-overlay.tsx +13 -2
- package/template/src/my-feedback-page.test.tsx +152 -0
- package/template/src/my-feedback-page.tsx +398 -0
- package/template/src/my-feedback-runtime.test.ts +93 -0
- package/template/src/my-feedback-runtime.ts +154 -0
- package/template/src/overlay-post-detail.test.tsx +59 -8
- package/template/src/overlay-post-detail.tsx +20 -69
- package/template/src/participation-client.ts +63 -0
- package/template/src/participation-runtime.test.ts +35 -0
- package/template/src/participation-runtime.ts +255 -0
- package/template/src/portal-comments.ts +35 -5
- package/template/src/portal-feedback-form.tsx +29 -6
- package/template/src/portal-feedback-runtime.ts +3 -0
- package/template/src/portal-feedback-server-function.ts +6 -3
- package/template/src/portal-feedback-submission.ts +19 -0
- package/template/src/portal-server-entry.ts +13 -0
- package/template/src/post-creation-flow.tsx +2 -2
- package/template/src/post-detail-content.test.tsx +233 -0
- package/template/src/post-detail-content.tsx +162 -0
- package/template/src/post-detail-query.ts +34 -0
- package/template/src/post-results.tsx +1 -1
- package/template/src/public-cache-policy.test.ts +11 -4
- package/template/src/public-cache-policy.ts +1 -1
- package/template/src/public-feedback-index.tsx +9 -39
- package/template/src/public-feedback-page.test.tsx +65 -0
- package/template/src/public-feedback-page.ts +12 -1
- package/template/src/public-portal-error.tsx +1 -1
- package/template/src/public-post-detail-skeleton.tsx +54 -0
- package/template/src/public-post-detail.tsx +437 -271
- package/template/src/public-post-route.test.tsx +39 -3
- package/template/src/public-post-route.tsx +15 -28
- package/template/src/public-release-links.test.ts +120 -0
- package/template/src/public-release-links.ts +130 -0
- package/template/src/public-roadmap-route.test.tsx +38 -2
- package/template/src/public-roadmap-view.tsx +56 -23
- package/template/src/public-shell.test.tsx +26 -0
- package/template/src/routeTree.gen.ts +70 -3
- package/template/src/router.tsx +2 -0
- package/template/src/routes/__root.tsx +3 -5
- package/template/src/routes/email-preferences.tsx +77 -0
- package/template/src/routes/my-feedback.tsx +16 -0
- package/template/src/routes/submit.tsx +1 -1
- package/template/src/safe-public-failure.ts +3 -1
- package/template/src/server.ts +87 -2
- package/template/src/submission-intent.ts +26 -0
- package/template/src/trusted-feedback-handler.test.ts +1 -1
- package/template/src/vote-toggle.tsx +42 -9
- package/template/vite.config.ts +2 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
2
|
+
import { and, count, eq, gt, inArray } from "drizzle-orm";
|
|
3
|
+
import { drizzle } from "drizzle-orm/d1";
|
|
4
|
+
import {
|
|
5
|
+
integer,
|
|
6
|
+
primaryKey,
|
|
7
|
+
sqliteTable,
|
|
8
|
+
text,
|
|
9
|
+
} from "drizzle-orm/sqlite-core";
|
|
10
|
+
|
|
11
|
+
export type ConnectedDatabase = D1Database;
|
|
12
|
+
export interface VerifiedParticipant {
|
|
13
|
+
subject: string;
|
|
14
|
+
displayName: string;
|
|
15
|
+
}
|
|
16
|
+
const participants = sqliteTable("participants", {
|
|
17
|
+
subject: text("subject").primaryKey(),
|
|
18
|
+
displayName: text("display_name").notNull(),
|
|
19
|
+
});
|
|
20
|
+
const contacts = sqliteTable("participant_contacts", {
|
|
21
|
+
subject: text("subject").primaryKey(),
|
|
22
|
+
email: text("email"),
|
|
23
|
+
emailEnabled: integer("email_enabled").notNull().default(0),
|
|
24
|
+
});
|
|
25
|
+
const nonces = sqliteTable("identity_nonces", {
|
|
26
|
+
nonce: text("nonce").primaryKey(),
|
|
27
|
+
expiresAt: integer("expires_at").notNull(),
|
|
28
|
+
});
|
|
29
|
+
const sessions = sqliteTable("participant_sessions", {
|
|
30
|
+
tokenHash: text("token_hash").primaryKey(),
|
|
31
|
+
subject: text("subject")
|
|
32
|
+
.notNull()
|
|
33
|
+
.references(() => participants.subject),
|
|
34
|
+
expiresAt: integer("expires_at").notNull(),
|
|
35
|
+
});
|
|
36
|
+
const votes = sqliteTable(
|
|
37
|
+
"participant_votes",
|
|
38
|
+
{
|
|
39
|
+
postSlug: text("post_slug").notNull(),
|
|
40
|
+
subject: text("subject")
|
|
41
|
+
.notNull()
|
|
42
|
+
.references(() => participants.subject),
|
|
43
|
+
},
|
|
44
|
+
(t) => [primaryKey({ columns: [t.postSlug, t.subject] })],
|
|
45
|
+
);
|
|
46
|
+
const attribution = sqliteTable("comment_attribution", {
|
|
47
|
+
commentId: text("comment_id").primaryKey(),
|
|
48
|
+
subject: text("subject")
|
|
49
|
+
.notNull()
|
|
50
|
+
.references(() => participants.subject),
|
|
51
|
+
displayName: text("display_name").notNull(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export function createConnectedStore(binding: D1Database) {
|
|
55
|
+
// Without read-replication sessions, D1 routes these queries to its primary.
|
|
56
|
+
const db = drizzle(binding);
|
|
57
|
+
return {
|
|
58
|
+
async acceptIdentity(
|
|
59
|
+
input: VerifiedParticipant & {
|
|
60
|
+
email?: string;
|
|
61
|
+
nonce: string;
|
|
62
|
+
challenge?: string;
|
|
63
|
+
sessionHash: string;
|
|
64
|
+
expiresAt: number;
|
|
65
|
+
},
|
|
66
|
+
) {
|
|
67
|
+
// A nonce collision rolls the entire D1 batch back, including session creation.
|
|
68
|
+
await db.batch([
|
|
69
|
+
db.insert(nonces).values([
|
|
70
|
+
{ nonce: `token:${input.nonce}`, expiresAt: input.expiresAt },
|
|
71
|
+
...(input.challenge
|
|
72
|
+
? [
|
|
73
|
+
{
|
|
74
|
+
nonce: `browser:${input.challenge}`,
|
|
75
|
+
expiresAt: input.expiresAt,
|
|
76
|
+
},
|
|
77
|
+
]
|
|
78
|
+
: []),
|
|
79
|
+
]),
|
|
80
|
+
db
|
|
81
|
+
.insert(participants)
|
|
82
|
+
.values({ subject: input.subject, displayName: input.displayName })
|
|
83
|
+
.onConflictDoUpdate({
|
|
84
|
+
target: participants.subject,
|
|
85
|
+
set: { displayName: input.displayName },
|
|
86
|
+
}),
|
|
87
|
+
db
|
|
88
|
+
.insert(contacts)
|
|
89
|
+
.values({ subject: input.subject, email: input.email ?? null })
|
|
90
|
+
.onConflictDoUpdate({
|
|
91
|
+
target: contacts.subject,
|
|
92
|
+
set: { email: input.email ?? null },
|
|
93
|
+
}),
|
|
94
|
+
db.insert(sessions).values({
|
|
95
|
+
tokenHash: input.sessionHash,
|
|
96
|
+
subject: input.subject,
|
|
97
|
+
expiresAt: input.expiresAt,
|
|
98
|
+
}),
|
|
99
|
+
]);
|
|
100
|
+
},
|
|
101
|
+
async session(
|
|
102
|
+
tokenHash: string,
|
|
103
|
+
now = Date.now(),
|
|
104
|
+
): Promise<VerifiedParticipant | undefined> {
|
|
105
|
+
return db
|
|
106
|
+
.select({
|
|
107
|
+
subject: participants.subject,
|
|
108
|
+
displayName: participants.displayName,
|
|
109
|
+
})
|
|
110
|
+
.from(sessions)
|
|
111
|
+
.innerJoin(participants, eq(participants.subject, sessions.subject))
|
|
112
|
+
.where(
|
|
113
|
+
and(eq(sessions.tokenHash, tokenHash), gt(sessions.expiresAt, now)),
|
|
114
|
+
)
|
|
115
|
+
.get();
|
|
116
|
+
},
|
|
117
|
+
async logout(tokenHash: string) {
|
|
118
|
+
await db.delete(sessions).where(eq(sessions.tokenHash, tokenHash));
|
|
119
|
+
},
|
|
120
|
+
async setVote(postSlug: string, subject: string, voted: boolean) {
|
|
121
|
+
if (voted)
|
|
122
|
+
await db
|
|
123
|
+
.insert(votes)
|
|
124
|
+
.values({ postSlug, subject })
|
|
125
|
+
.onConflictDoNothing();
|
|
126
|
+
else
|
|
127
|
+
await db
|
|
128
|
+
.delete(votes)
|
|
129
|
+
.where(and(eq(votes.postSlug, postSlug), eq(votes.subject, subject)));
|
|
130
|
+
},
|
|
131
|
+
async voteState(postSlug: string, subject?: string) {
|
|
132
|
+
const [total, own] = await db.batch([
|
|
133
|
+
db
|
|
134
|
+
.select({ value: count() })
|
|
135
|
+
.from(votes)
|
|
136
|
+
.where(eq(votes.postSlug, postSlug)),
|
|
137
|
+
db
|
|
138
|
+
.select({ subject: votes.subject })
|
|
139
|
+
.from(votes)
|
|
140
|
+
.where(
|
|
141
|
+
and(eq(votes.postSlug, postSlug), eq(votes.subject, subject ?? "")),
|
|
142
|
+
)
|
|
143
|
+
.limit(1),
|
|
144
|
+
]);
|
|
145
|
+
return { voteCount: total[0]?.value ?? 0, voted: own.length > 0 };
|
|
146
|
+
},
|
|
147
|
+
async voteCounts(slugs: string[]) {
|
|
148
|
+
const result = new Map<string, number>();
|
|
149
|
+
for (let offset = 0; offset < slugs.length; offset += 80) {
|
|
150
|
+
const rows = await db
|
|
151
|
+
.select({ slug: votes.postSlug, total: count() })
|
|
152
|
+
.from(votes)
|
|
153
|
+
.where(inArray(votes.postSlug, slugs.slice(offset, offset + 80)))
|
|
154
|
+
.groupBy(votes.postSlug);
|
|
155
|
+
for (const row of rows) result.set(row.slug, row.total);
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
},
|
|
159
|
+
async commentSubject(commentId: string) {
|
|
160
|
+
return (
|
|
161
|
+
await db
|
|
162
|
+
.select({ subject: attribution.subject })
|
|
163
|
+
.from(attribution)
|
|
164
|
+
.where(eq(attribution.commentId, commentId))
|
|
165
|
+
.get()
|
|
166
|
+
)?.subject;
|
|
167
|
+
},
|
|
168
|
+
async attributeComment(
|
|
169
|
+
commentId: string,
|
|
170
|
+
participant: VerifiedParticipant,
|
|
171
|
+
) {
|
|
172
|
+
await db
|
|
173
|
+
.insert(attribution)
|
|
174
|
+
.values({
|
|
175
|
+
commentId,
|
|
176
|
+
subject: participant.subject,
|
|
177
|
+
displayName: participant.displayName,
|
|
178
|
+
})
|
|
179
|
+
.onConflictDoNothing();
|
|
180
|
+
const owner = await db
|
|
181
|
+
.select({ subject: attribution.subject })
|
|
182
|
+
.from(attribution)
|
|
183
|
+
.where(eq(attribution.commentId, commentId))
|
|
184
|
+
.get();
|
|
185
|
+
if (owner?.subject !== participant.subject)
|
|
186
|
+
throw new Error("Comment attribution is unavailable.");
|
|
187
|
+
},
|
|
188
|
+
async commentAuthors(ids: string[]) {
|
|
189
|
+
const result = new Map<
|
|
190
|
+
string,
|
|
191
|
+
{ kind: "participant"; displayName: string; verified: true }
|
|
192
|
+
>();
|
|
193
|
+
for (let offset = 0; offset < ids.length; offset += 80) {
|
|
194
|
+
const rows = await db
|
|
195
|
+
.select()
|
|
196
|
+
.from(attribution)
|
|
197
|
+
.where(
|
|
198
|
+
inArray(attribution.commentId, ids.slice(offset, offset + 80)),
|
|
199
|
+
);
|
|
200
|
+
for (const row of rows)
|
|
201
|
+
result.set(row.commentId, {
|
|
202
|
+
kind: "participant",
|
|
203
|
+
displayName: row.displayName,
|
|
204
|
+
verified: true,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
},
|
|
209
|
+
async commentAuthor(commentId: string) {
|
|
210
|
+
const row = await db
|
|
211
|
+
.select({ displayName: attribution.displayName })
|
|
212
|
+
.from(attribution)
|
|
213
|
+
.where(eq(attribution.commentId, commentId))
|
|
214
|
+
.get();
|
|
215
|
+
return row
|
|
216
|
+
? {
|
|
217
|
+
kind: "participant" as const,
|
|
218
|
+
displayName: row.displayName,
|
|
219
|
+
verified: true as const,
|
|
220
|
+
}
|
|
221
|
+
: undefined;
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
export type ConnectedStore = ReturnType<typeof createConnectedStore>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { createFetchMock, Miniflare } from "miniflare";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
|
|
6
|
+
it.each([200, 302])(
|
|
7
|
+
"sends email inside the actual Workers fetch runtime and rejects provider redirects (%s)",
|
|
8
|
+
async (status) => {
|
|
9
|
+
const source = await readFile(
|
|
10
|
+
new URL("./email-delivery.ts", import.meta.url),
|
|
11
|
+
"utf8",
|
|
12
|
+
);
|
|
13
|
+
const sender = ts.transpileModule(source, {
|
|
14
|
+
compilerOptions: {
|
|
15
|
+
target: ts.ScriptTarget.ES2022,
|
|
16
|
+
module: ts.ModuleKind.ESNext,
|
|
17
|
+
},
|
|
18
|
+
}).outputText;
|
|
19
|
+
const network = createFetchMock();
|
|
20
|
+
network.disableNetConnect();
|
|
21
|
+
network
|
|
22
|
+
.get("https://api.resend.com")
|
|
23
|
+
.intercept({ path: "/emails", method: "POST" })
|
|
24
|
+
.reply(status, "{}", {
|
|
25
|
+
headers:
|
|
26
|
+
status === 302 ? { location: "https://unexpected.example/leak" } : {},
|
|
27
|
+
});
|
|
28
|
+
let redirected = 0;
|
|
29
|
+
network
|
|
30
|
+
.get("https://unexpected.example")
|
|
31
|
+
.intercept({ path: "/leak", method: "GET" })
|
|
32
|
+
.reply(() => {
|
|
33
|
+
redirected++;
|
|
34
|
+
return { statusCode: 200, data: "{}" };
|
|
35
|
+
});
|
|
36
|
+
const runtime = new Miniflare({
|
|
37
|
+
compatibilityDate: "2026-07-30",
|
|
38
|
+
modulesRoot: "/email-worker",
|
|
39
|
+
fetchMock: network,
|
|
40
|
+
modules: [
|
|
41
|
+
{
|
|
42
|
+
type: "ESModule",
|
|
43
|
+
path: "/email-worker/entry.mjs",
|
|
44
|
+
contents: `import {createEmailSender} from './sender.mjs'; export default {async fetch(){ try {await createEmailSender({provider:'resend',token:'test-provider-key'})({from:'updates@example.com',to:['recipient@example.com'],subject:'Update',text:'A Post changed.',headers:{}},'test-stable-key');return new Response('accepted');}catch(error){return new Response(error.message,{status:502});}}};`,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: "ESModule",
|
|
48
|
+
path: "/email-worker/sender.mjs",
|
|
49
|
+
contents: sender,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
try {
|
|
54
|
+
const response = await runtime.dispatchFetch(
|
|
55
|
+
"https://worker.example/send",
|
|
56
|
+
);
|
|
57
|
+
expect(await response.text()).not.toContain("test-provider-key");
|
|
58
|
+
expect(response.status).toBe(status === 200 ? 200 : 502);
|
|
59
|
+
expect(redirected).toBe(0);
|
|
60
|
+
} finally {
|
|
61
|
+
await runtime.dispose();
|
|
62
|
+
await network.close();
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export interface NotificationEmail {
|
|
2
|
+
from: string;
|
|
3
|
+
to: string[];
|
|
4
|
+
subject: string;
|
|
5
|
+
text: string;
|
|
6
|
+
headers: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
export type EmailSender = (
|
|
9
|
+
message: NotificationEmail,
|
|
10
|
+
deliveryKey: string,
|
|
11
|
+
) => Promise<void>;
|
|
12
|
+
export type CloudflareEmailBinding = {
|
|
13
|
+
send(message: NotificationEmail): Promise<unknown>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** The custom endpoint accepts the same JSON message as Resend and must honor Idempotency-Key. */
|
|
17
|
+
export function createEmailSender(
|
|
18
|
+
options:
|
|
19
|
+
| { provider: "cloudflare"; binding: CloudflareEmailBinding }
|
|
20
|
+
| { provider: "resend"; token: string; request?: typeof fetch }
|
|
21
|
+
| {
|
|
22
|
+
provider: "custom";
|
|
23
|
+
url: string;
|
|
24
|
+
token: string;
|
|
25
|
+
request?: typeof fetch;
|
|
26
|
+
},
|
|
27
|
+
): EmailSender {
|
|
28
|
+
if (options.provider === "cloudflare")
|
|
29
|
+
return async (message) => {
|
|
30
|
+
await options.binding.send(message);
|
|
31
|
+
};
|
|
32
|
+
const url =
|
|
33
|
+
options.provider === "resend"
|
|
34
|
+
? "https://api.resend.com/emails"
|
|
35
|
+
: options.url;
|
|
36
|
+
const parsed = new URL(url);
|
|
37
|
+
if (
|
|
38
|
+
parsed.protocol !== "https:" ||
|
|
39
|
+
parsed.username ||
|
|
40
|
+
parsed.password ||
|
|
41
|
+
parsed.hash
|
|
42
|
+
)
|
|
43
|
+
throw new Error(
|
|
44
|
+
"Email sender requires an HTTPS endpoint without credentials or fragments.",
|
|
45
|
+
);
|
|
46
|
+
if (!options.token.trim()) throw new Error("Email sender secret is missing.");
|
|
47
|
+
return async (message, deliveryKey) => {
|
|
48
|
+
const response = await (options.request ?? fetch)(url, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
// Workers supports manual/follow; manual also prevents forwarding credentials.
|
|
51
|
+
redirect: "manual",
|
|
52
|
+
signal: AbortSignal.timeout(10000),
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${options.token}`,
|
|
55
|
+
"Content-Type": "application/json",
|
|
56
|
+
"Idempotency-Key": deliveryKey,
|
|
57
|
+
},
|
|
58
|
+
body: JSON.stringify(message),
|
|
59
|
+
});
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
await response.body?.cancel();
|
|
62
|
+
throw new Error("Email provider did not accept this delivery.");
|
|
63
|
+
}
|
|
64
|
+
await response.body?.cancel();
|
|
65
|
+
};
|
|
66
|
+
}
|