@@ -3,6 +3,7 @@ const querystring = require('querystring');
33const extensions = require ( '@readme/oas-extensions' ) ;
44const getSchema = require ( './get-schema' ) ;
55const configureSecurity = require ( './configure-security' ) ;
6+ const removeUndefinedObjects = require ( './remove-undefined-objects' ) ;
67
78// const format = {
89// value: v => `__START_VALUE__${v}__END__`,
@@ -74,42 +75,6 @@ function isPrimitive(val) {
7475 return typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean' ;
7576}
7677
77- function isEmptyObject ( obj ) {
78- // Then remove all empty objects from the top level object
79- return typeof obj === 'object' && Object . keys ( obj ) . length === 0 ;
80- }
81-
82- // Modified from here: https://stackoverflow.com/a/43781499
83- function stripEmptyObjects ( obj ) {
84- Object . keys ( obj ) . forEach ( key => {
85- const value = obj [ key ] ;
86- if ( typeof value === 'object' ) {
87- // Recurse, strip out empty objects from children
88- stripEmptyObjects ( value ) ;
89- // Then remove all empty objects from the top level object
90- if ( isEmptyObject ( value ) ) {
91- delete obj [ key ] ;
92- }
93- }
94- } ) ;
95- }
96-
97- function removeUndefinedObjects ( obj ) {
98- // JSON.stringify removes undefined values
99- const withoutUndefined = JSON . parse ( JSON . stringify ( obj ) ) ;
100-
101- // Then we recursively remove all empty objects
102- stripEmptyObjects ( withoutUndefined ) ;
103-
104- // If the only thing that's leftover is an empty object
105- // then return nothing so we don't end up with default
106- // code samples with:
107- // --data '{}'
108- if ( isEmptyObject ( withoutUndefined ) ) return undefined ;
109-
110- return withoutUndefined ;
111- }
112-
11378module . exports = (
11479 oas ,
11580 pathOperation = { path : '' , method : '' } ,
0 commit comments