@@ -15,6 +15,15 @@ export type PrimitiveType = string | number | boolean | Date | utils.Date | util
15
15
/** Supported types for the operation parameters - primitives, primitive arrays, and simple non-nested objects */
16
16
export type SupportedParamType = PrimitiveType | PrimitiveType [ ] | { [ key : string ] : PrimitiveType } ;
17
17
18
+ /** URL encoding of space character, delimiter for spaceDelimited style */
19
+ const SPACE_URL_CODE = encodeURIComponent ( ' ' ) ;
20
+ /** URL encoding of pipe character, delimiter for pipeDelimited style */
21
+ const PIPE_URL_CODE = encodeURIComponent ( '|' ) ;
22
+ /** URL encoding of opening square bracket, used in deepObject style */
23
+ const OPENING_SQUARE_BRACKET_URL_CODE = encodeURIComponent ( '[' ) ;
24
+ /** URL encoding of closing square bracket, used in deepObject style */
25
+ const CLOSING_SQUARE_BRACKET_URL_CODE = encodeURIComponent ( ']' ) ;
26
+
18
27
/**
19
28
* Verify if property is of type utils.Date or utils.DateTime
20
29
* @param prop
@@ -23,6 +32,31 @@ export function isDateType(prop: any): prop is Date | utils.Date | utils.DateTim
23
32
return prop instanceof Date || prop instanceof utils . Date || prop instanceof utils . DateTime ;
24
33
}
25
34
35
+ /**
36
+ * Check if the parameter is a record of type <string, string>.
37
+ * @param param
38
+ */
39
+ export function isParamValueRecord ( param : any ) : param is { [ key : string ] : string } {
40
+ return typeof param === 'object' && Object . values ( param ) . every ( ( item ) => typeof item === 'string' ) ;
41
+ }
42
+
43
+ /** Query parameter value and serialization */
44
+ export type QueryParamValueSerialization = { value : SupportedParamType } & ParamSerialization ;
45
+
46
+ /**
47
+ * Serialize query parameters of request plugins
48
+ * @param queryParams
49
+ */
50
+ export function serializeRequestPluginQueryParams ( queryParams : { [ key : string ] : QueryParamValueSerialization } ) {
51
+ const queryParamsValues : { [ key : string ] : SupportedParamType } = { } ;
52
+ const queryParamSerialization : { [ key : string ] : ParamSerialization } = { } ;
53
+ Object . entries ( queryParams ) . forEach ( ( [ paramKey , paramValue ] ) => {
54
+ queryParamsValues [ paramKey ] = paramValue . value ;
55
+ queryParamSerialization [ paramKey ] = { explode : paramValue . explode , style : paramValue . style } ;
56
+ } ) ;
57
+ return serializeQueryParams ( queryParamsValues , queryParamSerialization ) ;
58
+ }
59
+
26
60
/**
27
61
* Serialize query parameters of type array
28
62
* OpenAPI Parameter Serialization {@link https://swagger.io/specification | documentation}
@@ -46,13 +80,13 @@ function serializeArrayQueryParams(queryParamName: string, queryParamValue: Prim
46
80
if ( emptyArray ) {
47
81
break ;
48
82
}
49
- return encodeURIComponent ( queryParamName ) + '=' + filteredArray . map ( ( v ) => isDateType ( v ) ? v . toJSON ( ) : encodeURIComponent ( v . toString ( ) ) ) . join ( '%20' ) ;
83
+ return encodeURIComponent ( queryParamName ) + '=' + filteredArray . map ( ( v ) => isDateType ( v ) ? v . toJSON ( ) : encodeURIComponent ( v . toString ( ) ) ) . join ( SPACE_URL_CODE ) ;
50
84
}
51
85
case 'pipeDelimited' : {
52
86
if ( emptyArray ) {
53
87
break ;
54
88
}
55
- return encodeURIComponent ( queryParamName ) + '=' + filteredArray . map ( ( v ) => isDateType ( v ) ? v . toJSON ( ) : encodeURIComponent ( v . toString ( ) ) ) . join ( '%7C' ) ;
89
+ return encodeURIComponent ( queryParamName ) + '=' + filteredArray . map ( ( v ) => isDateType ( v ) ? v . toJSON ( ) : encodeURIComponent ( v . toString ( ) ) ) . join ( PIPE_URL_CODE ) ;
56
90
}
57
91
}
58
92
}
@@ -81,15 +115,16 @@ function serializeObjectQueryParams(queryParamName: string, queryParamValue: { [
81
115
encodeURIComponent ( propName ) + ',' + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) ) ) . join ( ',' ) ;
82
116
} else if ( paramSerialization . style === 'spaceDelimited' && ! paramSerialization . explode && ! emptyObject ) {
83
117
return encodeURIComponent ( queryParamName ) + '=' + Object . entries ( filteredObject ) . map ( ( [ propName , propValue ] ) =>
84
- encodeURIComponent ( propName ) + '%20' + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) )
85
- ) . join ( '%20' ) ;
118
+ encodeURIComponent ( propName ) + SPACE_URL_CODE + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) )
119
+ ) . join ( SPACE_URL_CODE ) ;
86
120
} else if ( paramSerialization . style === 'pipeDelimited' && ! paramSerialization . explode && ! emptyObject ) {
87
121
return encodeURIComponent ( queryParamName ) + '=' + Object . entries ( filteredObject ) . map ( ( [ propName , propValue ] ) =>
88
- encodeURIComponent ( propName ) + '%7C' + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) )
89
- ) . join ( '%7C' ) ;
122
+ encodeURIComponent ( propName ) + PIPE_URL_CODE + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) )
123
+ ) . join ( PIPE_URL_CODE ) ;
90
124
} else if ( paramSerialization . style === 'deepObject' && paramSerialization . explode && ! emptyObject ) {
91
125
return Object . entries ( filteredObject ) . map ( ( [ propName , propValue ] ) =>
92
- encodeURIComponent ( queryParamName ) + '%5B' + encodeURIComponent ( propName ) + '%5D=' + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) )
126
+ encodeURIComponent ( queryParamName ) + OPENING_SQUARE_BRACKET_URL_CODE + encodeURIComponent ( propName ) + CLOSING_SQUARE_BRACKET_URL_CODE + '='
127
+ + ( isDateType ( propValue ) ? propValue . toJSON ( ) : encodeURIComponent ( propValue . toString ( ) ) )
93
128
) . join ( '&' ) ;
94
129
}
95
130
}
@@ -110,9 +145,8 @@ export function serializeQueryParams<T extends { [key: string]: SupportedParamTy
110
145
} else if ( typeof queryParamValue === 'object' && ! isDateType ( queryParamValue ) ) {
111
146
serializedValue = serializeObjectQueryParams ( queryParamName , queryParamValue , paramSerialization ) ;
112
147
} else {
113
- if ( paramSerialization . style === 'form' ) {
114
- serializedValue = encodeURIComponent ( queryParamName ) + '=' + ( isDateType ( queryParamValue ) ? queryParamValue . toJSON ( ) : encodeURIComponent ( queryParamValue . toString ( ) ) ) ;
115
- }
148
+ // NOTE: 'form' style is the default value for primitive types
149
+ serializedValue = encodeURIComponent ( queryParamName ) + '=' + ( isDateType ( queryParamValue ) ? queryParamValue . toJSON ( ) : encodeURIComponent ( queryParamValue . toString ( ) ) ) ;
116
150
}
117
151
if ( serializedValue ) {
118
152
acc [ queryParamName as keyof T ] = serializedValue ;
0 commit comments