Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sporto/gleam-qs
A query string parser for Gleam
https://github.com/sporto/gleam-qs
gleam gleam-lang
Last synced: about 1 month ago
JSON representation
A query string parser for Gleam
- Host: GitHub
- URL: https://github.com/sporto/gleam-qs
- Owner: sporto
- Created: 2021-05-19T02:12:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-21T10:42:05.000Z (5 months ago)
- Last Synced: 2024-08-16T17:23:19.877Z (4 months ago)
- Topics: gleam, gleam-lang
- Language: Gleam
- Homepage: https://hexdocs.pm/gleam_qs
- Size: 61.5 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Gleam QS
A query string parser for Gleam.
## Install
```
gleam add qs
```## Usage
QS has two modules `qs` (Basic) and `qs_adv` (Advance).
### Basic
A query `(QueryBasic)` in basic is a `Dict(String, List(String))`.
Basic has not concept of single or list values. Every value is a `List(String)`.
### Advanced
The Advance module provides configuration to explicitly define how single and list values are serialized.
A query `(QueryAdv)` is a `Dict(String, OneOrMany)`.
### Basic parsing
```gleam
import qs
import gleam/dict"?color=red&pet=cat&pet=dog"
|> qs.default_parse==
Ok(
dict.from_list(
[
#("color", ["red"]),
#("pet", ["cat", "dog"]),
]
)
)
```### Basic serialization
```gleam
import qs
import gleam/dictlet query = [
#("color", ["red"]),
#("pet", ["cat", "dog"])
]
|> dict.from_listqs.default_serialize(query)
> "?color=red&pet=cat&pet=dog"
```### Advanced parsing
By default advanced uses a Rails like query scheme. E.g.
```gleam
import qs_adv
import gleam/dict"?color=red&pets[]=cat&pets[]=dog"
|> qs_adv.default_parse==
Ok(
dict.from_list(
[
#("color", One("red")),
#("pets", Many(["cat", "dog"])),
]
)
)
```But this is configurable.
```gleam
let scheme = qs_adv.SchemeListAsSingleValue(
list_suffix: "[]",
separator: "|"
)let config = qs_adv.default_config()
|> qs_adv.with_scheme(scheme)"?color=red&pets[]=cat|dog"
|> qs_adv.parse(config)
```### Advanced serialization
```gleam
import qs_adv
import gleam/dictlet query = [
#("color", ["red"]),
#("pets", ["cat", "dog"])
]
|> dict.from_listqs_adv.default_serialize(query)
> "?color=red&pets[]=cat&pets[]=dog"
```