diff --git a/src/controllers/well-known_controller.ts b/src/controllers/well-known_controller.ts index 592b79210..6f83edbf2 100644 --- a/src/controllers/well-known_controller.ts +++ b/src/controllers/well-known_controller.ts @@ -36,16 +36,16 @@ function build(): express.Router { const secure = request.secure; // Get a Thing Description of the gateway - let td = Gateway.getDescription(host, secure); + const td = Gateway.getDescription(host, secure); // Add a link to root as the canonical URL of the Thing Description - if (td.links === undefined) { + if (typeof td.links === 'undefined') { td.links = []; } td.links.push({ rel: 'canonical', href: '/', - type: 'application/td+json' + type: 'application/td+json', }); // Send the Thing Description in response diff --git a/src/models/gateway.ts b/src/models/gateway.ts index 12cb9e439..ad6c5c0a6 100644 --- a/src/models/gateway.ts +++ b/src/models/gateway.ts @@ -1,7 +1,7 @@ /** * Gateway Model. * - * Represents the gateway and its interaction affordances, including + * Represents the gateway and its interaction affordances, including * acting as a Thing Description Directory. * * This Source Code Form is subject to the terms of the Mozilla Public @@ -10,12 +10,11 @@ */ import * as Constants from '../constants'; -import { ThingDescription } from './thing'; +import { ThingDescription } from './thing'; export default class Gateway { - /** - * + * * Get a JSON Thing Description for this gateway. * * @param {String} reqHost request host, if coming via HTTP @@ -25,220 +24,217 @@ export default class Gateway { static getDescription(reqHost?: string, reqSecure?: boolean): ThingDescription { const origin = `${reqSecure ? 'https' : 'http'}://${reqHost}`; const desc: ThingDescription = { - '@context': [ - 'https://www.w3.org/2022/wot/td/v1.1', - 'https://www.w3.org/2022/wot/discovery' - ], + '@context': ['https://www.w3.org/2022/wot/td/v1.1', 'https://www.w3.org/2022/wot/discovery'], '@type': 'ThingDirectory', - 'id': origin, - 'base': origin, - 'title': 'WebThings Gateway', - 'securityDefinitions': { - 'oauth2_sc': { - 'scheme': 'oauth2', - 'flow': 'code', - 'authorization': `${origin}${Constants.OAUTH_PATH}/authorize`, - 'token': `${origin}${Constants.OAUTH_PATH}/token`, - 'scopes': [Constants.THINGS_PATH, `${Constants.THINGS_PATH}:readwrite`], - } + id: origin, + base: origin, + title: 'WebThings Gateway', + securityDefinitions: { + oauth2_sc: { + scheme: 'oauth2', + flow: 'code', + authorization: `${origin}${Constants.OAUTH_PATH}/authorize`, + token: `${origin}${Constants.OAUTH_PATH}/token`, + scopes: [Constants.THINGS_PATH, `${Constants.THINGS_PATH}:readwrite`], + }, }, - 'security': 'oauth2_sc', - 'properties': { - 'things': { - 'title': 'Things', - 'description': 'Retrieve all Thing Descriptions', - 'type': 'array', - 'items': { - 'type': 'object' + security: 'oauth2_sc', + properties: { + things: { + title: 'Things', + description: 'Retrieve all Thing Descriptions', + type: 'array', + items: { + type: 'object', }, - 'forms': [ + forms: [ { - 'href': '/things', + href: '/things', 'htv:methodName': 'GET', - 'response': { - 'description': 'Success response', + response: { + description: 'Success response', 'htv:statusCodeValue': 200, - 'contentType': 'application/json' + contentType: 'application/json', }, - 'additionalResponses': [ + additionalResponses: [ { - 'description': 'Token must contain scope', - 'htv:statusCodeValue': 400 - } - ] - } - ] - } + description: 'Token must contain scope', + 'htv:statusCodeValue': 400, + }, + ], + }, + ], + }, }, - 'actions': { - 'createAnonymousThing': { - 'description': 'Create a Thing Description', - 'input': { - 'type': 'object' + actions: { + createAnonymousThing: { + description: 'Create a Thing Description', + input: { + type: 'object', }, - 'forms': [ + forms: [ { - 'href': '/things', + href: '/things', 'htv:methodName': 'POST', - 'contentType': 'application/json', - 'response': { - 'htv:statusCodeValue': 201 + contentType: 'application/json', + response: { + 'htv:statusCodeValue': 201, }, - 'additionalResponses': [ + additionalResponses: [ { - 'description': 'Invalid or duplicate Thing Description', - 'htv:statusCodeValue': 400 + description: 'Invalid or duplicate Thing Description', + 'htv:statusCodeValue': 400, }, { - 'description': 'Internal error saving new Thing Description', - 'htv:statusCodeValue': 500 - } - ] - } - ] + description: 'Internal error saving new Thing Description', + 'htv:statusCodeValue': 500, + }, + ], + }, + ], }, - 'retrieveThing': { - 'description': 'Retrieve a Thing Description', - 'uriVariables': { - 'id': { + retrieveThing: { + description: 'Retrieve a Thing Description', + uriVariables: { + id: { '@type': 'ThingID', - 'title': 'Thing Description ID', - 'type': 'string', - 'format': 'iri-reference' - } + title: 'Thing Description ID', + type: 'string', + format: 'iri-reference', + }, }, - 'output': { - 'description': 'The schema is implied by the content type', - 'type': 'object' + output: { + description: 'The schema is implied by the content type', + type: 'object', }, - 'safe': true, - 'idempotent': true, - 'forms': [ + safe: true, + idempotent: true, + forms: [ { - 'href': '/things/{id}', + href: '/things/{id}', 'htv:methodName': 'GET', - 'response': { - 'description': 'Success response', + response: { + description: 'Success response', 'htv:statusCodeValue': 200, - 'contentType': 'application/json' + contentType: 'application/json', }, - 'additionalResponses': [ + additionalResponses: [ { - 'description': 'TD with the given id not found', - 'htv:statusCodeValue': 404 - } - ] - } - ] + description: 'TD with the given id not found', + 'htv:statusCodeValue': 404, + }, + ], + }, + ], }, - 'updateThing': { - 'description': 'Update a Thing Description', - 'uriVariables': { - 'id': { + updateThing: { + description: 'Update a Thing Description', + uriVariables: { + id: { '@type': 'ThingID', - 'title': 'Thing Description ID', - 'type': 'string', - 'format': 'iri-reference' - } + title: 'Thing Description ID', + type: 'string', + format: 'iri-reference', + }, }, - 'input': { - 'type': 'object' + input: { + type: 'object', }, - 'forms': [ + forms: [ { - 'href': '/things/{id}', + href: '/things/{id}', 'htv:methodName': 'PUT', - 'contentType': 'application/json', - 'response': { - 'description': 'Success response', - 'htv:statusCodeValue': 200 + contentType: 'application/json', + response: { + description: 'Success response', + 'htv:statusCodeValue': 200, }, - 'additionalResponses': [ + additionalResponses: [ { - 'description': 'Invalid serialization or TD', - 'htv:statusCodeValue': 400 + description: 'Invalid serialization or TD', + 'htv:statusCodeValue': 400, }, { - 'description': 'Failed to update Thing', - 'htv:statusCodeValue': 500 - } - ] - } - ] + description: 'Failed to update Thing', + 'htv:statusCodeValue': 500, + }, + ], + }, + ], }, - 'partiallyUpdateThing': { - 'description': 'Partially update a Thing Description', - 'uriVariables': { - 'id': { + partiallyUpdateThing: { + description: 'Partially update a Thing Description', + uriVariables: { + id: { '@type': 'ThingID', - 'title': 'Thing Description ID', - 'type': 'string', - 'format': 'iri-reference' - } + title: 'Thing Description ID', + type: 'string', + format: 'iri-reference', + }, }, - 'input': { - 'type': 'object' + input: { + type: 'object', }, - 'forms': [ + forms: [ { - 'href': '/things/{id}', + href: '/things/{id}', 'htv:methodName': 'PATCH', - 'contentType': 'application/merge-patch+json', - 'response': { - 'description': 'Success response', - 'htv:statusCodeValue': 200 + contentType: 'application/merge-patch+json', + response: { + description: 'Success response', + 'htv:statusCodeValue': 200, }, - 'additionalResponses': [ + additionalResponses: [ { - 'description': 'Request body missing required parameters', - 'htv:statusCodeValue': 400 + description: 'Request body missing required parameters', + 'htv:statusCodeValue': 400, }, { - 'description': 'TD with the given id not found', - 'htv:statusCodeValue': 404 + description: 'TD with the given id not found', + 'htv:statusCodeValue': 404, }, { - 'description': 'Failed to update Thing', - 'htv:statusCodeValue': 500 - } - ] - } - ] + description: 'Failed to update Thing', + 'htv:statusCodeValue': 500, + }, + ], + }, + ], }, - 'deleteThing': { - 'description': 'Delete a Thing Description', - 'uriVariables': { - 'id': { + deleteThing: { + description: 'Delete a Thing Description', + uriVariables: { + id: { '@type': 'ThingID', - 'title': 'Thing Description ID', - 'type': 'string', - 'format': 'iri-reference' - } + title: 'Thing Description ID', + type: 'string', + format: 'iri-reference', + }, }, - 'forms': [ + forms: [ { - 'href': '/things/{id}', + href: '/things/{id}', 'htv:methodName': 'DELETE', - 'response': { - 'description': 'Success response', - 'htv:statusCodeValue': 204 + response: { + description: 'Success response', + 'htv:statusCodeValue': 204, }, - 'additionalResponses': [ + additionalResponses: [ { - 'description': 'TD with the given id not found', - 'htv:statusCodeValue': 404 + description: 'TD with the given id not found', + 'htv:statusCodeValue': 404, }, { - 'description': 'Failed to remove Thing', - 'htv:statusCodeValue': 500 - } - ] - } - ] - } - } - } + description: 'Failed to remove Thing', + 'htv:statusCodeValue': 500, + }, + ], + }, + ], + }, + }, + }; return desc; } -} \ No newline at end of file +} diff --git a/src/models/thing.ts b/src/models/thing.ts index 448cb2179..a5dc6c2a4 100644 --- a/src/models/thing.ts +++ b/src/models/thing.ts @@ -267,7 +267,7 @@ export default class Thing extends EventEmitter { this.floorplanVisibility = description.floorplanVisibility; this.floorplanX = description.floorplanX; this.floorplanY = description.floorplanY; - if (description.layoutIndex === undefined) { + if (typeof description.layoutIndex === 'undefined') { this.layoutIndex = Infinity; } else { this.layoutIndex = description.layoutIndex; @@ -285,7 +285,7 @@ export default class Thing extends EventEmitter { router.addProxyServer(this.id, description.baseHref); } - if (description.hasOwnProperty('links') && description.links != undefined) { + if (description.hasOwnProperty('links') && typeof description.links != 'undefined') { for (const link of description.links) { // For backwards compatibility if (link.mediaType) { @@ -677,7 +677,7 @@ export default class Thing extends EventEmitter { href: `${reqSecure ? 'wss' : 'ws'}://${reqHost}${this.href}`, }; - if(desc.links === undefined) { + if (typeof desc.links === 'undefined') { desc.links = []; } desc.links.push(wsLink); @@ -935,7 +935,7 @@ export default class Thing extends EventEmitter { } // Update the UI href - if (description.links === undefined) { + if (typeof description.links === 'undefined') { description.links = []; } if (description.hasOwnProperty('links')) { diff --git a/src/models/things.ts b/src/models/things.ts index 59193f0bd..e5877132a 100644 --- a/src/models/things.ts +++ b/src/models/things.ts @@ -340,7 +340,7 @@ class Things extends EventEmitter { index = Math.min(things.length - 1, Math.max(0, index)); const movePromises = things.map((t) => { - if ((thing.getLayoutIndex() < t.getLayoutIndex()) && t.getLayoutIndex() <= index) { + if (thing.getLayoutIndex() < t.getLayoutIndex() && t.getLayoutIndex() <= index) { return t.setLayoutIndex(t.getLayoutIndex() - 1); } else if (index <= t.getLayoutIndex() && t.getLayoutIndex() < thing.getLayoutIndex()) { return t.setLayoutIndex(t.getLayoutIndex() + 1);