diff --git a/examples/scripts/counter-client.js b/examples/scripts/counter-client.js index a8cf994cf..9720a3e91 100644 --- a/examples/scripts/counter-client.js +++ b/examples/scripts/counter-client.js @@ -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 { @@ -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; -} diff --git a/examples/scripts/counter.js b/examples/scripts/counter.js index ea7828f39..19939def7 100644 --- a/examples/scripts/counter.js +++ b/examples/scripts/counter.js @@ -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; } @@ -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; } @@ -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; } diff --git a/examples/security/oauth/consumer.js b/examples/security/oauth/consumer.js index b6a6fd436..24cddf77a 100644 --- a/examples/security/oauth/consumer.js +++ b/examples/security/oauth/consumer.js @@ -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. @@ -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"); diff --git a/examples/testthing/testclient.js b/examples/testthing/testclient.js index 8c75c1140..4a25299ae 100644 --- a/examples/testthing/testclient.js +++ b/examples/testthing/testclient.js @@ -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 */ }; @@ -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) { @@ -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 {