diff --git a/API.md b/API.md index 7c8cdad..1349d76 100644 --- a/API.md +++ b/API.md @@ -18,6 +18,8 @@
ExternalComponents
Array.<Object>
string
\| object
| base object whose prperties will be retained. |
| options.parser | Object
| asyncapi parser object |
| options.validate | boolean
| pass false to not validate file before merge |
+| options.referenceIntoComponents | boolean
| pass true value to resolve references into component |
**Example**
```js
@@ -123,3 +126,14 @@ This function checks for external reference.
| parsedJSON | Array.<Object>
|
| $refs | $RefParser
|
+
+
+## resolve(asyncapiDocuments, options) ⇒ Array.<Object>
+**Kind**: global function
+
+| Param | Type |
+| --- | --- |
+| asyncapiDocuments | Object
|
+| options | Object
|
+| options.referenceIntoComponents | boolean
|
+
diff --git a/example/bundle.js b/example/bundle.js
index f356323..14c9a12 100644
--- a/example/bundle.js
+++ b/example/bundle.js
@@ -1,10 +1,10 @@
const bundle = require('@asyncapi/bundler');
const fs = require('fs');
-async function main(){
- const document = await bundle([
- fs.readFileSync('./main.yaml', 'utf-8')
- ]);
+async function main() {
+ const document = await bundle([fs.readFileSync('./main.yaml', 'utf-8')], {
+ referenceIntoComponents: true
+ });
fs.writeFileSync('asyncapi.yaml', document.yml());
}
diff --git a/lib/index.js b/lib/index.js
index a3e3018..82cac93 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -8,6 +8,7 @@ const Document = require("./document");
* @param {string | object} options.base base object whose prperties will be retained.
* @param {Object} options.parser asyncapi parser object
* @param {boolean} options.validate pass false to not validate file before merge
+ * @param {boolean} options.referenceIntoComponents pass true value to resolve references into component
*
* @return {Document}
*
@@ -32,7 +33,7 @@ const bundle = async (files, options = {}) => {
/**
* Bundle all external references for each files.
*/
- const resolvedJsons = await resolve(parsedJsons);
+ const resolvedJsons = await resolve(parsedJsons, {referenceIntoComponents: options.referenceIntoComponents});
return new Document(resolvedJsons, options.base);
};
diff --git a/lib/parser.js b/lib/parser.js
index 485eb9a..49a42a5 100644
--- a/lib/parser.js
+++ b/lib/parser.js
@@ -10,9 +10,9 @@ class ExternalComponents {
this.resolvedJSON = resolvedJSON;
}
- getKey(){
+ getKey() {
const keys = this.ref.split('/');
- return keys[keys.length -1 ]
+ return keys[keys.length - 1]
}
getValue() {
@@ -30,7 +30,11 @@ async function parse(JSONSchema) {
for (let ref of refs) {
if (isExternalReference(ref)) {
const componentObject = await resolveExternalRefs(JSONSchema, $ref);
- merge(JSONSchema.components, componentObject);
+ if (JSONSchema.components) {
+ merge(JSONSchema.components, componentObject);
+ } else {
+ JSONSchema.components = componentObject
+ }
}
}
}
@@ -59,7 +63,7 @@ module.exports = {
* @returns {ExternalComponents}
*/
async function resolveExternalRefs(parsedJSON, $refs) {
- const componentObj = {messages: {}};
+ const componentObj = { messages: {} };
JSONPath({ json: parsedJSON, resultType: 'all', path: '$.channels.*.*.message' }).forEach(({ parent, parentProperty }) => {
const ref = parent[parentProperty]['$ref'];
if (isExternalReference(ref)) {
diff --git a/lib/util.js b/lib/util.js
index d58c0ea..1981200 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -1,6 +1,6 @@
const { ParserError } = require("./errors");
const $RefParser = require('@apidevtools/json-schema-ref-parser');
-const {parse} = require('./parser');
+const { parse } = require('./parser');
const yaml = require("js-yaml");
const _ = require('lodash');
@@ -16,10 +16,7 @@ exports.toJS = (asyncapiYAMLorJSON) => {
asyncapiYAMLorJSON.constructor &&
asyncapiYAMLorJSON.constructor.name === "Object"
) {
- return {
- initialFormat: "js",
- parsedJSON: asyncapiYAMLorJSON,
- };
+ return asyncapiYAMLorJSON
}
if (typeof asyncapiYAMLorJSON !== "string") {
@@ -44,12 +41,21 @@ exports.validate = async (parsedJSONs, parser) => {
}
};
-exports.resolve = async (asyncapiDocuments, options = {}) => {
-
+/**
+ *
+ * @param {Object} asyncapiDocuments
+ * @param {Object} options
+ * @param {boolean} options.referenceIntoComponents
+ * @returns {Array