Skip to content

Commit

Permalink
add Servers object + id() on models
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya committed Feb 22, 2022
1 parent 3a24bb2 commit 7f2bde3
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
27 changes: 8 additions & 19 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { IntentMessages } = require('./messages');
const { IntentInfo } = require('./info');
const { IntentOperations } = require('./operations');
const { IntentServer } = require('./server');
const { IntentServers } = require('./servers');
const ParserError = require('../errors/parser-error');
const utils = require('../utils');
// eslint-disable-next-line no-unused-vars
Expand Down Expand Up @@ -148,33 +149,22 @@ class IntentAsyncAPIDocument {
* @returns {boolean}
*/
hasServers() {
return this.servers().length > 0;
return this.servers().all().length > 0;
}

/**
*
* @returns {IntentServer[]}
* @returns {IntentServers}
*/
servers() {
const servers = [];
if (utils.isVersion2(this.document.json())) {
return Object.keys(this.document.servers()).map((server_id) => {
return new IntentServer(this.document, server_id);
Object.keys(this.document.servers()).forEach((server_id) => {
servers.push(new IntentServer(this.document, server_id));
});
}
return [];
}

/**
*
* @returns {IntentServer|undefined}
*/
server(server_name) {
if (utils.isVersion2(this.document.json())) {
const server = this.document.servers()[`${server_name}`];
if (!server) return undefined;
return new IntentServer(this.document, server_name);
}
return undefined;
return new IntentServers(servers);
}

/**
Expand All @@ -183,8 +173,7 @@ class IntentAsyncAPIDocument {
*/
securitySchemes() {
if (utils.isVersion2(this.document.json())) {
const servers = this.servers();
return servers.map((server) => {
return this.servers().all().map((server) => {
return server.security();
});
}
Expand Down
6 changes: 6 additions & 0 deletions lib/models/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class IntentChannel {
/**
*
* @param {string} document
* @param {string} channelId
*/
constructor(document, channelId) {
if (document === undefined || document === null || channelId === undefined || channelId === null) throw new ParserError(`Invalid JSON to instantiate the ${this.constructor.name} object.`);
Expand All @@ -41,6 +42,11 @@ class IntentChannel {
return new IntentOperations(operations);
}

/**
* @returns {string}
*/
id() { return this.channelId; }

/**
* @returns {string}
*/
Expand Down
6 changes: 6 additions & 0 deletions lib/models/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ class IntentMessage {
this.partOfIntentOperation = partOfIntentOperation;
}

/**
* @returns {string}
*/
id() { return this.uid(); }

/**
* @returns {string}
*/
uid() { return 'test-uid'; }

/**
* @returns {string}
*/
Expand Down
17 changes: 17 additions & 0 deletions lib/models/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

module.exports = {};

const ParserError = require('../errors/parser-error');
const { IntentOperation } = require('./operation');
const { IntentServerVariable } = require('./server-variable');
const { IntentSecurityScheme } = require('./security-scheme');
Expand All @@ -11,6 +12,22 @@ const { IntentSecurityScheme } = require('./security-scheme');
* @alias module:@asyncapi/parser#IntentServer
*/
class IntentServer {
/**
*
* @param {string} document
* @param {string} serverId
*/
constructor(document, serverId) {
if (document === undefined || document === null || serverId === undefined || serverId === null) throw new ParserError(`Invalid JSON to instantiate the ${this.constructor.name} object.`);
this.document = document;
this.serverId = serverId;
}

/**
* @returns {string}
*/
id() { return this.serverId; }

/**
* @returns {boolean}
*/
Expand Down
63 changes: 63 additions & 0 deletions lib/models/servers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

module.exports = {};
// eslint-disable-next-line no-unused-vars
const { IntentServer } = require('./server');

/**
*
* @class
* @alias module:@asyncapi/parser#IntentServers
*/
class IntentServers {
/**
*
* @param {IntentServer[]} servers
*/
constructor(servers) {
this.servers = servers;
}

/**
* @returns {IntentServer[]}
*/
filterBySend() {
return this.filterBy((server) => {
return server.operations().filterBySend().lenght > 0;
});
}

/**
* @returns {IntentServer[]}
*/
filterByReceive() {
return this.filterBy((server) => {
return server.operations().filterByReceive().lenght > 0;
});
}

/**
* @returns {IntentServer[]}
*/
filterById(serverId) {
return this.filterBy((server) => {
return server.id() === serverId;
});
}

/**
* @returns {IntentServer[]}
*/
filterBy(callbackFn) {
return this.servers.filter(callbackFn);
}

/**
* @returns {IntentServer[]}
*/
all() {
return this.servers;
}
}

module.exports.IntentServers = IntentServers;

0 comments on commit 7f2bde3

Please sign in to comment.