question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query strings should be modeled as a multimap

See original GitHub issue

As indicated by the Wikipedia page Query string:

While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field (e.g. field1=value1&field1=value2&field2=value3).

And indeed, many web frameworks I have used adopt this approach:

Therefore, I suggest itty-router follow the same convention.

Currently, itty-router models query strings as a Obj:

https://github.com/kwhitley/itty-router/blob/e8e76c9be5496ce2494743fb20dff802c41eb176/src/itty-router.d.ts#L22

where Obj is actually a map from some string to some string:

https://github.com/kwhitley/itty-router/blob/e8e76c9be5496ce2494743fb20dff802c41eb176/src/itty-router.d.ts#L1-L3

I suggest introducing MultiObj as the data model of query strings:

export type MultiObj = {
    [propName: string]: string | Array<string>
}

I’d like to help if you @kwhitley are interested in this 😃

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
oliverjamcommented, Nov 12, 2021

Would it not be better to stick to web standard APIs, since CF Workers/itty already rely on Request, Response etc? The URLSearchParams interface is specifically designed for this:

let query = new URLSearchParams("field1=value1&field1=value2&field2=value3");

query.get("field2"); // "value3"
query.get("field1") // "value1" (picks the first)
query.getAll("field1"); // ["value1", "value2"] (array of all matches)
query.getAll("field2"); // ["value3"] (always an array for type consistency)

for (let [name, value] of query) {
  console.log(name, value);
}
// field1 value1
// field1 value2
// field2 value3
1reaction
kwhitleycommented, Sep 17, 2022

This will be included in the next major release 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Multimap instead of Map for sending parameters for Rest ...
1 a Map and Set< String > should do the job: com.ning.http.client.FluentStringsMap map = new com.ning.http.client.
Read more >
Use MultiMap instead of HashMap for the parsed query params ...
Use multimap for the parsed query params instead of HashMap<String, Vec<String>> . Mulitmap is basically a thin wrapper around HashMap<T, Vec<T>> , but...
Read more >
How to make multiple values per key in a Java map possible ...
Need to add multiple values to a key in a Java HashMap but can't figure out ... Multimap<String, String> map = ArrayListMultimap.create(); ...
Read more >
Multiple Mapping - Dapper .NET - Medium
With Multiple Mapping we can easily handle mapping of object between our database and our object model that supports 1:1 relationships between ...
Read more >
How to Use Query Strings and Parameters for Marketers
A key-value pair can be separated into a key, a value, and a separator. The separator is the equals sign ( = )....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found