Skip to content

Commit

Permalink
Merge pull request #466 from tanyabouman/backendtask-docs
Browse files Browse the repository at this point in the history
update documentation examples in BackendTask and BackendTask.File
  • Loading branch information
dillonkearns authored Apr 25, 2024
2 parents 9fdd9f0 + d362cdc commit 7851061
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
17 changes: 11 additions & 6 deletions src/BackendTask.elm
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,19 @@ type alias BackendTask error value =
{-| Transform a request into an arbitrary value. The same underlying task will be performed,
but mapping allows you to change the resulting values by applying functions to the results.
import BackendTask
import BackendTask exposing (BackendTask)
import BackendTask.Http
import Json.Decode as Decode exposing (Decoder)
import FatalError exposing (FatalError)
import Json.Decode as Decode
starsMessage : BackendTask FatalError String
starsMessage =
BackendTask.Http.getJson
"https://api.github.com/repos/dillonkearns/elm-pages"
(Decode.field "stargazers_count" Decode.int)
|> BackendTask.map
(\stars -> "⭐️ " ++ String.fromInt stars)
|> BackendTask.allowFatal
-}
map : (a -> b) -> BackendTask error a -> BackendTask error b
Expand Down Expand Up @@ -282,9 +285,10 @@ resolve =

{-| Turn a list of `BackendTask`s into a single one.
import BackendTask
import BackendTask exposing (BackendTask)
import BackendTask.Http
import FatalError exposing (FatalError)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode as Decode
type alias Pokemon =
{ name : String
Expand Down Expand Up @@ -452,9 +456,10 @@ map2 fn request1 request2 =
{-| Build off of the response from a previous `BackendTask` request to build a follow-up request. You can use the data
from the previous response to build up the URL, headers, etc. that you send to the subsequent request.
import BackendTask
import BackendTask exposing (BackendTask)
import BackendTask.Http
import FatalError exposing (FatalError)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode as Decode
licenseData : BackendTask FatalError String
licenseData =
Expand Down
33 changes: 24 additions & 9 deletions src/BackendTask/File.elm
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ frontmatter frontmatterDecoder =
import BackendTask exposing (BackendTask)
import BackendTask.File as File
import Decode exposing (Decoder)
import FatalError exposing (FatalError)
import Json.Decode as Decode exposing (Decoder)
blogPost : BackendTask BlogPostMetadata
blogPost : BackendTask FatalError BlogPostMetadata
blogPost =
File.bodyWithFrontmatter blogPostDecoder
"blog/hello-world.md"
|> BackendTask.allowFatal
type alias BlogPostMetadata =
{ body : String
Expand Down Expand Up @@ -102,11 +104,13 @@ It's common to parse the body with a markdown parser or other format.
import BackendTask exposing (BackendTask)
import BackendTask.File as File
import Decode exposing (Decoder)
import FatalError exposing (FatalError)
import Html exposing (Html)
import Json.Decode as Decode
example :
BackendTask
FatalError
{ title : String
, body : List (Html msg)
}
Expand All @@ -126,6 +130,7 @@ It's common to parse the body with a markdown parser or other format.
)
)
"foo.md"
|> BackendTask.allowFatal
markdownToView :
String
Expand Down Expand Up @@ -191,13 +196,15 @@ just the metadata.
import BackendTask exposing (BackendTask)
import BackendTask.File as File
import Decode exposing (Decoder)
import FatalError exposing (FatalError)
import Json.Decode as Decode exposing (Decoder)
blogPost : BackendTask BlogPostMetadata
blogPost : BackendTask FatalError BlogPostMetadata
blogPost =
File.onlyFrontmatter
blogPostDecoder
"blog/hello-world.md"
|> BackendTask.allowFatal
type alias BlogPostMetadata =
{ title : String
Expand Down Expand Up @@ -281,10 +288,13 @@ Hey there! This is my first post :)
```
import BackendTask exposing (BackendTask)
import BackendTask.File as File
import FatalError exposing (FatalError)
data : BackendTask String
data : BackendTask FatalError String
data =
bodyWithoutFrontmatter "blog/hello-world.md"
File.bodyWithoutFrontmatter "blog/hello-world.md"
|> BackendTask.allowFatal
Then data will yield the value `"Hey there! This is my first post :)"`.
Expand Down Expand Up @@ -314,10 +324,12 @@ You could read a file called `hello.txt` in your root project directory like thi
import BackendTask exposing (BackendTask)
import BackendTask.File as File
import FatalError exposing (FatalError)
elmJsonFile : BackendTask String
elmJsonFile : BackendTask FatalError String
elmJsonFile =
File.rawFile "hello.txt"
|> BackendTask.allowFatal
-}
rawFile : String -> BackendTask { fatal : FatalError, recoverable : FileReadError decoderError } String
Expand All @@ -331,15 +343,18 @@ The Decode will strip off any unused JSON data.
import BackendTask exposing (BackendTask)
import BackendTask.File as File
import FatalError exposing (FatalError)
import Json.Decode as Decode
sourceDirectories : BackendTask (List String)
sourceDirectories : BackendTask FatalError (List String)
sourceDirectories =
File.jsonFile
(Decode.field
"source-directories"
(Decode.list Decode.string)
)
"elm.json"
|> BackendTask.allowFatal
-}
jsonFile :
Expand Down

0 comments on commit 7851061

Please sign in to comment.