From ad76c375b40d85875b9d88142ed0d49fc24d7dc4 Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Thu, 22 Feb 2024 14:48:27 +0100 Subject: [PATCH] refactor: report undefined for no expected value() (#1230) * refactor: report undefined for no expected value() * Update packages/core/src/interaction-output.ts Co-authored-by: Jan Romann * fix: test by adding DataSchema * refactor: make undefined return more explicitly * refactor: remove unnecessary comment * refactor: add test case for value() returning undefined if no DataSchema is provided * refactor: add warn logging in case undefined is returned * reword warning based on comment from @relu91 --------- Co-authored-by: Jan Romann --- packages/core/src/interaction-output.ts | 14 +++++++++++--- packages/core/test/InteractionOutputTest.ts | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/core/src/interaction-output.ts b/packages/core/src/interaction-output.ts index a369a2161..8eab52131 100644 --- a/packages/core/src/interaction-output.ts +++ b/packages/core/src/interaction-output.ts @@ -21,7 +21,7 @@ import { Content } from "./content"; import Ajv from "ajv"; import { createLoggers } from "./logger"; -const { debug } = createLoggers("core", "interaction-output"); +const { debug, warn } = createLoggers("core", "interaction-output"); // Problem: strict mode ajv does not accept unknown keywords in schemas // however property affordances could contain all sort of fields @@ -78,6 +78,14 @@ export class InteractionOutput implements WoT.InteractionOutput { } async value(): Promise { + // is there any value expected at all? + if (this.schema == null) { + warn( + `No schema defined. Hence undefined is reported for value() function. If you are invoking an action with no output that is on purpose, otherwise consider using arrayBuffer().` + ); + return undefined as unknown as T; + } + // the value has been already read? if (this.#value !== undefined) { return this.#value as T; @@ -91,8 +99,8 @@ export class InteractionOutput implements WoT.InteractionOutput { throw new NotReadableError("No form defined"); } - if (this.schema == null || this.schema.type == null) { - throw new NotReadableError("No schema defined"); + if (this.schema.type == null) { + throw new NotReadableError("No schema type defined"); } // is content type valid? diff --git a/packages/core/test/InteractionOutputTest.ts b/packages/core/test/InteractionOutputTest.ts index 8ea250aff..0d66b39dd 100644 --- a/packages/core/test/InteractionOutputTest.ts +++ b/packages/core/test/InteractionOutputTest.ts @@ -59,7 +59,7 @@ class InteractionOutputTests { const stream = Readable.from([1, 2, 3]); const content = new Content("application/json", stream); - const out = new InteractionOutput(content, {}); + const out = new InteractionOutput(content, {}, { type: "string" }); const result = []; const reader = out.data.getReader(); expect(reader).not.to.be.undefined; @@ -73,6 +73,20 @@ class InteractionOutputTests { return expect(out.value()).to.be.rejected; } + @test async "should return undefined for value call if no DataSchema is provided"() { + const stream = Readable.from([]); + const content = new Content("application/json", stream); + + const out = new InteractionOutput(content, {}); + + const result1 = await out.value(); + expect(result1).be.undefined; + + // try a second time also + const result2 = await out.value(); + expect(result2).be.undefined; + } + @test async "should return the value"() { const stream = Readable.from(Buffer.from("true", "utf-8")); const content = new Content("application/json", stream);