Skip to content

Commit

Permalink
feat: add x-origin property
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Apr 18, 2024
1 parent 2ebd3ba commit 0892753
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 223 deletions.
40 changes: 22 additions & 18 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,26 @@ console.log(document.string()); // get JSON string

| Param | Type | Description |
| --- | --- | --- |
| files | <code>Array.&lt;string&gt;</code> | <p>Array of stringified AsyncAPI documents in YAML format, that are to be bundled (or array of filepaths, resolved and passed via <code>Array.map()</code> and <code>fs.readFileSync</code>, which is the same, see <code>README.md</code>).</p> |
| files | <code>Array.&lt;string&gt;</code> | <p>Array of relative or absolute paths to AsyncAPI Documents that should be bundled.</p> |
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | <p>Base object whose properties will be retained.</p> |
| [options.baseDir] | <code>string</code> | <p>Relative or absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.</p> |
| [options.xOrigin] | <code>boolean</code> | <p>Pass <code>true</code> to generate properties <code>x-origin</code> that will contain historical values of dereferenced <code>$ref</code>s.</p> |

**Example**
**TypeScript**
```ts
import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
xOrigin: true,
});
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});

console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
}

main().catch(e => console.error(e));
Expand All @@ -102,14 +104,15 @@ main().catch(e => console.error(e));
```js
'use strict';

const { readFileSync, writeFileSync } = require('fs');
const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
xOrigin: true,
});
writeFileSync('asyncapi.yaml', document.yml());
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));
Expand All @@ -119,15 +122,16 @@ main().catch(e => console.error(e));
```js
'use strict';

import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
xOrigin: true,
});
writeFileSync('asyncapi.yaml', document.yml());
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));
main().catch(e => console.error(e));
```
56 changes: 30 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,18 @@ AsyncAPI Bundler can be easily used within your JavaScript projects as a Node.js
```js
'use strict';

const { readFileSync, writeFileSync } = require('fs');
const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');

async function main() {
const filePaths = ['./camera.yml','./audio.yml'];
const document = await bundle(
filePaths.map(filePath => readFileSync(filePath, 'utf-8')), {
base: readFileSync('./base.yml', 'utf-8'),
}
);

console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
if (document.yml()) {
console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
}
}

main().catch(e => console.error(e));
Expand Down Expand Up @@ -241,17 +240,17 @@ If `Optimizer` is not able to find `x-origin` properties during optimization of

**TypeScript**
```ts
import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});

console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
}
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));
```
Expand All @@ -260,15 +259,17 @@ main().catch(e => console.error(e));
```js
'use strict';

const { readFileSync, writeFileSync } = require('fs');
const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
writeFileSync('asyncapi.yaml', document.yml());
}
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));
```
Expand All @@ -277,15 +278,17 @@ main().catch(e => console.error(e));
```js
'use strict';

import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
writeFileSync('asyncapi.yaml', document.yml());
}
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}

main().catch(e => console.error(e));

Expand All @@ -299,9 +302,10 @@ main().catch(e => console.error(e));
| Param | Type | Description |
| --- | --- | --- |
| files | <code>Array.&lt;string&gt; | Array of stringified AsyncAPI documents in YAML format, that are to be bundled (or array of filepaths, resolved and passed via `Array.map()` and `fs.readFileSync`, which is the same). |
| files | <code>Array.&lt;string&gt;</code> | <p>Array of relative or absolute paths to AsyncAPI Documents that should be bundled.</p> |
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | Base object whose properties will be retained. |
| [options.base] | <code>string</code> \| <code>object</code> | <p>Base object whose properties will be retained.</p> |
| [options.baseDir] | <code>string</code> | <p>Relative or absolute path to directory relative to which paths to AsyncAPI Documents that should be bundled will be resolved.</p> |
| [options.xOrigin] | <code>boolean</code> | <p>Pass <code>true</code> to generate properties <code>x-origin</code> that will contain historical values of dereferenced <code>$ref</code>s.</p> |
Expand Down
5 changes: 3 additions & 2 deletions example/bundle-cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

'use strict';

const { readFileSync, writeFileSync } = require('fs');
const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
if (document.yml()) {
Expand Down
12 changes: 5 additions & 7 deletions example/bundle-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ const { readFileSync, writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');

async function main() {
const filePaths = ['./camera.yml','./audio.yml'];
const document = await bundle(
filePaths.map(filePath => readFileSync(filePath, 'utf-8')), {
base: readFileSync('./base.yml', 'utf-8'),
xOrigin: true
}
);
const filePaths = ['./camera.yml', './audio.yml'];
const document = await bundle(filePaths, {
base: readFileSync('./base.yml', 'utf-8'),
xOrigin: true,
});
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}
Expand Down
4 changes: 2 additions & 2 deletions example/bundle-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

'use strict';

import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
const document = await bundle(['./main.yaml'], {
xOrigin: true,
});
if (document.yml()) {
Expand Down
4 changes: 2 additions & 2 deletions example/bundle-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

'use strict';

import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
const document = await bundle(['./main.yaml'], {
xOrigin: true,
});
if (document.yml()) {
Expand Down
52 changes: 52 additions & 0 deletions example/example-data/social-media/comments-service/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
asyncapi: 3.0.0
info:
title: Comments Service
version: 1.0.0
description: This service is in charge of processing all the events related to comments.
servers:
mosquitto:
host: test.mosquitto.org
protocol: mqtt
tags:
- name: 'env:production'
description: This environment is meant for production use case
- name: 'kind:remote'
description: This server is a remote server. Not exposed by the application
- name: 'visibility:public'
description: This resource is public and available to everyone
bindings:
mqtt:
clientId: comment-service
channels:
commentLiked:
address: comment/liked
messages:
commentLiked:
$ref: '../common/messages.yaml#/commentLiked'
description: >-
Updates the likes count in the database and sends the new count to the
broker.
commentCountChange:
address: 'comment/{commentId}/changed'
messages:
commentChanged:
$ref: '../common/messages.yaml#/commentChanged'
description: >-
Sends the new count to the broker after it has been updated in the
database.
parameters:
commentId:
$ref: '../common/parameters.yaml#/commentId'
operations:
receiveCommentLiked:
action: receive
channel:
$ref: '#/channels/commentLiked'
messages:
- $ref: '#/channels/commentLiked/messages/commentLiked'
sendCommentChange:
action: send
channel:
$ref: '#/channels/commentCountChange'
messages:
- $ref: '#/channels/commentCountChange/messages/commentChanged'
16 changes: 16 additions & 0 deletions example/example-data/social-media/common/messages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
commentLiked:
description: Message that is being sent when a comment has been liked by someone.
payload:
$ref: './schemas.yaml#/commentLikedPayload'
likeComment:
description: Message that is being sent when someone wants to like a comment.
payload:
$ref: './schemas.yaml#/likeCommentPayload'
commentChanged:
description: Message that is being sent when a comment have been updated.
payload:
$ref: './schemas.yaml#/commentChangedPayload'
updateCommentLikes:
description: Message that is being sent when a comment have been updated.
payload:
$ref: './schemas.yaml#/updateCommentLikesPayload'
2 changes: 2 additions & 0 deletions example/example-data/social-media/common/parameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
commentId:
description: ID of the comment
50 changes: 50 additions & 0 deletions example/example-data/social-media/common/schemas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
commentLikedPayload:
type: object
title: commentLikedPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/commentId'
- description: Id of the comment that was liked
likeCommentPayload:
type: object
title: likeCommentPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/commentId'
- description: Id of the comment that should be liked
likedBy:
allOf:
- $ref: '#/userId'
- description: The id of the user that have liked the comment
commentChangedPayload:
type: object
title: commentChangedPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/commentId'
- description: Id of the comment that was changed, such as when someone liked it.
likeCount:
type: integer
description: The new like count of how many have liked the comment.
updateCommentLikesPayload:
type: object
title: updateCommentLikesPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/commentId'
- description: Id of the comment that was changed, such as when someone liked it.
likeCount:
type: integer
description: The new like count of how many have liked the comment.
commentId:
type: string
userId:
type: string
4 changes: 4 additions & 0 deletions example/example-data/social-media/common/servers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
websiteWebSocketServer:
host: mycompany.com
pathname: /ws
protocol: ws
Loading

0 comments on commit 0892753

Please sign in to comment.