bs-query-string

0.1.1 • Public • Published

bs-query-string

Simple reasonml solution for dealing with query strings.


Inspired by bs-json, BsQueryString.Parse works similarly to Json.Decode and BsQueryString.Stringify similarly to Json.Encode.

Installation

npm i --save bs-query-string

Update your bsconfig.json:

"bs-dependencies": [
  ...
+ "bs-query-string",
]

Parse query string

Example of parsing a query string into a record:

type productSearch = {
  query: string,
  sizes: array(string),
};

let parseQs = (qs: string): t => {
  let obj = BsQueryString.Parse.toQueryObj(qs);
  BsQueryString.Parse.{
    query: obj |> item("q", string),
    sizes: obj |> item("color", array),
  };
};

item will throw if the key is not found, or if multiple values were received for a single string value. You can use optional to return None or withDefault to assign default value:

type productSearch = {
  query: option(string),
  sizes: array(string),
};

let parseQs = (qs: string): t => {
  let obj = BsQueryString.Parse.toQueryObj(qs);
  BsQueryString.Parse.{
    query: obj |> optional(item("q", string)),
    sizes: obj |> withDefault([||], item("color", array)),
  };
};

This will produce following results:

// "q=jeans&size=S&size=XL"
let search = { q: Some("jeans"), sizes: [|"S", "XL"|] };

// "size=S&size=XL"
let search = { q: None, sizes: [|"S", "XL"|] };

// ""
let search = { q: None, sizes: [||] };

Stringify

Example of stringifying a record into a query string:

type productSearch = {
  query: option(string),
  sizes: array(string),
};

let stringifySearch = (search: productSearch) => {
  Stringify.(
    toQs([|
      ("query", optional(search.query, string)),
      ("array", item(search.sizes, array)),
    |])
  );
};

optional will not include the key if the corresponding value is None.

Package Sidebar

Install

npm i bs-query-string

Weekly Downloads

750

Version

0.1.1

License

MIT

Unpacked Size

13.7 kB

Total Files

12

Last publish

Collaborators

  • margaretkrutikova