Skip to content

Commit

Permalink
fix(api): Allow data field to empty
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Jan 28, 2023
1 parent ca9a81a commit 4e51506
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
82 changes: 37 additions & 45 deletions src/nodes/api/controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Joi = require('joi');

const BaseNode = require('../BaseNode');
const { renderTemplate } = require('../../helpers/mustache');
const { generateRenderTemplate } = require('../../helpers/mustache');

const nodeOptions = {
config: {
Expand Down Expand Up @@ -106,47 +106,50 @@ class Api extends BaseNode {
return;
}

const renderTemplate = generateRenderTemplate(
message,
this.node.context(),
this.homeAssistant.getStates()
);
let data;
if (parsedMessage.dataType.value === 'jsonata') {

if (parsedMessage.data.value.length !== 0) {
try {
data = this.evaluateJSONata(parsedMessage.data.value, {
message,
});
if (parsedMessage.dataType.value === 'jsonata') {
data = this.evaluateJSONata(parsedMessage.data.value, {
message,
});
} else {
data = JSON.parse(
renderTemplate(
typeof parsedMessage.data.value === 'object'
? JSON.stringify(parsedMessage.data.value)
: parsedMessage.data.value
)
);
}
} catch (e) {
this.status.setFailed('Error');
done(e.message);
return;
}
} else {
data = JSON.parse(
renderTemplate(
typeof parsedMessage.data.value === 'object'
? JSON.stringify(parsedMessage.data.value)
: parsedMessage.data.value,
message,
this.node.context(),
this.homeAssistant.getStates()
)
);
}

const method = parsedMessage.method.value;
let apiCall;

if (parsedMessage.protocol.value === 'http') {
const path = renderTemplate(
parsedMessage.path.value,
message,
this.node.context(),
this.homeAssistant.getStates()
).replace(/^\/(?:api\/)?/, '');
const path = renderTemplate(parsedMessage.path.value).replace(
/^\/(?:api\/)?/,
''
);

if (!path) {
done('HTTP request requires a valid path.');
this.status.setFailed();
return;
}

const method = parsedMessage.method.value;
this.debugToClient({ method, path, data });

apiCall = this.homeAssistant[method].bind(
Expand All @@ -156,39 +159,28 @@ class Api extends BaseNode {
parsedMessage.responseType.value
);
} else {
try {
const json = JSON.parse(data);

if (!json.type) {
done(
`A WebSocket request requires a 'type' property in the data object.`
);
this.status.setFailed();
return;
}

this.debugToClient(json);

apiCall = this.homeAssistant.send.bind(
this.homeAssistant,
json
if (!data.type) {
done(
`A WebSocket request requires a 'type' property in the data object.`
);
} catch (e) {
done(e.message);
this.status.setFailed();
return;
}

this.debugToClient(JSON.stringify(data));

apiCall = this.homeAssistant.send.bind(this.homeAssistant, data);
}

this.status.setSending();

const results = await apiCall().catch((err) => {
this.status.setFailed('API Error');
done(
'API Error. ' + err.message
? `Error Message: ${err.message}`
: ''
`API Error. ${
err.message ? `Error Message: ${err.message}` : ''
}`
);
this.status.setFailed('API Error');
});

try {
Expand Down
17 changes: 13 additions & 4 deletions src/nodes/api/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,19 @@ const ApiEditor: EditorNodeDef<ApiEditorNodeProperties> = {
ha.setup(this);
haServer.init(this, '#node-input-server');

$('#node-input-data').typedInput({
types: ['json', 'jsonata'],
typeField: '#node-input-dataType',
});
$('#node-input-data')
.on('change', function () {
// hack to hide error border when data field is empty
const $this = $(this);
const val = $this.val() as string;
if (val.length === 0) {
$this.next().removeClass('input-error');
}
})
.typedInput({
types: ['jsonata', 'json'],
typeField: '#node-input-dataType',
});

$('#node-input-protocol')
.on('change', function () {
Expand Down

1 comment on commit 4e51506

@enjibby
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I hope I didn't cause more problems than I solved!

Please sign in to comment.