@@ -55,28 +55,21 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) {
55
55
}
56
56
}
57
57
58
- const decodedValue = decodeURI ( param . value ) ;
59
- const tryJson = ( ) => {
60
- try {
61
- return JSON . parse ( decodedValue ) ;
62
- } catch ( e ) {
63
- return false ;
64
- }
65
- } ;
66
-
67
- const jsonResult = tryJson ( ) ;
68
-
69
58
// Handle object values
70
- if ( jsonResult && typeof jsonResult === "object" ) {
71
- if ( param . style === "deepObject" ) {
59
+ if ( param . style === "deepObject" ) {
60
+ const jsonResult = tryDecodeJsonParam ( param . value ) ;
61
+ if ( jsonResult && typeof jsonResult === "object" ) {
72
62
return Object . entries ( jsonResult ) . map (
73
63
( [ key , val ] ) =>
74
64
new sdk . QueryParam ( {
75
65
key : `${ param . name } [${ key } ]` ,
76
66
value : String ( val ) ,
77
67
} )
78
68
) ;
79
- } else if ( param . explode ) {
69
+ }
70
+ } else if ( param . explode ) {
71
+ const jsonResult = tryDecodeJsonParam ( param . value ) ;
72
+ if ( jsonResult && typeof jsonResult === "object" ) {
80
73
return Object . entries ( jsonResult ) . map (
81
74
( [ key , val ] ) =>
82
75
new sdk . QueryParam ( {
@@ -94,17 +87,9 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) {
94
87
}
95
88
}
96
89
97
- // Handle boolean values
98
- if ( typeof decodedValue === "boolean" ) {
99
- return new sdk . QueryParam ( {
100
- key : param . name ,
101
- value : decodedValue ? "true" : "false" ,
102
- } ) ;
103
- }
104
-
105
90
// Parameter allows empty value: "/hello?extended"
106
91
if ( param . allowEmptyValue ) {
107
- if ( decodedValue === "true" ) {
92
+ if ( param . value === "true" ) {
108
93
return new sdk . QueryParam ( {
109
94
key : param . name ,
110
95
value : null ,
@@ -150,18 +135,9 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) {
150
135
} ) ;
151
136
}
152
137
153
- const decodedValue = decodeURI ( param . value ) ;
154
- const tryJson = ( ) => {
155
- try {
156
- return JSON . parse ( decodedValue ) ;
157
- } catch ( e ) {
158
- return false ;
159
- }
160
- } ;
138
+ const jsonResult = tryDecodeJsonParam ( param . value ) ;
161
139
162
- const jsonResult = tryJson ( ) ;
163
-
164
- if ( typeof jsonResult === "object" ) {
140
+ if ( jsonResult && typeof jsonResult === "object" ) {
165
141
if ( param . style === "matrix" ) {
166
142
serializedValue = Object . entries ( jsonResult )
167
143
. map ( ( [ key , val ] ) => `;${ key } =${ val } ` )
@@ -172,7 +148,7 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) {
172
148
. join ( "," ) ;
173
149
}
174
150
} else {
175
- serializedValue = decodedValue || `: ${ param . name } ` ;
151
+ serializedValue = param . value ;
176
152
}
177
153
178
154
return new sdk . Variable ( {
@@ -191,17 +167,8 @@ function buildCookie(cookieParams: Param[]) {
191
167
const cookies = cookieParams
192
168
. map ( ( param ) => {
193
169
if ( param . value ) {
194
- const decodedValue = decodeURI ( param . value as string ) ;
195
- const tryJson = ( ) => {
196
- try {
197
- return JSON . parse ( decodedValue ) ;
198
- } catch ( e ) {
199
- return false ;
200
- }
201
- } ;
202
-
203
- const jsonResult = tryJson ( ) ;
204
- if ( typeof jsonResult === "object" ) {
170
+ const jsonResult = tryDecodeJsonParam ( param . value as string ) ;
171
+ if ( jsonResult && typeof jsonResult === "object" ) {
205
172
if ( param . style === "form" ) {
206
173
// Handle form style
207
174
if ( param . explode ) {
@@ -266,16 +233,9 @@ function setHeaders(
266
233
267
234
headerParams . forEach ( ( param ) => {
268
235
if ( param . value ) {
269
- const decodedValue = decodeURI ( param . value as string ) ;
270
- const tryJson = ( ) => {
271
- try {
272
- return JSON . parse ( decodedValue ) ;
273
- } catch ( e ) {
274
- return false ;
275
- }
276
- } ;
277
-
278
- const jsonResult = tryJson ( ) ;
236
+ const jsonResult = Array . isArray ( param . value )
237
+ ? param . value . map ( tryDecodeJsonParam )
238
+ : tryDecodeJsonParam ( param . value ) ;
279
239
if ( Array . isArray ( param . value ) ) {
280
240
if ( param . style === "simple" ) {
281
241
if ( param . explode ) {
@@ -324,6 +284,14 @@ function setHeaders(
324
284
}
325
285
}
326
286
287
+ function tryDecodeJsonParam ( value : string ) : any {
288
+ try {
289
+ return JSON . parse ( decodeURI ( value ) ) ;
290
+ } catch ( e ) {
291
+ return false ;
292
+ }
293
+ }
294
+
327
295
// TODO: this is all a bit hacky
328
296
function setBody ( clonedPostman : sdk . Request , body : Body ) {
329
297
if ( clonedPostman . body === undefined ) {
0 commit comments