Update docs and extract internal function.

This commit is contained in:
Dillon Kearns 2022-02-16 21:36:36 -08:00
parent c5b5f35504
commit 9c9dd49eca
7 changed files with 46 additions and 31 deletions

File diff suppressed because one or more lines are too long

View File

@ -8,6 +8,7 @@ module DataSource.Env exposing (get, expect)
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import DataSource.Http import DataSource.Http
import DataSource.Internal.Request
import Json.Decode as Decode import Json.Decode as Decode
import Json.Encode as Encode import Json.Encode as Encode
@ -15,7 +16,7 @@ import Json.Encode as Encode
{-| -} {-| -}
get : String -> DataSource (Maybe String) get : String -> DataSource (Maybe String)
get envVariableName = get envVariableName =
DataSource.Http.internalRequest DataSource.Internal.Request.request
{ name = "env" { name = "env"
, body = DataSource.Http.jsonBody (Encode.string envVariableName) , body = DataSource.Http.jsonBody (Encode.string envVariableName)
, expect = , expect =

View File

@ -44,6 +44,7 @@ plain old JSON in Elm.
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import DataSource.Http import DataSource.Http
import DataSource.Internal.Request
import Json.Decode as Decode exposing (Decoder) import Json.Decode as Decode exposing (Decoder)
@ -284,7 +285,7 @@ body =
read : String -> Decoder a -> DataSource a read : String -> Decoder a -> DataSource a
read filePath decoder = read filePath decoder =
DataSource.Http.internalRequest DataSource.Internal.Request.request
{ name = "read-file" { name = "read-file"
, body = DataSource.Http.stringBody "" filePath , body = DataSource.Http.stringBody "" filePath
, expect = decoder |> DataSource.Http.expectJson , expect = decoder |> DataSource.Http.expectJson

View File

@ -213,6 +213,7 @@ That will give us
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import DataSource.Http import DataSource.Http
import DataSource.Internal.Glob exposing (Glob(..)) import DataSource.Internal.Glob exposing (Glob(..))
import DataSource.Internal.Request
import Json.Decode as Decode import Json.Decode as Decode
import List.Extra import List.Extra
import Regex import Regex
@ -943,7 +944,7 @@ toNonEmptyWithDefault default list =
-} -}
toDataSource : Glob a -> DataSource (List a) toDataSource : Glob a -> DataSource (List a)
toDataSource glob = toDataSource glob =
DataSource.Http.internalRequest DataSource.Internal.Request.request
{ name = "glob" { name = "glob"
, body = , body =
DataSource.Internal.Glob.toPattern glob DataSource.Internal.Glob.toPattern glob

View File

@ -1,10 +1,10 @@
module DataSource.Http exposing module DataSource.Http exposing
( RequestDetails ( RequestDetails
, get, request , get, request
, Expect, expectString, expectJson , Expect, expectString, expectJson, expectBytes, expectWhatever
, expectResponse , Response(..), Metadata, Error(..)
, expectResponse, expectStringResponse, expectBytesResponse
, Body, emptyBody, stringBody, jsonBody , Body, emptyBody, stringBody, jsonBody
, Error(..), Metadata, Response(..), expectBytes, expectBytesResponse, expectStringResponse, expectWhatever, internalRequest
) )
{-| `DataSource.Http` requests are an alternative to doing Elm HTTP requests the traditional way using the `elm/http` package. {-| `DataSource.Http` requests are an alternative to doing Elm HTTP requests the traditional way using the `elm/http` package.
@ -40,12 +40,14 @@ in [this article introducing DataSource.Http requests and some concepts around i
## Decoding Request Body ## Decoding Request Body
@docs Expect, expectString, expectJson @docs Expect, expectString, expectJson, expectBytes, expectWhatever
## Expecting Responses ## Expecting Responses
@docs expectResponse @docs Response, Metadata, Error
@docs expectResponse, expectStringResponse, expectBytesResponse
## Building a DataSource.Http Request Body ## Building a DataSource.Http Request Body
@ -227,6 +229,7 @@ expectWhatever =
ExpectWhatever ExpectWhatever
{-| -}
expectStringResponse : (Result error body -> msg) -> (Response String -> Result error body) -> Expect msg expectStringResponse : (Result error body -> msg) -> (Response String -> Result error body) -> Expect msg
expectStringResponse toMsg toResult_ = expectStringResponse toMsg toResult_ =
ExpectResponse (toResult_ >> toMsg) ExpectResponse (toResult_ >> toMsg)
@ -238,26 +241,6 @@ expectBytesResponse toMsg toResult_ =
ExpectBytesResponse (toResult_ >> toMsg) ExpectBytesResponse (toResult_ >> toMsg)
{-| Build a `DataSource.Http` request (analogous to [Http.request](https://package.elm-lang.org/packages/elm/http/latest/Http#request)).
This function takes in all the details to build a `DataSource.Http` request, but you can build your own simplified helper functions
with this as a low-level detail, or you can use functions like [DataSource.Http.get](#get).
-}
internalRequest :
{ name : String
, body : Body
, expect : Expect a
}
-> DataSource a
internalRequest { name, body, expect } =
request
{ url = "elm-pages-internal://" ++ name
, method = "GET"
, headers = []
, body = body
}
expect
expectToString : Expect a -> String expectToString : Expect a -> String
expectToString expect = expectToString expect =
case expect of case expect of
@ -394,6 +377,7 @@ request request__ expect =
) )
{-| -}
type alias Metadata = type alias Metadata =
{ url : String { url : String
, statusCode : Int , statusCode : Int
@ -402,6 +386,7 @@ type alias Metadata =
} }
{-| -}
type Response body type Response body
= BadUrl_ String = BadUrl_ String
| Timeout_ | Timeout_
@ -410,6 +395,7 @@ type Response body
| GoodStatus_ Metadata body | GoodStatus_ Metadata body
{-| -}
type Error type Error
= BadUrl String = BadUrl String
| Timeout | Timeout

View File

@ -0,0 +1,25 @@
module DataSource.Internal.Request exposing (request)
{-| Build a `DataSource.Http` request (analogous to [Http.request](https://package.elm-lang.org/packages/elm/http/latest/Http#request)).
This function takes in all the details to build a `DataSource.Http` request, but you can build your own simplified helper functions
with this as a low-level detail, or you can use functions like [DataSource.Http.get](#get).
-}
import DataSource exposing (DataSource)
import DataSource.Http exposing (Body, Expect)
request :
{ name : String
, body : Body
, expect : Expect a
}
-> DataSource a
request { name, body, expect } =
DataSource.Http.request
{ url = "elm-pages-internal://" ++ name
, method = "GET"
, headers = []
, body = body
}
expect

View File

@ -8,6 +8,7 @@ module Server.Session exposing (Decoder, NotLoadedReason(..), Session(..), Sessi
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import DataSource.Http import DataSource.Http
import DataSource.Internal.Request
import Dict exposing (Dict) import Dict exposing (Dict)
import Dict.Extra import Dict.Extra
import Json.Decode import Json.Decode
@ -304,7 +305,7 @@ encrypt getSecrets input =
getSecrets getSecrets
|> DataSource.andThen |> DataSource.andThen
(\secrets -> (\secrets ->
DataSource.Http.internalRequest DataSource.Internal.Request.request
{ name = "encrypt" { name = "encrypt"
, body = , body =
DataSource.Http.jsonBody DataSource.Http.jsonBody
@ -332,7 +333,7 @@ decrypt getSecrets decoder input =
getSecrets getSecrets
|> DataSource.andThen |> DataSource.andThen
(\secrets -> (\secrets ->
DataSource.Http.internalRequest DataSource.Internal.Request.request
{ name = "decrypt" { name = "decrypt"
, body = , body =
DataSource.Http.jsonBody DataSource.Http.jsonBody