Skip to content

Commit

Permalink
allServers() and allChannels() at doc level
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya committed Dec 28, 2021
1 parent 293129c commit 5bae790
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ class AsyncAPIDocument extends Base {
return !!this.allMessages().size;
}

/**
* @returns {Map<string, Channel>}
*/
allChannels() {
const channels = new Map();

if (this.hasChannels()) {
this.channelNames().forEach(channelName => {
channels.set(channelName, this.channel(channelName));
});
}

if (this.hasComponents()) {
Object.entries(this.components().channels()).forEach(([key, value]) => {
// TODO This means that if two channels have the same name (but located in different place), the first one will be overwritten.
// Should we check if the key exists and if so, prefix the key somehow?
channels.set(key, value);
});
}

return channels;
}

/**
* @returns {Map<string, Message>}
*/
Expand Down Expand Up @@ -209,6 +232,29 @@ class AsyncAPIDocument extends Base {
return schemas;
}

/**
* @returns {Map<string, Server>}
*/
allServers() {
const servers = new Map();

if (this.hasServers()) {
this.serverNames().forEach(serverName => {
servers.set(serverName, this.server(serverName));
});
}

if (this.hasComponents()) {
Object.entries(this.components().servers()).forEach(([key, value]) => {
// TODO This means that if two servers have the same name (but located in different place), the first one will be overwritten.
// Should we check if the key exists and if so, prefix the key somehow?
servers.set(key, value);
});
}

return servers;
}

/**
* @returns {boolean}
*/
Expand Down
38 changes: 38 additions & 0 deletions test/models/asyncapi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,25 @@ describe('AsyncAPIDocument', function() {
});
});

describe('#allChannels()', function() {
it('should return a map with all the channels used in the document and overwrite the channel from channels', function() {
const doc = { channels: { test1: { description: 'test1' }, test2: { description: 'test2' } }, components: { channels: { test2: { description: 'test2-overwrite' } } }};
const d = new AsyncAPIDocument(doc);
const allChannels = d.allChannels();
expect(allChannels.size).to.be.equal(2);
expect(allChannels.get('test2').constructor.name).to.be.equal('Channel');
expect(allChannels.get('test2').json().description).to.be.equal('test2-overwrite');
});
it('should return an array with all the channels used in the document', function() {
const doc = { channels: { test1: { description: 'test1' }, test2: { description: 'test2' } }, components: { channels: { test3: { description: 'test3' } } }};
const d = new AsyncAPIDocument(doc);
const allChannels = d.allChannels();
expect(allChannels.size).to.be.equal(3);
expect(allChannels.get('test1').constructor.name).to.be.equal('Channel');
expect(allChannels.get('test1').json().description).to.be.equal('test1');
});
});

describe('#allMessages()', function() {
it('should return an array with all the messages used in the document and overwrite the message from channel', function() {
const doc = { channels: { test: { publish: { message: { name: 'test', test: false, k: 1 } } } }, components: { messages: { test: { test: true, k: 3 } } } };
Expand All @@ -317,6 +336,25 @@ describe('AsyncAPIDocument', function() {
});
});

describe('#allServers()', function() {
it('should return a map with all the servers used in the document and overwrite the server from servers', function() {
const doc = { servers: { test1: { url: 'test1' }, test2: { url: 'test2' } }, components: { servers: { test2: { url: 'test2-overwrite' } } }};
const d = new AsyncAPIDocument(doc);
const allServers = d.allServers();
expect(allServers.size).to.be.equal(2);
expect(allServers.get('test2').constructor.name).to.be.equal('Server');
expect(allServers.get('test2').json().url).to.be.equal('test2-overwrite');
});
it('should return an array with all the servers used in the document', function() {
const doc = { servers: { test1: { url: 'test1' }, test2: { url: 'test2' } }, components: { servers: { test3: { url: 'test3' } } }};
const d = new AsyncAPIDocument(doc);
const allServers = d.allServers();
expect(allServers.size).to.be.equal(3);
expect(allServers.get('test1').constructor.name).to.be.equal('Server');
expect(allServers.get('test1').json().url).to.be.equal('test1');
});
});

describe('#allSchemas()', function() {
it('should return additional items schemas when no items specified', function() {
const doc = {
Expand Down

0 comments on commit 5bae790

Please sign in to comment.