Skip to content

Commit

Permalink
fix: remove private members in HttpRequest and HttpResponse (#737)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AllanZhengYP authored Jan 15, 2020
1 parent ab2f5a7 commit d5602dc
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions packages/protocol-http/src/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class HttpRequest implements HttpMessage, Endpoint {
if (this.port) {
hostname += `:${this.port}`;
}
let queryString = this.query ? this.buildQueryString() : "";
let queryString = this.query ? buildQueryString(this.query) : "";
if (queryString && queryString[0] !== "?") {
queryString = `?${queryString}`;
}
Expand All @@ -72,41 +72,45 @@ export class HttpRequest implements HttpMessage, Endpoint {
...this,
headers: { ...this.headers }
});
if (cloned.query) cloned.query = this.cloneQuery(cloned.query);
if (cloned.query) cloned.query = cloneQuery(cloned.query);
return cloned;
}
}

private cloneQuery(query: QueryParameterBag): QueryParameterBag {
return Object.keys(query).reduce(
(carry: QueryParameterBag, paramName: string) => {
const param = query[paramName];
return {
...carry,
[paramName]: Array.isArray(param) ? [...param] : param
};
},
{}
);
}
function cloneQuery(query: QueryParameterBag): QueryParameterBag {
return Object.keys(query).reduce(
(carry: QueryParameterBag, paramName: string) => {
const param = query[paramName];
return {
...carry,
[paramName]: Array.isArray(param) ? [...param] : param
};
},
{}
);
}

private buildQueryString(): string {
const parts: string[] = [];
for (let key of Object.keys(this.query || {}).sort()) {
const value = this.query[key];
key = escapeUri(key);
function buildQueryString(query: QueryParameterBag): string {
const queryEntries = Object.entries(query || ({} as QueryParameterBag))
.map(([key, value]): [string, string | Array<string> | null] => [
escapeUri(key),
value
])
.map(([key, value]) => {
if (Array.isArray(value)) {
for (let i = 0, iLen = value.length; i < iLen; i++) {
parts.push(`${key}=${escapeUri(value[i])}`);
}
return value.map(val => `${key}=${escapeUri(val)}`);
} else {
let qsEntry = key;
if (value || typeof value === "string") {
qsEntry += `=${escapeUri(value)}`;
}
parts.push(qsEntry);
return [qsEntry];
}
}
})
.reduce((accummulator, entry) => {
accummulator.push(...entry);
return accummulator;
}, [] as Array<String>);

return parts.join("&");
}
return queryEntries.join("&");
}

0 comments on commit d5602dc

Please sign in to comment.