Skip to content

Commit

Permalink
Add option to toggle between binary and text/json outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
liaujianjie committed May 2, 2024
1 parent 0451f99 commit 7f54555
Showing 1 changed file with 51 additions and 35 deletions.
86 changes: 51 additions & 35 deletions packages/core/src/model/nodes/HttpCallNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type HttpCallNodeData = {
body: string;
useBodyInput?: boolean;

isBinaryOutput?: boolean;

errorOnNon200?: boolean;
};

Expand Down Expand Up @@ -93,22 +95,29 @@ export class HttpCallNodeImpl extends NodeImpl<HttpCallNode> {
}

getOutputDefinitions(): NodeOutputDefinition[] {
return [
{
dataType: 'string',
id: 'res_body' as PortId,
title: 'Body',
},
{
dataType: 'object',
id: 'json' as PortId,
title: 'JSON',
},
{
const outputDefinitions: NodeOutputDefinition[] = [];
if (this.data.isBinaryOutput) {
outputDefinitions.push({
dataType: 'binary',
id: 'binary' as PortId,
title: 'Binary',
},
});
} else {
outputDefinitions.push(
{
dataType: 'string',
id: 'res_body' as PortId,
title: 'Body',
},
{
dataType: 'object',
id: 'json' as PortId,
title: 'JSON',
},
);
}

outputDefinitions.push(
{
dataType: 'number',
id: 'statusCode' as PortId,
Expand All @@ -119,7 +128,9 @@ export class HttpCallNodeImpl extends NodeImpl<HttpCallNode> {
id: 'res_headers' as PortId,
title: 'Headers',
},
];
);

return outputDefinitions;
}

getEditors(): EditorDefinition<HttpCallNode>[] {
Expand Down Expand Up @@ -156,6 +167,11 @@ export class HttpCallNodeImpl extends NodeImpl<HttpCallNode> {
useInputToggleDataKey: 'useBodyInput',
language: 'json',
},
{
type: 'toggle',
label: 'Whether response body is expected to be a binary',
dataKey: 'isBinaryOutput',
},
{
type: 'toggle',
label: 'Error on non-200 status code',
Expand Down Expand Up @@ -249,32 +265,32 @@ export class HttpCallNodeImpl extends NodeImpl<HttpCallNode> {
},
};

const responseBlob = await response.blob();
const responseText = await responseBlob.text();

output['res_body' as PortId] = {
type: 'string',
value: responseText,
};

if (response.headers.get('content-type')?.includes('application/json')) {
const jsonData = JSON.parse(responseText);
output['json' as PortId] = {
type: 'object',
value: jsonData,
if (this.data.isBinaryOutput) {
const responseBlob = await response.blob();
output['binary' as PortId] = {
type: 'binary',
value: new Uint8Array(await responseBlob.arrayBuffer()),
};
} else {
output['json' as PortId] = {
type: 'control-flow-excluded',
value: undefined,
const responseText = await response.text();
output['res_body' as PortId] = {
type: 'string',
value: responseText,
};
if (response.headers.get('content-type')?.includes('application/json')) {
const jsonData = JSON.parse(responseText);
output['json' as PortId] = {
type: 'object',
value: jsonData,
};
} else {
output['json' as PortId] = {
type: 'control-flow-excluded',
value: undefined,
};
}
}

output['binary' as PortId] = {
type: 'binary',
value: new Uint8Array(await responseBlob.arrayBuffer()),
};

return output;
} catch (err) {
const { message } = getError(err);
Expand Down

0 comments on commit 7f54555

Please sign in to comment.