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
+ }
45
54
}
55
+ return found ;
56
+ } ;
46
57
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 ;
50
79
}
51
80
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.
53
82
let check = ( tag ) => {
54
83
let found = false ;
55
84
for ( let tagToCheckIndex in tagsToCheck ) {
@@ -61,99 +90,13 @@ module.exports = ({ Nunjucks }) => {
61
90
return found ;
62
91
} ;
63
92
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 ;
159
100
} ;
101
+ filter . containNoTag = containNoTag ;
102
+
0 commit comments