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 1 commit
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
37 changes: 37 additions & 0 deletions test/models/asyncapi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ describe('AsyncAPIDocument', function() {
});
});

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']);
});
});

magicmatatjahu marked this conversation as resolved.
Show resolved Hide resolved
describe('#server()', function() {
it('should return a specific server object', function() {
const doc = { servers: { test1: { url: 'test1' }, test2: { url: 'test2' } } };
Expand All @@ -79,6 +88,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 Down
77 changes: 77 additions & 0 deletions test/models/components_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ describe('Components', function() {
expect(d.messages().test2.json()).to.equal(doc.messages.test2);
});
});

describe('#hasMessages()', function() {
it('should return a boolean indicating if the components field has messages', function() {
const doc = { messages: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoMessages = { schemas: {} };
const d = new Components(doc);
const d2 = new Components(docNoMessages);
expect(d.hasMessages()).to.equal(true);
expect(d2.hasMessages()).to.equal(false);
});
});

describe('#message()', function() {
it('should return a specific Message object', function() {
Expand Down Expand Up @@ -49,6 +60,17 @@ describe('Components', function() {
expect(d.schemas().test2.json()).to.equal(doc.schemas.test2);
});
});

describe('#hasSchemas()', function() {
it('should return a boolean indicating if the components field has schemas', function() {
const doc = { schemas: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoSchemas = { messages: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasSchemas()).to.equal(true);
expect(d2.hasSchemas()).to.equal(false);
});
});

describe('#schema()', function() {
it('should return a specific Schema object', function() {
Expand Down Expand Up @@ -82,6 +104,17 @@ describe('Components', function() {
expect(d.securitySchemes().test2.json()).to.equal(doc.securitySchemes.test2);
});
});

describe('#hasSecuritySchemes()', function() {
it('should return a boolean indicating if the components field has securitySchemes', function() {
const doc = { securitySchemes: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoSchemas = { messages: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasSecuritySchemes()).to.equal(true);
expect(d2.hasSecuritySchemes()).to.equal(false);
});
});

describe('#securityScheme()', function() {
it('should return a specific securityScheme object', function() {
Expand Down Expand Up @@ -115,6 +148,17 @@ describe('Components', function() {
expect(d.parameters().test2.json()).to.equal(doc.parameters.test2);
});
});

describe('#hasParameters()', function() {
it('should return a boolean indicating if the components field has parameters', function() {
const doc = { parameters: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoSchemas = { messages: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasParameters()).to.equal(true);
expect(d2.hasParameters()).to.equal(false);
});
});

describe('#parameter()', function() {
it('should return a specific parameter object', function() {
Expand Down Expand Up @@ -148,6 +192,17 @@ describe('Components', function() {
expect(d.correlationIds().test2.json()).to.equal(doc.correlationIds.test2);
});
});

describe('#hasCorrelationIds()', function() {
it('should return a boolean indicating if the components field has correlationIds', function() {
const doc = { correlationIds: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoSchemas = { messages: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasCorrelationIds()).to.equal(true);
expect(d2.hasCorrelationIds()).to.equal(false);
});
});

describe('#correlationId()', function() {
it('should return a specific correlationId object', function() {
Expand Down Expand Up @@ -181,6 +236,17 @@ describe('Components', function() {
expect(d.operationTraits().test2.json()).to.equal(doc.operationTraits.test2);
});
});

describe('#hasOperationTraits()', function() {
it('should return a boolean indicating if the components field has operationTraits', function() {
const doc = { operationTraits: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoSchemas = { messages: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasOperationTraits()).to.equal(true);
expect(d2.hasOperationTraits()).to.equal(false);
});
});

describe('#operationTrait()', function() {
it('should return a specific operationTrait object', function() {
Expand Down Expand Up @@ -214,6 +280,17 @@ describe('Components', function() {
expect(d.messageTraits().test2.json()).to.equal(doc.messageTraits.test2);
});
});

describe('#hasMessageTraits()', function() {
it('should return a boolean indicating if the components field has messageTraits', function() {
const doc = { messageTraits: { test1: { test: 'test1' }, test2: { test: 'test2' } } };
const docNoSchemas = { messages: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasMessageTraits()).to.equal(true);
expect(d2.hasMessageTraits()).to.equal(false);
});
});

describe('#messageTrait()', function() {
it('should return a specific messageTrait object', function() {
Expand Down
Loading