Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions packages/resend/endpoints/contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { logEventFromContext } from 'corsair/core';
import { makeResendRequest } from '../client';
import type { ResendEndpoints } from '../index';
import type { ResendEndpointOutputs } from './types';

export const create: ResendEndpoints['contactsCreate'] = async (ctx, input) => {
const body: Record<string, unknown> = {
email: input.email,
};
if (input.first_name) body.first_name = input.first_name;
if (input.last_name) body.last_name = input.last_name;
if (input.unsubscribed !== undefined) body.unsubscribed = input.unsubscribed;
if (input.properties) body.properties = input.properties;
if (input.segments) body.segments = input.segments;
if (input.topics) body.topics = input.topics;

const response = await makeResendRequest<
ResendEndpointOutputs['contactsCreate']
>('contacts', ctx.key, {
method: 'POST',
body,
});

if (response.id && ctx.db.contacts) {
try {
await ctx.db.contacts.upsertByEntityId(response.id, {
...response,
});
} catch (error) {
console.warn('Failed to save contact to database:', error);
}
}

await logEventFromContext(
ctx,
'resend.contacts.create',
{ ...input },
'completed',
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return response;
};

export const get: ResendEndpoints['contactsGet'] = async (ctx, input) => {
const response = await makeResendRequest<
ResendEndpointOutputs['contactsGet']
>(`contacts/${input.id}`, ctx.key, {
method: 'GET',
});

if (response.id && ctx.db.contacts) {
try {
await ctx.db.contacts.upsertByEntityId(response.id, {
...response,
});
} catch (error) {
console.warn('Failed to save contact to database:', error);
}
}

await logEventFromContext(
ctx,
'resend.contacts.get',
{ ...input },
'completed',
);
return response;
};

export const list: ResendEndpoints['contactsList'] = async (ctx, input) => {
const query: Record<string, string | number | undefined> = {};
if (input?.limit) query.limit = input.limit;
if (input?.cursor) query.cursor = input.cursor;

const response = await makeResendRequest<
ResendEndpointOutputs['contactsList']
>('contacts', ctx.key, {
method: 'GET',
query,
});

if (response.data && ctx.db.contacts) {
try {
for (const contact of response.data) {
await ctx.db.contacts.upsertByEntityId(contact.id, {
...contact,
});
}
} catch (error) {
console.warn('Failed to save contacts to database:', error);
}
}

await logEventFromContext(
ctx,
'resend.contacts.list',
{ ...input },
'completed',
);
return response;
};

export const update: ResendEndpoints['contactsUpdate'] = async (ctx, input) => {
const { id, ...body } = input;

const response = await makeResendRequest<
ResendEndpointOutputs['contactsUpdate']
>(`contacts/${id}`, ctx.key, {
method: 'PATCH',
body,
});

if (response.id && ctx.db.contacts) {
try {
await ctx.db.contacts.upsertByEntityId(response.id, {
...response,
});
} catch (error) {
console.warn('Failed to save contact to database:', error);
}
}

await logEventFromContext(
ctx,
'resend.contacts.update',
{ ...input },
'completed',
);
return response;
};

export const deleteContact: ResendEndpoints['contactsDelete'] = async (
ctx,
input,
) => {
const response = await makeResendRequest<
ResendEndpointOutputs['contactsDelete']
>(`contacts/${input.id}`, ctx.key, {
method: 'DELETE',
});

if (response.deleted && ctx.db.contacts) {
try {
await ctx.db.contacts.deleteByEntityId(input.id);
} catch (error) {
console.warn('Failed to delete contact from database:', error);
}
}

await logEventFromContext(
ctx,
'resend.contacts.delete',
{ ...input },
'completed',
);
return response;
};
46 changes: 46 additions & 0 deletions packages/resend/endpoints/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const send: ResendEndpoints['emailsSend'] = async (ctx, input) => {
cc,
bcc,
reply_to,
scheduled_at,
attachments,
tags,
headers,
Expand All @@ -29,6 +30,7 @@ export const send: ResendEndpoints['emailsSend'] = async (ctx, input) => {
if (cc) body.cc = Array.isArray(cc) ? cc : [cc];
if (bcc) body.bcc = Array.isArray(bcc) ? bcc : [bcc];
if (reply_to) body.reply_to = Array.isArray(reply_to) ? reply_to : [reply_to];
if (scheduled_at) body.scheduled_at = scheduled_at;
if (attachments) body.attachments = attachments;
if (tags) body.tags = tags;
if (headers) body.headers = headers;
Expand Down Expand Up @@ -56,6 +58,50 @@ export const send: ResendEndpoints['emailsSend'] = async (ctx, input) => {
return response;
};

export const batch: ResendEndpoints['emailsBatch'] = async (ctx, input) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 New endpoints lack contract tests

The new emails.batch, emails.cancel, and five contact operations have no corresponding endpoint tests, leaving their provider paths, methods, request bodies, response schemas, and persistence behavior outside the required test coverage.

Rule Used: Flag any types on exported or public surfaces as... (source)

Knowledge Base Used: Provider plugin implementation conventions

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

const response = await makeResendRequest<
ResendEndpointOutputs['emailsBatch']
>('emails/batch', ctx.key, {
method: 'POST',
body: input.emails as unknown as Record<string, unknown>,
});

// Batch response only returns IDs, not full email objects
// Individual emails can be fetched via emails.get if needed

await logEventFromContext(
ctx,
'resend.emails.batch',
{ ...input },
'completed',
);
return response;
};

export const cancel: ResendEndpoints['emailsCancel'] = async (ctx, input) => {
const response = await makeResendRequest<
ResendEndpointOutputs['emailsCancel']
>(`emails/${input.id}`, ctx.key, {
method: 'DELETE',
});

if (response.cancelled && ctx.db.emails) {
try {
await ctx.db.emails.deleteByEntityId(input.id);
} catch (error) {
console.warn('Failed to delete email from database:', error);
}
}

await logEventFromContext(
ctx,
'resend.emails.cancel',
{ ...input },
'completed',
);
return response;
};

export const get: ResendEndpoints['emailsGet'] = async (ctx, input) => {
const response = await makeResendRequest<ResendEndpointOutputs['emailsGet']>(
`emails/${input.id}`,
Expand Down
19 changes: 19 additions & 0 deletions packages/resend/endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
create as contactsCreate,
deleteContact as contactsDelete,
get as contactsGet,
list as contactsList,
update as contactsUpdate,
} from './contacts';
import {
deleteDomain,
create as domainsCreate,
Expand All @@ -6,6 +13,8 @@ import {
verify as domainsVerify,
} from './domains';
import {
batch as emailsBatch,
cancel as emailsCancel,
get as emailsGet,
list as emailsList,
send as emailsSend,
Expand All @@ -15,6 +24,8 @@ export const Emails = {
send: emailsSend,
get: emailsGet,
list: emailsList,
batch: emailsBatch,
cancel: emailsCancel,
};

export const Domains = {
Expand All @@ -25,4 +36,12 @@ export const Domains = {
verify: domainsVerify,
};

export const Contacts = {
create: contactsCreate,
get: contactsGet,
list: contactsList,
update: contactsUpdate,
delete: contactsDelete,
};

export * from './types';
Loading
Loading