Skip to content

Commit

Permalink
strict checking for package examples
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Aug 2, 2023
1 parent b25d863 commit d0ff86f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
34 changes: 23 additions & 11 deletions examples/scripts/counter-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

WoTHelpers.fetch("coap://localhost:5683/counter")
const core_1 = require("@node-wot/core");
const binding_http_1 = require("@node-wot/binding-http");
const binding_coap_1 = require("@node-wot/binding-coap");
// create Servient and add HTTP/CoAP binding
const servient = new core_1.Servient();
servient.addClientFactory(new binding_http_1.HttpClientFactory());
servient.addClientFactory(new binding_coap_1.CoapClientFactory());
const wotHelper = new core_1.Helpers(servient);
function getFormIndexForDecrementWithCoAP(thing) {
var _a;
const forms = (_a = thing.getThingDescription().actions) === null || _a === void 0 ? void 0 : _a.decrement.forms;
if (forms !== undefined) {
for (let i = 0; i < forms.length; i++) {
if (/^coaps?:\/\/.*/.test(forms[i].href)) {
return i;
}
}
}
// return formIndex: 0 if no CoAP target IRI found
return 0;
}
wotHelper
.fetch("coap://localhost:5683/counter")
.then(async (td) => {
// using await for serial execution (note 'async' in then() of fetch())
try {
Expand Down Expand Up @@ -45,13 +67,3 @@ WoTHelpers.fetch("coap://localhost:5683/counter")
.catch((err) => {
console.error("Fetch error:", err);
});
function getFormIndexForDecrementWithCoAP(thing) {
const forms = thing.getThingDescription().actions.decrement.forms;
for (let i = 0; i < forms.length; i++) {
if (/^coaps?:\/\/.*/.test(forms[i].href)) {
return i;
}
}
// return formIndex: 0 if no CoAP target IRI found
return 0;
}
6 changes: 3 additions & 3 deletions examples/scripts/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ WoT.produce({
let fill = "black";
if (options && typeof options === "object" && "uriVariables" in options) {
console.log("options = " + JSON.stringify(options));
if ("fill" in options.uriVariables) {
if (options.uriVariables && "fill" in options.uriVariables) {
const uriVariables = options.uriVariables;
fill = uriVariables.fill;
}
Expand All @@ -229,7 +229,7 @@ WoT.produce({
let step = 1;
if (options && typeof options === "object" && "uriVariables" in options) {
console.log("options = " + JSON.stringify(options));
if ("step" in options.uriVariables) {
if (options.uriVariables && "step" in options.uriVariables) {
const uriVariables = options.uriVariables;
step = uriVariables.step;
}
Expand All @@ -246,7 +246,7 @@ WoT.produce({
let step = 1;
if (options && typeof options === "object" && "uriVariables" in options) {
console.log("options = " + JSON.stringify(options));
if ("step" in options.uriVariables) {
if (options.uriVariables && "step" in options.uriVariables) {
const uriVariables = options.uriVariables;
step = uriVariables.step;
}
Expand Down
15 changes: 12 additions & 3 deletions examples/security/oauth/consumer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2018 - 2020 Contributors to the Eclipse Foundation
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -12,10 +12,19 @@
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
WoTHelpers.fetch("https://localhost:8080/oauth").then((td) => {
const core_1 = require("@node-wot/core");
const binding_http_1 = require("@node-wot/binding-http");
const binding_coap_1 = require("@node-wot/binding-coap");
// create Servient and add HTTP/CoAP binding
const servient = new core_1.Servient();
servient.addClientFactory(new binding_http_1.HttpClientFactory());
servient.addClientFactory(new binding_coap_1.CoapClientFactory());
const wotHelper = new core_1.Helpers(servient);
wotHelper.fetch("https://localhost:8080/oauth").then((td) => {
WoT.consume(td).then(async (thing) => {
try {
const result = await (await thing.invokeAction("sayOk")).value();
const resp = await thing.invokeAction("sayOk");
const result = resp === null || resp === void 0 ? void 0 : resp.value();
console.log("oAuth token was", result);
} catch (error) {
console.log("It seems that I couldn't access the resource");
Expand Down
32 changes: 28 additions & 4 deletions examples/testthing/testclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

const core_1 = require("@node-wot/core");
const binding_http_1 = require("@node-wot/binding-http");
const binding_coap_1 = require("@node-wot/binding-coap");
// create Servient and add HTTP/CoAP binding
const servient = new core_1.Servient();
servient.addClientFactory(new binding_http_1.HttpClientFactory());
servient.addClientFactory(new binding_coap_1.CoapClientFactory());
const wotHelper = new core_1.Helpers(servient);
console.log = () => {
/* empty */
};
Expand All @@ -25,7 +33,11 @@ async function testPropertyRead(thing, name) {
const value = await res.value();
console.info("PASS " + name + " READ:", value);
} catch (err) {
console.error("FAIL " + name + " READ:", err.message);
if (err instanceof Error) {
console.error("FAIL " + name + " READ:", err.message);
} else {
console.error("FAIL " + name + " READ:", err);
}
}
}
async function testPropertyWrite(thing, name, value, shouldFail) {
Expand All @@ -35,11 +47,23 @@ async function testPropertyWrite(thing, name, value, shouldFail) {
if (!shouldFail) console.info("PASS " + name + " WRITE (" + displayValue + ")");
else console.error("FAIL " + name + " WRITE: (" + displayValue + ")");
} catch (err) {
if (!shouldFail) console.error("FAIL " + name + " WRITE (" + displayValue + "):", err.message);
else console.info("PASS " + name + " WRITE (" + displayValue + "):", err.message);
if (!shouldFail) {
if (err instanceof Error) {
console.error("FAIL " + name + " WRITE (" + displayValue + "):", err.message);
} else {
console.error("FAIL " + name + " WRITE (" + displayValue + "):", err);
}
} else {
if (err instanceof Error) {
console.info("PASS " + name + " WRITE (" + displayValue + "):", err.message);
} else {
console.info("PASS " + name + " WRITE (" + displayValue + "):", err);
}
}
}
}
WoTHelpers.fetch("http://localhost:8080/testthing")
wotHelper
.fetch("http://localhost:8080/testthing")
.then(async (td) => {
// using await for serial execution (note 'async' in then() of fetch())
try {
Expand Down

0 comments on commit d0ff86f

Please sign in to comment.