choonkeat / elm-webapp

A setup for writing http based, client-server app in elm, inspired wholly by lamdera.com
https://package.elm-lang.org/packages/choonkeat/elm-webapp/latest
MIT License
57 stars 7 forks source link

Compatibility issue between 2.0.0 and 3.0.0 #5

Closed medmouine closed 3 years ago

medmouine commented 3 years ago

Hi there are some signature incompatibilities between 2.0.0 and 3.0.0. Could you please provide a change log or something, so it would be easier to see what changed exactly between each version? I would be interested in contributing to the project in the future, but I would need some more clarity in the road map and planned features to set my self up. Thank you!

The signature issue was fixed by downgrading to from 3.0.0 to 2.0.0.

Related trace:

> make compile                                                                                                                                            [13:06:44]
elm make src/Server.elm --output build/Server.js
Detected problems in 1 module.
-- TYPE MISMATCH ------------------------------------------------ src/Server.elm

The 1st argument to `worker` is not what I expect:

38|     Webapp.Server.worker
39|>        { worker =
40|>            { init = init
41|>            , update = update
42|>            , subscriptions = subscriptions
43|>            }
44|>        , ports =
45|>            { writeResponse = writeResponse
46|>            , onHttpRequest = onHttpRequest
47|>            , onWebsocketEvent = \_ -> Sub.none -- onWebsocketEvent
48|>            , writeWebsocketMessage = \_ _ _ -> Cmd.none -- writeWebsocketMessage
49|>            }
50|>        , protocol =
51|>            { routeDecoder = routeDecoder
52|>            , updateFromRoute = updateFromRoute
53|>            , updateFromClient = updateFromClient
54|>            , serverMsgEncoder = Protocol.Auto.encodeProtocolMsgFromServer
55|>            , clientMsgDecoder = Protocol.Auto.decodeProtocolMsgFromClient
56|>            , headerDecoder = headerDecoder
57|>            , errorEncoder = Json.Encode.string
58|>            }
59|>        }

This argument is a record of type:

    { ports :
          { onHttpRequest :
                (
                Json.Encode.Value -> Webapp.Server.FrameworkMsg Msg x serverMsg
                )
                -> Sub (Webapp.Server.FrameworkMsg Msg x serverMsg)
          , onWebsocketEvent :
                (
                Json.Encode.Value -> Webapp.Server.FrameworkMsg Msg x serverMsg
                )
                -> Sub (Webapp.Server.FrameworkMsg Msg x serverMsg)
          , writeResponse :
                Request
                -> Response
                -> Cmd (Webapp.Server.FrameworkMsg Msg x serverMsg)
          , writeWebsocketMessage :
                Webapp.Server.Websocket.Connection
                -> Webapp.Server.Websocket.Key
                -> String
                -> Cmd (Webapp.Server.FrameworkMsg Msg x serverMsg)
          }
    , protocol :
          { clientMsgDecoder : Json.Decode.Decoder Protocol.MsgFromClient
          , errorEncoder : String -> Json.Encode.Value
          , headerDecoder :
                ServerState -> Json.Decode.Decoder Protocol.RequestContext
          , routeDecoder : Url.Url -> Maybe Route
          , serverMsgEncoder : MsgFromServer -> Json.Encode.Value
          , updateFromClient :
                Protocol.RequestContext
                -> Time.Posix
                -> Protocol.MsgFromClient
                -> ServerState
                -> ( ServerState, Task String MsgFromServer )
          , updateFromRoute :
                ( Method, Protocol.RequestContext, Maybe Route )
                -> Time.Posix
                -> Request
                -> ServerState
                -> ( ServerState, Cmd Msg )
          }
    , worker :
          { init : Flags -> ( ServerState, Cmd Msg )
          , subscriptions : ServerState -> Sub Msg
          , update : Msg -> ServerState -> ( ServerState, Cmd Msg )
          }
    }

But `worker` needs the 1st argument to be:

    { ports : Webapp.Server.Ports Msg x serverMsg
    , protocol :
          Webapp.Server.Protocol
              Msg
              x
              serverMsg
              clientMsg
              endpoint
              header
              ServerState
    , worker : Webapp.Server.PlatformWorker Flags ServerState Msg
    }

Hint: It looks like it takes too few arguments. I was expecting 1 more.

make: *** [build/Server.js] Error 1
medmouine commented 3 years ago

Also it would be great to have tags or releases to be able to identify which commits to check out for specific versions.

medmouine commented 3 years ago

Also the issue comes from the headerDecoder signature taking Posix -> ServerState -> Json.Decode.Decoder Protocol.RequestContext instead of the provided Protocol.RequestContext

Actual fix for 3.0.0:

headerDecoder : Posix -> ServerState -> Json.Decode.Decoder Protocol.RequestContext
headerDecoder p serverState =
    Json.Decode.oneOf
        [ Json.Decode.map Protocol.Cookied (Json.Decode.field "cookie" Json.Decode.string)
        , Json.Decode.succeed Protocol.Anonymous
        ]

Can you clarify why exactly headerDecoder need a posix argument in 3.0.0 compared to previous versions?

choonkeat commented 3 years ago

Thanks @medmouine for the prompt. I've written a release note for 3.0.0 https://github.com/choonkeat/elm-webapp/releases/tag/3.0.0 where I linked to freshly written comments wrt type signature changes in the commits.

Hope the notes helps 🙇

choonkeat commented 3 years ago

Can you clarify why exactly headerDecoder need a posix argument in 3.0.0 compared to previous versions?

The new Time.Posix argument holds the current time and is useful when you need to verify if the JWT has expired