Skip to content

Commit f6e46f0

Browse files
committed
address template feedback
1 parent 95b1efa commit f6e46f0

File tree

4 files changed

+26
-58
lines changed

4 files changed

+26
-58
lines changed

src/client/index.ts

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { v, type VString } from "convex/values";
1010
import { Webhook } from "svix";
1111
import {
12+
Template,
1213
vEmailEvent,
1314
type EmailEvent,
1415
type RunMutationCtx,
@@ -303,43 +304,20 @@ export class Resend {
303304
if (this.config.apiKey === "") throw new Error("API key is not set");
304305

305306
// Prepare the mutation args based on whether it's a template or traditional email
306-
if ("template" in sendEmailArgs) {
307-
// Template-based email
308-
const id = await ctx.runMutation(this.component.lib.sendEmail, {
309-
options: await configToRuntimeConfig(this.config, this.onEmailEvent),
310-
from: sendEmailArgs.from,
311-
to:
312-
typeof sendEmailArgs.to === "string"
313-
? [sendEmailArgs.to]
314-
: sendEmailArgs.to,
315-
cc: toArray(sendEmailArgs.cc),
316-
bcc: toArray(sendEmailArgs.bcc),
317-
subject: sendEmailArgs.subject,
318-
replyTo: sendEmailArgs.replyTo,
319-
headers: sendEmailArgs.headers,
320-
templateId: sendEmailArgs.template.id,
321-
templateVariables: JSON.stringify(sendEmailArgs.template.variables),
322-
});
323-
return id as EmailId;
324-
} else {
325-
// Traditional email
326-
const id = await ctx.runMutation(this.component.lib.sendEmail, {
327-
options: await configToRuntimeConfig(this.config, this.onEmailEvent),
328-
from: sendEmailArgs.from,
329-
to:
330-
typeof sendEmailArgs.to === "string"
331-
? [sendEmailArgs.to]
332-
: sendEmailArgs.to,
333-
cc: toArray(sendEmailArgs.cc),
334-
bcc: toArray(sendEmailArgs.bcc),
335-
replyTo: sendEmailArgs.replyTo,
336-
headers: sendEmailArgs.headers,
337-
subject: sendEmailArgs.subject,
338-
html: sendEmailArgs.html,
339-
text: sendEmailArgs.text,
340-
});
341-
return id as EmailId;
342-
}
307+
308+
// Traditional email
309+
const id = await ctx.runMutation(this.component.lib.sendEmail, {
310+
options: await configToRuntimeConfig(this.config, this.onEmailEvent),
311+
...sendEmailArgs,
312+
to:
313+
typeof sendEmailArgs.to === "string"
314+
? [sendEmailArgs.to]
315+
: sendEmailArgs.to,
316+
cc: toArray(sendEmailArgs.cc),
317+
bcc: toArray(sendEmailArgs.bcc),
318+
});
319+
320+
return id as EmailId;
343321
}
344322

345323
async sendEmailManually(
@@ -452,8 +430,7 @@ export class Resend {
452430
createdAt: number;
453431
html?: string;
454432
text?: string;
455-
templateId?: string;
456-
templateVariables?: string;
433+
template?: Template;
457434
} | null> {
458435
return await ctx.runQuery(this.component.lib.get, {
459436
emailId,

src/component/_generated/component.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9090
| "bounced"
9191
| "failed";
9292
subject?: string;
93-
templateId?: string;
94-
templateVariables?: string;
93+
template?: { id: string; variables: Record<string, string | number> };
9594
text?: string;
9695
to: Array<string>;
9796
} | null,
@@ -146,8 +145,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
146145
};
147146
replyTo?: Array<string>;
148147
subject?: string;
149-
templateId?: string;
150-
templateVariables?: string;
148+
template?: { id: string; variables: Record<string, string | number> };
151149
text?: string;
152150
to: Array<string>;
153151
},

src/component/lib.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
vEmailEvent,
1919
vOptions,
2020
vStatus,
21+
vTemplate,
2122
} from "./shared.js";
2223
import type { FunctionHandle } from "convex/server";
2324
import type { EmailEvent, RunMutationCtx, RunQueryCtx } from "./shared.js";
@@ -129,8 +130,7 @@ export const sendEmail = mutation({
129130
subject: v.optional(v.string()),
130131
html: v.optional(v.string()),
131132
text: v.optional(v.string()),
132-
templateId: v.optional(v.string()),
133-
templateVariables: v.optional(v.string()),
133+
template: v.optional(vTemplate),
134134
replyTo: v.optional(v.array(v.string())),
135135
headers: v.optional(
136136
v.array(
@@ -155,7 +155,7 @@ export const sendEmail = mutation({
155155
}
156156
// We require either html/text or a template to be provided.
157157
const hasContent = args.html !== undefined || args.text !== undefined;
158-
const hasTemplate = args.templateId !== undefined;
158+
const hasTemplate = args.template?.id !== undefined;
159159

160160
if (!hasContent && !hasTemplate) {
161161
throw new Error("Either html/text or template must be provided");
@@ -198,8 +198,7 @@ export const sendEmail = mutation({
198198
subject: args.subject,
199199
html: htmlContentId,
200200
text: textContentId,
201-
templateId: args.templateId,
202-
templateVariables: args.templateVariables,
201+
template: args.template,
203202
headers: args.headers,
204203
segment,
205204
status: "waiting",
@@ -689,13 +688,8 @@ async function createResendBatchPayload(
689688
};
690689

691690
// Add either template or traditional content
692-
if (email.templateId) {
693-
payload.template = {
694-
id: email.templateId,
695-
variables: email.templateVariables
696-
? JSON.parse(email.templateVariables)
697-
: {},
698-
};
691+
if (email.template) {
692+
payload.template = email.template;
699693
// Subject can be optionally provided with templates
700694
if (email.subject) {
701695
payload.subject = email.subject;

src/component/schema.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineSchema, defineTable } from "convex/server";
22
import { v } from "convex/values";
3-
import { vEventType, vOptions, vStatus } from "./shared.js";
3+
import { vEventType, vOptions, vStatus, vTemplate } from "./shared.js";
44

55
export default defineSchema({
66
content: defineTable({
@@ -31,8 +31,7 @@ export default defineSchema({
3131
replyTo: v.array(v.string()),
3232
html: v.optional(v.id("content")),
3333
text: v.optional(v.id("content")),
34-
templateId: v.optional(v.string()),
35-
templateVariables: v.optional(v.string()),
34+
template: v.optional(vTemplate),
3635
headers: v.optional(
3736
v.array(
3837
v.object({

0 commit comments

Comments
 (0)