Skip to content

Implement HasForeign instance #29

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

Merged
merged 3 commits into from
Nov 7, 2018

Conversation

afcady
Copy link

@afcady afcady commented Sep 17, 2018

This is three patches, to servant-foreign, servant-multipart, and servant-js.

https://github.com/haskell-servant/servant-multipart/compare/master...afcady:multipart-foreign?expand=1

https://github.com/haskell-servant/servant/compare/master...afcady:multipart-foreign?expand=1

https://github.com/haskell-servant/servant-js/compare/master...afcady:multipart-foreign?expand=1

The patch to servant-foreign adds a Boolean field to the Req type specifying whether the request body is JSON (as is currently assumed). The patch to servant-multipart implements a HasForeign instance that sets this field to False. The patch to servant-js changes the output of vanillaJS when the flag is false, so that JSON.stringify() does not get called on the body and the content-type is not set to JSON.

This address haskell-servant/servant-multipart#5

This enables the generated javascript to do multipart MIME file uploads.

It is the user's responsibility to generate a valid body; e.g.:

    var fileObject = new File([blob], 'video.webm', {
        type: 'video/webm'
    });
    var formData = new FormData();
    formData.append('video', fileObject);

    postVideo(formData, onSuccess, onError);
@@ -34,7 +34,7 @@ generateVanillaJSWith opts req = "\n" <>
<> " xhr.open('" <> decodeUtf8 method <> "', " <> url <> ", true);\n"
<> reqheaders
<> " xhr.setRequestHeader('Accept', 'application/json');\n"
<> (if isJust (req ^. reqBody) then " xhr.setRequestHeader('Content-Type', 'application/json');\n" else "")
<> (if isJust (req ^. reqBody) && (req ^. reqBodyIsJSON) then " xhr.setRequestHeader('Content-Type', 'application/json');\n" else "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about non-Vanilla drivers?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't use them and didn't implement anything for them.

@alpmestan
Copy link
Contributor

It would be nice to support other drivers, but worst case I'll just implement something similar for them if you don't feel like doing it yourself.

@phadej phadej merged commit 82fd379 into haskell-servant:master Nov 7, 2018
@phadej
Copy link
Contributor

phadej commented Nov 7, 2018

I'll verify that things work together when I'll make servant-0.15 (hopefully this week).

Thanks for you contribution, @afcady

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

Successfully merging this pull request may close these issues.

3 participants