Skip to content

Support MimeParse-ing on things other than Strings, and allow mixing and matching MimeRender destination types #9

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

iostat
Copy link
Contributor

@iostat iostat commented Nov 4, 2020

Hey!

This PR adds a couple of nifty features. One is that you can now MimeParse stuff other than Strings -- handy for when your request body might not be valid UTF-8 (e.g., is binary). Additionally, you can now mix and match different kinds of MimeRender instances -- previously, if some routes MimeRendered down to a String and others to a ByteString, for example -- you'd get a type mismatch (MimeRender a b String vs MimeRender c d ByteString) since not all of the final rendered types were the same, and the compiler picked whichever came first.

To enable the latter, I had to generalize how responses are written to the output stream (hence the ResponseWriter type), and needed to add an NPM package to facilitate testing (stream-buffers, which allow you to treat a Buffer as a node stream).

You may find there is no obvious test for the mix-and-match MimeRender functionality -- the test is the test suite itself compiling! Note that MagicContentType now MimeRenders to a ByteString, whereas there are remaining HTML and JSON endpoints which MimeRender to String:

@@ -44,8 +46,8 @@ magicMimeTypeRenderString = "It's magic, you know!"
 instance hasMediaTypeMagic :: HasMediaType MagicContentType where
   getMediaType _ = magicMimeType
 
-instance magicallyRenderMagicContentType :: MimeRender a MagicContentType String where
-  mimeRender _ _ = magicMimeTypeRenderString
+instance magicallyRenderMagicContentType :: MimeRender a MagicContentType ByteString where
+  mimeRender _ _ = ByteString.toUTF8 magicMimeTypeRenderString

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.

1 participant