-
-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for the new Resend audience API endpoints (#850)
* feat: Add support for the new Resend audience API endpoints * update pnpm-lock.yaml * update pnpm-lock.yaml * Merge branch 'main' into feat/support-for-resend-audience-api * latest pnpm-lock.yaml * Create lovely-items-end.md * Update pnpm-lock.yaml --------- Co-authored-by: Matt Aitken <[email protected]>
- Loading branch information
1 parent
adf66d2
commit 4dd6cf1
Showing
8 changed files
with
593 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@trigger.dev/resend": patch | ||
--- | ||
|
||
Add support for the new Resend audience API endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { IntegrationTaskKey, retry } from "@trigger.dev/sdk"; | ||
import type { ResendRunTask } from "./index"; | ||
import { Resend } from "resend"; | ||
import { handleResendError } from "./utils"; | ||
|
||
type CreateAudienceResult = NonNullable<Awaited<ReturnType<Resend["audiences"]["create"]>>["data"]>; | ||
type GetAudienceResult = NonNullable<Awaited<ReturnType<Resend["audiences"]["get"]>>["data"]>; | ||
type DeleteAudienceResult = NonNullable<Awaited<ReturnType<Resend["audiences"]["remove"]>>["data"]>; | ||
type ListAudiencesResult = NonNullable<Awaited<ReturnType<Resend["audiences"]["list"]>>["data"]>; | ||
|
||
export class Audiences { | ||
constructor(private runTask: ResendRunTask) {} | ||
|
||
create( | ||
key: IntegrationTaskKey, | ||
payload: Parameters<Resend["audiences"]["create"]>[0], | ||
options?: Parameters<Resend["audiences"]["create"]>[1] | ||
): Promise<CreateAudienceResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.audiences.create(payload, options); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Create Audience", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "Name", | ||
text: payload.name, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
get(key: IntegrationTaskKey, payload: string): Promise<GetAudienceResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.audiences.get(payload); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Get Audience", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "ID", | ||
text: payload, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
remove(key: IntegrationTaskKey, payload: string): Promise<DeleteAudienceResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.audiences.remove(payload); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Remove Audience", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "ID", | ||
text: payload, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
list(key: IntegrationTaskKey): Promise<ListAudiencesResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.audiences.list(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "List Audiences", | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import { IntegrationTaskKey, retry } from "@trigger.dev/sdk"; | ||
import type { ResendRunTask } from "./index"; | ||
import { Resend } from "resend"; | ||
import { handleResendError } from "./utils"; | ||
|
||
type CreateContactResult = NonNullable<Awaited<ReturnType<Resend["contacts"]["create"]>>["data"]>; | ||
type GetContactResult = NonNullable<Awaited<ReturnType<Resend["contacts"]["get"]>>["data"]>; | ||
type UpdateContactResult = NonNullable<Awaited<ReturnType<Resend["contacts"]["update"]>>["data"]>; | ||
type DeleteContactResult = NonNullable<Awaited<ReturnType<Resend["contacts"]["remove"]>>["data"]>; | ||
type ListContactsResult = NonNullable<Awaited<ReturnType<Resend["contacts"]["list"]>>["data"]>; | ||
|
||
export class Contacts { | ||
constructor(private runTask: ResendRunTask) {} | ||
|
||
create( | ||
key: IntegrationTaskKey, | ||
payload: Parameters<Resend["contacts"]["create"]>[0], | ||
options?: Parameters<Resend["contacts"]["create"]>[1] | ||
): Promise<CreateContactResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.contacts.create(payload, options); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Create Contact", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "Email", | ||
text: payload.email, | ||
}, | ||
...(payload.first_name && payload.last_name | ||
? [{ label: "Name", text: payload.first_name + " " + payload.last_name }] | ||
: []), | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
get( | ||
key: IntegrationTaskKey, | ||
payload: Parameters<Resend["contacts"]["get"]>[0] | ||
): Promise<GetContactResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.contacts.get(payload); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Get Contact", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "Id", | ||
text: payload.id, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
update( | ||
key: IntegrationTaskKey, | ||
payload: Parameters<Resend["contacts"]["update"]>[0] | ||
): Promise<UpdateContactResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.contacts.update(payload); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Update Contact", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "Id", | ||
text: payload.id, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
remove( | ||
key: IntegrationTaskKey, | ||
payload: Parameters<Resend["contacts"]["remove"]>[0] | ||
): Promise<DeleteContactResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.contacts.remove(payload); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "Remove Contact", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "Id", | ||
text: payload.id, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
|
||
list( | ||
key: IntegrationTaskKey, | ||
payload: Parameters<Resend["contacts"]["list"]>[0] | ||
): Promise<ListContactsResult> { | ||
return this.runTask( | ||
key, | ||
async (client, task) => { | ||
const { error, data } = await client.contacts.list(payload); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error("No data returned from Resend"); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
name: "List Contacts", | ||
params: payload, | ||
properties: [ | ||
{ | ||
label: "Audience Id", | ||
text: payload.audience_id, | ||
}, | ||
], | ||
retry: retry.standardBackoff, | ||
}, | ||
handleResendError | ||
); | ||
} | ||
} |
Oops, something went wrong.