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

Add ToRequestBody RequestBody instance #11

Open
pete-murphy opened this issue Sep 4, 2023 · 2 comments
Open

Add ToRequestBody RequestBody instance #11

pete-murphy opened this issue Sep 4, 2023 · 2 comments

Comments

@pete-murphy
Copy link
Contributor

pete-murphy commented Sep 4, 2023

I was hoping to use fetch in Node, and to pass a Buffer as request body, but this is not currently possible.

main :: Effect Unit
main = do
  buffer <- Buffer.fromString "Hello, world" UTF8
  Aff.launchAff_ do
    void (Fetch.fetchBody "http://example.com" { body: buffer })

This fails to compile because there's no ToRequestBody instance for Buffer.

I opened an issue in web-fetch purescript-web/purescript-web-fetch#15 about adding RequestBody.fromBuffer (which would enable adding a ToRequestBody Buffer instance here) but that's not the right approach as it would incur a dependency on node-buffer and make both the "core" web-fetch and this fetch not portable between environments.

I think maybe a better solution would be to have instance ToRequestBody RequestBody in this library, and then a hypothetical node-fetch could wrap this fetch implementation and have its own RequestBody module with Node-specific conversion functions like fromBuffer :: Buffer -> RequestBody:

main :: Effect Unit
main = do
  buffer <- Buffer.fromString "Hello, world" UTF8
  Aff.launchAff_ do
    void (Fetch.fetchBody "http://example.com" { body: RequestBody.fromBuffer buffer })

Let me know if that makes sense, or if there are reasons for omitting the instance ToRequestBody RequestBody that I'm over-looking. Thanks!

@sigma-andex
Copy link
Member

fetch depends on fetch-core (a fork of web-fetch). We planned to move that to purescript-contrib and deprecated web-fetch.

@srghma
Copy link

srghma commented Sep 30, 2024

If purescript module is not imported - the import "node-buffer" will not happen, no error will be thrown

So, maybe this class can be added to this repo?

(Just idea)

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

3 participants