@sayedhfatimi/convex-email 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/README.md +709 -2
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +7 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +9 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +20 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/resend/inbound.d.ts +5 -0
- package/dist/adapters/resend/inbound.d.ts.map +1 -0
- package/dist/adapters/resend/inbound.js +207 -0
- package/dist/adapters/resend/inbound.js.map +1 -0
- package/dist/adapters/resend/index.d.ts +4 -0
- package/dist/adapters/resend/index.d.ts.map +1 -0
- package/dist/adapters/resend/index.js +27 -0
- package/dist/adapters/resend/index.js.map +1 -0
- package/dist/adapters/resend/parse.d.ts +3 -0
- package/dist/adapters/resend/parse.d.ts.map +1 -0
- package/dist/adapters/resend/parse.js +94 -0
- package/dist/adapters/resend/parse.js.map +1 -0
- package/dist/adapters/resend/send.d.ts +15 -0
- package/dist/adapters/resend/send.d.ts.map +1 -0
- package/dist/adapters/resend/send.js +117 -0
- package/dist/adapters/resend/send.js.map +1 -0
- package/dist/adapters/resend/webhook.d.ts +6 -0
- package/dist/adapters/resend/webhook.d.ts.map +1 -0
- package/dist/adapters/resend/webhook.js +66 -0
- package/dist/adapters/resend/webhook.js.map +1 -0
- package/dist/adapters/ses/inbound.d.ts +3 -0
- package/dist/adapters/ses/inbound.d.ts.map +1 -0
- package/dist/adapters/ses/inbound.js +52 -0
- package/dist/adapters/ses/inbound.js.map +1 -0
- package/dist/adapters/ses/index.d.ts +4 -0
- package/dist/adapters/ses/index.d.ts.map +1 -0
- package/dist/adapters/ses/index.js +28 -0
- package/dist/adapters/ses/index.js.map +1 -0
- package/dist/adapters/ses/parse.d.ts +7 -0
- package/dist/adapters/ses/parse.d.ts.map +1 -0
- package/dist/adapters/ses/parse.js +209 -0
- package/dist/adapters/ses/parse.js.map +1 -0
- package/dist/adapters/ses/send.d.ts +10 -0
- package/dist/adapters/ses/send.d.ts.map +1 -0
- package/dist/adapters/ses/send.js +179 -0
- package/dist/adapters/ses/send.js.map +1 -0
- package/dist/adapters/ses/sigv4.d.ts +19 -0
- package/dist/adapters/ses/sigv4.d.ts.map +1 -0
- package/dist/adapters/ses/sigv4.js +50 -0
- package/dist/adapters/ses/sigv4.js.map +1 -0
- package/dist/adapters/ses/sns.d.ts +23 -0
- package/dist/adapters/ses/sns.d.ts.map +1 -0
- package/dist/adapters/ses/sns.js +281 -0
- package/dist/adapters/ses/sns.js.map +1 -0
- package/dist/adapters/types.d.ts +11 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +2 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/client/index.d.ts +214 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +304 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +62 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +345 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/attachments.d.ts +21 -0
- package/dist/component/attachments.d.ts.map +1 -0
- package/dist/component/attachments.js +66 -0
- package/dist/component/attachments.js.map +1 -0
- package/dist/component/cleanup.d.ts +15 -0
- package/dist/component/cleanup.d.ts.map +1 -0
- package/dist/component/cleanup.js +152 -0
- package/dist/component/cleanup.js.map +1 -0
- package/dist/component/config.d.ts +34 -0
- package/dist/component/config.d.ts.map +1 -0
- package/dist/component/config.js +33 -0
- package/dist/component/config.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +8 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/credentials.d.ts +19 -0
- package/dist/component/credentials.d.ts.map +1 -0
- package/dist/component/credentials.js +45 -0
- package/dist/component/credentials.js.map +1 -0
- package/dist/component/deliver.d.ts +58 -0
- package/dist/component/deliver.d.ts.map +1 -0
- package/dist/component/deliver.js +328 -0
- package/dist/component/deliver.js.map +1 -0
- package/dist/component/events.d.ts +15 -0
- package/dist/component/events.d.ts.map +1 -0
- package/dist/component/events.js +141 -0
- package/dist/component/events.js.map +1 -0
- package/dist/component/inbound.d.ts +354 -0
- package/dist/component/inbound.d.ts.map +1 -0
- package/dist/component/inbound.js +933 -0
- package/dist/component/inbound.js.map +1 -0
- package/dist/component/queries.d.ts +76 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +175 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +473 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +185 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/send.d.ts +112 -0
- package/dist/component/send.d.ts.map +1 -0
- package/dist/component/send.js +393 -0
- package/dist/component/send.js.map +1 -0
- package/dist/component/status.d.ts +2 -0
- package/dist/component/status.d.ts.map +1 -0
- package/dist/component/status.js +8 -0
- package/dist/component/status.js.map +1 -0
- package/dist/component/suppression.d.ts +36 -0
- package/dist/component/suppression.d.ts.map +1 -0
- package/dist/component/suppression.js +89 -0
- package/dist/component/suppression.js.map +1 -0
- package/dist/component/unsubscribe.d.ts +58 -0
- package/dist/component/unsubscribe.d.ts.map +1 -0
- package/dist/component/unsubscribe.js +129 -0
- package/dist/component/unsubscribe.js.map +1 -0
- package/dist/component/webhooks.d.ts +36 -0
- package/dist/component/webhooks.d.ts.map +1 -0
- package/dist/component/webhooks.js +71 -0
- package/dist/component/webhooks.js.map +1 -0
- package/dist/shared/address.d.ts +9 -0
- package/dist/shared/address.d.ts.map +1 -0
- package/dist/shared/address.js +76 -0
- package/dist/shared/address.js.map +1 -0
- package/dist/shared/authResults.d.ts +15 -0
- package/dist/shared/authResults.d.ts.map +1 -0
- package/dist/shared/authResults.js +86 -0
- package/dist/shared/authResults.js.map +1 -0
- package/dist/shared/htmlToText.d.ts +13 -0
- package/dist/shared/htmlToText.d.ts.map +1 -0
- package/dist/shared/htmlToText.js +221 -0
- package/dist/shared/htmlToText.js.map +1 -0
- package/dist/shared/mime.d.ts +4 -0
- package/dist/shared/mime.d.ts.map +1 -0
- package/dist/shared/mime.js +63 -0
- package/dist/shared/mime.js.map +1 -0
- package/dist/shared/readCapped.d.ts +7 -0
- package/dist/shared/readCapped.d.ts.map +1 -0
- package/dist/shared/readCapped.js +38 -0
- package/dist/shared/readCapped.js.map +1 -0
- package/dist/shared/sanitize.d.ts +10 -0
- package/dist/shared/sanitize.d.ts.map +1 -0
- package/dist/shared/sanitize.js +68 -0
- package/dist/shared/sanitize.js.map +1 -0
- package/dist/shared/threading.d.ts +6 -0
- package/dist/shared/threading.d.ts.map +1 -0
- package/dist/shared/threading.js +23 -0
- package/dist/shared/threading.js.map +1 -0
- package/dist/shared/types.d.ts +260 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +2 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/shared/unsubToken.d.ts +15 -0
- package/dist/shared/unsubToken.d.ts.map +1 -0
- package/dist/shared/unsubToken.js +83 -0
- package/dist/shared/unsubToken.js.map +1 -0
- package/dist/shared/validators.d.ts +760 -0
- package/dist/shared/validators.d.ts.map +1 -0
- package/dist/shared/validators.js +200 -0
- package/dist/shared/validators.js.map +1 -0
- package/package.json +88 -4
- package/src/adapters/index.ts +8 -0
- package/src/adapters/registry.ts +28 -0
- package/src/adapters/resend/inbound.ts +245 -0
- package/src/adapters/resend/index.ts +29 -0
- package/src/adapters/resend/parse.ts +118 -0
- package/src/adapters/resend/send.ts +125 -0
- package/src/adapters/resend/webhook.ts +77 -0
- package/src/adapters/ses/inbound.ts +63 -0
- package/src/adapters/ses/index.ts +30 -0
- package/src/adapters/ses/parse.ts +236 -0
- package/src/adapters/ses/send.ts +206 -0
- package/src/adapters/ses/sigv4.ts +56 -0
- package/src/adapters/ses/sns.ts +321 -0
- package/src/adapters/types.ts +27 -0
- package/src/client/index.ts +549 -0
- package/src/component/_generated/api.ts +78 -0
- package/src/component/_generated/component.ts +475 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/attachments.ts +69 -0
- package/src/component/cleanup.ts +173 -0
- package/src/component/config.ts +52 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/credentials.ts +63 -0
- package/src/component/deliver.ts +380 -0
- package/src/component/events.ts +179 -0
- package/src/component/inbound.ts +1107 -0
- package/src/component/queries.ts +203 -0
- package/src/component/schema.ts +222 -0
- package/src/component/send.ts +467 -0
- package/src/component/status.ts +8 -0
- package/src/component/suppression.ts +114 -0
- package/src/component/unsubscribe.ts +154 -0
- package/src/component/webhooks.ts +84 -0
- package/src/shared/address.ts +75 -0
- package/src/shared/authResults.ts +94 -0
- package/src/shared/htmlToText.ts +218 -0
- package/src/shared/mime.ts +80 -0
- package/src/shared/readCapped.ts +39 -0
- package/src/shared/sanitize.ts +73 -0
- package/src/shared/threading.ts +26 -0
- package/src/shared/types.ts +254 -0
- package/src/shared/unsubToken.ts +95 -0
- package/src/shared/validators.ts +282 -0
- package/src/test.ts +25 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createFunctionHandle,
|
|
3
|
+
type FunctionReference,
|
|
4
|
+
type FunctionReturnType,
|
|
5
|
+
type GenericActionCtx,
|
|
6
|
+
type GenericDataModel,
|
|
7
|
+
type GenericMutationCtx,
|
|
8
|
+
type GenericQueryCtx,
|
|
9
|
+
httpActionGeneric,
|
|
10
|
+
type PaginationOptions,
|
|
11
|
+
type PublicHttpAction,
|
|
12
|
+
} from "convex/server";
|
|
13
|
+
import type { ComponentApi } from "../component/_generated/component.js";
|
|
14
|
+
import type {
|
|
15
|
+
CredentialPurpose,
|
|
16
|
+
EventForApp,
|
|
17
|
+
InboundForApp,
|
|
18
|
+
MessageStatus,
|
|
19
|
+
ProviderName,
|
|
20
|
+
ResendCredentials,
|
|
21
|
+
RuntimeConfig,
|
|
22
|
+
SesCredentials,
|
|
23
|
+
Stream,
|
|
24
|
+
UnsubscribeCredentials,
|
|
25
|
+
} from "../shared/types.js";
|
|
26
|
+
|
|
27
|
+
export type {
|
|
28
|
+
CredentialPurpose,
|
|
29
|
+
EventForApp,
|
|
30
|
+
EventType,
|
|
31
|
+
InboundForApp,
|
|
32
|
+
MessageStatus,
|
|
33
|
+
ProviderName,
|
|
34
|
+
ResendCredentials,
|
|
35
|
+
SesCredentials,
|
|
36
|
+
Stream,
|
|
37
|
+
UnsubscribeCredentials,
|
|
38
|
+
} from "../shared/types.js";
|
|
39
|
+
export {
|
|
40
|
+
vCredentialPurpose,
|
|
41
|
+
vEventForApp,
|
|
42
|
+
vInboundForApp,
|
|
43
|
+
vProviderName,
|
|
44
|
+
} from "../shared/validators.js";
|
|
45
|
+
|
|
46
|
+
export type MessageId = string;
|
|
47
|
+
export type AttachmentId = string;
|
|
48
|
+
export type InboundId = string;
|
|
49
|
+
|
|
50
|
+
type Verdict = NonNullable<InboundForApp["verdicts"]["spf"]>;
|
|
51
|
+
export type Verdicts = { spf?: Verdict; dkim?: Verdict; dmarc?: Verdict };
|
|
52
|
+
|
|
53
|
+
export type MessageView = NonNullable<
|
|
54
|
+
FunctionReturnType<ComponentApi["queries"]["get"]>
|
|
55
|
+
>;
|
|
56
|
+
export type InboundView = FunctionReturnType<
|
|
57
|
+
ComponentApi["inbound"]["listUnhandled"]
|
|
58
|
+
>[number];
|
|
59
|
+
|
|
60
|
+
export type SendInput = {
|
|
61
|
+
from: string;
|
|
62
|
+
to: string | string[];
|
|
63
|
+
cc?: string | string[];
|
|
64
|
+
bcc?: string | string[];
|
|
65
|
+
replyTo?: string | string[];
|
|
66
|
+
subject: string;
|
|
67
|
+
html?: string;
|
|
68
|
+
text?: string;
|
|
69
|
+
attachments?: AttachmentId[];
|
|
70
|
+
/** A component message id or an RFC Message-ID. */
|
|
71
|
+
inReplyTo?: string;
|
|
72
|
+
/** Unsubscribe category; adds List-Unsubscribe headers. */
|
|
73
|
+
category?: string;
|
|
74
|
+
tenant?: string;
|
|
75
|
+
stream?: Stream;
|
|
76
|
+
tags?: Record<string, string>;
|
|
77
|
+
headers?: { name: string; value: string }[];
|
|
78
|
+
idempotencyKey?: string;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type EmailOptions = {
|
|
82
|
+
/** Every provider this deployment talks to. */
|
|
83
|
+
providers: ProviderName[];
|
|
84
|
+
/** The provider new mail goes out on; flipping it is the cutover. */
|
|
85
|
+
sendVia: ProviderName;
|
|
86
|
+
/** App function returning the credentials for `{ provider, tenant, purpose }`. */
|
|
87
|
+
credentials: FunctionReference<
|
|
88
|
+
"action",
|
|
89
|
+
"internal",
|
|
90
|
+
{
|
|
91
|
+
provider: ProviderName | null;
|
|
92
|
+
tenant: string | null;
|
|
93
|
+
purpose: CredentialPurpose;
|
|
94
|
+
},
|
|
95
|
+
ResendCredentials | SesCredentials | UnsubscribeCredentials
|
|
96
|
+
>;
|
|
97
|
+
onInbound?: FunctionReference<
|
|
98
|
+
"mutation",
|
|
99
|
+
"internal",
|
|
100
|
+
InboundForApp,
|
|
101
|
+
{ handled: boolean; note?: string }
|
|
102
|
+
>;
|
|
103
|
+
onEvent?: FunctionReference<"mutation", "internal", EventForApp, null>;
|
|
104
|
+
/** Default `true`: only allowlisted recipients are really sent to. */
|
|
105
|
+
testMode?: boolean;
|
|
106
|
+
testAllowlist?: string[];
|
|
107
|
+
/** Default tenant for calls that do not pass one. */
|
|
108
|
+
tenant?: string;
|
|
109
|
+
resend?: { ownDomains: string[] };
|
|
110
|
+
unsubscribe?: { url: string; mailto?: string };
|
|
111
|
+
keepRawMime?: boolean;
|
|
112
|
+
messageIdDomain?: string;
|
|
113
|
+
/** `Authentication-Results` authserv-ids to trust for inbound verdicts. */
|
|
114
|
+
trustedAuthServIds?: string[];
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
type QueryCtx = Pick<GenericQueryCtx<GenericDataModel>, "runQuery">;
|
|
118
|
+
type MutationCtx = Pick<
|
|
119
|
+
GenericMutationCtx<GenericDataModel>,
|
|
120
|
+
"runQuery" | "runMutation"
|
|
121
|
+
>;
|
|
122
|
+
type ActionCtx = Pick<
|
|
123
|
+
GenericActionCtx<GenericDataModel>,
|
|
124
|
+
"runQuery" | "runMutation" | "runAction"
|
|
125
|
+
>;
|
|
126
|
+
// Convex types `runQuery` and `runMutation` differently in an action context,
|
|
127
|
+
// so each method accepts either kind; the calls themselves are identical.
|
|
128
|
+
type RunQueryCtx = QueryCtx | MutationCtx | ActionCtx;
|
|
129
|
+
type RunMutationCtx = MutationCtx | ActionCtx;
|
|
130
|
+
type RunActionCtx = ActionCtx;
|
|
131
|
+
|
|
132
|
+
const asQuery = (ctx: RunQueryCtx) => ctx as QueryCtx;
|
|
133
|
+
const asMutation = (ctx: RunMutationCtx) => ctx as MutationCtx;
|
|
134
|
+
|
|
135
|
+
const TENANT = /^[A-Za-z0-9._~-]{1,128}$/;
|
|
136
|
+
const MAX_TOKEN = 2048;
|
|
137
|
+
const TEXT = { "content-type": "text/plain; charset=utf-8" };
|
|
138
|
+
|
|
139
|
+
const plain = (status: number, body: string, extra?: HeadersInit) =>
|
|
140
|
+
new Response(body, { status, headers: { ...TEXT, ...extra } });
|
|
141
|
+
|
|
142
|
+
export class Email {
|
|
143
|
+
constructor(
|
|
144
|
+
public component: ComponentApi,
|
|
145
|
+
public options: EmailOptions,
|
|
146
|
+
) {
|
|
147
|
+
if (!options.providers.includes(options.sendVia)) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`sendVia "${options.sendVia}" is not among the configured providers`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
options.providers.includes("resend") &&
|
|
154
|
+
options.onInbound &&
|
|
155
|
+
ownDomains(options).length === 0
|
|
156
|
+
) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
'resend.ownDomains is required when "resend" is a provider and onInbound is set: without it every Resend inbound message is ignored',
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The per-call config the component stores; it never carries credentials. */
|
|
164
|
+
private async config(): Promise<RuntimeConfig> {
|
|
165
|
+
// Handles are created on each call: they need a live function context.
|
|
166
|
+
const h = (ref: FunctionReference<"action" | "mutation", "internal">) =>
|
|
167
|
+
createFunctionHandle(ref as never) as Promise<string>;
|
|
168
|
+
const o = this.options;
|
|
169
|
+
return {
|
|
170
|
+
providers: o.providers,
|
|
171
|
+
sendVia: o.sendVia,
|
|
172
|
+
credentialsHandle: await h(o.credentials),
|
|
173
|
+
onInboundHandle: o.onInbound ? await h(o.onInbound) : undefined,
|
|
174
|
+
onEventHandle: o.onEvent ? await h(o.onEvent) : undefined,
|
|
175
|
+
testMode: o.testMode ?? true,
|
|
176
|
+
testAllowlist: o.testAllowlist ?? [],
|
|
177
|
+
defaultTenant: o.tenant,
|
|
178
|
+
adapterOptions: o.resend
|
|
179
|
+
? { resend: { ...o.resend, ownDomains: ownDomains(o) } }
|
|
180
|
+
: {},
|
|
181
|
+
unsubscribe: o.unsubscribe,
|
|
182
|
+
keepRawMime: o.keepRawMime ?? false,
|
|
183
|
+
messageIdDomain: o.messageIdDomain,
|
|
184
|
+
trustedAuthServIds: o.trustedAuthServIds,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Queue a message. Returns at once; delivery happens in the background. */
|
|
189
|
+
async send(ctx: RunMutationCtx, msg: SendInput): Promise<MessageId> {
|
|
190
|
+
const { messageId } = await asMutation(ctx).runMutation(
|
|
191
|
+
this.component.send.enqueue,
|
|
192
|
+
{
|
|
193
|
+
config: await this.config(),
|
|
194
|
+
message: msg,
|
|
195
|
+
},
|
|
196
|
+
);
|
|
197
|
+
return messageId;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Queue a `failed` message again (delivery gave up after its retries).
|
|
202
|
+
* Throws for any other status, or when the provider accepted the message
|
|
203
|
+
* and reported the failure afterwards. When an attempt got no answer from
|
|
204
|
+
* the provider (a network error or timeout) the message may already have
|
|
205
|
+
* been delivered: that is refused unless `force` is set. Suppression and
|
|
206
|
+
* unsubscribes are checked again; returns `queued`, or `suppressed` when no
|
|
207
|
+
* To recipient remains.
|
|
208
|
+
*/
|
|
209
|
+
async retrySend(
|
|
210
|
+
ctx: RunMutationCtx,
|
|
211
|
+
id: MessageId,
|
|
212
|
+
opts: { force?: boolean } = {},
|
|
213
|
+
): Promise<"queued" | "suppressed"> {
|
|
214
|
+
return await asMutation(ctx).runMutation(this.component.send.retry, {
|
|
215
|
+
messageId: id,
|
|
216
|
+
...(opts.force ? { force: true } : {}),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async storeAttachment(
|
|
221
|
+
ctx: RunMutationCtx,
|
|
222
|
+
a: {
|
|
223
|
+
bytes: ArrayBuffer;
|
|
224
|
+
filename: string;
|
|
225
|
+
contentType: string;
|
|
226
|
+
contentId?: string;
|
|
227
|
+
disposition?: "attachment" | "inline";
|
|
228
|
+
tenant?: string;
|
|
229
|
+
},
|
|
230
|
+
): Promise<AttachmentId> {
|
|
231
|
+
return await asMutation(ctx).runMutation(this.component.attachments.store, {
|
|
232
|
+
...a,
|
|
233
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async attachmentUploadUrl(ctx: RunMutationCtx): Promise<string> {
|
|
238
|
+
return await asMutation(ctx).runMutation(
|
|
239
|
+
this.component.attachments.uploadUrl,
|
|
240
|
+
{},
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async registerUpload(
|
|
245
|
+
ctx: RunMutationCtx,
|
|
246
|
+
a: {
|
|
247
|
+
storageId: string;
|
|
248
|
+
filename: string;
|
|
249
|
+
contentType: string;
|
|
250
|
+
tenant?: string;
|
|
251
|
+
},
|
|
252
|
+
): Promise<AttachmentId> {
|
|
253
|
+
return await asMutation(ctx).runMutation(
|
|
254
|
+
this.component.attachments.register,
|
|
255
|
+
{
|
|
256
|
+
...a,
|
|
257
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
258
|
+
},
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async status(ctx: RunQueryCtx, id: MessageId): Promise<MessageStatus | null> {
|
|
263
|
+
return await asQuery(ctx).runQuery(this.component.queries.status, {
|
|
264
|
+
messageId: id,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async get(ctx: RunQueryCtx, id: MessageId): Promise<MessageView | null> {
|
|
269
|
+
return await asQuery(ctx).runQuery(this.component.queries.get, {
|
|
270
|
+
messageId: id,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The text and HTML of a stored message (read it when an inbound message
|
|
276
|
+
* arrived `bodyByReference`). An action context is needed: only actions
|
|
277
|
+
* can read file storage.
|
|
278
|
+
*/
|
|
279
|
+
async inboundBody(
|
|
280
|
+
ctx: RunActionCtx,
|
|
281
|
+
id: MessageId,
|
|
282
|
+
): Promise<{ text?: string; html?: string }> {
|
|
283
|
+
return await ctx.runAction(this.component.queries.body, {
|
|
284
|
+
messageId: id,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async attachmentUrl(
|
|
289
|
+
ctx: RunQueryCtx,
|
|
290
|
+
id: AttachmentId,
|
|
291
|
+
): Promise<string | null> {
|
|
292
|
+
return await asQuery(ctx).runQuery(this.component.queries.attachmentUrl, {
|
|
293
|
+
attachmentId: id,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Hand in a raw RFC 822 message the app already holds. */
|
|
298
|
+
async receiveRaw(
|
|
299
|
+
ctx: RunActionCtx,
|
|
300
|
+
a: {
|
|
301
|
+
mime: ArrayBuffer;
|
|
302
|
+
tenant?: string;
|
|
303
|
+
source: string;
|
|
304
|
+
verdicts?: Verdicts;
|
|
305
|
+
},
|
|
306
|
+
): Promise<InboundId> {
|
|
307
|
+
return await ctx.runAction(this.component.inbound.receiveRaw, {
|
|
308
|
+
config: await this.config(),
|
|
309
|
+
...a,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* An HTTP action for a provider's webhook. With `tenantPathPrefix` (the
|
|
315
|
+
* route's own `pathPrefix`, e.g. "/email/webhook/ses/") exactly one segment
|
|
316
|
+
* after the prefix is the tenant; anything else is a 400.
|
|
317
|
+
*/
|
|
318
|
+
webhookHandler(
|
|
319
|
+
provider: ProviderName,
|
|
320
|
+
opts: { tenantPathPrefix?: string } = {},
|
|
321
|
+
): PublicHttpAction {
|
|
322
|
+
const prefix = opts.tenantPathPrefix;
|
|
323
|
+
if (
|
|
324
|
+
prefix !== undefined &&
|
|
325
|
+
!(prefix.startsWith("/") && prefix.endsWith("/"))
|
|
326
|
+
) {
|
|
327
|
+
throw new Error("tenantPathPrefix must start and end with /");
|
|
328
|
+
}
|
|
329
|
+
return httpActionGeneric(async (ctx, request) => {
|
|
330
|
+
let tenant: string | undefined;
|
|
331
|
+
if (prefix !== undefined) {
|
|
332
|
+
const tenantFromRequest = tenantAfterPrefix(
|
|
333
|
+
new URL(request.url).pathname,
|
|
334
|
+
prefix,
|
|
335
|
+
);
|
|
336
|
+
if (tenantFromRequest === null) return plain(400, "invalid tenant");
|
|
337
|
+
tenant = tenantFromRequest;
|
|
338
|
+
}
|
|
339
|
+
try {
|
|
340
|
+
const headers: Record<string, string> = {};
|
|
341
|
+
request.headers.forEach((value, key) => {
|
|
342
|
+
headers[key.toLowerCase()] = value;
|
|
343
|
+
});
|
|
344
|
+
const body = await request.text();
|
|
345
|
+
const reply = await ctx.runAction(this.component.webhooks.ingest, {
|
|
346
|
+
config: await this.config(),
|
|
347
|
+
provider,
|
|
348
|
+
request: { headers, body, tenant },
|
|
349
|
+
});
|
|
350
|
+
return plain(reply.status, reply.body);
|
|
351
|
+
} catch {
|
|
352
|
+
// Nothing from the failure leaves: it may name a credential.
|
|
353
|
+
return plain(500, "webhook error");
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* An HTTP action for the unsubscribe link. POST (RFC 8058 one-click) records
|
|
360
|
+
* the unsubscribe; GET only checks the link, then redirects to
|
|
361
|
+
* `redirectTo?ok=1&category=…` (or `ok=0`) when set.
|
|
362
|
+
*/
|
|
363
|
+
unsubscribeHandler(opts: { redirectTo?: string } = {}): PublicHttpAction {
|
|
364
|
+
return httpActionGeneric(async (ctx, request) => {
|
|
365
|
+
const method = request.method;
|
|
366
|
+
if (method !== "GET" && method !== "POST") {
|
|
367
|
+
return plain(405, "method not allowed", { allow: "GET, POST" });
|
|
368
|
+
}
|
|
369
|
+
let token = new URL(request.url).searchParams.get("t");
|
|
370
|
+
if (token === null && method === "POST") {
|
|
371
|
+
const type = request.headers.get("content-type") ?? "";
|
|
372
|
+
if (
|
|
373
|
+
type.toLowerCase().startsWith("application/x-www-form-urlencoded")
|
|
374
|
+
) {
|
|
375
|
+
const text = await request.text();
|
|
376
|
+
if (text.length <= MAX_TOKEN * 2) {
|
|
377
|
+
token = new URLSearchParams(text).get("t");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
let result: { ok: boolean; category?: string } = { ok: false };
|
|
382
|
+
if (token !== null && token !== "" && token.length <= MAX_TOKEN) {
|
|
383
|
+
try {
|
|
384
|
+
result = await ctx.runAction(this.component.unsubscribe.handle, {
|
|
385
|
+
config: await this.config(),
|
|
386
|
+
method,
|
|
387
|
+
token,
|
|
388
|
+
});
|
|
389
|
+
} catch {
|
|
390
|
+
result = { ok: false };
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (method === "POST") {
|
|
394
|
+
return plain(result.ok ? 200 : 400, result.ok ? "ok" : "invalid link");
|
|
395
|
+
}
|
|
396
|
+
// GET never records. The app's page (or ours) POSTs the token back.
|
|
397
|
+
if (opts.redirectTo !== undefined) {
|
|
398
|
+
const [base, hash] = splitHash(opts.redirectTo);
|
|
399
|
+
const query =
|
|
400
|
+
result.ok && token !== null
|
|
401
|
+
? `ok=1&category=${encodeURIComponent(result.category ?? "")}&t=${encodeURIComponent(token)}`
|
|
402
|
+
: "ok=0";
|
|
403
|
+
const sep = base.includes("?") ? "&" : "?";
|
|
404
|
+
return new Response(null, {
|
|
405
|
+
status: 302,
|
|
406
|
+
headers: {
|
|
407
|
+
location: `${base}${sep}${query}${hash}`,
|
|
408
|
+
"referrer-policy": "no-referrer",
|
|
409
|
+
"cache-control": "no-store",
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
if (!result.ok || token === null) return plain(400, "invalid link");
|
|
414
|
+
return new Response(confirmPage(new URL(request.url).pathname, token), {
|
|
415
|
+
status: 200,
|
|
416
|
+
headers: {
|
|
417
|
+
"content-type": "text/html; charset=utf-8",
|
|
418
|
+
"referrer-policy": "no-referrer",
|
|
419
|
+
"cache-control": "no-store",
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
suppressions = {
|
|
426
|
+
list: async (
|
|
427
|
+
ctx: RunQueryCtx,
|
|
428
|
+
a: { tenant?: string; paginationOpts: PaginationOptions },
|
|
429
|
+
) =>
|
|
430
|
+
await asQuery(ctx).runQuery(this.component.suppression.list, {
|
|
431
|
+
...a,
|
|
432
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
433
|
+
}),
|
|
434
|
+
add: async (
|
|
435
|
+
ctx: RunMutationCtx,
|
|
436
|
+
a: { tenant?: string; address: string },
|
|
437
|
+
): Promise<void> => {
|
|
438
|
+
await asMutation(ctx).runMutation(this.component.suppression.add, {
|
|
439
|
+
...a,
|
|
440
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
441
|
+
reason: "manual",
|
|
442
|
+
});
|
|
443
|
+
},
|
|
444
|
+
remove: async (
|
|
445
|
+
ctx: RunMutationCtx,
|
|
446
|
+
a: { tenant?: string; address: string },
|
|
447
|
+
): Promise<void> => {
|
|
448
|
+
await asMutation(ctx).runMutation(this.component.suppression.remove, {
|
|
449
|
+
...a,
|
|
450
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
451
|
+
});
|
|
452
|
+
},
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
unsubscribes = {
|
|
456
|
+
list: async (ctx: RunQueryCtx, a: { tenant?: string; address: string }) =>
|
|
457
|
+
await asQuery(ctx).runQuery(this.component.unsubscribe.list, {
|
|
458
|
+
...a,
|
|
459
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
460
|
+
}),
|
|
461
|
+
set: async (
|
|
462
|
+
ctx: RunMutationCtx,
|
|
463
|
+
a: {
|
|
464
|
+
tenant?: string;
|
|
465
|
+
address: string;
|
|
466
|
+
category: string;
|
|
467
|
+
unsubscribed: boolean;
|
|
468
|
+
},
|
|
469
|
+
): Promise<void> => {
|
|
470
|
+
await asMutation(ctx).runMutation(this.component.unsubscribe.set, {
|
|
471
|
+
...a,
|
|
472
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
473
|
+
});
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
async listUnhandled(
|
|
478
|
+
ctx: RunQueryCtx,
|
|
479
|
+
a: { tenant?: string; limit: number },
|
|
480
|
+
): Promise<InboundView[]> {
|
|
481
|
+
return await asQuery(ctx).runQuery(this.component.inbound.listUnhandled, {
|
|
482
|
+
...a,
|
|
483
|
+
tenant: a.tenant ?? this.options.tenant,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Try an inbound message again. `acted: false` means nothing was done
|
|
489
|
+
* (the row was not `failed` or `unhandled`); `state` is the row's state.
|
|
490
|
+
*/
|
|
491
|
+
async retryInbound(
|
|
492
|
+
ctx: RunActionCtx,
|
|
493
|
+
id: InboundId,
|
|
494
|
+
): Promise<{ acted: boolean; state: InboundView["state"] }> {
|
|
495
|
+
return await ctx.runAction(this.component.inbound.retry, { id });
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Remove bodies and attachment files older than `olderThanMs`, keeping
|
|
500
|
+
* message rows and events. Bounded per call: schedule it again while `more`.
|
|
501
|
+
*/
|
|
502
|
+
async cleanup(
|
|
503
|
+
ctx: RunMutationCtx,
|
|
504
|
+
a: { olderThanMs: number },
|
|
505
|
+
): Promise<{ removed: number; more: boolean }> {
|
|
506
|
+
return await asMutation(ctx).runMutation(this.component.cleanup.run, a);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** `" @Mail.Example.com "` and `"mail.example.com"` name the same domain. */
|
|
511
|
+
function ownDomains(options: EmailOptions): string[] {
|
|
512
|
+
return (options.resend?.ownDomains ?? []).flatMap((d) => {
|
|
513
|
+
const domain = d.trim().replace(/^@/, "").trim().toLowerCase();
|
|
514
|
+
return domain ? [domain] : [];
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const escapeHtml = (value: string) =>
|
|
519
|
+
value.replace(
|
|
520
|
+
/[&<>"']/g,
|
|
521
|
+
(c) =>
|
|
522
|
+
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
|
523
|
+
c
|
|
524
|
+
] as string,
|
|
525
|
+
);
|
|
526
|
+
|
|
527
|
+
/** One confirm button; the form posts the token back to this route. */
|
|
528
|
+
function confirmPage(action: string, token: string): string {
|
|
529
|
+
return `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Unsubscribe</title></head><body><form method="post" action="${escapeHtml(action)}"><input type="hidden" name="t" value="${escapeHtml(token)}"><button type="submit">Confirm unsubscribe</button></form></body></html>`;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** The one tenant segment after `prefix`, or null when the path is not that. */
|
|
533
|
+
function tenantAfterPrefix(pathname: string, prefix: string): string | null {
|
|
534
|
+
if (!pathname.startsWith(prefix)) return null;
|
|
535
|
+
const rest = pathname.slice(prefix.length);
|
|
536
|
+
if (rest === "" || rest.includes("/")) return null;
|
|
537
|
+
let tenant: string;
|
|
538
|
+
try {
|
|
539
|
+
tenant = decodeURIComponent(rest);
|
|
540
|
+
} catch {
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
return TENANT.test(tenant) ? tenant : null;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function splitHash(url: string): [string, string] {
|
|
547
|
+
const i = url.indexOf("#");
|
|
548
|
+
return i === -1 ? [url, ""] : [url.slice(0, i), url.slice(i)];
|
|
549
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type * as attachments from "../attachments.js";
|
|
12
|
+
import type * as cleanup from "../cleanup.js";
|
|
13
|
+
import type * as config from "../config.js";
|
|
14
|
+
import type * as credentials from "../credentials.js";
|
|
15
|
+
import type * as deliver from "../deliver.js";
|
|
16
|
+
import type * as events from "../events.js";
|
|
17
|
+
import type * as inbound from "../inbound.js";
|
|
18
|
+
import type * as queries from "../queries.js";
|
|
19
|
+
import type * as send from "../send.js";
|
|
20
|
+
import type * as status from "../status.js";
|
|
21
|
+
import type * as suppression from "../suppression.js";
|
|
22
|
+
import type * as unsubscribe from "../unsubscribe.js";
|
|
23
|
+
import type * as webhooks from "../webhooks.js";
|
|
24
|
+
|
|
25
|
+
import type {
|
|
26
|
+
ApiFromModules,
|
|
27
|
+
FilterApi,
|
|
28
|
+
FunctionReference,
|
|
29
|
+
} from "convex/server";
|
|
30
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
31
|
+
|
|
32
|
+
const fullApi: ApiFromModules<{
|
|
33
|
+
attachments: typeof attachments;
|
|
34
|
+
cleanup: typeof cleanup;
|
|
35
|
+
config: typeof config;
|
|
36
|
+
credentials: typeof credentials;
|
|
37
|
+
deliver: typeof deliver;
|
|
38
|
+
events: typeof events;
|
|
39
|
+
inbound: typeof inbound;
|
|
40
|
+
queries: typeof queries;
|
|
41
|
+
send: typeof send;
|
|
42
|
+
status: typeof status;
|
|
43
|
+
suppression: typeof suppression;
|
|
44
|
+
unsubscribe: typeof unsubscribe;
|
|
45
|
+
webhooks: typeof webhooks;
|
|
46
|
+
}> = anyApi as any;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
50
|
+
*
|
|
51
|
+
* Usage:
|
|
52
|
+
* ```js
|
|
53
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const api: FilterApi<
|
|
57
|
+
typeof fullApi,
|
|
58
|
+
FunctionReference<any, "public">
|
|
59
|
+
> = anyApi as any;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
63
|
+
*
|
|
64
|
+
* Usage:
|
|
65
|
+
* ```js
|
|
66
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export const internal: FilterApi<
|
|
70
|
+
typeof fullApi,
|
|
71
|
+
FunctionReference<any, "internal">
|
|
72
|
+
> = anyApi as any;
|
|
73
|
+
|
|
74
|
+
export const components = componentsGeneric() as unknown as {
|
|
75
|
+
transactional: import("@convex-dev/workpool/_generated/component.js").ComponentApi<"transactional">;
|
|
76
|
+
bulk: import("@convex-dev/workpool/_generated/component.js").ComponentApi<"bulk">;
|
|
77
|
+
callbacks: import("@convex-dev/workpool/_generated/component.js").ComponentApi<"callbacks">;
|
|
78
|
+
};
|