From 7f54555407e303901100b8eb79945b26f9e59731 Mon Sep 17 00:00:00 2001 From: Liau Jian Jie Date: Fri, 3 May 2024 00:04:13 +0800 Subject: [PATCH] Add option to toggle between binary and text/json outputs --- packages/core/src/model/nodes/HttpCallNode.ts | 86 +++++++++++-------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/packages/core/src/model/nodes/HttpCallNode.ts b/packages/core/src/model/nodes/HttpCallNode.ts index 4d1ed7f97..9fe70d87a 100644 --- a/packages/core/src/model/nodes/HttpCallNode.ts +++ b/packages/core/src/model/nodes/HttpCallNode.ts @@ -28,6 +28,8 @@ export type HttpCallNodeData = { body: string; useBodyInput?: boolean; + isBinaryOutput?: boolean; + errorOnNon200?: boolean; }; @@ -93,22 +95,29 @@ export class HttpCallNodeImpl extends NodeImpl { } 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, @@ -119,7 +128,9 @@ export class HttpCallNodeImpl extends NodeImpl { id: 'res_headers' as PortId, title: 'Headers', }, - ]; + ); + + return outputDefinitions; } getEditors(): EditorDefinition[] { @@ -156,6 +167,11 @@ export class HttpCallNodeImpl extends NodeImpl { 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', @@ -249,32 +265,32 @@ export class HttpCallNodeImpl extends NodeImpl { }, }; - 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);