Skip to content

Commit 1ffd1a8

Browse files
authored
fix http request step (#55)
* fix http request step * fix errors * better codegen
1 parent 0333346 commit 1ffd1a8

File tree

2 files changed

+86
-19
lines changed

2 files changed

+86
-19
lines changed

lib/codegen-templates/http-request.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,45 @@
33
* This is a string template used for code generation - keep as string export
44
*/
55
export default `export async function httpRequestStep(input: {
6-
url: string;
7-
method: string;
8-
headers: Record<string, string>;
9-
body: unknown;
6+
endpoint: string;
7+
httpMethod: string;
8+
httpHeaders?: string;
9+
httpBody?: string;
1010
}) {
1111
"use step";
1212
13-
const response = await fetch(input.url, {
14-
method: input.method,
15-
headers: input.headers,
16-
body: JSON.stringify(input.body),
13+
let headers = {};
14+
if (input.httpHeaders) {
15+
try {
16+
headers = JSON.parse(input.httpHeaders);
17+
} catch {
18+
// If parsing fails, use empty headers
19+
}
20+
}
21+
22+
let body: string | undefined;
23+
if (input.httpMethod !== "GET" && input.httpBody) {
24+
try {
25+
const parsedBody = JSON.parse(input.httpBody);
26+
if (Object.keys(parsedBody).length > 0) {
27+
body = JSON.stringify(parsedBody);
28+
}
29+
} catch {
30+
if (input.httpBody.trim() && input.httpBody.trim() !== "{}") {
31+
body = input.httpBody;
32+
}
33+
}
34+
}
35+
36+
const response = await fetch(input.endpoint, {
37+
method: input.httpMethod,
38+
headers,
39+
body,
1740
});
1841
19-
const data = await response.json();
20-
return data;
42+
const contentType = response.headers.get("content-type");
43+
if (contentType?.includes("application/json")) {
44+
return await response.json();
45+
}
46+
return await response.text();
2147
}`;

lib/steps/http-request.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,60 @@ type HttpRequestResult =
99
| { success: true; data: unknown; status: number }
1010
| { success: false; error: string; status?: number };
1111

12+
function parseHeaders(httpHeaders?: string): Record<string, string> {
13+
if (!httpHeaders) {
14+
return {};
15+
}
16+
try {
17+
return JSON.parse(httpHeaders);
18+
} catch {
19+
return {};
20+
}
21+
}
22+
23+
function parseBody(httpMethod: string, httpBody?: string): string | undefined {
24+
if (httpMethod === "GET" || !httpBody) {
25+
return;
26+
}
27+
try {
28+
const parsedBody = JSON.parse(httpBody);
29+
return Object.keys(parsedBody).length > 0
30+
? JSON.stringify(parsedBody)
31+
: undefined;
32+
} catch {
33+
const trimmed = httpBody.trim();
34+
return trimmed && trimmed !== "{}" ? httpBody : undefined;
35+
}
36+
}
37+
38+
function parseResponse(response: Response): Promise<unknown> {
39+
const contentType = response.headers.get("content-type");
40+
if (contentType?.includes("application/json")) {
41+
return response.json();
42+
}
43+
return response.text();
44+
}
45+
1246
export async function httpRequestStep(input: {
13-
url: string;
14-
method: string;
15-
headers: Record<string, string>;
16-
body: unknown;
47+
endpoint: string;
48+
httpMethod: string;
49+
httpHeaders?: string;
50+
httpBody?: string;
1751
}): Promise<HttpRequestResult> {
1852
"use step";
1953

54+
if (!input.endpoint) {
55+
return {
56+
success: false,
57+
error: "HTTP request failed: URL is required",
58+
};
59+
}
60+
2061
try {
21-
const response = await fetch(input.url, {
22-
method: input.method,
23-
headers: input.headers,
24-
body: input.body ? JSON.stringify(input.body) : undefined,
62+
const response = await fetch(input.endpoint, {
63+
method: input.httpMethod,
64+
headers: parseHeaders(input.httpHeaders),
65+
body: parseBody(input.httpMethod, input.httpBody),
2566
});
2667

2768
if (!response.ok) {
@@ -33,7 +74,7 @@ export async function httpRequestStep(input: {
3374
};
3475
}
3576

36-
const data = await response.json();
77+
const data = await parseResponse(response);
3778
return { success: true, data, status: response.status };
3879
} catch (error) {
3980
return {

0 commit comments

Comments
 (0)