-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: query string array support (#182)
* fix: query string array support * fix: remove elvis operator * fix: use the objectToQueryString function at tepper runner * chore: bump version to 0.4.2 * fix: skip undefined values
- Loading branch information
1 parent
6ca1f1a
commit b3b475a
Showing
7 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { objectToQueryString } from "./objectToQueryString" | ||
|
||
describe("objectToQueryString", () => { | ||
it("serializes a query with strings", () => { | ||
const query = { | ||
hello: "world", | ||
} | ||
|
||
const result = objectToQueryString(query) | ||
|
||
expect(result).toEqual("hello=world") | ||
}) | ||
|
||
it("serializes a query with an array", () => { | ||
const query = { | ||
tags: ["first-tag", "second-tag"], | ||
} | ||
|
||
const result = objectToQueryString(query) | ||
|
||
expect(result).toEqual("tags[]=first-tag&tags[]=second-tag") | ||
}) | ||
|
||
it("skips undefined values", () => { | ||
const query = { empty: undefined } | ||
|
||
const result = objectToQueryString(query) | ||
|
||
expect(result).toEqual("") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { URLSearchParams } from "url" | ||
|
||
export function objectToQueryString(query: object) { | ||
const params = new URLSearchParams() | ||
const arrayIndicator = "__array_indicator__" | ||
|
||
for (const [key, value] of Object.entries(query)) { | ||
if (Array.isArray(value)) { | ||
for (const v of value) { | ||
params.append(key + arrayIndicator, v) | ||
} | ||
} else if (value != null) { | ||
params.append(key, (value && value.toString()) || "") | ||
} | ||
} | ||
|
||
return params.toString().replace(new RegExp(arrayIndicator, "g"), "[]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters