Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict checking for package examples #1049

Merged
16 changes: 10 additions & 6 deletions examples/scripts/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ WoT.produce({
const listToDelete = [];
for (const id of countdowns.keys()) {
const as = countdowns.get(id);
if (as.output !== undefined) {
if (as !== undefined && as.output !== undefined) {
const prev = as.output;
as.output--;
console.log("\t" + id + ", from " + prev + " to " + as.output);
Expand Down Expand Up @@ -125,18 +125,22 @@ WoT.produce({
};
const ii = resp;
console.log("init countdown value = " + JSON.stringify(resp));
countdowns.set(resp.href, resp);
countdowns.set(resp.href !== undefined ? resp.href : "", resp);
return ii;
});
thing.setActionHandler("stopCountdown", async (params, options) => {
if (params) {
const value = await params.value();
if (typeof value === "string" && countdowns.has(value)) {
const as = countdowns.get(value);
as.output = 0;
as.status = Status.completed;
console.log("Countdown stopped for href: " + value);
return undefined;
if (as !== undefined) {
as.output = 0;
as.status = Status.completed;
console.log("Countdown stopped for href: " + value);
return null;
} else {
throw Error("Countdown value is undefined for href, " + value);
}
} else {
throw Error("Input provided for stopCountdown is no string or invalid href, " + value);
}
Expand Down
23 changes: 13 additions & 10 deletions examples/scripts/counter-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

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;
}
WoTHelpers.fetch("coap://localhost:5683/counter")
.then(async (td) => {
// using await for serial execution (note 'async' in then() of fetch())
Expand Down Expand Up @@ -45,13 +58,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
21 changes: 12 additions & 9 deletions examples/scripts/smart-coffee-machine-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
// This is an example of Web of Things consumer ("client" mode) Thing script.
// It considers a fictional smart coffee machine in order to demonstrate the capabilities of Web of Things.
// An accompanying tutorial is available at http://www.thingweb.io/smart-coffee-machine.html.

// Print data and an accompanying message in a distinguishable way
function log(msg, data) {
console.info("======================");
console.info(msg);
console.dir(data);
console.info("======================");
}
WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) => {
try {
const thing = await WoT.consume(td);
Expand All @@ -40,7 +48,7 @@ WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) =
const makeCoffee = await thing.invokeAction("makeDrink", undefined, {
uriVariables: { drinkId: "latte", size: "l", quantity: 3 },
});
const makeCoffeep = await makeCoffee.value();
const makeCoffeep = await (makeCoffee === null || makeCoffee === void 0 ? void 0 : makeCoffee.value());
if (makeCoffeep.result) {
log("Enjoy your drink!", makeCoffeep);
} else {
Expand All @@ -57,7 +65,9 @@ WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) =
time: "10:00",
mode: "everyday",
});
const scheduledTaskp = await scheduledTask.value();
const scheduledTaskp = await (scheduledTask === null || scheduledTask === void 0
? void 0
: scheduledTask.value());
log(scheduledTaskp.message, scheduledTaskp);
// See how it has been added to the schedules property
const schedules = await (await thing.readProperty("schedules")).value();
Expand All @@ -74,10 +84,3 @@ WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) =
console.error("Script error:", err);
}
});
// Print data and an accompanying message in a distinguishable way
function log(msg, data) {
console.info("======================");
console.info(msg);
console.dir(data);
console.info("======================");
}
5 changes: 3 additions & 2 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 @@ -15,7 +15,8 @@
WoTHelpers.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
6 changes: 3 additions & 3 deletions examples/testthing/testclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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);
console.error("FAIL " + name + " READ:", JSON.stringify(err));
}
}
async function testPropertyWrite(thing, name, value, shouldFail) {
Expand All @@ -35,8 +35,8 @@ 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) console.error("FAIL " + name + " WRITE (" + displayValue + "):", JSON.stringify(err));
else console.info("PASS " + name + " WRITE (" + displayValue + "):", JSON.stringify(err));
}
}
WoTHelpers.fetch("http://localhost:8080/testthing")
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export default class Helpers implements Resolver {
}

// TODO: specialize fetch to retrieve just thing descriptions
// see https://github.com/eclipse-thingweb/node-wot/issues/1055
public fetch(uri: string): Promise<unknown> {
return new Promise<unknown>((resolve, reject) => {
const client = this.srv.getClientFor(Helpers.extractScheme(uri));
Expand Down
45 changes: 22 additions & 23 deletions packages/examples/src/scripts/countdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

import { InteractionOptions } from "wot-typescript-definitions";

function uuidv4(): string {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
Expand Down Expand Up @@ -91,8 +89,8 @@ WoT.produce({
console.log("Update countdowns");
const listToDelete: string[] = [];
for (const id of countdowns.keys()) {
const as: ActionStatus = countdowns.get(id);
if (as.output !== undefined) {
const as = countdowns.get(id);
if (as?.output !== undefined) {
const prev = as.output;
as.output--;
console.log("\t" + id + ", from " + prev + " to " + as.output);
Expand All @@ -117,20 +115,17 @@ WoT.produce({
}, 1000);

// set property handlers (using async-await)
thing.setPropertyReadHandler(
"countdowns",
async (options: InteractionOptions): Promise<WoT.InteractionInput> => {
const cts: string[] = [];
for (const id of countdowns.keys()) {
cts.push(id);
}
return cts;
thing.setPropertyReadHandler("countdowns", async (options): Promise<WoT.InteractionInput> => {
const cts: string[] = [];
for (const id of countdowns.keys()) {
cts.push(id);
}
);
return cts;
});
// set action handlers (using async-await)
thing.setActionHandler(
"startCountdown",
async (params: WoT.InteractionOutput, options: InteractionOptions): Promise<WoT.InteractionInput> => {
async (params: WoT.InteractionOutput, options): Promise<WoT.InteractionInput> => {
let initValue = 100;
if (params) {
const value = await params.value();
Expand All @@ -145,21 +140,25 @@ WoT.produce({
};
const ii: WoT.InteractionInput = resp;
console.log("init countdown value = " + JSON.stringify(resp));
countdowns.set(resp.href, resp);
countdowns.set(resp.href ?? "", resp);
return ii;
}
);
thing.setActionHandler(
"stopCountdown",
async (params: WoT.InteractionOutput, options: InteractionOptions): Promise<WoT.InteractionInput> => {
async (params: WoT.InteractionOutput, options): Promise<WoT.InteractionInput> => {
if (params) {
const value = await params.value();
if (typeof value === "string" && countdowns.has(value)) {
const as: ActionStatus = countdowns.get(value);
as.output = 0;
as.status = Status.completed;
console.log("Countdown stopped for href: " + value);
return undefined;
const as = countdowns.get(value);
if (as !== undefined) {
as.output = 0;
as.status = Status.completed;
console.log("Countdown stopped for href: " + value);
return null;
} else {
throw Error("Countdown value is undefined for href, " + value);
}
} else {
throw Error("Input provided for stopCountdown is no string or invalid href, " + value);
}
Expand All @@ -170,11 +169,11 @@ WoT.produce({
);
thing.setActionHandler(
"monitorCountdown",
async (params: WoT.InteractionOutput, options: InteractionOptions): Promise<WoT.InteractionInput> => {
async (params: WoT.InteractionOutput, options): Promise<WoT.InteractionInput> => {
if (params) {
const value = await params.value();
if (typeof value === "string" && countdowns.has(value)) {
const as: ActionStatus = countdowns.get(value);
const as = countdowns.get(value);
return JSON.stringify(as);
} else {
throw Error("Input provided for monitorCountdown is no string or invalid href, " + value);
Expand Down
12 changes: 7 additions & 5 deletions packages/examples/src/scripts/counter-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import { Helpers } from "@node-wot/core";
import { ThingDescription } from "wot-typescript-definitions";

let WoTHelpers: Helpers;
let WoTHelpers!: Helpers;

function getFormIndexForDecrementWithCoAP(thing: WoT.ConsumedThing): number {
const forms = thing.getThingDescription().actions.decrement.forms;
for (let i = 0; i < forms.length; i++) {
if (/^coaps?:\/\/.*/.test(forms[i].href)) {
return i;
const forms = thing.getThingDescription().actions?.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
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/src/scripts/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,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 as Record<string, string>;
fill = uriVariables.fill;
}
Expand All @@ -234,7 +234,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 as Record<string, unknown>;
step = uriVariables.step as number;
}
Expand All @@ -251,7 +251,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 as Record<string, unknown>;
step = uriVariables.step as number;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { ThingDescription } from "wot-typescript-definitions";
import { Helpers } from "@node-wot/core";
let WoTHelpers: Helpers;
let WoTHelpers!: Helpers;

// Print data and an accompanying message in a distinguishable way
function log(msg: string, data: unknown) {
Expand Down Expand Up @@ -60,7 +60,7 @@ WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) =
const makeCoffee = await thing.invokeAction("makeDrink", undefined, {
uriVariables: { drinkId: "latte", size: "l", quantity: 3 },
});
const makeCoffeep = (await makeCoffee.value()) as Record<string, unknown>;
const makeCoffeep = (await makeCoffee?.value()) as Record<string, unknown>;
if (makeCoffeep.result) {
log("Enjoy your drink!", makeCoffeep);
} else {
Expand All @@ -79,7 +79,7 @@ WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) =
time: "10:00",
mode: "everyday",
});
const scheduledTaskp = (await scheduledTask.value()) as Record<string, string>;
const scheduledTaskp = (await scheduledTask?.value()) as Record<string, string>;
log(scheduledTaskp.message, scheduledTaskp);

// See how it has been added to the schedules property
Expand Down
5 changes: 3 additions & 2 deletions packages/examples/src/security/oauth/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import { Helpers } from "@node-wot/core";
import { ThingDescription } from "wot-typescript-definitions";

let WoTHelpers: Helpers;
let WoTHelpers!: Helpers;

WoTHelpers.fetch("https://localhost:8080/oauth").then((td) => {
WoT.consume(td as ThingDescription).then(async (thing) => {
try {
const result = await (await thing.invokeAction("sayOk")).value();
const resp = await thing.invokeAction("sayOk");
const result = resp?.value();
console.log("oAuth token was", result);
} catch (error) {
console.log("It seems that I couldn't access the resource");
Expand Down
Loading