Skip to content

Commit a8851f7

Browse files
aleksej-axiologyherman-axiology
authored andcommitted
feat: add support of objects in query params
1 parent 5df5d2a commit a8851f7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

templates/base/http-clients/fetch-http-client.ejs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ export class HttpClient<SecurityDataType = unknown> {
8181
}
8282

8383
protected addArrayQueryParam(query: QueryParamsType, key: string) {
84-
const value = query[key];
85-
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
84+
const value = Array.isArray(query[key])
85+
? query[key].map((value, index) => [index, value])
86+
: Object.entries(query[key]);
87+
return value.map(([arrayKey, value]: any) => this.encodeQueryParam(`${key}[${arrayKey}]`, value)).join("&");
8688
}
8789

8890
protected toQueryString(rawQuery?: QueryParamsType): string {
8991
const query = rawQuery || {};
9092
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
9193
return keys
92-
.map((key) =>
93-
Array.isArray(query[key])
94-
? this.addArrayQueryParam(query, key)
95-
: this.addQueryParam(query, key),
96-
)
97-
.join("&");
94+
.map((key) =>
95+
typeof query[key] === "object" || Array.isArray(query[key])
96+
? this.addArrayQueryParam(query, key)
97+
: this.addQueryParam(query, key),
98+
)
99+
.join("&");
98100
}
99101

100102
protected addQueryParams(rawQuery?: QueryParamsType): string {

0 commit comments

Comments
 (0)