File tree Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Expand file tree Collapse file tree 4 files changed +47
-1
lines changed 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 26
26
"dependencies" : {
27
27
"@trigger.dev/integration-kit" : " workspace:^2.0.14" ,
28
28
"@trigger.dev/sdk" : " workspace:^2.0.14" ,
29
- "resend" : " ^0.9.1 "
29
+ "resend" : " ^1.0.0 "
30
30
},
31
31
"engines" : {
32
32
"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 1
1
import type { IntegrationClient , TriggerIntegration } from "@trigger.dev/sdk" ;
2
2
import { Resend as ResendClient } from "resend" ;
3
+ import { ErrorResponse } from "./ErrorInterface"
3
4
4
5
import type { AuthenticatedTask } from "@trigger.dev/sdk" ;
5
6
6
7
type SendEmailData = Parameters < InstanceType < typeof ResendClient > [ "sendEmail" ] > [ 0 ] ;
7
8
8
9
type SendEmailResponse = { id : string } ;
9
10
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
+
10
36
export const sendEmail : AuthenticatedTask <
11
37
InstanceType < typeof ResendClient > ,
12
38
SendEmailData ,
13
39
SendEmailResponse
14
40
> = {
41
+ onError,
15
42
run : async ( params , client ) => {
16
43
return client . sendEmail ( params ) as Promise < SendEmailResponse > ;
17
44
} ,
You can’t perform that action at this time.
0 commit comments