@node-wot/td-tools
Advanced tools
Comparing version 0.8.8 to 0.8.9
@@ -148,3 +148,3 @@ "use strict"; | ||
} | ||
if (!isAbsoluteUrl(form.href) && !thing.base) | ||
if (!isAbsoluteUrl(form.href) && thing.base == null) | ||
throw new Error(`Form of Property '${propName}' has relative URI while TD has no base field`); | ||
@@ -163,3 +163,3 @@ allForms.push(form); | ||
} | ||
if (!isAbsoluteUrl(form.href) && !thing.base) | ||
if (!isAbsoluteUrl(form.href) && thing.base == null) | ||
throw new Error(`Form of Action '${actName}' has relative URI while TD has no base field`); | ||
@@ -178,3 +178,3 @@ allForms.push(form); | ||
} | ||
if (!isAbsoluteUrl(form.href) && !thing.base) | ||
if (!isAbsoluteUrl(form.href) && thing.base == null) | ||
throw new Error(`Form of Event '${evtName}' has relative URI while TD has no base field`); | ||
@@ -199,4 +199,5 @@ allForms.push(form); | ||
function serializeTD(thing) { | ||
var _a; | ||
const copy = JSON.parse(JSON.stringify(thing)); | ||
if (!copy.security || copy.security.length === 0) { | ||
if (copy.security == null || copy.security.length === 0) { | ||
copy.securityDefinitions = { | ||
@@ -207,9 +208,9 @@ nosec_sc: { scheme: "nosec" }, | ||
} | ||
if (copy.forms && copy.forms.length === 0) { | ||
if (((_a = copy.forms) === null || _a === void 0 ? void 0 : _a.length) === 0) { | ||
delete copy.forms; | ||
} | ||
if (copy.properties && Object.keys(copy.properties).length === 0) { | ||
if (copy.properties != null && Object.keys(copy.properties).length === 0) { | ||
delete copy.properties; | ||
} | ||
else if (copy.properties) { | ||
else if (copy.properties != null) { | ||
for (const propName in copy.properties) { | ||
@@ -228,6 +229,6 @@ const prop = copy.properties[propName]; | ||
} | ||
if (copy.actions && Object.keys(copy.actions).length === 0) { | ||
if (copy.actions != null && Object.keys(copy.actions).length === 0) { | ||
delete copy.actions; | ||
} | ||
else if (copy.actions) { | ||
else if (copy.actions != null) { | ||
for (const actName in copy.actions) { | ||
@@ -243,6 +244,6 @@ const act = copy.actions[actName]; | ||
} | ||
if (copy.events && Object.keys(copy.events).length === 0) { | ||
if (copy.events != null && Object.keys(copy.events).length === 0) { | ||
delete copy.events; | ||
} | ||
if (copy.links && copy.links.length === 0) { | ||
if ((copy === null || copy === void 0 ? void 0 : copy.links.length) === 0) { | ||
delete copy.links; | ||
@@ -249,0 +250,0 @@ } |
@@ -25,3 +25,3 @@ "use strict"; | ||
this.href = href; | ||
if (contentType) | ||
if (contentType != null) | ||
this.contentType = contentType; | ||
@@ -28,0 +28,0 @@ } |
@@ -86,3 +86,3 @@ "use strict"; | ||
const foundTmExtendsRel = data.links.find((link) => link.rel === "tm:extends"); | ||
if (foundTmExtendsRel) | ||
if (foundTmExtendsRel != null) | ||
return true; | ||
@@ -105,12 +105,6 @@ } | ||
static getModelVersion(data) { | ||
if ("version" in data && | ||
data.version && | ||
typeof data.version === "object" && | ||
"model" in data.version && | ||
typeof data.version.model === "string") { | ||
return data.version.model; | ||
} | ||
else { | ||
return undefined; | ||
} | ||
var _a; | ||
return typeof (data === null || data === void 0 ? void 0 : data.version) === "object" && typeof ((_a = data === null || data === void 0 ? void 0 : data.version) === null || _a === void 0 ? void 0 : _a.model) === "string" | ||
? data.version.model | ||
: undefined; | ||
} | ||
@@ -126,3 +120,3 @@ static validateThingModel(data) { | ||
valid: isValid, | ||
errors: errors, | ||
errors, | ||
}; | ||
@@ -186,3 +180,3 @@ } | ||
http.get(uri, (res) => { | ||
if (!res.statusCode || res.statusCode !== 200) { | ||
if (res.statusCode == null || res.statusCode !== 200) { | ||
reject(new Error(`http status code not 200 but ${res.statusCode} for ${uri}`)); | ||
@@ -214,3 +208,3 @@ } | ||
.get(uri, (res) => { | ||
if (!res.statusCode || res.statusCode !== 200) { | ||
if (res.statusCode == null || res.statusCode !== 200) { | ||
reject(new Error(`https status code not 200 but ${res.statusCode} for ${uri}`)); | ||
@@ -283,3 +277,3 @@ } | ||
const refObj = this.parseTmRef(affUri); | ||
if (!refObj.uri) { | ||
if (refObj.uri == null) { | ||
throw new Error(`Missing remote path in ${affUri}`); | ||
@@ -315,3 +309,3 @@ } | ||
} | ||
if (!options.baseUrl) { | ||
if (options.baseUrl == null) { | ||
options.baseUrl = "."; | ||
@@ -338,3 +332,3 @@ } | ||
const sub = submodelObj[key]; | ||
if (options.selfComposition) { | ||
if (options.selfComposition === true) { | ||
if (!data.links) { | ||
@@ -346,3 +340,3 @@ throw new Error("You used self composition but links are missing; they are needed to extract the instance name"); | ||
const instanceName = el.instanceName; | ||
if (!instanceName) { | ||
if (instanceName == null) { | ||
throw new Error("Self composition is not possible without instance names"); | ||
@@ -379,3 +373,3 @@ } | ||
} | ||
if (!data.links || options.selfComposition) { | ||
if (!data.links || options.selfComposition === true) { | ||
data.links = []; | ||
@@ -404,3 +398,3 @@ } | ||
const refs = {}; | ||
if (!data) { | ||
if (data == null) { | ||
return refs; | ||
@@ -435,3 +429,3 @@ } | ||
for (const key in properties) { | ||
if (dest.properties && dest.properties[key]) { | ||
if (dest.properties && dest.properties[key] != null) { | ||
extendedModel.properties[key] = Object.assign(Object.assign({}, properties[key]), dest.properties[key]); | ||
@@ -531,3 +525,3 @@ } | ||
let errors; | ||
if (keys && keys.length > 0 && (map === undefined || map === null)) { | ||
if ((keys === null || keys === void 0 ? void 0 : keys.length) > 0 && (map === undefined || map === null)) { | ||
isValid = false; | ||
@@ -548,3 +542,3 @@ errors = `No map provided for model ${model.title}`; | ||
valid: isValid, | ||
errors: errors, | ||
errors, | ||
}; | ||
@@ -565,3 +559,3 @@ } | ||
removeDependency(dep) { | ||
if (dep) { | ||
if (dep != null) { | ||
this.deps = this.deps.filter((el) => el !== dep); | ||
@@ -568,0 +562,0 @@ } |
export declare class AssetInterfaceDescriptionUtil { | ||
transformToTD(aid: string, template?: string, submodelRegex?: string): string; | ||
transformAAS2TD(aas: string, template?: string, submodelRegex?: string): string; | ||
@@ -7,2 +6,3 @@ transformSM2TD(aid: string, template?: string, submodelRegex?: string): string; | ||
transformTD2SM(tdAsString: string, protocols?: string[]): string; | ||
transformToTD(aid: string, template?: string, submodelRegex?: string): string; | ||
private getProtocolPrefixes; | ||
@@ -9,0 +9,0 @@ private updateProtocolPrefixes; |
@@ -32,2 +32,3 @@ "use strict"; | ||
const debug_1 = __importDefault(require("debug")); | ||
const isAbsoluteUrl = require("is-absolute-url"); | ||
const namespace = "node-wot:td-tools:asset-interface-description-util"; | ||
@@ -38,5 +39,2 @@ const logDebug = (0, debug_1.default)(`${namespace}:debug`); | ||
class AssetInterfaceDescriptionUtil { | ||
transformToTD(aid, template, submodelRegex) { | ||
return this.transformAAS2TD(aid, template, submodelRegex); | ||
} | ||
transformAAS2TD(aas, template, submodelRegex) { | ||
@@ -92,4 +90,5 @@ const smInformation = this.getSubmodelInformation(aas, submodelRegex); | ||
transformTD2SM(tdAsString, protocols) { | ||
var _a; | ||
const td = TDParser.parseTD(tdAsString); | ||
const aidID = td.id ? td.id : "ID" + Math.random(); | ||
const aidID = (_a = td.id) !== null && _a !== void 0 ? _a : "ID" + Math.random(); | ||
logInfo("TD " + td.title + " parsed..."); | ||
@@ -133,2 +132,5 @@ if (protocols === undefined || protocols.length === 0) { | ||
} | ||
transformToTD(aid, template, submodelRegex) { | ||
return this.transformAAS2TD(aid, template, submodelRegex); | ||
} | ||
getProtocolPrefixes(td) { | ||
@@ -157,5 +159,5 @@ const protocols = []; | ||
updateProtocolPrefixes(forms, protocols) { | ||
if (forms) { | ||
if (forms != null) { | ||
for (const interactionForm of forms) { | ||
if (interactionForm.href) { | ||
if (interactionForm.href != null) { | ||
const positionColon = interactionForm.href.indexOf(":"); | ||
@@ -173,3 +175,3 @@ if (positionColon > 0) { | ||
getBaseFromEndpointMetadata(endpointMetadata) { | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) && endpointMetadata.value instanceof Array) { | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
@@ -184,3 +186,3 @@ if (v.idShort === "base") { | ||
getContentTypeFromEndpointMetadata(endpointMetadata) { | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) && endpointMetadata.value instanceof Array) { | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
@@ -196,12 +198,12 @@ if (v.idShort === "contentType") { | ||
const securityDefinitions = {}; | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) && endpointMetadata.value instanceof Array) { | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
if (v.idShort === "securityDefinitions") { | ||
if (v.value && v.value instanceof Array) { | ||
if (v.value instanceof Array) { | ||
for (const securityDefinitionsValues of v.value) { | ||
if (securityDefinitionsValues.idShort) { | ||
if (securityDefinitionsValues.idShort != null) { | ||
if (securityDefinitionsValues.value instanceof Array) { | ||
for (const securityDefinitionsValue of securityDefinitionsValues.value) { | ||
if (securityDefinitionsValue.idShort === "scheme") { | ||
if (securityDefinitionsValue.value) { | ||
if (securityDefinitionsValue.value != null) { | ||
const ss = { scheme: securityDefinitionsValue.value }; | ||
@@ -223,8 +225,8 @@ securityDefinitions[securityDefinitionsValues.idShort] = ss; | ||
const security = []; | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) && endpointMetadata.value instanceof Array) { | ||
if ((endpointMetadata === null || endpointMetadata === void 0 ? void 0 : endpointMetadata.value) instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
if (v.idShort === "security") { | ||
if (v.value && v.value instanceof Array) { | ||
if (v.value instanceof Array) { | ||
for (const securityValue of v.value) { | ||
if (securityValue.value) { | ||
if (securityValue.value != null) { | ||
security.push(securityValue.value); | ||
@@ -253,3 +255,6 @@ } | ||
if (v.idShort === "href") { | ||
if (form.href && form.href.length > 0) { | ||
if (v.value != null && isAbsoluteUrl(v.value)) { | ||
form.href = v.value; | ||
} | ||
else if (form.href && form.href.length > 0) { | ||
form.href = form.href + v.value; | ||
@@ -262,12 +267,12 @@ } | ||
else if (typeof v.idShort === "string" && v.idShort.length > 0) { | ||
if (v.value) { | ||
form[v.idShort] = v.value; | ||
if (v.valueType && | ||
v.valueType && | ||
v.valueType.dataObjectType && | ||
v.valueType.dataObjectType.name && | ||
if (v.value != null) { | ||
const tdTerm = v.idShort.replace("_", ":"); | ||
form[tdTerm] = v.value; | ||
if (v.valueType != null && | ||
v.valueType.dataObjectType != null && | ||
v.valueType.dataObjectType.name != null && | ||
typeof v.valueType.dataObjectType.name === "string") { | ||
switch (v.valueType.dataObjectType.name) { | ||
case "boolean": | ||
form[v.idShort] = form[v.idShort] === "true"; | ||
form[tdTerm] = form[v.value] === "true"; | ||
break; | ||
@@ -290,3 +295,3 @@ case "float": | ||
case "positiveInteger": | ||
form[v.idShort] = Number(form[v.idShort]); | ||
form[tdTerm] = Number(form[v.value]); | ||
break; | ||
@@ -305,8 +310,10 @@ } | ||
processSubmodel(smInformation, submodel, submodelRegex) { | ||
if (submodel instanceof Object && submodel.idShort && submodel.idShort === "AssetInterfacesDescription") { | ||
if (submodel.submodelElements && submodel.submodelElements instanceof Array) { | ||
if (submodel instanceof Object && | ||
submodel.idShort != null && | ||
submodel.idShort === "AssetInterfacesDescription") { | ||
if (submodel.submodelElements instanceof Array) { | ||
for (const submodelElement of submodel.submodelElements) { | ||
if (submodelElement instanceof Object) { | ||
logDebug("SubmodelElement.idShort: " + submodelElement.idShort); | ||
if (submodelRegex && typeof submodelRegex === "string" && submodelRegex.length > 0) { | ||
if (typeof submodelRegex === "string" && submodelRegex.length > 0) { | ||
const regex = new RegExp(submodelRegex); | ||
@@ -326,3 +333,3 @@ if (!regex.test(submodelElement.idShort)) { | ||
var _a, _b, _c; | ||
if (submodelElement.value && submodelElement.value instanceof Array) { | ||
if (submodelElement.value instanceof Array) { | ||
let endpointMetadata = {}; | ||
@@ -349,5 +356,5 @@ for (const smValue of submodelElement.value) { | ||
logInfo("InterfaceMetadata"); | ||
if (smValue.value && smValue.value instanceof Array) { | ||
if (smValue.value instanceof Array) { | ||
for (const interactionValue of smValue.value) { | ||
if (interactionValue.idShort === "Properties") { | ||
if (interactionValue.idShort === "properties") { | ||
if (interactionValue.value instanceof Array) { | ||
@@ -360,3 +367,3 @@ for (const iValue of interactionValue.value) { | ||
const propInter = { | ||
endpointMetadata: endpointMetadata, | ||
endpointMetadata, | ||
interaction: iValue, | ||
@@ -368,3 +375,3 @@ }; | ||
} | ||
else if (interactionValue.idShort === "Operations") { | ||
else if (interactionValue.idShort === "actions") { | ||
if (interactionValue.value instanceof Array) { | ||
@@ -377,3 +384,3 @@ for (const iValue of interactionValue.value) { | ||
const actInter = { | ||
endpointMetadata: endpointMetadata, | ||
endpointMetadata, | ||
interaction: iValue, | ||
@@ -385,3 +392,3 @@ }; | ||
} | ||
else if (interactionValue.idShort === "Events") { | ||
else if (interactionValue.idShort === "events") { | ||
if (interactionValue.value instanceof Array) { | ||
@@ -394,3 +401,3 @@ for (const iValue of interactionValue.value) { | ||
const evInter = { | ||
endpointMetadata: endpointMetadata, | ||
endpointMetadata, | ||
interaction: iValue, | ||
@@ -418,3 +425,3 @@ }; | ||
}; | ||
if (aidModel instanceof Object && aidModel.submodels) { | ||
if (aidModel instanceof Object && aidModel.submodels != null) { | ||
if (aidModel.submodels instanceof Array) { | ||
@@ -429,3 +436,3 @@ for (const submodel of aidModel.submodels) { | ||
_transform(smInformation, template) { | ||
const thing = template ? JSON.parse(template) : {}; | ||
const thing = template != null ? JSON.parse(template) : {}; | ||
for (const [key, value] of smInformation.thing) { | ||
@@ -438,3 +445,3 @@ if (typeof value === "string") { | ||
} | ||
if (!thing["@context"]) { | ||
if (thing["@context"] == null) { | ||
thing["@context"] = "https://www.w3.org/2022/wot/td/v1.1"; | ||
@@ -445,3 +452,3 @@ } | ||
} | ||
if (!thing.securityDefinitions) { | ||
if (thing.securityDefinitions == null) { | ||
thing.securityDefinitions = {}; | ||
@@ -473,3 +480,3 @@ } | ||
for (const aasDescriptionEntry of aasDescription) { | ||
if (aasDescriptionEntry.language && aasDescriptionEntry.text) { | ||
if (aasDescriptionEntry.language != null && aasDescriptionEntry.text != null) { | ||
const language = aasDescriptionEntry.language; | ||
@@ -495,6 +502,6 @@ const text = aasDescriptionEntry.text; | ||
else if (interactionValue.idShort === "range") { | ||
if (interactionValue.min) { | ||
if (interactionValue.min != null) { | ||
thing.properties[key].min = interactionValue.min; | ||
} | ||
if (interactionValue.max) { | ||
if (interactionValue.max != null) { | ||
thing.properties[key].max = interactionValue.max; | ||
@@ -565,3 +572,3 @@ } | ||
const values = []; | ||
if (td.base) { | ||
if (td.base != null) { | ||
values.push({ | ||
@@ -575,3 +582,3 @@ idShort: "base", | ||
const securityValues = []; | ||
if (td.security) { | ||
if (td.security != null) { | ||
for (const secKey of td.security) { | ||
@@ -619,3 +626,2 @@ securityValues.push({ | ||
createInterfaceMetadata(td, protocol) { | ||
var _a; | ||
const properties = []; | ||
@@ -631,3 +637,3 @@ const actions = []; | ||
for (const formElementProperty of propertyValue.forms) { | ||
if ((_a = formElementProperty.href) === null || _a === void 0 ? void 0 : _a.startsWith(protocol)) { | ||
if (formElementProperty.href != null && formElementProperty.href.startsWith(protocol)) { | ||
formElementPicked = formElementProperty; | ||
@@ -642,3 +648,3 @@ break; | ||
const propertyValues = []; | ||
if (propertyValue.type) { | ||
if (propertyValue.type != null) { | ||
propertyValues.push({ | ||
@@ -651,3 +657,3 @@ idShort: "type", | ||
} | ||
if (propertyValue.title) { | ||
if (propertyValue.title != null) { | ||
propertyValues.push({ | ||
@@ -660,3 +666,3 @@ idShort: "title", | ||
} | ||
if (propertyValue.observable) { | ||
if (propertyValue.observable != null) { | ||
propertyValues.push({ | ||
@@ -669,6 +675,7 @@ idShort: "observable", | ||
} | ||
if (formElementPicked) { | ||
if (formElementPicked != null) { | ||
const propertyForm = []; | ||
for (const formTerm in formElementPicked) { | ||
for (let formTerm in formElementPicked) { | ||
const formValue = formElementPicked[formTerm]; | ||
formTerm = formTerm.replace(":", "_"); | ||
if (typeof formValue === "string") { | ||
@@ -700,3 +707,3 @@ propertyForm.push({ | ||
} | ||
else if (propertyValue.description) { | ||
else if (propertyValue.description != null) { | ||
description = []; | ||
@@ -710,3 +717,3 @@ description.push({ | ||
idShort: propertyKey, | ||
description: description, | ||
description, | ||
value: propertyValues, | ||
@@ -724,3 +731,3 @@ modelType: "SubmodelElementCollection", | ||
values.push({ | ||
idShort: "Properties", | ||
idShort: "properties", | ||
value: properties, | ||
@@ -730,3 +737,3 @@ modelType: "SubmodelElementCollection", | ||
values.push({ | ||
idShort: "Actions", | ||
idShort: "actions", | ||
value: actions, | ||
@@ -736,3 +743,3 @@ modelType: "SubmodelElementCollection", | ||
values.push({ | ||
idShort: "Events", | ||
idShort: "events", | ||
value: events, | ||
@@ -739,0 +746,0 @@ modelType: "SubmodelElementCollection", |
{ | ||
"name": "@node-wot/td-tools", | ||
"version": "0.8.8", | ||
"version": "0.8.9", | ||
"description": "W3C Web of Things (WoT) Thing Description parser, serializer, and other tools", | ||
@@ -17,27 +17,2 @@ "author": "Eclipse Thingweb <thingweb-dev@eclipse.org> (https://thingweb.io/)", | ||
"types": "dist/td-tools.d.ts", | ||
"devDependencies": { | ||
"@testdeck/mocha": "^0.1.2", | ||
"@types/chai": "^4.2.18", | ||
"@types/node": "16.18.35", | ||
"@types/readable-stream": "^2.3.13", | ||
"@typescript-eslint/eslint-plugin": "^4.30.0", | ||
"@typescript-eslint/parser": "^4.30.0", | ||
"chai": "^4.3.4", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-config-standard": "^16.0.3", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^5.1.0", | ||
"eslint-plugin-unused-imports": "^1.1.4", | ||
"mocha": "^9.2.2", | ||
"prettier": "^2.3.2", | ||
"stream-http": "^3.2.0", | ||
"ts-loader": "8", | ||
"ts-node": "^10.9.1", | ||
"typescript": "4.7.4", | ||
"typescript-standard": "^0.3.36", | ||
"webpack": "^5.76.0", | ||
"webpack-cli": "^4.8.0" | ||
}, | ||
"dependencies": { | ||
@@ -49,4 +24,4 @@ "ajv": "^8.11.0", | ||
"url-toolkit": "2.1.6", | ||
"wot-thing-model-types": "1.1.0-24-May-2023", | ||
"wot-typescript-definitions": "0.8.0-SNAPSHOT.26" | ||
"wot-thing-model-types": "1.1.0-05-July-2023", | ||
"wot-typescript-definitions": "0.8.0-SNAPSHOT.27" | ||
}, | ||
@@ -53,0 +28,0 @@ "scripts": { |
@@ -161,3 +161,3 @@ /******************************************************************************** | ||
// check if base field required | ||
if (!isAbsoluteUrl(form.href) && !thing.base) | ||
if (!isAbsoluteUrl(form.href) && thing.base == null) | ||
throw new Error(`Form of Property '${propName}' has relative URI while TD has no base field`); | ||
@@ -180,3 +180,3 @@ // add | ||
// check if base field required | ||
if (!isAbsoluteUrl(form.href) && !thing.base) | ||
if (!isAbsoluteUrl(form.href) && thing.base == null) | ||
throw new Error(`Form of Action '${actName}' has relative URI while TD has no base field`); | ||
@@ -199,3 +199,3 @@ // add | ||
// check if base field required | ||
if (!isAbsoluteUrl(form.href) && !thing.base) | ||
if (!isAbsoluteUrl(form.href) && thing.base == null) | ||
throw new Error(`Form of Event '${evtName}' has relative URI while TD has no base field`); | ||
@@ -228,3 +228,3 @@ // add | ||
// clean-ups | ||
if (!copy.security || copy.security.length === 0) { | ||
if (copy.security == null || copy.security.length === 0) { | ||
copy.securityDefinitions = { | ||
@@ -236,9 +236,9 @@ nosec_sc: { scheme: "nosec" }, | ||
if (copy.forms && copy.forms.length === 0) { | ||
if (copy.forms?.length === 0) { | ||
delete copy.forms; | ||
} | ||
if (copy.properties && Object.keys(copy.properties).length === 0) { | ||
if (copy.properties != null && Object.keys(copy.properties).length === 0) { | ||
delete copy.properties; | ||
} else if (copy.properties) { | ||
} else if (copy.properties != null) { | ||
// add mandatory fields (if missing): observable, writeOnly, and readOnly | ||
@@ -259,5 +259,5 @@ for (const propName in copy.properties) { | ||
if (copy.actions && Object.keys(copy.actions).length === 0) { | ||
if (copy.actions != null && Object.keys(copy.actions).length === 0) { | ||
delete copy.actions; | ||
} else if (copy.actions) { | ||
} else if (copy.actions != null) { | ||
// add mandatory fields (if missing): idempotent and safe | ||
@@ -274,7 +274,7 @@ for (const actName in copy.actions) { | ||
} | ||
if (copy.events && Object.keys(copy.events).length === 0) { | ||
if (copy.events != null && Object.keys(copy.events).length === 0) { | ||
delete copy.events; | ||
} | ||
if (copy.links && copy.links.length === 0) { | ||
if (copy?.links.length === 0) { | ||
delete copy.links; | ||
@@ -281,0 +281,0 @@ } |
@@ -70,3 +70,3 @@ /******************************************************************************** | ||
this.href = href; | ||
if (contentType) this.contentType = contentType; | ||
if (contentType != null) this.contentType = contentType; | ||
} | ||
@@ -73,0 +73,0 @@ } |
@@ -113,3 +113,3 @@ /******************************************************************************** | ||
const foundTmExtendsRel = data.links.find((link) => link.rel === "tm:extends"); | ||
if (foundTmExtendsRel) return true; | ||
if (foundTmExtendsRel != null) return true; | ||
} | ||
@@ -140,13 +140,5 @@ | ||
public static getModelVersion(data: ThingModel): string | undefined { | ||
if ( | ||
"version" in data && | ||
data.version && | ||
typeof data.version === "object" && | ||
"model" in data.version && | ||
typeof data.version.model === "string" | ||
) { | ||
return data.version.model; | ||
} else { | ||
return undefined; | ||
} | ||
return typeof data?.version === "object" && typeof data?.version?.model === "string" | ||
? data.version.model | ||
: undefined; | ||
} | ||
@@ -172,3 +164,3 @@ | ||
valid: isValid, | ||
errors: errors, | ||
errors, | ||
}; | ||
@@ -247,3 +239,3 @@ } | ||
http.get(uri, (res) => { | ||
if (!res.statusCode || res.statusCode !== 200) { | ||
if (res.statusCode == null || res.statusCode !== 200) { | ||
reject(new Error(`http status code not 200 but ${res.statusCode} for ${uri}`)); | ||
@@ -275,3 +267,3 @@ } | ||
.get(uri, (res) => { | ||
if (!res.statusCode || res.statusCode !== 200) { | ||
if (res.statusCode == null || res.statusCode !== 200) { | ||
reject(new Error(`https status code not 200 but ${res.statusCode} for ${uri}`)); | ||
@@ -351,3 +343,3 @@ } | ||
const refObj = this.parseTmRef(affUri); | ||
if (!refObj.uri) { | ||
if (refObj.uri == null) { | ||
throw new Error(`Missing remote path in ${affUri}`); | ||
@@ -385,3 +377,3 @@ } | ||
} | ||
if (!options.baseUrl) { | ||
if (options.baseUrl == null) { | ||
options.baseUrl = "."; | ||
@@ -415,3 +407,3 @@ } | ||
const sub = submodelObj[key]; | ||
if (options.selfComposition) { | ||
if (options.selfComposition === true) { | ||
if (!data.links) { | ||
@@ -426,3 +418,3 @@ throw new Error( | ||
const instanceName = el.instanceName; | ||
if (!instanceName) { | ||
if (instanceName == null) { | ||
throw new Error("Self composition is not possible without instance names"); | ||
@@ -461,3 +453,3 @@ } | ||
} | ||
if (!data.links || options.selfComposition) { | ||
if (!data.links || options.selfComposition === true) { | ||
data.links = []; | ||
@@ -488,3 +480,3 @@ } | ||
const refs = {} as Record<string, unknown>; | ||
if (!data) { | ||
if (data == null) { | ||
return refs; | ||
@@ -522,3 +514,3 @@ } | ||
for (const key in properties) { | ||
if (dest.properties && dest.properties[key]) { | ||
if (dest.properties && dest.properties[key] != null) { | ||
extendedModel.properties[key] = { ...properties[key], ...dest.properties[key] }; | ||
@@ -640,3 +632,3 @@ } else { | ||
let errors; | ||
if (keys && keys.length > 0 && (map === undefined || map === null)) { | ||
if (keys?.length > 0 && (map === undefined || map === null)) { | ||
isValid = false; | ||
@@ -656,3 +648,3 @@ errors = `No map provided for model ${model.title}`; | ||
valid: isValid, | ||
errors: errors, | ||
errors, | ||
}; | ||
@@ -677,3 +669,3 @@ } | ||
private removeDependency(dep?: string) { | ||
if (dep) { | ||
if (dep != null) { | ||
this.deps = this.deps.filter((el) => el !== dep); | ||
@@ -680,0 +672,0 @@ } else { |
@@ -24,2 +24,3 @@ /******************************************************************************** | ||
import { FormElementBase, PropertyElement } from "wot-thing-model-types"; | ||
import isAbsoluteUrl = require("is-absolute-url"); | ||
const namespace = "node-wot:td-tools:asset-interface-description-util"; | ||
@@ -39,7 +40,2 @@ const logDebug = debug(`${namespace}:debug`); | ||
export class AssetInterfaceDescriptionUtil { | ||
/** @deprecated use transformAAS2TD method instead */ | ||
public transformToTD(aid: string, template?: string, submodelRegex?: string): string { | ||
return this.transformAAS2TD(aid, template, submodelRegex); | ||
} | ||
/** | ||
@@ -137,3 +133,3 @@ * Transform AAS in JSON format to a WoT ThingDescription (TD) | ||
const aidID = td.id ? td.id : "ID" + Math.random(); | ||
const aidID = td.id ?? "ID" + Math.random(); | ||
@@ -193,2 +189,7 @@ logInfo("TD " + td.title + " parsed..."); | ||
/** @deprecated use transformAAS2TD method instead */ | ||
public transformToTD(aid: string, template?: string, submodelRegex?: string): string { | ||
return this.transformAAS2TD(aid, template, submodelRegex); | ||
} | ||
/* | ||
@@ -225,5 +226,5 @@ * PRIVATE IMPLEMENTATION METHODS ARE FOLLOWING | ||
private updateProtocolPrefixes(forms: [FormElementBase, ...FormElementBase[]], protocols: string[]): void { | ||
if (forms) { | ||
if (forms != null) { | ||
for (const interactionForm of forms) { | ||
if (interactionForm.href) { | ||
if (interactionForm.href != null) { | ||
const positionColon = interactionForm.href.indexOf(":"); | ||
@@ -242,3 +243,3 @@ if (positionColon > 0) { | ||
private getBaseFromEndpointMetadata(endpointMetadata?: Record<string, unknown>): string { | ||
if (endpointMetadata?.value && endpointMetadata.value instanceof Array) { | ||
if (endpointMetadata?.value instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
@@ -255,3 +256,3 @@ if (v.idShort === "base") { | ||
private getContentTypeFromEndpointMetadata(endpointMetadata?: Record<string, unknown>): string { | ||
if (endpointMetadata?.value && endpointMetadata.value instanceof Array) { | ||
if (endpointMetadata?.value instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
@@ -274,9 +275,9 @@ if (v.idShort === "contentType") { | ||
if (endpointMetadata?.value && endpointMetadata.value instanceof Array) { | ||
if (endpointMetadata?.value instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
if (v.idShort === "securityDefinitions") { | ||
// const securitySchemes: Array<SecurityScheme> = []; | ||
if (v.value && v.value instanceof Array) { | ||
if (v.value instanceof Array) { | ||
for (const securityDefinitionsValues of v.value) { | ||
if (securityDefinitionsValues.idShort) { | ||
if (securityDefinitionsValues.idShort != null) { | ||
// key | ||
@@ -286,3 +287,3 @@ if (securityDefinitionsValues.value instanceof Array) { | ||
if (securityDefinitionsValue.idShort === "scheme") { | ||
if (securityDefinitionsValue.value) { | ||
if (securityDefinitionsValue.value != null) { | ||
const ss: SecurityScheme = { scheme: securityDefinitionsValue.value }; | ||
@@ -307,8 +308,8 @@ securityDefinitions[securityDefinitionsValues.idShort] = ss; | ||
const security: string[] = []; | ||
if (endpointMetadata?.value && endpointMetadata.value instanceof Array) { | ||
if (endpointMetadata?.value instanceof Array) { | ||
for (const v of endpointMetadata.value) { | ||
if (v.idShort === "security") { | ||
if (v.value && v.value instanceof Array) { | ||
if (v.value instanceof Array) { | ||
for (const securityValue of v.value) { | ||
if (securityValue.value) { | ||
if (securityValue.value != null) { | ||
security.push(securityValue.value); | ||
@@ -352,3 +353,5 @@ } | ||
if (v.idShort === "href") { | ||
if (form.href && form.href.length > 0) { | ||
if (v.value != null && isAbsoluteUrl(v.value)) { | ||
form.href = v.value; | ||
} else if (form.href && form.href.length > 0) { | ||
form.href = form.href + v.value; // TODO handle leading/trailing slashes | ||
@@ -359,14 +362,14 @@ } else { | ||
} else if (typeof v.idShort === "string" && v.idShort.length > 0) { | ||
// TODO is this still relevant? | ||
// pick *any* value (and possibly override, e.g, contentType) | ||
// TODO Should we add all value's (e.g., dataMapping might be empty array) ? | ||
// if (typeof v.value === "string" ||typeof v.value === "number" || typeof v.value === "boolean") { | ||
if (v.value) { | ||
form[v.idShort] = v.value; | ||
// pick *any* value (and possibly override, e.g. contentType) | ||
if (v.value != null) { | ||
// Note: AID does not allow idShort to contain values with colon (i.e., ":") --> "_" used instead | ||
// --> THIS MAY LEAD TO PROBLEMS BUT THAT'S HOW IT IS SPECIFIED | ||
const tdTerm = (v.idShort as string).replace("_", ":"); | ||
form[tdTerm] = v.value; | ||
// use valueType to convert the string value | ||
// TODO Should we add/support all value's (e.g., dataMapping might be empty array) ? | ||
if ( | ||
v.valueType && | ||
v.valueType && | ||
v.valueType.dataObjectType && | ||
v.valueType.dataObjectType.name && | ||
v.valueType != null && | ||
v.valueType.dataObjectType != null && | ||
v.valueType.dataObjectType.name != null && | ||
typeof v.valueType.dataObjectType.name === "string" | ||
@@ -377,3 +380,3 @@ ) { | ||
case "boolean": | ||
form[v.idShort] = form[v.idShort] === "true"; | ||
form[tdTerm] = form[v.value] === "true"; | ||
break; | ||
@@ -396,3 +399,3 @@ case "float": | ||
case "positiveInteger": | ||
form[v.idShort] = Number(form[v.idShort]); | ||
form[tdTerm] = Number(form[v.value]); | ||
break; | ||
@@ -417,8 +420,12 @@ // TODO handle more XSD types ? | ||
): void { | ||
if (submodel instanceof Object && submodel.idShort && submodel.idShort === "AssetInterfacesDescription") { | ||
if (submodel.submodelElements && submodel.submodelElements instanceof Array) { | ||
if ( | ||
submodel instanceof Object && | ||
submodel.idShort != null && | ||
submodel.idShort === "AssetInterfacesDescription" | ||
) { | ||
if (submodel.submodelElements instanceof Array) { | ||
for (const submodelElement of submodel.submodelElements) { | ||
if (submodelElement instanceof Object) { | ||
logDebug("SubmodelElement.idShort: " + submodelElement.idShort); | ||
if (submodelRegex && typeof submodelRegex === "string" && submodelRegex.length > 0) { | ||
if (typeof submodelRegex === "string" && submodelRegex.length > 0) { | ||
const regex = new RegExp(submodelRegex); | ||
@@ -440,3 +447,3 @@ if (!regex.test(submodelElement.idShort)) { | ||
// EndpointMetadata vs. InterfaceMetadata | ||
if (submodelElement.value && submodelElement.value instanceof Array) { | ||
if (submodelElement.value instanceof Array) { | ||
// Note: iterate twice over to collect first EndpointMetadata | ||
@@ -465,5 +472,5 @@ let endpointMetadata: Record<string, unknown> = {}; | ||
logInfo("InterfaceMetadata"); | ||
if (smValue.value && smValue.value instanceof Array) { | ||
if (smValue.value instanceof Array) { | ||
for (const interactionValue of smValue.value) { | ||
if (interactionValue.idShort === "Properties") { | ||
if (interactionValue.idShort === "properties") { | ||
if (interactionValue.value instanceof Array) { | ||
@@ -476,3 +483,3 @@ for (const iValue of interactionValue.value) { | ||
const propInter: AASInteraction = { | ||
endpointMetadata: endpointMetadata, | ||
endpointMetadata, | ||
interaction: iValue, | ||
@@ -483,3 +490,3 @@ }; | ||
} | ||
} else if (interactionValue.idShort === "Operations") { | ||
} else if (interactionValue.idShort === "actions") { | ||
if (interactionValue.value instanceof Array) { | ||
@@ -492,3 +499,3 @@ for (const iValue of interactionValue.value) { | ||
const actInter: AASInteraction = { | ||
endpointMetadata: endpointMetadata, | ||
endpointMetadata, | ||
interaction: iValue, | ||
@@ -499,3 +506,3 @@ }; | ||
} | ||
} else if (interactionValue.idShort === "Events") { | ||
} else if (interactionValue.idShort === "events") { | ||
if (interactionValue.value instanceof Array) { | ||
@@ -508,3 +515,3 @@ for (const iValue of interactionValue.value) { | ||
const evInter: AASInteraction = { | ||
endpointMetadata: endpointMetadata, | ||
endpointMetadata, | ||
interaction: iValue, | ||
@@ -535,3 +542,3 @@ }; | ||
if (aidModel instanceof Object && aidModel.submodels) { | ||
if (aidModel instanceof Object && aidModel.submodels != null) { | ||
if (aidModel.submodels instanceof Array) { | ||
@@ -548,3 +555,3 @@ for (const submodel of aidModel.submodels) { | ||
private _transform(smInformation: SubmodelInformation, template?: string): string { | ||
const thing: Thing = template ? JSON.parse(template) : {}; | ||
const thing: Thing = template != null ? JSON.parse(template) : {}; | ||
@@ -561,3 +568,3 @@ // walk over thing information and set them | ||
// required TD fields | ||
if (!thing["@context"]) { | ||
if (thing["@context"] == null) { | ||
thing["@context"] = "https://www.w3.org/2022/wot/td/v1.1"; | ||
@@ -572,3 +579,3 @@ } | ||
// TODO: possible collisions for "security" names *could* be handled by cnt | ||
if (!thing.securityDefinitions) { | ||
if (thing.securityDefinitions == null) { | ||
thing.securityDefinitions = {}; | ||
@@ -624,3 +631,3 @@ } | ||
for (const aasDescriptionEntry of aasDescription) { | ||
if (aasDescriptionEntry.language && aasDescriptionEntry.text) { | ||
if (aasDescriptionEntry.language != null && aasDescriptionEntry.text != null) { | ||
const language: string = aasDescriptionEntry.language; | ||
@@ -643,6 +650,6 @@ const text: string = aasDescriptionEntry.text; | ||
} else if (interactionValue.idShort === "range") { | ||
if (interactionValue.min) { | ||
if (interactionValue.min != null) { | ||
thing.properties[key].min = interactionValue.min; | ||
} | ||
if (interactionValue.max) { | ||
if (interactionValue.max != null) { | ||
thing.properties[key].max = interactionValue.max; | ||
@@ -725,3 +732,3 @@ } | ||
// base ? | ||
if (td.base) { | ||
if (td.base != null) { | ||
values.push({ | ||
@@ -747,3 +754,3 @@ idShort: "base", | ||
const securityValues: Array<unknown> = []; | ||
if (td.security) { | ||
if (td.security != null) { | ||
for (const secKey of td.security) { | ||
@@ -812,3 +819,3 @@ securityValues.push({ | ||
for (const formElementProperty of propertyValue.forms) { | ||
if (formElementProperty.href?.startsWith(protocol)) { | ||
if (formElementProperty.href != null && formElementProperty.href.startsWith(protocol)) { | ||
formElementPicked = formElementProperty; | ||
@@ -827,3 +834,3 @@ // found matching form --> abort loop | ||
// type | ||
if (propertyValue.type) { | ||
if (propertyValue.type != null) { | ||
propertyValues.push({ | ||
@@ -837,3 +844,3 @@ idShort: "type", | ||
// title | ||
if (propertyValue.title) { | ||
if (propertyValue.title != null) { | ||
propertyValues.push({ | ||
@@ -847,3 +854,3 @@ idShort: "title", | ||
// observable | ||
if (propertyValue.observable) { | ||
if (propertyValue.observable != null) { | ||
propertyValues.push({ | ||
@@ -860,3 +867,3 @@ idShort: "observable", | ||
// forms | ||
if (formElementPicked) { | ||
if (formElementPicked != null) { | ||
const propertyForm: Array<unknown> = []; | ||
@@ -868,4 +875,9 @@ | ||
// walk over string values like: "href", "contentType", "htv:methodName", ... | ||
for (const formTerm in formElementPicked) { | ||
for (let formTerm in formElementPicked) { | ||
const formValue = formElementPicked[formTerm]; | ||
// Note: AID does not allow idShort to contain values with colon (i.e., ":") --> "_" used instead | ||
// TODO are there more characters we need to deal with? | ||
formTerm = formTerm.replace(":", "_"); | ||
if (typeof formValue === "string") { | ||
@@ -900,3 +912,3 @@ propertyForm.push({ | ||
} | ||
} else if (propertyValue.description) { | ||
} else if (propertyValue.description != null) { | ||
// fallback | ||
@@ -912,3 +924,3 @@ description = []; | ||
idShort: propertyKey, | ||
description: description, | ||
description, | ||
value: propertyValues, | ||
@@ -933,3 +945,3 @@ modelType: "SubmodelElementCollection", | ||
values.push({ | ||
idShort: "Properties", | ||
idShort: "properties", | ||
value: properties, | ||
@@ -940,3 +952,3 @@ modelType: "SubmodelElementCollection", | ||
values.push({ | ||
idShort: "Actions", | ||
idShort: "actions", | ||
value: actions, | ||
@@ -947,3 +959,3 @@ modelType: "SubmodelElementCollection", | ||
values.push({ | ||
idShort: "Events", | ||
idShort: "events", | ||
value: events, | ||
@@ -950,0 +962,0 @@ modelType: "SubmodelElementCollection", |
@@ -17,5 +17,6 @@ # Thing Description Utilities | ||
The file `counterHTTP.json` describes the counter sample in AID format for http binding. The `AssetInterfaceDescriptionUtil` utility class allows to transform the AID format to a valid WoT TD format which in the end can be properly consumed by node-wot. | ||
The file `counterHTTP.json` describes the counter sample in AAS/AID format for http binding. The `AssetInterfaceDescriptionUtil` utility class allows to transform the AID format to a valid WoT TD format which in the end can be properly consumed by node-wot. | ||
The example tries to load an AID file in AID format and transforms it to a regular WoT TD. | ||
The example `aid-to-td.js` tries to transform an AID submodel (from an AAS file) into a regular WoT TD. | ||
Note: Besides converting the AID submodel it is also possible to convert a full AAS file (see `transformTD2AAS(...)`). | ||
@@ -43,6 +44,9 @@ ```js | ||
}); | ||
// pick AID submodel | ||
const aid = JSON.stringify(JSON.parse(aas).submodels[0]); | ||
// transform AID to WoT TD | ||
let tdAID = assetInterfaceDescriptionUtil.transformAAS2TD(aas, `{"title": "counter"}`); | ||
// Note: transformAAS2TD() may have up to 3 input parameters | ||
// * aas (required): AAS in JSON format | ||
const tdAID = assetInterfaceDescriptionUtil.transformSM2TD(aid, `{"title": "counter"}`); | ||
// Note: transformSM2TD() may have up to 3 input parameters | ||
// * aid (required): AID submodel in JSON format | ||
// * template (optional): Initial TD template | ||
@@ -70,3 +74,4 @@ // * submodelRegex (optional): Submodel filter based on regular expression | ||
The example tries to load online counter TD and converts it to AAS JSON format. | ||
The example `td-to-aid.js` tries to load the online counter TD and converts it to an AID submodel in JSON format. | ||
Note: Besides converting it into an AID submodel it is also possible to convert it into a full AAS form (see `transformTD2AAS(...)`). | ||
@@ -84,5 +89,5 @@ ```js | ||
const sm = assetInterfaceDescriptionUtil.transformTD2AAS(JSON.stringify(counterTD), ["http", "coap"]); | ||
const sm = assetInterfaceDescriptionUtil.transformTD2SM(JSON.stringify(counterTD), ["http", "coap"]); | ||
// print JSON format of AAS | ||
// print JSON format of AID submodel | ||
console.log(sm); | ||
@@ -105,2 +110,2 @@ } catch (err) { | ||
`node td-to-aid.js` | ||
... will show the online counter im AAS/AID JSON format (usable by AASX Package Explorer 2023-08-01.alpha). | ||
... will show the online counter im AAS/AID JSON format (compliant with AAS V3.0 and can be imported by AASX Package Explorer). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
232983
0
3940
+ Addedwot-thing-description-types@1.1.0-05-July-2023(transitive)
+ Addedwot-thing-model-types@1.1.0-05-July-2023(transitive)
+ Addedwot-typescript-definitions@0.8.0-SNAPSHOT.27(transitive)
- Removedwot-thing-description-types@1.1.0-24-May-2023(transitive)
- Removedwot-thing-model-types@1.1.0-24-May-2023(transitive)
- Removedwot-typescript-definitions@0.8.0-SNAPSHOT.26(transitive)