Skip to content

Commit

Permalink
feat: bundle references into components (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns authored Sep 12, 2022
1 parent db38898 commit e73b9b9
Show file tree
Hide file tree
Showing 17 changed files with 470 additions and 68 deletions.
66 changes: 62 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
<dl>
<dt><a href="#bundle">bundle(files, options)</a> ⇒ <code><a href="#Document">Document</a></code></dt>
<dd></dd>
<dt><a href="#parse">parse(JSONSchema)</a></dt>
<dd><p>resolves external references and updates $refs</p>
</dd>
<dt><a href="#isExternalReference">isExternalReference(ref)</a> ⇒ <code>boolean</code></dt>
<dd><p>This function checks for external reference.</p>
</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 @@ -62,18 +72,66 @@ console.log(document.string()); // get json string
| files | <code>Array.&lt;string&gt;</code> \| <code>Array.&lt;Object&gt;</code> | files that are to be bundled |
| options | <code>Object</code> | |
| 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
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="parse"></a>

## parse(JSONSchema)
resolves external references and updates $refs

**Kind**: global function

| Param | Type |
| --- | --- |
| JSONSchema | <code>Array.&lt;Object&gt;</code> |

<a name="isExternalReference"></a>

## isExternalReference(ref) ⇒ <code>boolean</code>
This function checks for external reference.

**Kind**: global function

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

<a name="resolveExternalRefs"></a>

## resolveExternalRefs(parsedJSON, $refs) ⇒ <code>ExternalComponents</code>
**Kind**: global function

| Param | Type |
| --- | --- |
| 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> |

92 changes: 85 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Overview](#overview)
- [Installation](#installation)
- [Usage](#usage)
* [Resolving external references into components](#resolving-external-references-into-components)
- [bundle(files, options)](#bundlefiles-options)

<!-- tocstop -->
Expand All @@ -24,7 +25,7 @@ An official library that lets you bundle/merge your specification files into one
```yaml

# asyncapi.yaml
asyncapi: '2.2.0'
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
Expand All @@ -50,7 +51,7 @@ messages:
description: Email of the user

# After combining
asyncapi: 2.2.0
asyncapi: 2.4.0
info:
title: Account Service
version: 1.0.0
Expand Down Expand Up @@ -80,7 +81,7 @@ channels:
```yaml

# signup.yaml
asyncapi: '2.2.0'
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
Expand All @@ -101,7 +102,7 @@ channels:


# login.yaml
asyncapi: '2.2.0'
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
Expand All @@ -119,7 +120,7 @@ channels:

# After combining
# asyncapi.yaml
asyncapi: '2.2.0'
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
Expand Down Expand Up @@ -177,6 +178,82 @@ const document = await bundle(
console.log(document.json()); // the complete bundled asyncapi document.
```

### Resolving external references into components
You can resolve external references by moving them to Messages object, under `compoents/messages`.

<details>
<summary>For Example</summary>

```yml
# asyncapi.yaml
asyncapi: '2.4.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'

#messages.yaml
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user

# After combining
asyncapi: 2.4.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup:
subscribe:
message:
$ref: '#/components/messages/UserSignedUp'
components:
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
```
</details>
</br>
```ts
const bundle = require('@asyncapi/bundler')
const fs = require('fs')
const path = require('path')

const document = await bundle(
fs.readFileSync(path.resolve('./asyncapi.yml'), 'utf-8'),
{ referenceIntoComponents: true }
);

console.log(document.json());
```


<a name="bundle"></a>

## bundle(files, options)
Expand All @@ -187,5 +264,6 @@ console.log(document.json()); // the complete bundled asyncapi document.
| files | <code>Array.&lt;string&gt;</code> \| <code>Array.&lt;Object&gt;</code> | files that are to be bundled |
| options | <code>Object</code> | |
| 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 to resovle external references to components. |


30 changes: 30 additions & 0 deletions example/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const bundle = require('@asyncapi/bundler');
const fs = require('fs');

async function main() {
const document = await bundle([fs.readFileSync('./main.yaml', 'utf-8')], {
referenceIntoComponents: true
});
fs.writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));
19 changes: 19 additions & 0 deletions example/main.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.

Loading

0 comments on commit e73b9b9

Please sign in to comment.