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

View File

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

View File

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

View File

@ -1,10 +1,10 @@
module DataSource.Http exposing
( RequestDetails
, get, request
, Expect, expectString, expectJson
, expectResponse
, Expect, expectString, expectJson, expectBytes, expectWhatever
, Response(..), Metadata, Error(..)
, expectResponse, expectStringResponse, expectBytesResponse
, 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.
@ -40,12 +40,14 @@ in [this article introducing DataSource.Http requests and some concepts around i
## Decoding Request Body
@docs Expect, expectString, expectJson
@docs Expect, expectString, expectJson, expectBytes, expectWhatever
## Expecting Responses
@docs expectResponse
@docs Response, Metadata, Error
@docs expectResponse, expectStringResponse, expectBytesResponse
## Building a DataSource.Http Request Body
@ -227,6 +229,7 @@ expectWhatever =
ExpectWhatever
{-| -}
expectStringResponse : (Result error body -> msg) -> (Response String -> Result error body) -> Expect msg
expectStringResponse toMsg toResult_ =
ExpectResponse (toResult_ >> toMsg)
@ -238,26 +241,6 @@ expectBytesResponse toMsg toResult_ =
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 =
case expect of
@ -394,6 +377,7 @@ request request__ expect =
)
{-| -}
type alias Metadata =
{ url : String
, statusCode : Int
@ -402,6 +386,7 @@ type alias Metadata =
}
{-| -}
type Response body
= BadUrl_ String
| Timeout_
@ -410,6 +395,7 @@ type Response body
| GoodStatus_ Metadata body
{-| -}
type Error
= BadUrl String
| 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.Http
import DataSource.Internal.Request
import Dict exposing (Dict)
import Dict.Extra
import Json.Decode
@ -304,7 +305,7 @@ encrypt getSecrets input =
getSecrets
|> DataSource.andThen
(\secrets ->
DataSource.Http.internalRequest
DataSource.Internal.Request.request
{ name = "encrypt"
, body =
DataSource.Http.jsonBody
@ -332,7 +333,7 @@ decrypt getSecrets decoder input =
getSecrets
|> DataSource.andThen
(\secrets ->
DataSource.Http.internalRequest
DataSource.Internal.Request.request
{ name = "decrypt"
, body =
DataSource.Http.jsonBody