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 all 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
66 changes: 62 additions & 4 deletions API.md
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
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
@@ -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() {
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
@@ -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.