Rename more stuff
This commit is contained in:
53
advanced/part1/src/Api.elm
Normal file
53
advanced/part1/src/Api.elm
Normal file
@@ -0,0 +1,53 @@
|
||||
module Api exposing (addServerError, listErrors, optionalError, url)
|
||||
|
||||
import Http
|
||||
import Json.Decode as Decode exposing (Decoder, decodeString, field, string)
|
||||
import Json.Decode.Pipeline as Pipeline exposing (optional)
|
||||
import Url.Builder
|
||||
|
||||
|
||||
|
||||
-- URL
|
||||
|
||||
|
||||
{-| Get a URL to the Conduit API.
|
||||
-}
|
||||
url : List String -> String
|
||||
url paths =
|
||||
-- NOTE: Url.Builder takes care of percent-encoding special URL characters.
|
||||
-- See https://package.elm-lang.org/packages/elm/url/latest/Url#percentEncode
|
||||
Url.Builder.crossOrigin "https://conduit.productionready.io"
|
||||
("api" :: paths)
|
||||
[]
|
||||
|
||||
|
||||
|
||||
-- ERRORS
|
||||
|
||||
|
||||
addServerError : List String -> List String
|
||||
addServerError list =
|
||||
"Server error" :: list
|
||||
|
||||
|
||||
{-| Many API endpoints include an "errors" field in their BadStatus responses.
|
||||
-}
|
||||
listErrors : Decoder (List String) -> Http.Error -> List String
|
||||
listErrors decoder error =
|
||||
case error of
|
||||
Http.BadStatus response ->
|
||||
response.body
|
||||
|> decodeString (field "errors" decoder)
|
||||
|> Result.withDefault [ "Server error" ]
|
||||
|
||||
err ->
|
||||
[ "Server error" ]
|
||||
|
||||
|
||||
optionalError : String -> Decoder (List String -> a) -> Decoder a
|
||||
optionalError fieldName =
|
||||
let
|
||||
errorToString errorMessage =
|
||||
String.join " " [ fieldName, errorMessage ]
|
||||
in
|
||||
optional fieldName (Decode.list (Decode.map errorToString string)) []
|
||||
Reference in New Issue
Block a user