Skip to content

Billuc/convert_http_query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

convert_http_query

Package Version Hex Docs

Encode and decode HTTP queries from/to Gleam types

Easily convert your Gleam types to HTTP queries. This is particularly useful to have typed queries automatically encoded in your frontend and decoded in your backend.

Installation

gleam add convert_http_query@1

Usage

import convert
import convert/http/query

pub type User {
  User(name: String, age: Int, children: List(String))
}

pub fn user_converter() {
  convert.object({
    use name <- convert.field("name", fn (v: User) { Ok(v.name) }, convert.string())
    use age <- convert.field("age", fn (v: User) { Ok(v.age) }, convert.int())
    use children <- convert.field("children", fn (v: User) { Ok(v.children) }, convert.list(convert.string()))

    convert.success(User(name:, age:, children:))
  })
}

pub fn main() {
  User("John", 42, ["Alice", "Bob"])
  |> query.encode(user_converter())
  // => [
  //  #("name", "John"),
  //  #("age", "42"),
  //  #("children.0", "Alice"),
  //  #("children.1", "Bob"),
  // ]

  [
    #("name", "Thomas"),
    #("age", "27"),
  ]
  |> query.decode(user_converter())
  // => User("Thomas", 27, [])
}

Further documentation can be found at https://hexdocs.pm/convert_http_query.

Features

  • Encode and decode HTTP queries from and to Gleam types
  • Support for all Gleam primitive types
  • Support for lists, results, optionals, dicts
  • Allow decoding to objects and enums

Limitations

Not all dict keys are supported. Currently, only primitive types such as Bool, Int, Float, String and BitArray.

Development

gleam run   # Run the project
gleam test  # Run the tests

About

Encode and decode HTTP queries from/to Gleam types automatically

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages