Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/iron
2 changes: 1 addition & 1 deletion examples/lamp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Thing from '../src/thing.js';
import ThingServer from '../src/server.js';
import ThingServer from '../src/thing-server.js';

const partialTD = {
title: 'My Lamp',
Expand Down
42 changes: 25 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

272 changes: 272 additions & 0 deletions src/interaction-affordance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
import ValidationError from './validation-error.js';

/**
* Expected Response
*
* @typedef {Object} ExpectedResponse
* @property {string} contentType
*/

/**
* Additional Expected Response
*
* @typedef {Object} AdditionalExpectedResponse
* @property {boolean} [success]
* @property {string} [contentType]
* @property {string} [schema]
*/

/**
* Data Schema
*
* @typedef {Object} DataSchema
* @ts-ignore
* @property {string|Array<string>} ['@type']
* @property {string} [title]
* @property {Record<string,string>} [titles]
* @property {string} [description]
* @property {Record<string,string>} [descriptions]
* @property {any} [const]
* @property {any} [default]
* @property {string} [unit]
* @property {Array<DataSchema>} [oneOf]
* @property {Array<any>} [enum]
* @property {boolean} [readOnly]
* @property {boolean} [writeOnly]
* @property {string} [format]
* @property {'object'|'array'|'string'|'number'|'integer'|'boolean'|'null'} [type]
*/

/**
* Form
*
* @typedef {Object} Form
* @property {string} href
* @property {string} [contentType]
* @property {string} [contentCoding]
* @property {string|Array<string>} [security]
* @property {string|Array<string>} [scopes]
* @property {ExpectedResponse} [response]
* @property {Array<AdditionalExpectedResponse>} [additionalResponses]
* @property {string} [subprotocol]
* @property {string|Array<string>} [op]
*/

// TODO: Constrain set of possible values for op

/**
* Interaction Affordance
*
* Represents an InteractionAffordance from the W3C WoT Thing Description 1.1
* specification
* https://www.w3.org/TR/wot-thing-description/#interactionaffordance
*/
class InteractionAffordance {
/**
* @type {string|Array<string>|undefined}
*/
'@type';

/**
* @type {string|undefined}
*/
title;

/**
* @type {Map<string, string>|undefined}
*/
titles;

/**
* @type {string|undefined}
*/
description;

/**
* @type {Map<string, string>|undefined}
*/
descriptions;

/**
* @type {Array<Form>}
*/
forms = [];

/**
* @type{Map<string,DataSchema>|undefined}
*/
uriVariables;

/**
*
* @param {string} name The name of the InteractionAffordance from its key in
* a properties, actions or events Map.
* @param {Object<string, any>} description A description of an
* InteractionAffordance, i.e. a PropertyAffordance, ActionAffordance or
* EventAffordance.
*/
constructor(name, description) {
let validationError = new ValidationError([]);

if (
!name ||
!description ||
typeof name != 'string' ||
typeof description != 'object'
) {
throw new ValidationError([
{
field: `(root)`,
description:
'Tried to instantiate an InteractionAffordance with an invalid name or description',
},
]);
}

this.name = name;

// Parse @type member
try {
this.#parseSemanticTypeMember(description['@type']);
} catch (error) {
if (error instanceof ValidationError) {
validationError.validationErrors.push(...error.validationErrors);
} else {
throw error;
}
}

// Parse title member
try {
this.#parseTitleMember(description.title);
} catch (error) {
if (error instanceof ValidationError) {
validationError.validationErrors.push(...error.validationErrors);
} else {
throw error;
}
}

// TODO: Parse titles member
// TODO: Parse forms member
// TOOD: Parse uriVariables member

// Parse description member
try {
this.#parseDescriptionMember(description.description);
} catch (error) {
if (error instanceof ValidationError) {
validationError.validationErrors.push(...error.validationErrors);
} else {
throw error;
}
}

// TODO: Parse descriptions member
}

/**
* Parse the semantic type member.
*
* @param {string|Array<string>|undefined} type The provided value of semantic type.
*/
#parseSemanticTypeMember(type) {
if (!type) {
return;
}

// Check that @type has a valid type
if (!(typeof type == 'string' || Array.isArray(type))) {
throw new ValidationError([
{
field: `properties.${this.name}['@type']`,
description: '@type member is not a string or Array',
},
]);
}

// If @type is a string then use that value
if (typeof type == 'string') {
this['@type'] = type;
return;
}

// If @type is an array then validate its contents then set this['@type']
if (Array.isArray(type)) {
if (Array.length < 1) {
return;
}
/**
* @type {Array<string>}
*/
let types = [];

/**
* @type {Array<Object>}
*/
let errors = [];

type.forEach((typeItem) => {
if (typeof typeItem == 'string') {
types.push(typeItem);
} else {
errors.push({
field: `properties.${this.name}['@type']`,
description: '@type member is not string or Array of string',
});
}
});
if (errors.length > 0) {
throw new ValidationError(errors);
} else {
this['@type'] = types;
}
}
}

/**
* Parse title member.
*
* @param {string|undefined} title
*/
#parseTitleMember(title) {
if (!title) {
return;
}

if (typeof title !== 'string') {
throw new ValidationError([
{
field: `properties.${this.name}.title`,
description: 'title member is not a string',
},
]);
}

this.title = title;
}

/**
* Parse description member.
*
* @param {string|undefined} description
*/
#parseDescriptionMember(description) {
if (!description) {
return;
}

if (typeof description !== 'string') {
throw new ValidationError([
{
field: `properties.${this.name}.description`,
description: 'description member is not a string',
},
]);
}

this.description = description;
}
}

export default InteractionAffordance;
Loading
Loading