2017-09-26 06:08:07 +00:00
|
|
|
module HTTPure.Query
|
|
|
|
( Query
|
|
|
|
, read
|
|
|
|
) where
|
|
|
|
|
2017-09-26 07:14:06 +00:00
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Data.Array as Array
|
|
|
|
import Data.Maybe as Maybe
|
|
|
|
import Data.String as String
|
|
|
|
import Data.Tuple as Tuple
|
2018-07-08 23:16:48 +00:00
|
|
|
import Foreign.Object as Object
|
2017-09-26 06:08:07 +00:00
|
|
|
import Node.HTTP as HTTP
|
|
|
|
|
2018-07-08 23:16:48 +00:00
|
|
|
-- | The `Query` type is a `Object` of `Strings`, with one entry per query
|
2017-09-26 14:47:05 +00:00
|
|
|
-- | parameter in the request. For any query parameters that don't have values
|
2018-07-08 23:16:48 +00:00
|
|
|
-- | (`/some/path?query`), the value in the `Object` for that parameter will be
|
2017-09-26 14:47:05 +00:00
|
|
|
-- | the string `"true"`. Note that this type has an implementation of `Lookup`
|
2018-07-08 23:16:48 +00:00
|
|
|
-- | for `String` keys defined by `lookupObject` in [Lookup.purs](./Lookup.purs)
|
|
|
|
-- | because `lookupObject` is defined for any `Object` of `Monoids`. So you can
|
2017-09-27 13:59:50 +00:00
|
|
|
-- | do something like `query !! "foo"` to get the value of the query parameter
|
2017-09-26 07:14:06 +00:00
|
|
|
-- | "foo".
|
2018-07-08 23:16:48 +00:00
|
|
|
type Query = Object.Object String
|
2017-09-26 06:08:07 +00:00
|
|
|
|
2018-07-08 23:16:48 +00:00
|
|
|
-- | The `Map` of query segments in the given HTTP `Request`.
|
2017-09-26 06:08:07 +00:00
|
|
|
read :: HTTP.Request -> Query
|
2017-09-26 07:14:06 +00:00
|
|
|
read =
|
2018-07-08 23:16:48 +00:00
|
|
|
HTTP.requestURL >>> split "?" >>> last >>> split "&" >>> nonempty >>> toObject
|
2017-09-26 07:14:06 +00:00
|
|
|
where
|
2018-07-08 23:16:48 +00:00
|
|
|
toObject = map toTuple >>> Object.fromFoldable
|
2017-09-26 07:14:06 +00:00
|
|
|
nonempty = Array.filter ((/=) "")
|
|
|
|
split = String.Pattern >>> String.split
|
|
|
|
first = Array.head >>> Maybe.fromMaybe ""
|
|
|
|
last = Array.tail >>> Maybe.fromMaybe [] >>> String.joinWith ""
|
|
|
|
toTuple item = Tuple.Tuple (first itemParts) $ value $ last itemParts
|
|
|
|
where
|
|
|
|
value val = if val == "" then "true" else val
|
|
|
|
itemParts = split "=" item
|