-
Notifications
You must be signed in to change notification settings - Fork 367
feat(resend): add Resend integration with contacts, batch emails, sch… #973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a33dabb
cd6a4aa
5f1f057
32ea9b1
449e85c
6f6ca55
1dd215b
c7fb3bb
a1b5749
39fc0c1
e501f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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', | ||
| ); | ||
| 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; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ export const send: ResendEndpoints['emailsSend'] = async (ctx, input) => { | |
| cc, | ||
| bcc, | ||
| reply_to, | ||
| scheduled_at, | ||
| attachments, | ||
| tags, | ||
| headers, | ||
|
|
@@ -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; | ||
|
|
@@ -56,6 +58,50 @@ export const send: ResendEndpoints['emailsSend'] = async (ctx, input) => { | |
| return response; | ||
| }; | ||
|
|
||
| export const batch: ResendEndpoints['emailsBatch'] = async (ctx, input) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new Rule Used: Flag 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}`, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.