@api-components/amf-helper-mixin
Advanced tools
Comparing version 4.2.0 to 4.3.0
{ | ||
"name": "@api-components/amf-helper-mixin", | ||
"description": "A mixin with common functions user by most AMF components to compyte AMF values", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"license": "Apache-2.0", | ||
@@ -31,3 +31,2 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@advanced-rest-client/testing-karma-sl": "^1.4.3", | ||
"@api-components/api-model-generator": "^0.2.8", | ||
@@ -37,2 +36,4 @@ "@open-wc/eslint-config": "^2.0.0", | ||
"@open-wc/testing-karma": "^3.4.0", | ||
"@web/test-runner": "^0.9.12", | ||
"@web/test-runner-playwright": "^0.6.6", | ||
"deepmerge": "^4.2.2", | ||
@@ -60,5 +61,4 @@ "es-dev-server": "^1.53.0", | ||
"format": "npm run format:eslint && npm run format:prettier", | ||
"test": "karma start --coverage", | ||
"test:watch": "karma start --auto-watch=true --single-run=false", | ||
"test:sl": "karma start karma.sl.config.js --compatibility auto --coverage", | ||
"test": "web-test-runner test/**/*.test.js --coverage --node-resolve --playwright --browsers chromium firefox webkit", | ||
"test:watch": "web-test-runner test/**/*.test.js --node-resolve --watch", | ||
"prepare": "node test/model.js" | ||
@@ -65,0 +65,0 @@ }, |
@@ -497,2 +497,8 @@ import { Namespace } from './Namespace'; | ||
_resolveRecursive(shape: object): void; | ||
_mergeShapes(shapeA: object, shapeB: object): object; | ||
_mergeSourceMapsSources(shapeA: object, shapeB: object): object[]; | ||
_computeApi(model: object): object | undefined; | ||
_isWebAPI(model: object): boolean; | ||
_isAsyncAPI(model: object): boolean; | ||
_isAPI(model: object): boolean; | ||
} |
@@ -451,3 +451,3 @@ /** | ||
_computeApiVersion(amf) { | ||
const api = this._computeWebApi(amf); | ||
const api = this._computeApi(amf); | ||
if (!api) { | ||
@@ -543,2 +543,61 @@ return undefined; | ||
/** | ||
* Computes AMF's `http://schema.org/API` model | ||
* | ||
* @param {Array<Object>|Object} model AMF json/ld model for an API | ||
* @return {Object} API declaration. | ||
*/ | ||
_computeApi(model) { | ||
const enc = this._computeEncodes(model); | ||
if (!enc) { | ||
return undefined; | ||
} | ||
if (this._hasType(enc, this.ns.aml.vocabularies.apiContract.API)) { | ||
return enc; | ||
} | ||
return undefined; | ||
} | ||
/** | ||
* Returns whether an AMF node is a WebAPI node | ||
* | ||
* @param {Array<Object>|Object} model AMF json/ld model for an API | ||
* @return {Boolean} | ||
*/ | ||
_isWebAPI(model) { | ||
const enc = this._computeEncodes(model); | ||
if (!enc) { | ||
return false; | ||
} | ||
return this._hasType(enc, this.ns.aml.vocabularies.apiContract.WebAPI); | ||
} | ||
/** | ||
* Returns whether an AMF node is an AsyncAPI node | ||
* | ||
* @param {Array<Object>|Object} model AMF json/ld model for an API | ||
* @return {Boolean} | ||
*/ | ||
_isAsyncAPI(model) { | ||
const enc = this._computeEncodes(model); | ||
if (!enc) { | ||
return false; | ||
} | ||
return this._hasType(enc, this.ns.aml.vocabularies.apiContract.AsyncAPI); | ||
} | ||
/** | ||
* Returns whether an AMF node is an API node | ||
* | ||
* @param {Array<Object>|Object} model AMF json/ld model for an API | ||
* @return {Boolean} | ||
*/ | ||
_isAPI(model) { | ||
const enc = this._computeEncodes(model); | ||
if (!enc) { | ||
return false; | ||
} | ||
return this._hasType(enc, this.ns.aml.vocabularies.apiContract.API); | ||
} | ||
/** | ||
* Computes value for `server` property that is later used with other computations. | ||
@@ -550,3 +609,3 @@ * | ||
_computeServer(model) { | ||
const api = this._computeWebApi(model); | ||
const api = this._computeApi(model); | ||
if (!api) { | ||
@@ -601,3 +660,3 @@ return undefined; | ||
} | ||
let api = this._computeWebApi(amf); | ||
let api = this._computeApi(amf); | ||
if (Array.isArray(api)) { | ||
@@ -718,4 +777,4 @@ [api] = api; | ||
* Appends endpoint's path to url | ||
* @param {string} url | ||
* @param {Object} endpoint | ||
* @param {string} url | ||
* @param {Object} endpoint | ||
* @return {string} | ||
@@ -799,3 +858,3 @@ */ | ||
_computeProtocols(model) { | ||
const api = this._computeWebApi(model); | ||
const api = this._computeApi(model); | ||
if (!api) { | ||
@@ -1136,3 +1195,3 @@ return undefined; | ||
} | ||
Object.assign(shape, copy); | ||
this._mergeShapes(shape, copy); | ||
/* eslint-disable-next-line no-param-reassign */ | ||
@@ -1249,2 +1308,47 @@ shape.__apicResolved = true; | ||
} | ||
/** | ||
* Merge two shapes together. If the resulting shape has one of the "special merge" keys, | ||
* then the special merge function for that key will be used to match that property | ||
* @param shapeA AMF node | ||
* @param shapeB AMF node | ||
* @return {*} Merged AMF node | ||
* @private | ||
*/ | ||
_mergeShapes(shapeA, shapeB) { | ||
const merged = { ...shapeA, ...shapeB }; | ||
const specialMerges = [ | ||
{ key: this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.sources), merger: this._mergeSourceMapsSources.bind(this) }, | ||
]; | ||
specialMerges.forEach(({ key, merger }) => { | ||
if (this._hasProperty(merged, key)) { | ||
merged[key] = merger(shapeA, shapeB); | ||
} | ||
}); | ||
return Object.assign(shapeA, merged); | ||
} | ||
/** | ||
* Obtains source map sources value from two shapes and returns the merged result | ||
* If neither shape has a sources node, then an empty object will be returned. | ||
* Result is wrapped in an array as per AMF model standard | ||
* @param shapeA AMF node | ||
* @param shapeB AMF node | ||
* @return {(*|{})[]} Empty object or resulting merge, wrapped in an array | ||
* @private | ||
*/ | ||
_mergeSourceMapsSources(shapeA, shapeB) { | ||
const sourcesKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.sources); | ||
let aSources = shapeA[sourcesKey] || {}; | ||
if (Array.isArray(aSources)) { | ||
/* eslint-disable prefer-destructuring */ | ||
aSources = aSources[0]; | ||
} | ||
let bSources = shapeB[sourcesKey] || {}; | ||
if (Array.isArray(bSources)) { | ||
/* eslint-disable prefer-destructuring */ | ||
bSources = bSources[0]; | ||
} | ||
return [Object.assign(aSources, bSources)]; | ||
} | ||
}; |
@@ -85,2 +85,4 @@ interface Document { | ||
WebAPI: string; | ||
AsyncAPI: string; | ||
API: string; | ||
UserDocumentationFragment: string; | ||
@@ -87,0 +89,0 @@ Example: string; |
@@ -101,2 +101,4 @@ | ||
ns.aml.vocabularies.apiContract.WebAPI = `${contractKey}WebAPI`; | ||
ns.aml.vocabularies.apiContract.API = `${contractKey}API`; | ||
ns.aml.vocabularies.apiContract.AsyncAPI = `${contractKey}AsyncAPI`; | ||
ns.aml.vocabularies.apiContract.UserDocumentationFragment = `${contractKey}UserDocumentationFragment`; | ||
@@ -103,0 +105,0 @@ ns.aml.vocabularies.apiContract.Example = `${contractKey}Example`; |
Sorry, the diff of this file is too big to display
287069
13
2371
18