Skip to content

Commit

Permalink
fix remaining problems for consumed-thing.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Sep 22, 2023
1 parent 919518c commit e2f5a08
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/core/src/consumed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class InternalPropertySubscription extends InternalSubscription {
options.formIndex = this.matchingUnsubscribeForm();
}
const { form } = this.thing.getClientFor(tp.forms, "unobserveproperty", Affordance.PropertyAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.thing.title}' did not get suitable form`);
}

Expand Down Expand Up @@ -219,7 +219,7 @@ class InternalPropertySubscription extends InternalSubscription {
for (let i = 0; i < forms.length; i++) {
let score = 0;
const form = forms[i];
if (form.op === operation || (Array.isArray(form.op) && form.op.includes(operation))) {
if (form.op === operation || (form?.op?.includes(operation) === true && Array.isArray(form.op) === true)) {
score += 1;
}

Expand Down Expand Up @@ -256,7 +256,7 @@ function findFormIndexWithScoring(
for (let i = 0; i < forms.length; i++) {
let score = 0;
const form = forms[i];
if (form.op === operation || (Array.isArray(form.op) && form.op.includes(operation))) {
if (form.op === operation || (form?.op?.includes(operation) === true && Array.isArray(form.op) === true)) {
score += 1;
}

Expand Down Expand Up @@ -304,7 +304,7 @@ class InternalEventSubscription extends InternalSubscription {
}

const { form } = this.thing.getClientFor(te.forms, "unsubscribeevent", Affordance.EventAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.thing.title}' did not get suitable form`);
}

Expand Down Expand Up @@ -462,7 +462,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
const logStatement = () =>
debug(`ConsumedThing '${this.title}' setting credentials for ${client} based on thing security`);

if (form && Array.isArray(form.security) && form.security.length > 0) {
if (form != null && Array.isArray(form.security) && form.security.length > 0) {
// Note security member in form objects overrides (i.e., completely replace) all definitions activated at the Thing level
// see https://www.w3.org/TR/wot-thing-description/#security-serialization-json

Expand Down Expand Up @@ -556,7 +556,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
}

let { client, form } = this.getClientFor(tp.forms, "readproperty", Affordance.PropertyAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.title}' did not get suitable form`);
}
if (client == null) {
Expand Down Expand Up @@ -600,7 +600,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
// collect attributes that are "readable" only
const tp = this.properties[propertyName];
const { form } = this.getClientFor(tp.forms, "readproperty", Affordance.PropertyAffordance, options);
if (form) {
if (form != null) {
propertyNames.push(propertyName);
}
}
Expand All @@ -622,7 +622,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
throw new Error(`ConsumedThing '${this.title}' does not have property ${propertyName}`);
}
let { client, form } = this.getClientFor(tp.forms, "writeproperty", Affordance.PropertyAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.title}' did not get suitable form`);
}
if (client == null) {
Expand Down Expand Up @@ -665,7 +665,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
throw new Error(`ConsumedThing '${this.title}' does not have action ${actionName}`);
}
let { client, form } = this.getClientFor(ta.forms, "invokeaction", Affordance.ActionAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.title}' did not get suitable form`);
}
if (client == null) {
Expand All @@ -691,7 +691,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
if (!content.type) content.type = form.contentType ?? "application/json";

// check if returned media type is the same as expected media type (from TD)
if (form.response) {
if (form.response != null) {
if (content.type !== form.response.contentType) {
throw new Error(`Unexpected type in response`);
}
Expand All @@ -718,7 +718,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
throw new Error(`ConsumedThing '${this.title}' does not have property ${name}`);
}
const { client, form } = this.getClientFor(tp.forms, "observeproperty", Affordance.PropertyAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.title}' did not get suitable form`);
}
if (client == null) {
Expand Down Expand Up @@ -776,7 +776,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
throw new Error(`ConsumedThing '${this.title}' does not have event ${name}`);
}
const { client, form } = this.getClientFor(te.forms, "subscribeevent", Affordance.EventAffordance, options);
if (!form) {
if (form == null) {
throw new Error(`ConsumedThing '${this.title}' did not get suitable form`);
}
if (client == null) {
Expand Down

0 comments on commit e2f5a08

Please sign in to comment.