1
- import { Response } from "cross-fetch" ;
2
1
import { z } from "zod" ;
3
2
import {
4
3
InngestCommHandler ,
@@ -24,6 +23,21 @@ class RemixCommHandler extends InngestCommHandler {
24
23
} : {
25
24
request : Request ;
26
25
} ) : Promise < Response > => {
26
+ /**
27
+ * If `Response` isn't included in this environment, it's probably a Node
28
+ * env that isn't already polyfilling. In this case, we can polyfill it
29
+ * here to be safe.
30
+ */
31
+ let Res : typeof Response ;
32
+
33
+ if ( typeof Response === "undefined" ) {
34
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-var-requires
35
+ Res = require ( "cross-fetch" ) . Response ;
36
+ } else {
37
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
38
+ Res = Response ;
39
+ }
40
+
27
41
const headers = { "x-inngest-sdk" : this . sdkHeader . join ( "" ) } ;
28
42
29
43
let reqUrl : URL ;
@@ -37,7 +51,7 @@ class RemixCommHandler extends InngestCommHandler {
37
51
isIntrospection = reqUrl . searchParams . has ( queryKeys . Introspect ) ;
38
52
reqUrl . searchParams . delete ( queryKeys . Introspect ) ;
39
53
} catch ( err ) {
40
- return new Response ( JSON . stringify ( err ) , {
54
+ return new Res ( JSON . stringify ( err ) , {
41
55
status : 500 ,
42
56
headers,
43
57
} ) ;
@@ -67,14 +81,14 @@ class RemixCommHandler extends InngestCommHandler {
67
81
hasSigningKey : Boolean ( this . signingKey ) ,
68
82
} ;
69
83
70
- return new Response ( JSON . stringify ( introspection ) , {
84
+ return new Res ( JSON . stringify ( introspection ) , {
71
85
status : 200 ,
72
86
headers,
73
87
} ) ;
74
88
}
75
89
76
90
// Grab landing page and serve
77
- return new Response ( landing , {
91
+ return new Res ( landing , {
78
92
status : 200 ,
79
93
headers : {
80
94
...headers ,
@@ -90,7 +104,7 @@ class RemixCommHandler extends InngestCommHandler {
90
104
process . env [ envKeys . DevServerUrl ]
91
105
) ;
92
106
93
- return new Response ( JSON . stringify ( { message } ) , {
107
+ return new Res ( JSON . stringify ( { message } ) , {
94
108
status,
95
109
headers,
96
110
} ) ;
@@ -111,20 +125,20 @@ class RemixCommHandler extends InngestCommHandler {
111
125
const stepRes = await this . runStep ( fnId , stepId , await req . json ( ) ) ;
112
126
113
127
if ( stepRes . status === 500 ) {
114
- return new Response ( JSON . stringify ( stepRes . error ) , {
128
+ return new Res ( JSON . stringify ( stepRes . error ) , {
115
129
status : stepRes . status ,
116
130
headers,
117
131
} ) ;
118
132
}
119
133
120
- return new Response ( JSON . stringify ( stepRes . body ) , {
134
+ return new Res ( JSON . stringify ( stepRes . body ) , {
121
135
status : stepRes . status ,
122
136
headers,
123
137
} ) ;
124
138
}
125
139
}
126
140
127
- return new Response ( null , { status : 405 , headers } ) ;
141
+ return new Res ( null , { status : 405 , headers } ) ;
128
142
} ;
129
143
}
130
144
}
@@ -133,6 +147,22 @@ class RemixCommHandler extends InngestCommHandler {
133
147
* In Remix, serve and register any declared functions with Inngest, making them
134
148
* available to be triggered by events.
135
149
*
150
+ * Remix requires that you export both a "loader" for serving `GET` requests,
151
+ * and an "action" for serving other requests, therefore exporting both is
152
+ * required.
153
+ *
154
+ * See {@link https://remix.run/docs/en/v1/guides/resource-routes}
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import { serve } from "inngest/remix";
159
+ * import fns from "~/inngest";
160
+ *
161
+ * const handler = serve("My Remix App", fns);
162
+ *
163
+ * export { handler as loader, handler as action };
164
+ * ```
165
+ *
136
166
* @public
137
167
*/
138
168
export const serve : ServeHandler = ( nameOrInngest , fns , opts ) : any => {
0 commit comments