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

Filter out undefined query parameters and headers #209

Merged
merged 4 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EndpointDefaults, RequestParameters, Route } from "@octokit/types";

import { lowercaseKeys } from "./util/lowercase-keys";
import { mergeDeep } from "./util/merge-deep";
import { removeUndefinedProperties } from "./util/remove-undefined-properties";

export function merge(
defaults: EndpointDefaults | null,
Expand All @@ -18,6 +19,10 @@ export function merge(
// lowercase header names before merging with defaults to avoid duplicates
options.headers = lowercaseKeys(options.headers);

// remove properties with undefined values before merging
options.headers = removeUndefinedProperties(options.headers);
options = removeUndefinedProperties(options);

const mergedOptions = mergeDeep(defaults || {}, options) as EndpointDefaults;

// mediaType.previews arrays are merged, instead of overwritten
Expand Down
9 changes: 9 additions & 0 deletions src/util/remove-undefined-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function removeUndefinedProperties(obj: any): any {
const copy = Object.assign({}, obj);
for (const key in copy) {
if (copy[key] === undefined) {
delete copy[key];
}
}
return copy;
}
gr2m marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions test/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,33 @@ describe("endpoint()", () => {
},
});
});

it("Undefined query parameter", () => {
const options = endpoint({
method: "GET",
url: "/notifications",
before: undefined,
});

expect(options).toEqual({
method: "GET",
url: "https://api.github.com/notifications",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});
});

it("Undefined header value", () => {
const options = endpoint({
method: "GET",
url: "/notifications",
headers: {
"if-modified-since": undefined,
},
});

expect(options).not.toHaveProperty("headers.if-modified-since");
});
});