@@ -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 ,
@@ -579,8 +579,13 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
579579 ) : InteractionOutput {
580580 // infer media type from form if not in response metadata
581581 content . type ??= form . contentType ?? "application/json" ;
582-
583582 // check if returned media type is the same as expected media type (from TD)
583+ this . checkMediaTypeOrThrow ( content , form ) ;
584+ return new InteractionOutput ( content , form , outputDataSchema , { ignoreValidation } ) ;
585+ }
586+
587+ // check if returned media type is the same as expected media type (from TD)
588+ private checkMediaTypeOrThrow ( content : Content , form : Form ) {
584589 if ( form . response != null ) {
585590 const parsedMediaTypeContent = ContentType . parse ( content . type ) ;
586591 const parsedMediaTypeForm = ContentType . parse ( form . response . contentType ) ;
@@ -590,7 +595,19 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
590595 ) ;
591596 }
592597 }
593- return new InteractionOutput ( content , form , outputDataSchema , { ignoreValidation } ) ;
598+ }
599+
600+ private handleActionInteractionOutput (
601+ content : Content ,
602+ form : Form ,
603+ outputDataSchema : WoT . DataSchema | undefined ,
604+ ignoreValidation : boolean
605+ ) : ActionInteractionOutput {
606+ // infer media type from form if not in response metadata
607+ content . type ??= form . contentType ?? "application/json" ;
608+ // check if returned media type is the same as expected media type (from TD)
609+ this . checkMediaTypeOrThrow ( content , form ) ;
610+ return new ActionInteractionOutput ( content , form , outputDataSchema , { ignoreValidation } ) ;
594611 }
595612
596613 async _readProperties ( propertyNames : string [ ] ) : Promise < WoT . PropertyReadMap > {
@@ -680,7 +697,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
680697 actionName : string ,
681698 parameter ?: InteractionInput ,
682699 options ?: WoT . InteractionOptions
683- ) : Promise < WoT . InteractionOutput > {
700+ ) : Promise < WoT . ActionInteractionOutput > {
684701 const ta = this . actions [ actionName ] ;
685702 if ( ta == null ) {
686703 throw new Error ( `ConsumedThing '${ this . title } ' does not have action ${ actionName } ` ) ;
@@ -710,7 +727,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
710727 const content = await client . invokeResource ( form , input ) ;
711728 try {
712729 const ignoreValidation = ta . synchronous === undefined ? true : ! ta . synchronous ;
713- return this . handleInteractionOutput ( content , form , ta . output , ignoreValidation ) ;
730+ return this . handleActionInteractionOutput ( content , form , ta . output , ignoreValidation ) ;
714731 } catch ( e ) {
715732 const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
716733 throw new Error ( `Error while processing action for ${ ta . title } . ${ error . message } ` ) ;
0 commit comments