@@ -3,60 +3,90 @@ import type { HttpRequestNodeData } from "./Node";
33import { NonRetriableError } from "inngest" ;
44import ky , { type Options } from "ky" ;
55import Handlebars from "handlebars" ;
6+ import { httpRequestChannel } from "@/inngest/channels/http-request" ;
7+ import type { Realtime } from "@inngest/realtime" ;
8+ import type { NodeStatus } from "@/components/react-flow/NodeStatusIndicator" ;
69
710Handlebars . registerHelper ( "json" , ( context ) => {
811 const jsonString = JSON . stringify ( context , null , 2 ) ;
912 return new Handlebars . SafeString ( jsonString ) ;
1013} ) ;
1114
15+ const publishStatus =
16+ ( publish : Realtime . PublishFn ) =>
17+ async ( nodeId : string , status : NodeStatus ) => {
18+ await publish (
19+ httpRequestChannel ( ) . status ( {
20+ nodeId,
21+ status,
22+ } ) ,
23+ ) ;
24+ } ;
25+
1226export const HttpRequestExecutor : NodeExecutor < HttpRequestNodeData > = async ( {
27+ nodeId,
1328 data,
1429 context,
1530 step,
31+ publish,
1632} ) => {
17- if ( ! data . variableName ) {
18- throw new NonRetriableError ( "[Http request]: Variable name not configured" ) ;
19- }
20- if ( ! data . endpoint ) {
21- throw new NonRetriableError ( "[Http request]: No endopoint configured" ) ;
22- }
23- if ( ! data . method ) {
24- throw new NonRetriableError ( "[Http request]: No method configured" ) ;
25- }
33+ try {
34+ await publishStatus ( publish ) ( nodeId , "loading" ) ;
2635
27- return await step . run ( "http-request" , async ( ) => {
28- const endpoint = Handlebars . compile ( data . endpoint ) ( context ) ;
29- const method = data . method || "GET" ;
30- const options : Options = {
31- method ,
32- headers : {
33- "Content-Type" : "application/json" ,
34- } ,
35- } ;
36-
37- if ( method !== "GET" ) {
38- const resolved = Handlebars . compile ( data . body ) ( context ) ;
39- JSON . parse ( resolved ) ;
40- options . body = resolved ;
36+ if ( ! data . variableName ) {
37+ throw new NonRetriableError (
38+ "[HttpRequestExecutor]: No variable name configured" ,
39+ ) ;
40+ }
41+ if ( ! data . endpoint ) {
42+ throw new NonRetriableError (
43+ "[HttpRequestExecutor]: No endopoint configured" ,
44+ ) ;
45+ }
46+ if ( ! data . method ) {
47+ throw new NonRetriableError (
48+ "[HttpRequestExecutor]: No method configured" ,
49+ ) ;
4150 }
51+ return await step . run ( "http-request" , async ( ) => {
52+ const endpoint = Handlebars . compile ( data . endpoint ) ( context ) ;
53+ const method = data . method || "GET" ;
54+ const options : Options = {
55+ method,
56+ headers : {
57+ "Content-Type" : "application/json" ,
58+ } ,
59+ } ;
4260
43- const response = await ky ( endpoint , options ) ;
44- const contentType = response . headers . get ( "content-type" ) ;
45- const responseData = contentType ?. includes ( "application/json" )
46- ? await response . json ( )
47- : await response . text ( ) ;
48-
49- const responsePayload = {
50- httpResponse : {
51- status : response . status ,
52- statusText : response . statusText ,
53- data : responseData ,
54- } ,
55- } ;
56-
57- return {
58- ...context ,
59- [ data . variableName ] : responsePayload ,
60- } ;
61- } ) ;
61+ if ( method !== "GET" ) {
62+ const resolved = Handlebars . compile ( data . body ) ( context ) ;
63+ JSON . parse ( resolved ) ;
64+ options . body = resolved ;
65+ }
66+
67+ const response = await ky ( endpoint , options ) ;
68+ const contentType = response . headers . get ( "content-type" ) ;
69+ const responseData = contentType ?. includes ( "application/json" )
70+ ? await response . json ( )
71+ : await response . text ( ) ;
72+
73+ const responsePayload = {
74+ httpResponse : {
75+ status : response . status ,
76+ statusText : response . statusText ,
77+ data : responseData ,
78+ } ,
79+ } ;
80+
81+ await publishStatus ( publish ) ( nodeId , "success" ) ;
82+
83+ return {
84+ ...context ,
85+ [ data . variableName ] : responsePayload ,
86+ } ;
87+ } ) ;
88+ } catch ( error ) {
89+ await publishStatus ( publish ) ( nodeId , "error" ) ;
90+ throw error ;
91+ }
6292} ;
0 commit comments