Skip to content

Open api async api ux support #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 18, 2022
Merged
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
4 changes: 3 additions & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,6 @@ The core validation report is an object, which contains three objects (as you ca

## Test Usage

Test Examples are located under examples directory. For typo examples you need to fill 'typoCount' field to that corresponds to the number of typos existing in the TD.
Test Examples are located under examples directory.
For typo examples you need to fill 'typoCount' field that corresponds to the number of typos existing in the TD.
For protocol detection examples you need to fill 'protocolSchemes' field that corresponds to the protocols TD uses.
135 changes: 130 additions & 5 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const apply = require('ajv-formats-draft2019')
const lzs = require('lz-string')
const jsYaml = require('js-yaml')

const coreAssertions = require("./shared")
const tdSchema = require("./td-schema.json")
Expand All @@ -20,6 +21,9 @@ module.exports.compress = compress
module.exports.decompress = decompress
module.exports.checkTypos = checkTypos
module.exports.checkTmOptionalPointer = coreAssertions.checkTmOptionalPointer
module.exports.detectProtocolSchemes = detectProtocolSchemes
module.exports.convertTDJsonToYaml = convertTDJsonToYaml
module.exports.convertTDYamlToJson = convertTDYamlToJson

const jsonValidator = require('json-dup-key-validator')

Expand Down Expand Up @@ -979,7 +983,7 @@ const TYPO_LOOKUP_TABLE = createSchemaLookupTable(tdSchema)
/**
* Checks possible typos in a TD
* @param {object} td The TD to apply typo check on
* @returns List of possible typos where the typo consists of string value of typo itself and the message, another string value, to be prompted to the user for the fix
* @returns List of possible typos where the typo consists of string value of typo itself and the message, another string value, to be prompted to the user for the fix
*/
function checkTypos(td) {
const typos = []
Expand All @@ -992,7 +996,7 @@ const TYPO_LOOKUP_TABLE = createSchemaLookupTable(tdSchema)
try {
tdJson = JSON.parse(td)
} catch(err) {
console.log("Error occurred while parsing JSON!")
console.log("checkTypos: Error occurred while parsing JSON!")
}

searchTypos(typos, tdJson, lookupTable, searchDepth, searchPath)
Expand Down Expand Up @@ -1168,7 +1172,7 @@ function findPathsInSchema(lookupTable, schema, path) {
* Stores the keys under a specific path
* @param {Map} lookupTable The map that stores the paths in the schema
* @param {string} path The path that is owner of the current keys
* @param {Set} keys The set of keys that is going to be put
* @param {Set} keys The set of keys that is going to be put
*/
function putKeysToPath(lookupTable, path, keys) {
pathKeys = lookupTable.get(path)
Expand All @@ -1188,7 +1192,7 @@ function putKeysToPath(lookupTable, path, keys) {
/**
* Gets the reference object in the schema
* @param {object} schema The object that represent the schema
* @param {string} ref The reference value in the schema
* @param {string} ref The reference value in the schema
* @returns The reference object the ref maps to
*/
function getRefObjectOfSchema(schema, ref) {
Expand Down Expand Up @@ -1301,4 +1305,125 @@ function calculateSimilarity(actual, desired) {
}

return similarity
}
}

/**
* 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) {
console.log("detectProtocolSchemes: Error occurred while parsing JSON!")
}

if (!tdJson) {
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
* @returns TD in yaml string form
*/
function convertTDJsonToYaml(td) {
if (td === "") {
return
}

try {
return jsYaml.dump(JSON.parse(td))
} catch(err) {
console.log("TD generation problem: " + err)
}
}

/**
* Convert TD from json to yaml
* @param {string} td TD in yaml string from
* @returns TD in json string form
*/
function convertTDYamlToJson(td) {
if (td === "") {
return
}

try {
return JSON.stringify(jsYaml.load(td))
} catch (err) {
console.log("TD generation problem: " + err)
}
}
Loading