File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " @trigger.dev/resend " : patch
3+ ---
4+
5+ Better handle retrying rate-limited resend requests
Original file line number Diff line number Diff line change 2626 "dependencies" : {
2727 "@trigger.dev/integration-kit" : " workspace:^2.0.14" ,
2828 "@trigger.dev/sdk" : " workspace:^2.0.14" ,
29- "resend" : " ^0.9.1 "
29+ "resend" : " ^1.0.0 "
3030 },
3131 "engines" : {
3232 "node" : " >=16.8.0"
Original file line number Diff line number Diff line change 1+ import type { RESEND_ERROR_CODE_KEY } from "resend/build/src/interfaces" ;
2+ import type { RESEND_ERROR_CODE_NUMBER } from "resend/build/src/interfaces" ;
3+
4+
5+ interface ErrorResponse extends Error {
6+ message : string ;
7+ status : RESEND_ERROR_CODE_NUMBER ;
8+ type : RESEND_ERROR_CODE_KEY ;
9+ response : {
10+ headers : { [ key : string ] : string } ;
11+ } ;
12+ }
13+
14+ export type { ErrorResponse } ;
Original file line number Diff line number Diff line change 11import type { IntegrationClient , TriggerIntegration } from "@trigger.dev/sdk" ;
22import { Resend as ResendClient } from "resend" ;
3+ import { ErrorResponse } from "./ErrorInterface"
34
45import type { AuthenticatedTask } from "@trigger.dev/sdk" ;
56
67type SendEmailData = Parameters < InstanceType < typeof ResendClient > [ "sendEmail" ] > [ 0 ] ;
78
89type SendEmailResponse = { id : string } ;
910
11+ function isRequestError ( error : unknown ) : error is ErrorResponse {
12+ return typeof error === "object" && error !== null && "status" in error ;
13+ }
14+
15+ function onError ( error : unknown ) {
16+ console . log ( error ) ;
17+ if ( ! isRequestError ( error ) ) {
18+ return ;
19+ }
20+
21+ // Check if this is a rate limit error
22+ if ( error . status === 429 ) {
23+ const rateLimitReset = error . response . headers [ "ratelimit-reset" ] ;
24+
25+ if ( rateLimitReset ) {
26+ const resetDate = new Date ( Number ( rateLimitReset ) * 1000 ) ;
27+
28+ return {
29+ retryAt : resetDate ,
30+ error,
31+ } ;
32+ }
33+ }
34+ }
35+
1036export const sendEmail : AuthenticatedTask <
1137 InstanceType < typeof ResendClient > ,
1238 SendEmailData ,
1339 SendEmailResponse
1440> = {
41+ onError,
1542 run : async ( params , client ) => {
1643 return client . sendEmail ( params ) as Promise < SendEmailResponse > ;
1744 } ,
You can’t perform that action at this time.
0 commit comments