1module Simple.Spec where
5import Effect.Class.Console (log)
6import Yoga.Fetch.Om.Simple (FetchError, FetchResponse, get, getWithHeaders, post, post_, put, put_, patch, patch_, delete, delete_)
7import Yoga.Om (Om, handleErrors)
9type User = { id :: Int, name :: String, email :: String }
10type CreateUser = { name :: String, email :: String }
12exampleGet :: forall ctx err. Om ctx (fetchError :: FetchError | err) User
13exampleGet = get @User "https://api.example.com/users/42" {}
15exampleGetWithAuth :: forall ctx err. Om ctx (fetchError :: FetchError | err) User
16exampleGetWithAuth = get @User "https://api.example.com/users/42" { authorization: "Bearer abc123" }
18exampleGetPlainText :: forall ctx err. Om ctx (fetchError :: FetchError | err) String
19exampleGetPlainText = get @String "https://api.example.com/health" {}
21exampleGetWithHeaders :: forall ctx err. Om ctx (fetchError :: FetchError | err) (FetchResponse User)
22exampleGetWithHeaders = do
23 { headers, body: user } <- getWithHeaders @User "https://api.example.com/users/42" {}
24 log $ "Got user: " <> user.name
25 pure { headers, body: user }
27examplePost :: forall ctx err. Om ctx (fetchError :: FetchError | err) User
28examplePost = post @User "https://api.example.com/users" {} { name: "Alice", email: "alice@example.com" }
30examplePostFireAndForget :: forall ctx err. Om ctx (fetchError :: FetchError | err) Unit
31examplePostFireAndForget = post_ "https://api.example.com/users" {} { name: "Alice", email: "alice@example.com" }
33examplePut :: forall ctx err. Om ctx (fetchError :: FetchError | err) User
34examplePut = put @User "https://api.example.com/users/42" {} { name: "Bob", email: "bob@example.com" }
36examplePut_ :: forall ctx err. Om ctx (fetchError :: FetchError | err) Unit
37examplePut_ = put_ "https://api.example.com/users/42" {} { name: "Bob", email: "bob@example.com" }
39examplePatch :: forall ctx err. Om ctx (fetchError :: FetchError | err) User
40examplePatch = patch @User "https://api.example.com/users/42" {} { name: "Bob" }
42examplePatch_ :: forall ctx err. Om ctx (fetchError :: FetchError | err) Unit
43examplePatch_ = patch_ "https://api.example.com/users/42" {} { name: "Bob" }
45exampleDelete :: forall ctx err. Om ctx (fetchError :: FetchError | err) User
46exampleDelete = delete @User "https://api.example.com/users/42" {}
48exampleDelete_ :: forall ctx err. Om ctx (fetchError :: FetchError | err) Unit
49exampleDelete_ = delete_ "https://api.example.com/users/42" {}
51exampleHandleError :: forall ctx. Om ctx () Unit
52exampleHandleError = do
53 user <- get @User "https://api.example.com/users/42" {}
54 # handleErrors { fetchError: \_ -> pure { id: 0, name: "unknown", email: "" } }
55 log $ "Got user: " <> user.name
57exampleComposition :: forall ctx. Om ctx () Unit
58exampleComposition = do
59 user <- post @User "https://api.example.com/users" {} { name: "Alice", email: "alice@example.com" }
61 { fetchError: \e -> do
62 log $ "Failed with status " <> show e.status <> ": " <> e.body
63 pure { id: 0, name: "", email: "" }
65 _ <- put @User ("https://api.example.com/users/" <> show user.id) {} { name: "Alice Updated", email: user.email }
66 # handleErrors { fetchError: \_ -> pure user }
67 delete_ ("https://api.example.com/users/" <> show user.id) {}
68 # handleErrors { fetchError: \_ -> pure unit }
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list