Skip to content

Commit

Permalink
feat: add option for referencing to component
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns committed Aug 4, 2022
1 parent 17c2e86 commit 175d9f0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
14 changes: 14 additions & 0 deletions API.md
Expand Up @@ -18,6 +18,8 @@
</dd>
<dt><a href="#resolveExternalRefs">resolveExternalRefs(parsedJSON, $refs)</a> ⇒ <code>ExternalComponents</code></dt>
<dd></dd>
<dt><a href="#resolve">resolve(asyncapiDocuments, options)</a> ⇒ <code>Array.&lt;Object&gt;</code></dt>
<dd></dd>
</dl>

<a name="Document"></a>
Expand Down Expand Up @@ -72,6 +74,7 @@ console.log(document.string()); // get json string
| options.base | <code>string</code> \| <code>object</code> | base object whose prperties will be retained. |
| options.parser | <code>Object</code> | asyncapi parser object |
| options.validate | <code>boolean</code> | pass false to not validate file before merge |
| options.referenceIntoComponents | <code>boolean</code> | pass true value to resolve references into component |

**Example**
```js
Expand Down Expand Up @@ -123,3 +126,14 @@ This function checks for external reference.
| parsedJSON | <code>Array.&lt;Object&gt;</code> |
| $refs | <code>$RefParser</code> |

<a name="resolve"></a>

## resolve(asyncapiDocuments, options) ⇒ <code>Array.&lt;Object&gt;</code>
**Kind**: global function

| Param | Type |
| --- | --- |
| asyncapiDocuments | <code>Object</code> |
| options | <code>Object</code> |
| options.referenceIntoComponents | <code>boolean</code> |

8 changes: 4 additions & 4 deletions 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());
}

Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -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}
*
Expand All @@ -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);
};
Expand Down
12 changes: 8 additions & 4 deletions lib/parser.js
Expand Up @@ -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() {
Expand All @@ -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
}
}
}
}
Expand Down Expand Up @@ -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)) {
Expand Down
24 changes: 15 additions & 9 deletions 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');

Expand All @@ -16,10 +16,7 @@ exports.toJS = (asyncapiYAMLorJSON) => {
asyncapiYAMLorJSON.constructor &&
asyncapiYAMLorJSON.constructor.name === "Object"
) {
return {
initialFormat: "js",
parsedJSON: asyncapiYAMLorJSON,
};
return asyncapiYAMLorJSON
}

if (typeof asyncapiYAMLorJSON !== "string") {
Expand All @@ -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<Object>}
*/
exports.resolve = async (asyncapiDocuments, options) => {

let docs = [];

for(const asyncapiDocument of asyncapiDocuments) {
await parse(asyncapiDocument);
for (const asyncapiDocument of asyncapiDocuments) {
if (options.referenceIntoComponents) {
await parse(asyncapiDocument);
}
const bundledAsyncAPIDocument = await $RefParser.bundle(asyncapiDocument);
docs.push(bundledAsyncAPIDocument);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/asyncapi.yaml
Expand Up @@ -7,4 +7,4 @@ channels:
user/signedup:
subscribe:
message:
$ref: './messages.yaml#/messages/UserSignedUp'
$ref: 'tests/messages.yaml#/messages/UserSignedUp'
14 changes: 14 additions & 0 deletions tests/lib/index.spec.js
Expand Up @@ -16,4 +16,18 @@ describe("bundler should ", () => {
);
expect(response).toBeDefined();
});

test("should bundle references into components", async () => {
const files = ['./tests/asyncapi.yaml']
const doc = await bundle(
files.map(file => fs.readFileSync(path.resolve(process.cwd(), file), "utf-8")),
{
referenceIntoComponents: true
}
)

const asyncapiObject = doc.json();
expect(asyncapiObject.channels['user/signedup'].subscribe.message['$ref']).toMatch('#/components/messages/UserSignedUp')
})

});

0 comments on commit 175d9f0

Please sign in to comment.