Skip to content

Commit

Permalink
chore: refactor TD based on wot-thing-description-types
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed May 5, 2022
1 parent a446f48 commit 687fc05
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 64 deletions.
12 changes: 8 additions & 4 deletions packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
********************************************************************************/

import * as WoT from "wot-typescript-definitions";
import * as TDT from "wot-thing-description-types";

import { Subject } from "rxjs/Subject";

Expand All @@ -39,8 +40,10 @@ import {
} from "./protocol-interfaces";

export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
security: Array<string>;
securityDefinitions: { [key: string]: TD.SecurityType };
security: string | [string, ...string[]];
securityDefinitions: {
[key: string]: TDT.SecurityScheme;
};

id: string;
title: string;
Expand Down Expand Up @@ -121,7 +124,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
addDefaultLanguage(thing: ExposedThing): void {
// add @language : "en" if no @language set
if (Array.isArray(thing["@context"])) {
const arrayContext = thing["@context"];
const arrayContext: TDT.ThingContext = thing["@context"];
let languageSet = false;
for (const arrayEntry of arrayContext) {
if (typeof arrayEntry === "object") {
Expand All @@ -131,7 +134,8 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
}
}
if (!languageSet) {
arrayContext.push({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(arrayContext as any).push({
"@language": TD.DEFAULT_CONTEXT_LANGUAGE,
});
}
Expand Down
5 changes: 3 additions & 2 deletions packages/td-tools/src/td-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as TD from "./thing-description";

import isAbsoluteUrl = require("is-absolute-url");
import URLToolkit = require("url-toolkit");
import { ThingContext } from "wot-thing-description-types";

/** Parses a TD into a Thing object */

Expand Down Expand Up @@ -52,7 +53,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
if (thing["@context"] === undefined) {
thing["@context"] = [TD.DEFAULT_CONTEXT_V1, TD.DEFAULT_CONTEXT_V11];
} else if (Array.isArray(thing["@context"])) {
let semContext: Array<string> = thing["@context"];
let semContext = thing["@context"] as Array<string>;
const indexV1 = semContext.indexOf(TD.DEFAULT_CONTEXT_V1);
const indexV11 = semContext.indexOf(TD.DEFAULT_CONTEXT_V11);
if (indexV1 === -1 && indexV11 === -1) {
Expand Down Expand Up @@ -87,7 +88,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
semContext.unshift(TD.DEFAULT_CONTEXT_V11);
}
}
thing["@context"] = semContext;
thing["@context"] = semContext as ThingContext;
}
} else if (thing["@context"] !== TD.DEFAULT_CONTEXT_V1 && thing["@context"] !== TD.DEFAULT_CONTEXT_V11) {
const semContext = thing["@context"];
Expand Down
85 changes: 27 additions & 58 deletions packages/td-tools/src/thing-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,31 @@

// global W3C WoT Scripting API definitions
import * as WoT from "wot-typescript-definitions";
import * as TDT from "wot-thing-description-types";

export const DEFAULT_CONTEXT_V1 = "https://www.w3.org/2019/wot/td/v1";
export const DEFAULT_CONTEXT_V11 = "https://www.w3.org/2022/wot/td/v1.1";
export const DEFAULT_CONTEXT_LANGUAGE = "en";
export const DEFAULT_THING_TYPE = "Thing";

/* TODOs / Questions
~ In Thing index structure could be read-only (sanitizing needs write access)
*/

export declare type MultiLanguage = Record<string, unknown>; // object?

/** Implements the Thing Description as software object */
export default class Thing {
id: string;
title: string;
titles: MultiLanguage;
description: string;
descriptions: MultiLanguage;
support: string;
modified: string;
created: string;
version: VersionInfo;
export default class Thing implements TDT.ThingDescription {
title: TDT.Title;
securityDefinitions: {
[key: string]: SecurityType;
[key: string]: TDT.SecurityScheme;
};

security: Array<string>;
base: string;
security: string | [string, ...string[]];

properties: {
[key: string]: ThingProperty;
};

actions: {
[key: string]: ThingAction;
};

events: {
[key: string]: ThingEvent;
};

uriVariables?: {
[key: string]: DataSchema;
};

links: Array<Link>;
forms: Array<Form>;
"@context": TDT.ThingContext;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;

constructor() {
this["@context"] = [DEFAULT_CONTEXT_V1, DEFAULT_CONTEXT_V11];
this["@type"] = DEFAULT_THING_TYPE;
this.security = [];
this.security = "";
this.properties = {};
this.actions = {};
this.events = {};
Expand All @@ -80,10 +49,10 @@ export default class Thing {

/** Basis from implementing the Thing Interaction descriptions for Property, Action, and Event */
export interface ThingInteraction {
title?: string;
titles?: MultiLanguage;
description?: string;
descriptions?: MultiLanguage;
title?: TDT.Title;
titles?: TDT.Titles;
description?: TDT.Description;
descriptions?: TDT.Descriptions;
scopes?: Array<string>;
uriVariables?: {
[key: string]: DataSchema;
Expand Down Expand Up @@ -146,10 +115,10 @@ export type DataSchema = WoT.DataSchema &

export class BaseSchema {
type?: string;
title?: string;
titles?: MultiLanguage;
description?: string;
descriptions?: MultiLanguage;
title?: TDT.Title;
titles?: TDT.Titles;
description?: TDT.Description;
descriptions?: TDT.Descriptions;
writeOnly?: boolean;
readOnly?: boolean;
oneOf?: Array<DataSchema>;
Expand Down Expand Up @@ -286,10 +255,10 @@ export abstract class ThingProperty extends BaseSchema implements ThingInteracti

// ThingInteraction
forms?: Array<Form>;
title?: string;
titles?: MultiLanguage;
description?: string;
descriptions?: MultiLanguage;
title?: TDT.Title;
titles?: TDT.Titles;
description?: TDT.Description;
descriptions?: TDT.Descriptions;
scopes?: Array<string>;
uriVariables?: {
[key: string]: DataSchema;
Expand All @@ -310,10 +279,10 @@ export abstract class ThingAction implements ThingInteraction {

// ThingInteraction
forms?: Array<Form>;
title?: string;
titles?: MultiLanguage;
description?: string;
descriptions?: MultiLanguage;
title?: TDT.Title;
titles?: TDT.Titles;
description?: TDT.Description;
descriptions?: TDT.Descriptions;
scopes?: Array<string>;
uriVariables?: {
[key: string]: DataSchema;
Expand All @@ -332,10 +301,10 @@ export abstract class ThingEvent implements ThingInteraction {

// ThingInteraction
forms?: Array<Form>;
title?: string;
titles?: MultiLanguage;
description?: string;
descriptions?: MultiLanguage;
title?: TDT.Title;
titles?: TDT.Titles;
description?: TDT.Description;
descriptions?: TDT.Descriptions;
scopes?: Array<string>;
uriVariables?: {
[key: string]: DataSchema;
Expand Down

0 comments on commit 687fc05

Please sign in to comment.