File tree Expand file tree Collapse file tree 8 files changed +153
-11
lines changed Expand file tree Collapse file tree 8 files changed +153
-11
lines changed Original file line number Diff line number Diff line change 9
9
"elm/browser" : " 1.0.2" ,
10
10
"elm/core" : " 1.0.5" ,
11
11
"elm/html" : " 1.0.0" ,
12
- "elm/http" : " 2 .0.0" ,
12
+ "elm/http" : " 1 .0.0" ,
13
13
"elm/json" : " 1.1.3" ,
14
14
"elm/time" : " 1.0.0" ,
15
15
"elm/url" : " 1.0.0" ,
16
16
"hmsk/elm-vite-plugin-helper" : " 1.0.1" ,
17
- "lukewestby/elm-http-builder" : " 8.0.0" ,
18
- "matheus23/elm-tailwind-modules-base" : " 1.0.0" ,
19
17
"rtfeldman/elm-css" : " 18.0.0"
20
18
},
21
19
"indirect" : {
Original file line number Diff line number Diff line change 1
- module Api exposing (Response )
1
+ module Api exposing (.. )
2
2
3
+ import Api.Endpoint as Endpoint exposing (Endpoint )
4
+ import Http
5
+ import Json.Decode exposing (Decoder )
3
6
4
- type Response
5
- = NoteCreated String
6
- | NoteCreateFailed String
7
- | NoteRead String
8
- | NoteNotFound
7
+
8
+ {- | The authentication credentials for the Viewer (that is, the currently logged-in user.)
9
+ This simply includes the JWT token.
10
+ -}
11
+ type Cred
12
+ = Cred String
13
+
14
+
15
+ credHeader : Cred -> Http .Header
16
+ credHeader ( Cred c) =
17
+ Http . header " Authorization" ( " Bearer " ++ c)
18
+
19
+
20
+
21
+ -- http
22
+
23
+
24
+ get : Endpoint -> Maybe Cred -> Decoder a -> Http .Request a
25
+ get url cred decoder =
26
+ Endpoint . request
27
+ { method = " GET"
28
+ , url = url
29
+ , expect = Http . expectJson decoder
30
+ , headers =
31
+ case cred of
32
+ Just c ->
33
+ [ credHeader c ]
34
+
35
+ Nothing ->
36
+ []
37
+ , body = Http . emptyBody
38
+ , timeout = Nothing
39
+ , withCredentials = False
40
+ }
41
+
42
+
43
+ post : Endpoint -> Maybe Cred -> Http .Body -> Decoder a -> Http .Request a
44
+ post url cred body decoder =
45
+ Endpoint . request
46
+ { method = " POST"
47
+ , url = url
48
+ , expect = Http . expectJson decoder
49
+ , headers =
50
+ case cred of
51
+ Just c ->
52
+ [ credHeader c ]
53
+
54
+ Nothing ->
55
+ []
56
+ , body = body
57
+ , timeout = Nothing
58
+ , withCredentials = False
59
+ }
Original file line number Diff line number Diff line change
1
+ module Api.Endpoint exposing (..)
2
+
3
+ import Http
4
+ import Url.Builder exposing (QueryParameter )
5
+
6
+
7
+ type Endpoint
8
+ = Endpoint String
9
+
10
+
11
+ unwrap : Endpoint -> String
12
+ unwrap ( Endpoint e) =
13
+ e
14
+
15
+
16
+ url : List String -> List QueryParameter -> Endpoint
17
+ url paths params =
18
+ Endpoint <|
19
+ Url . Builder . crossOrigin " http://localhost:8000" ( " api" :: " v1" :: paths) params
20
+
21
+
22
+ request :
23
+ { body : Http . Body
24
+ , expect : Http . Expect a
25
+ , headers : List Http . Header
26
+ , method : String
27
+ , timeout : Maybe Float
28
+ , url : Endpoint
29
+ , withCredentials : Bool
30
+ }
31
+ -> Http . Request a
32
+ request c =
33
+ Http . request
34
+ { body = c. body
35
+ , expect = c. expect
36
+ , headers = c. headers
37
+ , method = c. method
38
+ , timeout = c. timeout
39
+ , url = unwrap c. url
40
+ , withCredentials = c. withCredentials
41
+ }
Original file line number Diff line number Diff line change
1
+ module Data.Note exposing (..)
2
+
3
+ import Url.Parser as P exposing (Parser )
4
+
5
+
6
+ type Slug
7
+ = Slug String
8
+
9
+
10
+ urlSlugParser : Parser (Slug -> a ) a
11
+ urlSlugParser =
12
+ P . custom " SLUG" ( \ s -> Just ( Slug s))
Original file line number Diff line number Diff line change 1
1
module Model exposing (Model , Page (..) )
2
2
3
- import Api
4
3
import Browser.Navigation exposing (Key )
4
+ import Viewer exposing (Viewer )
5
5
6
6
7
7
type alias Model =
8
- { apiResponse : Maybe Api . Response
8
+ { viewer : Viewer
9
9
, curPage : Page
10
10
, navKey : Key
11
11
}
Original file line number Diff line number Diff line change
1
+ module Route exposing (..)
2
+
3
+ import Data.Note
4
+ import Url.Parser as Parser exposing ((</>) , Parser )
5
+
6
+
7
+ type Route
8
+ = Home
9
+ | SignIn
10
+ | SignUp
11
+ | Logout
12
+ | Note Data . Note . Slug
13
+
14
+
15
+ parser : Parser (Route -> a ) a
16
+ parser =
17
+ Parser . oneOf
18
+ [ Parser . map Home Parser . top
19
+ , Parser . map SignIn ( Parser . s " sign-in" )
20
+ , Parser . map SignUp ( Parser . s " sign-up" )
21
+ , Parser . map Logout ( Parser . s " logout" )
22
+ , Parser . map Note ( Parser . s " n" </> Data . Note . urlSlugParser)
23
+ ]
Original file line number Diff line number Diff line change
1
+ module Session exposing (..)
2
+
3
+
4
+ type Session
5
+ = LoggedIn
6
+ | Guest
Original file line number Diff line number Diff line change
1
+ module Viewer exposing (..)
2
+
3
+ import Api exposing (Cred )
4
+
5
+
6
+ {- | The logged-in user currently viewing this page. It stores enough data to
7
+ be able to render the menu bar, along with Cred so it's impossible to have a
8
+ Viewer if you aren't logged in.
9
+ -}
10
+ type Viewer
11
+ = Viewer Cred
You can’t perform that action at this time.
0 commit comments