Skip to content

Commit

Permalink
fix: filter out undefined query parameters and headers (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio authored Oct 5, 2020
1 parent 0e84bb2 commit 50d1b53
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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
removeUndefinedProperties(options);
removeUndefinedProperties(options.headers);

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

// mediaType.previews arrays are merged, instead of overwritten
Expand Down
8 changes: 8 additions & 0 deletions src/util/remove-undefined-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function removeUndefinedProperties(obj: any): any {
for (const key in obj) {
if (obj[key] === undefined) {
delete obj[key];
}
}
return obj;
}
29 changes: 29 additions & 0 deletions test/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,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");
});
});

0 comments on commit 50d1b53

Please sign in to comment.