Skip to content

Commit 4ec7354

Browse files
refactor: add "empty" ActionInteractionOutput
query and cancel not implemented
1 parent bd523be commit 4ec7354

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

packages/core/src/consumed-thing.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import ContentType from "content-type";
3838
import ContentManager from "./content-serdes";
3939

4040
import UriTemplate = require("uritemplate");
41-
import { InteractionOutput } from "./interaction-output";
41+
import { InteractionOutput, ActionInteractionOutput } from "./interaction-output";
4242
import {
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}`);

packages/core/src/interaction-output.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,16 @@ export class InteractionOutput implements WoT.InteractionOutput {
140140
return json as T;
141141
}
142142
}
143+
144+
export class ActionInteractionOutput extends InteractionOutput implements WoT.ActionInteractionOutput {
145+
async query<T extends WoT.InteractionOutput>(
146+
params?: WoT.InteractionInput,
147+
options?: WoT.InteractionOptions
148+
): Promise<T> {
149+
throw new Error("Not yet implemented");
150+
}
151+
152+
async cancel(params?: WoT.InteractionInput, options?: WoT.InteractionOptions): Promise<void> {
153+
throw new Error("Not yet implemented");
154+
}
155+
}

0 commit comments

Comments
 (0)