haskell-servant / servant

Main repository for the servant libraries — DSL for describing, serving, querying, mocking, documenting web applications and more!
https://docs.servant.dev/
1.8k stars 407 forks source link

add servant-queryparam #1678

Closed deemp closed 1 year ago

deemp commented 1 year ago

Hi! I'd like to add here the servant-queryparam (ex - named-servant) family of libraries. These libraries add support for QueryParams as records or named parameters.

What I changed:

I first used a modified version of the symbols package that I provided using a Nix flake. I later moved the type family from https://github.com/kcsongor/symbols/pull/4 to servant-named-core. So, technically, a flake is not necessary to build servant-named, but it can improve the build time.

Example

servant-named-server provides an example of constructing an OpenAPI3 specification for an API with query parameters.

To run the example, run nix develop -c cabal run servant-named-server:exe:example.

The output should look like

---
Query parameters without prefixes
---

{
    "components": {},
    "info": {
        "title": "",
        "version": ""
    },
    "openapi": "3.0.0",
    "paths": {
        "/get": {
            "get": {
                "parameters": [
                    {
                        "in": "query",
                        "name": "user",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "content": {
                            "application/json;charset=utf-8": {
                                "schema": {
                                    "maximum": 9223372036854775807,
                                    "minimum": -9223372036854775808,
                                    "type": "integer"
                                }
                            }
                        },
                        "description": ""
                    },
                    "400": {
                        "description": "Invalid `user`"
                    }
                }
            }
        }
    }
}

---
Query parameters with prefixes
---

{
    "components": {},
    "info": {
        "title": "",
        "version": ""
    },
    "openapi": "3.0.0",
    "paths": {
        "/get": {
            "get": {
                "parameters": [
                    {
                        "in": "query",
                        "name": "_get_user",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "content": {
                            "application/json;charset=utf-8": {
                                "schema": {
                                    "maximum": 9223372036854775807,
                                    "minimum": -9223372036854775808,
                                    "type": "integer"
                                }
                            }
                        },
                        "description": ""
                    },
                    "400": {
                        "description": "Invalid `_get_user`"
                    }
                }
            }
        }
    }
}
maksbotan commented 1 year ago

@tchoutri what do you think about the inclusion of this package in servant repo?

deemp commented 1 year ago

I decided to make a separate repo https://github.com/deemp/servant-queryparam