Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions templates/base/http-clients/fetch-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,35 @@ export class HttpClient<SecurityDataType = unknown> {
return this.encodeQueryParam(key, query[key]);
}

protected isPlainObject(value: unknown): value is Record<string, unknown> {
if (value === null || typeof value !== "object" || Array.isArray(value)) {
return false;
}

const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}

protected addArrayQueryParam(query: QueryParamsType, key: string) {
const value = query[key];
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
const rawValue = query[key];
const value = Array.isArray(rawValue)
? rawValue.map((value, index) => [index, value])
: Object.entries(rawValue as Record<string, unknown>);
return value.map(([arrayKey, value]: any) => this.encodeQueryParam(`${key}[${arrayKey}]`, value)).join("&");
}

protected toQueryString(rawQuery?: QueryParamsType): string {
const query = rawQuery || {};
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
return keys
.map((key) =>
Array.isArray(query[key])
.map((key) => {
const value = query[key];

return Array.isArray(value) || this.isPlainObject(value)
? this.addArrayQueryParam(query, key)
: this.addQueryParam(query, key),
)
.join("&");
: this.addQueryParam(query, key);
})
.join("&");
}

protected addQueryParams(rawQuery?: QueryParamsType): string {
Expand Down
Loading