/
Path.purs
28 lines (24 loc) 路 990 Bytes
/
Path.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module HTTPurple.Path
( Path
, read
) where
import Prelude
import Data.Array (filter, head)
import Data.Maybe (fromMaybe)
import Data.String (Pattern(Pattern), split)
import HTTPurple.Utils (urlDecode)
import Node.HTTP (Request, requestURL)
-- | The `Path` type is just sugar for an `Array` of `String` segments that are
-- | sent in a request and indicates the path of the resource being requested.
-- | Note that this type has an implementation of `Lookup` for `Int` keys
-- | defined by `lookupArray` in [Lookup.purs](./Lookup.purs) because
-- | `lookupArray` is defined for any `Array` of `Monoids`. So you can do
-- | something like `path !! 2` to get the path segment at index 2.
type Path = Array String
-- | Given an HTTP `Request` object, extract the `Path`.
read :: Request -> Path
read = requestURL >>> split' "?" >>> first >>> split' "/" >>> nonempty >>> map urlDecode
where
nonempty = filter ((/=) "")
split' = Pattern >>> split
first = head >>> fromMaybe ""