Skip to content

Commit

Permalink
Merge pull request #590 from hasanheroglu/589-replace-protocol-detection
Browse files Browse the repository at this point in the history
refactor: replace deprecated detectProtocolSchemes
  • Loading branch information
egekorkan committed Aug 1, 2024
2 parents a77021b + 9d8e5d1 commit 8b539d8
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 283 deletions.
86 changes: 0 additions & 86 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ module.exports.decompress = decompress;
module.exports.checkTmOptionalPointer = coreAssertions.checkTmOptionalPointer;
module.exports.checkLinkedAffordances = coreAssertions.checkLinkedAffordances;
module.exports.checkLinkedStructure = coreAssertions.checkLinkedStructure;
module.exports.detectProtocolSchemes = detectProtocolSchemes;
module.exports.convertTDJsonToYaml = convertTDJsonToYaml;
module.exports.convertTDYamlToJson = convertTDYamlToJson;

Expand Down Expand Up @@ -1297,91 +1296,6 @@ function decompress(data) {
return lzs.decompressFromEncodedURIComponent(data);
}

/**
* Detect protocl schemes of a TD
* @param {string} td TD string to detect protocols of
* return List of available protocol schemes
*/
function detectProtocolSchemes(td) {
let tdJson;

try {
tdJson = JSON.parse(td);
} catch (err) {
return [];
}

const baseUriProtocol = getHrefProtocol(tdJson.base);
const thingProtocols = detectProtocolInForms(tdJson.forms);
const actionsProtocols = detectProtocolInAffordance(tdJson.actions);
const eventsProtocols = detectProtocolInAffordance(tdJson.events);
const propertiesProtcols = detectProtocolInAffordance(tdJson.properties);
const protocolSchemes = [
...new Set([
baseUriProtocol,
...thingProtocols,
...actionsProtocols,
...eventsProtocols,
...propertiesProtcols,
]),
].filter((p) => p !== undefined);

return protocolSchemes;
}

/**
* Detect protocols in a TD affordance
* @param {object} affordance That belongs to a TD
* @returns List of protocol schemes
*/
function detectProtocolInAffordance(affordance) {
if (!affordance) {
return [];
}

let protocolSchemes = [];

for (const key in affordance) {
if (key) {
protocolSchemes = protocolSchemes.concat(detectProtocolInForms(affordance[key].forms));
}
}

return protocolSchemes;
}

/**
* Detect protocols in a TD forms or a TD affordance forms
* @param {object} forms Forms field of a TD or a TD affordance
* @returns List of protocol schemes
*/
function detectProtocolInForms(forms) {
if (!forms) {
return [];
}

const protocolSchemes = [];

forms.forEach((form) => {
protocolSchemes.push(getHrefProtocol(form.href));
});

return protocolSchemes;
}

/**
* Get protocol used in href
* @param {string} href URI string
* @returns Protocol name
*/
function getHrefProtocol(href) {
if (!href) {
return;
}

return href.split(":")[0];
}

/**
* Convert TD from json to yaml
* @param {string} td TD in json string form
Expand Down
69 changes: 0 additions & 69 deletions packages/core/tests/protocol-detection.test.js

This file was deleted.

22 changes: 0 additions & 22 deletions packages/core/tests/protocol-detection/httpAndMqtt.json

This file was deleted.

14 changes: 0 additions & 14 deletions packages/core/tests/protocol-detection/noProtocol.json

This file was deleted.

33 changes: 0 additions & 33 deletions packages/core/tests/protocol-detection/onlyHttp.json

This file was deleted.

33 changes: 0 additions & 33 deletions packages/core/tests/protocol-detection/onlyMqtt.json

This file was deleted.

23 changes: 0 additions & 23 deletions packages/core/tests/protocol-detection/secureProtocols.json

This file was deleted.

6 changes: 6 additions & 0 deletions packages/web/package-lock.json

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

1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@thingweb/async-api-converter": "^1.0.0",
"@thingweb/json-spell-checker": "^1.0.0",
"@thingweb/open-api-converter": "^1.0.0",
"@thingweb/td-utils": "^1.0.0",
"d3": "^3.5.17",
"d3-tip": "^0.6.7",
"express": "^4.19.2",
Expand Down
7 changes: 4 additions & 3 deletions packages/web/src/scripts/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { AASView } from './aas'
import { defaultsView, defaultsJsonBtn, defaultsYamlBtn, defaultsAddBtn } from './defaults'
import { visualize } from './visualize'
import { validationView, validationTab } from './validation'
import { convertTDYamlToJson, detectProtocolSchemes } from '../../../core/dist/web-bundle.min.js'
import { convertTDYamlToJson } from '../../../core/dist/web-bundle.min.js'
import { detectProtocolSchemes } from '@thingweb/td-utils/dist/web-bundle.min.js'
import { generateOAP, generateAAP, addDefaultsUtil, validate, generateAAS, resetValidationStatus } from './util'
import { editorList, getEditorData } from './editor'
import { textIcon } from './main.js'
Expand Down Expand Up @@ -239,7 +240,7 @@ function enableAPIConversionWithProtocol(editorInstance) {
if (protocolSchemes) {

if (openApiTab.checked === true) {
if (["http", "https"].some(p => protocolSchemes.includes(p))) {
if (["http", "https"].some(p => Object.keys(protocolSchemes).includes(p))) {
generateOAP(editorInstance["_domElement"].dataset.modeId, editorInstance)
openApiView.classList.remove("hidden")
} else {
Expand All @@ -248,7 +249,7 @@ function enableAPIConversionWithProtocol(editorInstance) {
}

if (asyncApiTab.checked === true) {
if (["mqtt", "mqtts"].some(p => protocolSchemes.includes(p))) {
if (["mqtt", "mqtts"].some(p => Object.keys(protocolSchemes).includes(p))) {
generateAAP(editorInstance["_domElement"].dataset.modeId, editorInstance)
asyncApiView.classList.remove("hidden")
} else {
Expand Down

0 comments on commit 8b539d8

Please sign in to comment.