Skip to content

Commit 55aaa43

Browse files
feat: extend the components and asyncapi model with has-like functions (#192)
1 parent 2822f39 commit 55aaa43

File tree

7 files changed

+297
-9
lines changed

7 files changed

+297
-9
lines changed

lib/models/asyncapi.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ class AsyncAPIDocument extends Base {
7272
return createMapOfType(this._json.servers, Server);
7373
}
7474

75+
/**
76+
* @returns {string[]}
77+
*/
78+
serverNames() {
79+
if (!this._json.servers) return [];
80+
return Object.keys(this._json.servers);
81+
}
82+
7583
/**
7684
* @param {string} name - Name of the server.
7785
* @returns {Server}
@@ -80,6 +88,20 @@ class AsyncAPIDocument extends Base {
8088
return getMapValueOfType(this._json.servers, name, Server);
8189
}
8290

91+
/**
92+
* @returns {boolean}
93+
*/
94+
hasDefaultContentType() {
95+
return !!this._json.defaultContentType;
96+
}
97+
98+
/**
99+
* @returns {string|null}
100+
*/
101+
defaultContentType() {
102+
return this._json.defaultContentType || null;
103+
}
104+
83105
/**
84106
* @returns {boolean}
85107
*/
@@ -110,13 +132,6 @@ class AsyncAPIDocument extends Base {
110132
return getMapValueOfType(this._json.channels, name, Channel, this);
111133
}
112134

113-
/**
114-
* @returns {string}
115-
*/
116-
defaultContentType() {
117-
return this._json.defaultContentType || null;
118-
}
119-
120135
/**
121136
* @returns {boolean}
122137
*/

lib/models/components.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class Components extends Base {
2828
}
2929

3030
/**
31+
* @returns {boolean}
32+
*/
33+
hasMessages() {
34+
return !!this._json.messages;
35+
}
36+
37+
/**
38+
* @param {string} name - Name of the message.
3139
* @returns {Message}
3240
*/
3341
message(name) {
@@ -42,6 +50,14 @@ class Components extends Base {
4250
}
4351

4452
/**
53+
* @returns {boolean}
54+
*/
55+
hasSchemas() {
56+
return !!this._json.schemas;
57+
}
58+
59+
/**
60+
* @param {string} name - Name of the schema.
4561
* @returns {Schema}
4662
*/
4763
schema(name) {
@@ -54,8 +70,16 @@ class Components extends Base {
5470
securitySchemes() {
5571
return createMapOfType(this._json.securitySchemes, SecurityScheme);
5672
}
73+
74+
/**
75+
* @returns {boolean}
76+
*/
77+
hasSecuritySchemes() {
78+
return !!this._json.securitySchemes;
79+
}
5780

5881
/**
82+
* @param {string} name - Name of the security schema.
5983
* @returns {SecurityScheme}
6084
*/
6185
securityScheme(name) {
@@ -70,6 +94,14 @@ class Components extends Base {
7094
}
7195

7296
/**
97+
* @returns {boolean}
98+
*/
99+
hasParameters() {
100+
return !!this._json.parameters;
101+
}
102+
103+
/**
104+
* @param {string} name - Name of the channel parameter.
73105
* @returns {ChannelParameter}
74106
*/
75107
parameter(name) {
@@ -84,6 +116,14 @@ class Components extends Base {
84116
}
85117

86118
/**
119+
* @returns {boolean}
120+
*/
121+
hasCorrelationIds() {
122+
return !!this._json.correlationIds;
123+
}
124+
125+
/**
126+
* @param {string} name - Name of the correlationId.
87127
* @returns {CorrelationId}
88128
*/
89129
correlationId(name) {
@@ -98,6 +138,14 @@ class Components extends Base {
98138
}
99139

100140
/**
141+
* @returns {boolean}
142+
*/
143+
hasOperationTraits() {
144+
return !!this._json.operationTraits;
145+
}
146+
147+
/**
148+
* @param {string} name - Name of the operation trait.
101149
* @returns {OperationTrait}
102150
*/
103151
operationTrait(name) {
@@ -112,6 +160,14 @@ class Components extends Base {
112160
}
113161

114162
/**
163+
* @returns {boolean}
164+
*/
165+
hasMessageTraits() {
166+
return !!this._json.messageTraits;
167+
}
168+
169+
/**
170+
* @param {string} name - Name of the message trait.
115171
* @returns {MessageTrait}
116172
*/
117173
messageTrait(name) {

lib/models/schema.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { createMapOfType, mix } = require('../utils');
1+
const { createMapOfType, getMapValueOfType, mix } = require('../utils');
22

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

@@ -192,6 +192,14 @@ class Schema extends Base {
192192
properties() {
193193
return createMapOfType(this._json.properties, Schema);
194194
}
195+
196+
/**
197+
* @param {string} name - Name of the property.
198+
* @returns {Schema}
199+
*/
200+
property(name) {
201+
return getMapValueOfType(this._json.properties, name, Schema);
202+
}
195203

196204
/**
197205
* @returns {boolean|Schema}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "test"
99
},
1010
"scripts": {
11-
"test": "nyc --reporter=html --reporter=text mocha --exclude test/browser_test.js --recursive && npm run test-browser",
11+
"test": "npm run test-lib && npm run test-browser",
1212
"bundle": "browserify lib/browser.js | uglifyjs > dist/bundle.js",
1313
"docs": "jsdoc2md lib/parser.js -f lib/**/*.js > API.md",
1414
"types": "jsdoc -t node_modules/tsd-jsdoc/dist -r lib -d ./ && node ./scripts/fix-ts-types.js",
@@ -17,6 +17,7 @@
1717
"get-version": "echo $npm_package_version",
1818
"lint": "eslint --max-warnings 0 --config .eslintrc .",
1919
"gen-readme-toc": "markdown-toc -i README.md",
20+
"test-lib": "nyc --reporter=html --reporter=text mocha --exclude test/browser_test.js --recursive",
2021
"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"
2122
},
2223
"bugs": {

test/models/asyncapi_test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ describe('AsyncAPIDocument', function() {
5656
expect(d.servers().test2.constructor.name).to.equal('Server');
5757
expect(d.servers().test2.json()).to.equal(doc.servers.test2);
5858
});
59+
60+
it('should return an empty object if the AsyncAPI document has no defined servers', function() {
61+
const doc = {};
62+
const d = new AsyncAPIDocument(doc);
63+
expect(typeof d.servers()).to.be.equal('object');
64+
expect(d.servers()).to.deep.equal({});
65+
});
66+
});
67+
68+
describe('#serverNames()', function() {
69+
it('should return an array of strings', function() {
70+
const doc = { servers: { test1: { url: 'test1' }, test2: { url: 'test2' } } };
71+
const d = new AsyncAPIDocument(doc);
72+
expect(Array.isArray(d.serverNames())).to.be.equal(true);
73+
expect(d.serverNames()).to.deep.equal(['test1', 'test2']);
74+
});
75+
76+
it('should return an empty array if the AsyncAPI document has no defined servers', function() {
77+
const doc = {};
78+
const d = new AsyncAPIDocument(doc);
79+
expect(Array.isArray(d.serverNames())).to.be.equal(true);
80+
expect(d.serverNames()).to.deep.equal([]);
81+
});
5982
});
6083

6184
describe('#server()', function() {
@@ -79,6 +102,34 @@ describe('AsyncAPIDocument', function() {
79102
});
80103
});
81104

105+
describe('#hasDefaultContentType()', function() {
106+
it('should return true if field exists', function() {
107+
const doc = { defaultContentType: 'application/json' };
108+
const d = new AsyncAPIDocument(doc);
109+
expect(d.hasDefaultContentType()).to.be.equal(true);
110+
});
111+
112+
it('should return false if field does not exist', function() {
113+
const doc = {};
114+
const d = new AsyncAPIDocument(doc);
115+
expect(d.hasDefaultContentType()).to.be.equal(false);
116+
});
117+
});
118+
119+
describe('#defaultContentType()', function() {
120+
it('should return string if field exists', function() {
121+
const doc = { defaultContentType: 'application/json' };
122+
const d = new AsyncAPIDocument(doc);
123+
expect(d.defaultContentType()).to.be.equal('application/json');
124+
});
125+
126+
it('should return null if field does not exist', function() {
127+
const doc = {};
128+
const d = new AsyncAPIDocument(doc);
129+
expect(d.defaultContentType()).to.be.equal(null);
130+
});
131+
});
132+
82133
describe('#hasChannels()', function() {
83134
it('should return a boolean indicating if the AsyncAPI document has channels', function() {
84135
const doc = { channels: { test1: { description: 'test1' }, test2: { description: 'test2' } } };
@@ -100,6 +151,13 @@ describe('AsyncAPIDocument', function() {
100151
expect(d.channels().test2.constructor.name).to.equal('Channel');
101152
expect(d.channels().test2.json()).to.equal(doc.channels.test2);
102153
});
154+
155+
it('should return an empty object if the AsyncAPI document has no defined channels', function() {
156+
const doc = {};
157+
const d = new AsyncAPIDocument(doc);
158+
expect(typeof d.channels()).to.be.equal('object');
159+
expect(d.servers()).to.deep.equal({});
160+
});
103161
});
104162

105163
describe('#channelNames()', function() {
@@ -109,6 +167,13 @@ describe('AsyncAPIDocument', function() {
109167
expect(Array.isArray(d.channelNames())).to.be.equal(true);
110168
expect(d.channelNames()).to.deep.equal(['test1', 'test2']);
111169
});
170+
171+
it('should return an empty array if the AsyncAPI document has no defined channels', function() {
172+
const doc = {};
173+
const d = new AsyncAPIDocument(doc);
174+
expect(Array.isArray(d.channelNames())).to.be.equal(true);
175+
expect(d.channelNames()).to.deep.equal([]);
176+
});
112177
});
113178

114179
describe('#channel()', function() {

0 commit comments

Comments
 (0)