Skip to content

Commit abb14de

Browse files
committed
Implement URL parameter support
1 parent 2c1d317 commit abb14de

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-fetch-hook",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "React fetch hook",
55
"main": "./dist/index.js",
66
"scripts": {

src/__tests__/flattenInput.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,25 @@ describe("flattenInput", () => {
2020
it("with object recursive", () => {
2121
expect(flattenInput(1, { x: 1, y: 2, z: { x1: 3 } })).toMatchObject([1, "x", 1, "y", 2, "z", "x1", 3]);
2222
});
23+
24+
it("with URL instance", () => {
25+
const url = new URL("https://google.com");
26+
url.search = new URLSearchParams({ a: "aaa", b: "bbb" }).toString();
27+
const result = flattenInput(url, { x: 1 });
28+
expect(result).toMatchObject([
29+
"",
30+
"google.com",
31+
"google.com",
32+
"https://google.com/?a=aaa&b=bbb",
33+
"https://google.com",
34+
"",
35+
"/",
36+
"",
37+
"https:",
38+
"?a=aaa&b=bbb",
39+
"",
40+
"x",
41+
1
42+
]);
43+
});
2344
});

src/usePromise.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ export const flattenInput = (...inputs: $ReadOnlyArray<mixed>): $ReadOnlyArray<m
1616
const keys = Object.keys(input);
1717
return [...accumulator, ...flattenInput(...keys.reduce((a, k) => [...a, k, input[k]], []))];
1818
}
19+
if (input instanceof URL) {
20+
return [
21+
...accumulator,
22+
...flattenInput(
23+
input.hash,
24+
input.host,
25+
input.hostname,
26+
input.href,
27+
input.origin,
28+
input.password,
29+
input.pathname,
30+
input.port,
31+
input.protocol,
32+
input.search,
33+
input.username
34+
)
35+
];
36+
}
1937
return [...accumulator, input];
2038
}, []);
2139
};

0 commit comments

Comments
 (0)