@@ -8,8 +8,8 @@ const { AList } = require('./lib/AList.js');
8
8
// TODO add a memoized cache
9
9
10
10
function clean ( obj , property ) {
11
- if ( obj && ( typeof obj [ property ] === 'object' ) && ( ! Object . keys ( obj [ property ] ) . length ) ) {
12
- delete obj [ property ] ;
11
+ if ( obj && ( ! Array . isArray ( obj [ property ] ) ) && ( typeof obj [ property ] === 'object' ) && ( ! Object . keys ( obj [ property ] ) . length ) ) {
12
+ if ( property !== 'paths' ) delete obj [ property ] ;
13
13
}
14
14
return obj ;
15
15
}
@@ -36,10 +36,12 @@ function extract(obj, options) {
36
36
37
37
const defaults = { } ;
38
38
defaults . info = false ;
39
+ defaults . removeDocs = false ;
39
40
defaults . removeExamples = false ;
40
41
defaults . removeExtensions = false ;
41
42
defaults . server = false ;
42
43
defaults . security = false ;
44
+ defaults . openai = false ;
43
45
defaults . operationid = [ ] ;
44
46
options = Object . assign ( { } , defaults , options ) ;
45
47
@@ -116,13 +118,13 @@ function extract(obj, options) {
116
118
}
117
119
else {
118
120
paths = { } ;
119
- paths [ options . path ] = { } ;
121
+ if ( options . path ) paths [ options . path ] = { } ;
120
122
if ( options . method ) paths [ options . path ] [ options . method ] = { } ;
121
123
if ( options . method && obj . paths [ options . path ] [ options . method ] ) {
122
124
paths [ options . path ] [ options . method ] = clone ( obj . paths [ options . path ] [ options . method ] ) ;
123
125
deref ( paths [ options . path ] [ options . method ] , src , obj ) ;
124
126
}
125
- else {
127
+ else if ( options . path ) {
126
128
for ( let o in obj . paths [ options . path ] ) {
127
129
if ( ( o !== 'description' ) && ( o !== 'summary' ) &&
128
130
( ! o . startsWith ( 'x-' ) ) ) {
@@ -161,6 +163,15 @@ function extract(obj, options) {
161
163
deref ( src . parameters , src , obj ) ;
162
164
deref ( src . components , src , obj ) ;
163
165
166
+ if ( options . openai ) {
167
+ recurse ( src , { } , function ( obj , key , state ) {
168
+ if ( obj && key === 'description' && obj . description . length > 300 ) {
169
+ state . parent [ state . pkey ] . description = obj . description . substring ( 0 , 300 ) ;
170
+ }
171
+ } ) ;
172
+ }
173
+
174
+ clean ( src , 'paths' ) ;
164
175
clean ( src , 'definitions' ) ;
165
176
clean ( src , 'headers' ) ;
166
177
clean ( src , 'responses' ) ;
@@ -171,20 +182,26 @@ function extract(obj, options) {
171
182
clean ( src . components , 'schemas' ) ;
172
183
clean ( src , 'components' ) ;
173
184
185
+ const al = new AList ( src ) ;
174
186
if ( options . removeExamples ) {
175
- const al = new AList ( src ) ;
176
187
for ( let [ value , parents ] of al ) {
177
188
AList . deleteProperty ( value , 'example' ) ;
178
189
AList . deleteProperty ( value , 'examples' ) ;
179
190
}
180
191
}
181
192
182
193
if ( options . removeExtensions ) {
183
- const al = new AList ( src ) ;
184
194
for ( let [ value , parents ] of al ) {
185
195
AList . deletePrefix ( value , 'x-' ) ;
186
196
}
187
197
}
198
+
199
+ if ( options . removeDocs ) {
200
+ for ( let [ value , parents ] of al ) {
201
+ AList . deleteProperty ( value , 'externalDocs' ) ;
202
+ }
203
+ }
204
+
188
205
return src ;
189
206
}
190
207
0 commit comments