Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Responses of Type unit #5

Closed
SCullman opened this issue Sep 17, 2019 · 6 comments
Closed

Support Responses of Type unit #5

SCullman opened this issue Sep 17, 2019 · 6 comments

Comments

@SCullman
Copy link
Contributor

SCullman commented Sep 17, 2019

For example, I usually return just the response code on DELETE requests.
Fetch.delete<Data,unit> fails because there is no Auto decoder for unit.

Same for PATCH,POST and PUT. It might even make sense for GET, if the request just triggers a side effect, is used as RPC.

Proposals

  • Either adding an Auto decoder for unit in Thoth.Json
  • or handling here in Thoth.Fetch
@MangelMaxime
Copy link
Contributor

We should support it in Thoth.Json(.Net)

The question is how should be represents unit in JSON?

@SCullman
Copy link
Contributor Author

As a workaround, I add the following ExtraCoder:

module Extra =
    let unitDecoder : Decoder<unit> =fun _ _ -> () |> Ok
    let unitEncoder () :  JsonValue = failwith "Cannot encode unit"
    let unit extra = 
        Extra.withCustom unitEncoder unitDecoder  (defaultArg extra Extra.empty) |> Some

Then I pass ?extra = Extra.unit extra to all my calls of Thoth.Fetch.

@SCullman
Copy link
Contributor Author

My proposed Decode/Encoder doesn't work.
line 44

            let! body = response.text()
            return Decode.unsafeFromString decoder body

body is an empty string, and Decode.unsafeFromString fails with Uncaught SyntaxError: Unexpected identifier

@SCullman
Copy link
Contributor Author

My temporary fix:

            let! body = response.text()
            let result = if body = ""
                         then unbox ()
                         else Decode.unsafeFromString decoder body
            return result

Same for tryFetchAs. The ExtraCoder (which is never called) is still needed.

@SCullman
Copy link
Contributor Author

@MangelMaxime
You were right to fix it int Thoth.Json. Fix is in thoth-org/Thoth.Json#25

@SCullman
Copy link
Contributor Author

And similar fix added also to Thoth.Json.Net.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants