Skip to content

Commit 19a47fe

Browse files
authored
feat: use new generic filters (#10)
1 parent 5ce0d87 commit 19a47fe

File tree

6 files changed

+104
-150
lines changed

6 files changed

+104
-150
lines changed

.tp-config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"required": false
1010
}
1111
},
12-
"generator": ">=0.40.1 <2.0.0"
12+
"generator": ">=0.41.0 <2.0.0",
13+
"filters": [
14+
"@asyncapi/generator-filters"
15+
]
1316
}

filters/all.js

+86-143
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,84 @@
1-
const _ = require('lodash');
2-
const Markdown = require('markdown-it');
3-
const OpenAPISampler = require('openapi-sampler');
4-
5-
module.exports = ({ Nunjucks }) => {
6-
Nunjucks.addFilter('split', (string, separator) => {
7-
if (typeof string !== 'string') return string;
8-
const regex = new RegExp(separator, 'g');
9-
return string.split(regex);
10-
});
11-
12-
Nunjucks.addFilter('firstKey', (obj) => {
13-
if (!obj) return '';
14-
return Object.keys(obj)[0];
15-
});
16-
17-
Nunjucks.addFilter('isExpandable', (obj) => {
18-
if (
19-
obj.type() === 'object' ||
20-
obj.type() === 'array' ||
21-
(obj.oneOf() && obj.oneOf().length) ||
22-
(obj.anyOf() && obj.anyOf().length) ||
23-
(obj.allOf() && obj.allOf().length) ||
24-
obj.items() ||
25-
obj.additionalItems() ||
26-
(obj.properties() && Object.keys(obj.properties()).length) ||
27-
obj.additionalProperties() ||
28-
(obj.extensions() && Object.keys(obj.extensions()).filter(e => !e.startsWith('x-parser-')).length) ||
29-
obj.patternProperties()
30-
) return true;
31-
32-
return false;
33-
});
34-
35-
/**
36-
* Check if there is a channel which does not have one of the tags specified.
37-
*/
38-
Nunjucks.addFilter('containTags', (object, tagsToCheck) => {
39-
if (!object) {
40-
throw new Error("object for containsTag was not provided?");
41-
}
42-
43-
if (!tagsToCheck) {
44-
throw new Error("tagsToCheck for containsTag was not provided?");
1+
const filter = module.exports;
2+
3+
function isExpandable(obj) {
4+
if (
5+
obj.type() === 'object' ||
6+
obj.type() === 'array' ||
7+
(obj.oneOf() && obj.oneOf().length) ||
8+
(obj.anyOf() && obj.anyOf().length) ||
9+
(obj.allOf() && obj.allOf().length) ||
10+
obj.items() ||
11+
obj.additionalItems() ||
12+
(obj.properties() && Object.keys(obj.properties()).length) ||
13+
obj.additionalProperties() ||
14+
(obj.extensions() && Object.keys(obj.extensions()).filter(e => !e.startsWith('x-parser-')).length) ||
15+
obj.patternProperties()
16+
) return true;
17+
18+
return false;
19+
}
20+
filter.isExpandable = isExpandable;
21+
22+
function nonParserExtensions(schema) {
23+
if (!schema || !schema.extensions || typeof schema.extensions !== 'function') return new Map();
24+
const extensions = Object.entries(schema.extensions());
25+
return new Map(extensions.filter(e => !e[0].startsWith('x-parser-')).filter(Boolean));
26+
}
27+
filter.nonParserExtensions = nonParserExtensions;
28+
29+
/**
30+
* Check if there is a channel which does not have one of the tags specified.
31+
*/
32+
function containTags(object, tagsToCheck) {
33+
if (!object) {
34+
throw new Error("object for containsTag was not provided?");
35+
}
36+
37+
if (!tagsToCheck) {
38+
throw new Error("tagsToCheck for containsTag was not provided?");
39+
}
40+
41+
//Ensure if only 1 tag are provided it is converted to array.
42+
if (tagsToCheck && !Array.isArray(tagsToCheck)) {
43+
tagsToCheck = [tagsToCheck];
44+
}
45+
46+
//Check if pubsub contain one of the tags to check.
47+
let check = (tag) => {
48+
let found = false;
49+
for (let tagToCheckIndex in tagsToCheck) {
50+
let tagToCheck = tagsToCheck[tagToCheckIndex]._json;
51+
if (tagToCheck.name === tag.name) {
52+
found = true;
53+
}
4554
}
55+
return found;
56+
};
4657

47-
//Ensure if only 1 tag are provided it is converted to array.
48-
if (tagsToCheck && !Array.isArray(tagsToCheck)) {
49-
tagsToCheck = [tagsToCheck];
58+
//Ensure tags are checked for the group tags
59+
let containTags = object._json.tags ? object._json.tags.find(check) != null : false;
60+
return containTags;
61+
};
62+
filter.containTags = containTags;
63+
64+
/**
65+
* Check if there is a channel which does not have one of the tags specified.
66+
*/
67+
function containNoTag(channels, tagsToCheck) {
68+
if (!channels) {
69+
throw new Error("Channels for containNoTag was not provided?");
70+
}
71+
for (let channelIndex in channels) {
72+
let channel = channels[channelIndex]._json;
73+
//Check if the channel contains publish or subscribe which does not contain tags
74+
if (channel.publish && (!channel.publish.tags || channel.publish.tags.length == 0) ||
75+
channel.subscribe && (!channel.subscribe.tags || channel.subscribe.tags.length == 0)
76+
) {
77+
//one does not contain tags
78+
return true;
5079
}
5180

52-
//Check if pubsub contain one of the tags to check.
81+
//Check if channel publish or subscribe does not contain one of the tags to check.
5382
let check = (tag) => {
5483
let found = false;
5584
for (let tagToCheckIndex in tagsToCheck) {
@@ -61,99 +90,13 @@ module.exports = ({ Nunjucks }) => {
6190
return found;
6291
};
6392

64-
//Ensure tags are checked for the group tags
65-
let containTags = object._json.tags ? object._json.tags.find(check) != null : false;
66-
return containTags;
67-
});
68-
69-
/**
70-
* Check if there is a channel which does not have one of the tags specified.
71-
*/
72-
Nunjucks.addFilter('containNoTag', (channels, tagsToCheck) => {
73-
if (!channels) {
74-
throw new Error("Channels for containNoTag was not provided?");
75-
}
76-
for (let channelIndex in channels) {
77-
let channel = channels[channelIndex]._json;
78-
//Check if the channel contains publish or subscribe which does not contain tags
79-
if (channel.publish && (!channel.publish.tags || channel.publish.tags.length == 0) ||
80-
channel.subscribe && (!channel.subscribe.tags || channel.subscribe.tags.length == 0)
81-
) {
82-
//one does not contain tags
83-
return true;
84-
}
85-
86-
//Check if channel publish or subscribe does not contain one of the tags to check.
87-
let check = (tag) => {
88-
let found = false;
89-
for (let tagToCheckIndex in tagsToCheck) {
90-
let tagToCheck = tagsToCheck[tagToCheckIndex]._json;
91-
if (tagToCheck.name === tag.name) {
92-
found = true;
93-
}
94-
}
95-
return found;
96-
};
97-
98-
//Ensure pubsub tags are checked for the group tags
99-
let publishContainsNoTag = channel.publish && channel.publish.tags ? channel.publish.tags.find(check) == null : false;
100-
if (publishContainsNoTag === true) return true;
101-
let subscribeContainsNoTag = channel.subscribe && channel.subscribe.tags ? channel.subscribe.tags.find(check) == null : false;
102-
if (subscribeContainsNoTag === true) return true;
103-
}
104-
return false;
105-
});
106-
107-
Nunjucks.addFilter('isArray', (arr) => {
108-
return Array.isArray(arr);
109-
});
110-
111-
Nunjucks.addFilter('isObject', (obj) => {
112-
return typeof obj === 'object' && obj !== null;
113-
});
114-
115-
Nunjucks.addFilter('contains', (array, element) => {
116-
if (!array || !Array.isArray(array)) return false;
117-
return array.includes(element);
118-
});
119-
120-
Nunjucks.addFilter('log', (anything) => {
121-
console.log(anything);
122-
});
123-
124-
Nunjucks.addFilter('markdown2html', (md) => {
125-
return Markdown().render(md || '');
126-
});
127-
128-
Nunjucks.addFilter('getPayloadExamples', (msg) => {
129-
if (Array.isArray(msg.examples()) && msg.examples().find(e => e.payload)) {
130-
// Instead of flat or flatmap use this.
131-
return _.flatMap(msg.examples().map(e => e.payload).filter(Boolean));
132-
}
133-
134-
if (msg.payload() && msg.payload().examples()) {
135-
return msg.payload().examples();
136-
}
137-
});
138-
139-
Nunjucks.addFilter('getHeadersExamples', (msg) => {
140-
if (Array.isArray(msg.examples()) && msg.examples().find(e => e.headers)) {
141-
// Instead of flat or flatmap use this.
142-
return _.flatMap(msg.examples().map(e => e.headers).filter(Boolean));
143-
}
144-
145-
if (msg.headers() && msg.headers().examples()) {
146-
return msg.headers().examples();
147-
}
148-
});
149-
150-
Nunjucks.addFilter('generateExample', (schema) => {
151-
return JSON.stringify(OpenAPISampler.sample(schema) || '', null, 2);
152-
});
153-
154-
Nunjucks.addFilter('nonParserExtensions', (schema) => {
155-
if (!schema || !schema.extensions || typeof schema.extensions !== 'function') return new Map();
156-
const extensions = Object.entries(schema.extensions());
157-
return new Map(extensions.filter(e => !e[0].startsWith('x-parser-')).filter(Boolean));
158-
});
93+
//Ensure pubsub tags are checked for the group tags
94+
let publishContainsNoTag = channel.publish && channel.publish.tags ? channel.publish.tags.find(check) == null : false;
95+
if (publishContainsNoTag === true) return true;
96+
let subscribeContainsNoTag = channel.subscribe && channel.subscribe.tags ? channel.subscribe.tags.find(check) == null : false;
97+
if (subscribeContainsNoTag === true) return true;
98+
}
99+
return false;
159100
};
101+
filter.containNoTag = containNoTag;
102+

package-lock.json

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
"access": "public"
2929
},
3030
"dependencies": {
31-
"lodash": "^4.17.15",
32-
"markdown-it": "^10.0.0",
33-
"openapi-sampler": "^1.0.0-beta.15"
31+
"@asyncapi/generator-filters": "^1.0.0"
3432
},
3533
"devDependencies": {
3634
"@semantic-release/commit-analyzer": "^8.0.1",

partials/schema-prop.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<div class="children {% if odd %}bg-grey-lightest{% else %}bg-grey-lighter{% endif %} py-4 rounded">
108108
{% if prop.properties() %}
109109
{% for pName, p in prop.properties() %}
110-
{{ schemaProp(p, pName, odd=(not odd), required=(prop.required() | contains(pName))) }}
110+
{{ schemaProp(p, pName, odd=(not odd), required=(prop.required() | includes(pName))) }}
111111
{% endfor %}
112112
{% endif %}
113113

@@ -145,7 +145,7 @@
145145
<p class="pl-6 mb-2 text-xs font-bold uppercase text-grey-darker">Items:</p>
146146
{% if prop.items() | isArray %}
147147
{% for it in prop.items() %}
148-
{{ schemaProp(it, loop.index0, odd=(not odd), required=(prop.required() | contains(pName))) }}
148+
{{ schemaProp(it, loop.index0, odd=(not odd), required=(prop.required() | includes(pName))) }}
149149
{% endfor %}
150150
{% else %}
151151
{{ schemaProp(prop.items(), '0', odd=(not odd)) }}

partials/servers.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h5 class="text-sm text-grey mt-1">Security:</h5>
5252
<ul class="list-reset">
5353
{% for sec in server.security() %}
5454
<li>
55-
{% set def = asyncapi.components().securityScheme(sec.json() | firstKey) %}
55+
{% set def = asyncapi.components().securityScheme(sec.json() | keys | head) %}
5656

5757
<span class="font-bold no-underline text-grey-dark text-xs uppercase mr-1">
5858
{% if def.type() === 'apiKey' %}

0 commit comments

Comments
 (0)