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: extend the components model with has-like functions #192

Merged
merged 6 commits into from
Nov 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class AsyncAPIDocument extends Base {
return createMapOfType(this._json.servers, Server);
}

/**
* @returns {string[]}
*/
serverNames() {
if (!this._json.servers) return [];
return Object.keys(this._json.servers);
}

/**
* @param {string} name - Name of the server.
* @returns {Server}
Expand All @@ -80,6 +88,20 @@ class AsyncAPIDocument extends Base {
return getMapValueOfType(this._json.servers, name, Server);
}

/**
* @returns {boolean}
*/
hasDefaultContentType() {
return !!this._json.defaultContentType;
}

/**
* @returns {string|null}
*/
defaultContentType() {
return this._json.defaultContentType || null;
}

/**
* @returns {boolean}
*/
Expand Down Expand Up @@ -110,13 +132,6 @@ class AsyncAPIDocument extends Base {
return getMapValueOfType(this._json.channels, name, Channel, this);
}

/**
* @returns {string}
*/
defaultContentType() {
return this._json.defaultContentType || null;
}

/**
* @returns {boolean}
*/
Expand Down
56 changes: 56 additions & 0 deletions lib/models/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class Components extends Base {
}

/**
* @returns {boolean}
*/
hasMessages() {
return !!this._json.messages;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the message.
* @returns {Message}
*/
message(name) {
Expand All @@ -42,6 +50,14 @@ class Components extends Base {
}

/**
* @returns {boolean}
*/
hasSchemas() {
return !!this._json.schemas;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the schema.
* @returns {Schema}
*/
schema(name) {
Expand All @@ -54,8 +70,16 @@ class Components extends Base {
securitySchemes() {
return createMapOfType(this._json.securitySchemes, SecurityScheme);
}

/**
* @returns {boolean}
*/
hasSecuritySchemes() {
return !!this._json.securitySchemes;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the security schema.
* @returns {SecurityScheme}
*/
securityScheme(name) {
Expand All @@ -70,6 +94,14 @@ class Components extends Base {
}

/**
* @returns {boolean}
*/
hasParameters() {
return !!this._json.parameters;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the channel parameter.
* @returns {ChannelParameter}
*/
parameter(name) {
Expand All @@ -84,6 +116,14 @@ class Components extends Base {
}

/**
* @returns {boolean}
*/
hasCorrelationIds() {
return !!this._json.correlationIds;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the correlationId.
* @returns {CorrelationId}
*/
correlationId(name) {
Expand All @@ -98,6 +138,14 @@ class Components extends Base {
}

/**
* @returns {boolean}
*/
hasOperationTraits() {
return !!this._json.operationTraits;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the operation trait.
* @returns {OperationTrait}
*/
operationTrait(name) {
Expand All @@ -112,6 +160,14 @@ class Components extends Base {
}

/**
* @returns {boolean}
*/
hasMessageTraits() {
return !!this._json.messageTraits;
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} name - Name of the message trait.
* @returns {MessageTrait}
*/
messageTrait(name) {
Expand Down
10 changes: 9 additions & 1 deletion lib/models/schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createMapOfType, mix } = require('../utils');
const { createMapOfType, getMapValueOfType, mix } = require('../utils');

const Base = require('./base');

Expand Down Expand Up @@ -192,6 +192,14 @@ class Schema extends Base {
properties() {
return createMapOfType(this._json.properties, Schema);
}

/**
* @param {string} name - Name of the property.
* @returns {Schema}
*/
property(name) {
return getMapValueOfType(this._json.properties, name, Schema);
}

/**
* @returns {boolean|Schema}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "test"
},
"scripts": {
"test": "nyc --reporter=html --reporter=text mocha --exclude test/browser_test.js --recursive && npm run test-browser",
"test": "npm run test-lib && npm run test-browser",
"bundle": "browserify lib/browser.js | uglifyjs > dist/bundle.js",
"docs": "jsdoc2md lib/parser.js -f lib/**/*.js > API.md",
"types": "jsdoc -t node_modules/tsd-jsdoc/dist -r lib -d ./ && node ./scripts/fix-ts-types.js",
Expand All @@ -17,6 +17,7 @@
"get-version": "echo $npm_package_version",
"lint": "eslint --max-warnings 0 --config .eslintrc .",
"gen-readme-toc": "markdown-toc -i README.md",
"test-lib": "nyc --reporter=html --reporter=text mocha --exclude test/browser_test.js --recursive",
"test-browser": "npm run bundle && cp dist/bundle.js test/sample_browser/ && start-server-and-test 'http-server test/sample_browser --cors -s' 8080 'mocha --timeout 3000 test/browser_test.js' && rimraf test/sample_browser/bundle.js"
},
"bugs": {
Expand Down
65 changes: 65 additions & 0 deletions test/models/asyncapi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ describe('AsyncAPIDocument', function() {
expect(d.servers().test2.constructor.name).to.equal('Server');
expect(d.servers().test2.json()).to.equal(doc.servers.test2);
});

it('should return an empty object if the AsyncAPI document has no defined servers', function() {
const doc = {};
const d = new AsyncAPIDocument(doc);
expect(typeof d.servers()).to.be.equal('object');
expect(d.servers()).to.deep.equal({});
});
});

describe('#serverNames()', function() {
it('should return an array of strings', function() {
const doc = { servers: { test1: { url: 'test1' }, test2: { url: 'test2' } } };
const d = new AsyncAPIDocument(doc);
expect(Array.isArray(d.serverNames())).to.be.equal(true);
expect(d.serverNames()).to.deep.equal(['test1', 'test2']);
});

it('should return an empty array if the AsyncAPI document has no defined servers', function() {
const doc = {};
const d = new AsyncAPIDocument(doc);
expect(Array.isArray(d.serverNames())).to.be.equal(true);
expect(d.serverNames()).to.deep.equal([]);
});
});

describe('#server()', function() {
Expand All @@ -79,6 +102,34 @@ describe('AsyncAPIDocument', function() {
});
});

describe('#hasDefaultContentType()', function() {
it('should return true if field exists', function() {
const doc = { defaultContentType: 'application/json' };
const d = new AsyncAPIDocument(doc);
expect(d.hasDefaultContentType()).to.be.equal(true);
});

it('should return false if field does not exist', function() {
const doc = {};
const d = new AsyncAPIDocument(doc);
expect(d.hasDefaultContentType()).to.be.equal(false);
});
});

describe('#defaultContentType()', function() {
it('should return string if field exists', function() {
const doc = { defaultContentType: 'application/json' };
const d = new AsyncAPIDocument(doc);
expect(d.defaultContentType()).to.be.equal('application/json');
});

it('should return null if field does not exist', function() {
const doc = {};
const d = new AsyncAPIDocument(doc);
expect(d.defaultContentType()).to.be.equal(null);
});
});

describe('#hasChannels()', function() {
it('should return a boolean indicating if the AsyncAPI document has channels', function() {
const doc = { channels: { test1: { description: 'test1' }, test2: { description: 'test2' } } };
Expand All @@ -100,6 +151,13 @@ describe('AsyncAPIDocument', function() {
expect(d.channels().test2.constructor.name).to.equal('Channel');
expect(d.channels().test2.json()).to.equal(doc.channels.test2);
});

it('should return an empty object if the AsyncAPI document has no defined channels', function() {
const doc = {};
const d = new AsyncAPIDocument(doc);
expect(typeof d.channels()).to.be.equal('object');
expect(d.servers()).to.deep.equal({});
});
});

describe('#channelNames()', function() {
Expand All @@ -109,6 +167,13 @@ describe('AsyncAPIDocument', function() {
expect(Array.isArray(d.channelNames())).to.be.equal(true);
expect(d.channelNames()).to.deep.equal(['test1', 'test2']);
});

it('should return an empty array if the AsyncAPI document has no defined channels', function() {
const doc = {};
const d = new AsyncAPIDocument(doc);
expect(Array.isArray(d.channelNames())).to.be.equal(true);
expect(d.channelNames()).to.deep.equal([]);
});
});

describe('#channel()', function() {
Expand Down
Loading