Skip to content
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

feat: bundle references into components #46

Merged
merged 15 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 41 additions & 2 deletions API.md
Expand Up @@ -10,6 +10,12 @@
<dl>
<dt><a href="#bundle">bundle(files, options)</a> ⇒ <code><a href="#Document">Document</a></code></dt>
<dd></dd>
<dt><a href="#isExternalReference">isExternalReference(ref)</a> ⇒ <code>boolean</code></dt>
<dd></dd>
<dt><a href="#resolve">resolve(parsedJSONs)</a></dt>
<dd></dd>
<dt><a href="#getExternalChannelRefs">getExternalChannelRefs(parsedJSON)</a></dt>
<dd></dd>
</dl>

<a name="Document"></a>
Expand Down Expand Up @@ -67,13 +73,46 @@ console.log(document.string()); // get json string

**Example**
```js
const bundler = requrie('@asyncapi/bundler');
const bundle = requrie('@asyncapi/bundler');
const fs = require('fs');
const path = requrie('path');

const document = await bundler.bundle(fs.readFileSync(
const document = await bundle(fs.readFileSync(
path.resolve('./asyncapi.yaml', 'utf-8')
));

console.log(document.yml());
```
<a name="bundle..resolvedJsons"></a>

### bundle~resolvedJsons
Bundle all external references for each files.

**Kind**: inner constant of [<code>bundle</code>](#bundle)
<a name="isExternalReference"></a>

## isExternalReference(ref) ⇒ <code>boolean</code>
**Kind**: global function

| Param | Type |
| --- | --- |
| ref | <code>string</code> |

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

## resolve(parsedJSONs)
**Kind**: global function

| Param | Type |
| --- | --- |
| parsedJSONs | <code>Array.&lt;object&gt;</code> |

<a name="getExternalChannelRefs"></a>

## getExternalChannelRefs(parsedJSON)
**Kind**: global function

| Param | Type |
| --- | --- |
| parsedJSON | <code>object</code> |

30 changes: 30 additions & 0 deletions example/asyncapi.yaml
@@ -0,0 +1,30 @@
asyncapi: 2.2.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signup:
subscribe:
message:
$ref: '#/components/messages/UserSignedUp'
test:
subscribe:
message:
$ref: '#/components/messages/TestMessage'
components:
messages:
TestMessage:
payload:
type: string
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
11 changes: 11 additions & 0 deletions example/bundle.js
@@ -0,0 +1,11 @@
const bundle = require('@asyncapi/bundler');
const fs = require('fs');

async function main(){
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
const document = await bundle([
fs.readFileSync('./main.yaml', 'utf-8')
]);
fs.writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));
19 changes: 19 additions & 0 deletions example/main.yaml
@@ -0,0 +1,19 @@
asyncapi: '2.2.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signup:
subscribe:
message:
$ref: './messages.yaml#/messages/UserSignedUp'
test:
subscribe:
message:
$ref: '#/components/messages/TestMessage'
components:
messages:
TestMessage:
payload:
type: string
17 changes: 17 additions & 0 deletions example/messages.yaml
@@ -0,0 +1,17 @@
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
UserLoggedIn:
payload:
type: object
properties:
id: string
77 changes: 77 additions & 0 deletions example/package-lock.json

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

15 changes: 15 additions & 0 deletions example/package.json
@@ -0,0 +1,15 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [],
"author": "",
"license": "ISC",
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
"dependencies": {
"@asyncapi/bundler": "file:../"
}
}
4 changes: 2 additions & 2 deletions lib/document.js
Expand Up @@ -21,8 +21,8 @@ class Document {
*/
constructor(parsedJSONList, base) {
this._doc = {};
for (const { parsedJSON } of parsedJSONList) {
this._doc = _.merge(this._doc, parsedJSON);
for (const resolvedJSON of parsedJSONList) {
this._doc = _.merge(this._doc, resolvedJSON);
}

if (typeof base !== "undefined") {
Expand Down
22 changes: 7 additions & 15 deletions lib/index.js
Expand Up @@ -13,36 +13,28 @@ const Document = require("./document");
*
* @example
*
* const bundler = requrie('@asyncapi/bundler');
* const bundle = requrie('@asyncapi/bundler');
* const fs = require('fs');
* const path = requrie('path');
*
* const document = await bundler.bundle(fs.readFileSync(
* const document = await bundle(fs.readFileSync(
* path.resolve('./asyncapi.yaml', 'utf-8')
* ));
*
* console.log(document.yml());
*/
const bundle = async (files, options = {}) => {
if (typeof options.base !== "undefined") {
options.base = toJS(options.base).parsedJSON;
options.base = toJS(options.base);
}

if (typeof options.parser === "undefined") {
options.parser = require("@asyncapi/parser");
}

if (typeof options.validate === "undefined") {
options.validate = true;
}
const parsedJsons = files.map(file => toJS(file));
/**
* Bundle all external references for each files.
*/
const resolvedJsons = await resolve(parsedJsons);

if (options.validate) {
await validate(resolvedJsons, options.parser);
}

return new Document(parsedJsons, options.base);
return new Document(resolvedJsons, options.base);
};

module.exports = bundle;
99 changes: 99 additions & 0 deletions lib/parser.js
@@ -0,0 +1,99 @@
const $RefParser = require('@apidevtools/json-schema-ref-parser');
const { JSONPath } = require('jsonpath-plus');
const { takeWhile, merge } = require('lodash');

class ExternalComponents {
ref;
resolvedJSON;
constructor(ref, resolvedJSON) {
this.ref = ref;
this.resolvedJSON = resolvedJSON;
}

getKey(){
const keys = this.ref.split('/');
return keys[keys.length -1 ]
}

getValue() {
return this.resolvedJSON;
}
}

async function parse(JSONSchema) {
const $ref = await $RefParser.resolve(JSONSchema);
const refs = crawlChanelPropertiesForRefs(JSONSchema);
for (let ref of refs) {
if (isExternalReference(ref)) {
const componentObject = await resolveExternalRefs(JSONSchema, $ref);
merge(JSONSchema.components, componentObject);
console.log(JSONSchema.components)
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
}
}





Souvikns marked this conversation as resolved.
Show resolved Hide resolved
}


function crawlChanelPropertiesForRefs(JSONSchema) {
return JSONPath({ json: JSONSchema, path: `$.channels.*.*.message['$ref']` });
}


/**
*
* @param {string} ref
Souvikns marked this conversation as resolved.
Show resolved Hide resolved
* @returns {boolean}
*/
function isExternalReference(ref) {
return !ref.startsWith('#')
}



module.exports = {
parse,
resolve
}

/**
*
* @param {object[]} parsedJSONs
*/
async function resolve(parsedJSONs) {
const $refParser = new $RefParser();
const resolvedJSONs = [];
for (let parsedJSON of parsedJSONs) {
const $ref = await $refParser.resolve(parsedJSON);
const externalChannelRefs = getExternalChannelRefs(parsedJSON);

}

}

async function resolveExternalRefs(parsedJSON, $refs) {
const componentObj = {messages: {}};
JSONPath({ json: parsedJSON, resultType: 'all', path: '$.channels.*.*.message' }).forEach(({ parent, parentProperty }) => {
const ref = parent[parentProperty]['$ref'];
if (isExternalReference(ref)) {
const value = $refs.get(ref);
const component = new ExternalComponents(ref, value);
componentObj.messages[String(component.getKey())] = component.getValue()
parent[parentProperty]['$ref'] = `#/components/messages/${component.getKey()}`;
}

})

return componentObj
}

/**
*
* @param {object} parsedJSON
*/
function getExternalChannelRefs(parsedJSON) {
return JSONPath({ path: '', json: parsedJSON })
}