Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove private members in HttpRequest and HttpResponse #737

Merged
merged 1 commit into from
Jan 15, 2020
Merged
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
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("&");
}