Skip to content

Commit d5602dc

Browse files
authored
fix: remove private members in HttpRequest and HttpResponse (#737)
Sometimes yarn/npm will install different version of protocol-http package under individual clients and root node_modules because of hoisting policy. Private class member will block the ts compiling Reference: microsoft/TypeScript#18499
1 parent ab2f5a7 commit d5602dc

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

Diff for: packages/protocol-http/src/httpRequest.ts

+30-26
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class HttpRequest implements HttpMessage, Endpoint {
6060
if (this.port) {
6161
hostname += `:${this.port}`;
6262
}
63-
let queryString = this.query ? this.buildQueryString() : "";
63+
let queryString = this.query ? buildQueryString(this.query) : "";
6464
if (queryString && queryString[0] !== "?") {
6565
queryString = `?${queryString}`;
6666
}
@@ -72,41 +72,45 @@ export class HttpRequest implements HttpMessage, Endpoint {
7272
...this,
7373
headers: { ...this.headers }
7474
});
75-
if (cloned.query) cloned.query = this.cloneQuery(cloned.query);
75+
if (cloned.query) cloned.query = cloneQuery(cloned.query);
7676
return cloned;
7777
}
78+
}
7879

79-
private cloneQuery(query: QueryParameterBag): QueryParameterBag {
80-
return Object.keys(query).reduce(
81-
(carry: QueryParameterBag, paramName: string) => {
82-
const param = query[paramName];
83-
return {
84-
...carry,
85-
[paramName]: Array.isArray(param) ? [...param] : param
86-
};
87-
},
88-
{}
89-
);
90-
}
80+
function cloneQuery(query: QueryParameterBag): QueryParameterBag {
81+
return Object.keys(query).reduce(
82+
(carry: QueryParameterBag, paramName: string) => {
83+
const param = query[paramName];
84+
return {
85+
...carry,
86+
[paramName]: Array.isArray(param) ? [...param] : param
87+
};
88+
},
89+
{}
90+
);
91+
}
9192

92-
private buildQueryString(): string {
93-
const parts: string[] = [];
94-
for (let key of Object.keys(this.query || {}).sort()) {
95-
const value = this.query[key];
96-
key = escapeUri(key);
93+
function buildQueryString(query: QueryParameterBag): string {
94+
const queryEntries = Object.entries(query || ({} as QueryParameterBag))
95+
.map(([key, value]): [string, string | Array<string> | null] => [
96+
escapeUri(key),
97+
value
98+
])
99+
.map(([key, value]) => {
97100
if (Array.isArray(value)) {
98-
for (let i = 0, iLen = value.length; i < iLen; i++) {
99-
parts.push(`${key}=${escapeUri(value[i])}`);
100-
}
101+
return value.map(val => `${key}=${escapeUri(val)}`);
101102
} else {
102103
let qsEntry = key;
103104
if (value || typeof value === "string") {
104105
qsEntry += `=${escapeUri(value)}`;
105106
}
106-
parts.push(qsEntry);
107+
return [qsEntry];
107108
}
108-
}
109+
})
110+
.reduce((accummulator, entry) => {
111+
accummulator.push(...entry);
112+
return accummulator;
113+
}, [] as Array<String>);
109114

110-
return parts.join("&");
111-
}
115+
return queryEntries.join("&");
112116
}

0 commit comments

Comments
 (0)