@@ -38,7 +38,7 @@ import ContentType from "content-type";
3838import ContentManager from "./content-serdes" ;
3939
4040import UriTemplate = require( "uritemplate" ) ;
41- import { InteractionOutput } from "./interaction-output" ;
41+ import { InteractionOutput , ActionInteractionOutput } from "./interaction-output" ;
4242import {
4343 ActionElement ,
4444 EventElement ,
@@ -564,7 +564,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
564564
565565 const content = await client . readResource ( form ) ;
566566 try {
567- return this . handleInteractionOutput ( content , form , tp , false ) ;
567+ return this . handleInteractionOutput ( content , form , tp ) ;
568568 } catch ( e ) {
569569 const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
570570 throw new Error ( `Error while processing property for ${ tp . title } . ${ error . message } ` ) ;
@@ -574,13 +574,17 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
574574 private handleInteractionOutput (
575575 content : Content ,
576576 form : Form ,
577- outputDataSchema : WoT . DataSchema | undefined ,
578- ignoreValidation : boolean
577+ outputDataSchema : WoT . DataSchema | undefined
579578 ) : InteractionOutput {
580579 // infer media type from form if not in response metadata
581580 content . type ??= form . contentType ?? "application/json" ;
582-
583581 // check if returned media type is the same as expected media type (from TD)
582+ this . checkMediaTypeOrThrow ( content , form ) ;
583+ return new InteractionOutput ( content , form , outputDataSchema ) ;
584+ }
585+
586+ // check if returned media type is the same as expected media type (from TD)
587+ private checkMediaTypeOrThrow ( content : Content , form : Form ) {
584588 if ( form . response != null ) {
585589 const parsedMediaTypeContent = ContentType . parse ( content . type ) ;
586590 const parsedMediaTypeForm = ContentType . parse ( form . response . contentType ) ;
@@ -590,7 +594,19 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
590594 ) ;
591595 }
592596 }
593- return new InteractionOutput ( content , form , outputDataSchema , { ignoreValidation } ) ;
597+ }
598+
599+ private handleActionInteractionOutput (
600+ content : Content ,
601+ form : Form ,
602+ outputDataSchema : WoT . DataSchema | undefined ,
603+ synchronous ?: boolean
604+ ) : ActionInteractionOutput {
605+ // infer media type from form if not in response metadata
606+ content . type ??= form . contentType ?? "application/json" ;
607+ // check if returned media type is the same as expected media type (from TD)
608+ this . checkMediaTypeOrThrow ( content , form ) ;
609+ return new ActionInteractionOutput ( content , form , outputDataSchema , synchronous ) ;
594610 }
595611
596612 async _readProperties ( propertyNames : string [ ] ) : Promise < WoT . PropertyReadMap > {
@@ -680,7 +696,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
680696 actionName : string ,
681697 parameter ?: InteractionInput ,
682698 options ?: WoT . InteractionOptions
683- ) : Promise < WoT . InteractionOutput > {
699+ ) : Promise < WoT . ActionInteractionOutput > {
684700 const ta = this . actions [ actionName ] ;
685701 if ( ta == null ) {
686702 throw new Error ( `ConsumedThing '${ this . title } ' does not have action ${ actionName } ` ) ;
@@ -709,8 +725,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
709725
710726 const content = await client . invokeResource ( form , input ) ;
711727 try {
712- const ignoreValidation = ta . synchronous === undefined ? true : ! ta . synchronous ;
713- return this . handleInteractionOutput ( content , form , ta . output , ignoreValidation ) ;
728+ return this . handleActionInteractionOutput ( content , form , ta . output , ta . synchronous ) ;
714729 } catch ( e ) {
715730 const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
716731 throw new Error ( `Error while processing action for ${ ta . title } . ${ error . message } ` ) ;
@@ -753,7 +768,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
753768 // next
754769 ( content ) => {
755770 try {
756- listener ( this . handleInteractionOutput ( content , form , tp , false ) ) ;
771+ listener ( this . handleInteractionOutput ( content , form , tp ) ) ;
757772 } catch ( e ) {
758773 const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
759774 warn ( `Error while processing observe property for ${ tp . title } . ${ error . message } ` ) ;
@@ -809,7 +824,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
809824 formWithoutURITemplates ,
810825 ( content ) => {
811826 try {
812- listener ( this . handleInteractionOutput ( content , form , te . data , false ) ) ;
827+ listener ( this . handleInteractionOutput ( content , form , te . data ) ) ;
813828 } catch ( e ) {
814829 const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
815830 warn ( `Error while processing event for ${ te . title } . ${ error . message } ` ) ;
0 commit comments