Skip to content

Commit 9d04652

Browse files
committed
fix(coap-server): add forms to affordances in a more type-safe way
1 parent 8adf840 commit 9d04652

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

packages/binding-coap/src/coap-server.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage
3232
import slugify from "slugify";
3333
import { Readable } from "stream";
3434
import { MdnsIntroducer } from "./mdns-introducer";
35-
import { PropertyElement, DataSchema } from "wot-thing-description-types";
35+
import { PropertyElement, DataSchema, ActionElement, EventElement } from "wot-thing-description-types";
3636
import { CoapServerConfig } from "./coap";
3737
import { DataSchemaValue } from "wot-typescript-definitions";
3838

3939
const { debug, warn, info, error } = createLoggers("binding-coap", "coap-server");
4040

4141
type CoreLinkFormatParameters = Map<string, string[] | number[]>;
4242

43+
type AffordanceElement = PropertyElement | ActionElement | EventElement;
44+
4345
// TODO: Move to core?
4446
type AugmentedInteractionOptions = WoT.InteractionOptions & { formIndex: number };
4547

@@ -228,6 +230,15 @@ export default class CoapServer implements ProtocolServer {
228230
return opValues;
229231
}
230232

233+
private addFormToAffordance(form: TD.Form, affordance: AffordanceElement): void {
234+
const affordanceForms = affordance.forms;
235+
if (affordanceForms == null) {
236+
affordance.forms ??= [form];
237+
} else {
238+
affordanceForms.push(form);
239+
}
240+
}
241+
231242
private fillInPropertyBindingData(thing: ExposedThing, base: string, offeredMediaType: string) {
232243
for (const [propertyName, property] of Object.entries(thing.properties)) {
233244
const opValues = ProtocolHelpers.getPropertyOpValues(property);
@@ -241,7 +252,7 @@ export default class CoapServer implements ProtocolServer {
241252
property.uriVariables
242253
);
243254

244-
property.forms.push(form);
255+
this.addFormToAffordance(form, property);
245256
this.logHrefAssignment(form, "Property", propertyName);
246257
}
247258
}
@@ -258,7 +269,7 @@ export default class CoapServer implements ProtocolServer {
258269
action.uriVariables
259270
);
260271

261-
action.forms.push(form);
272+
this.addFormToAffordance(form, action);
262273
this.logHrefAssignment(form, "Action", actionName);
263274
}
264275
}
@@ -275,7 +286,7 @@ export default class CoapServer implements ProtocolServer {
275286
event.uriVariables
276287
);
277288

278-
event.forms.push(form);
289+
this.addFormToAffordance(form, event);
279290
this.logHrefAssignment(form, "Event", eventName);
280291
}
281292
}

0 commit comments

Comments
 (0)