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

Http request with BodyInit #267

Merged

Conversation

a-khakimov
Copy link

@a-khakimov a-khakimov commented May 5, 2024

I made a small draft that solves my problem #266. I suggest making such a change to support http requests with a body for other types, not only text.

@davesmith00000
Copy link
Member

Hi @a-khakimov - sorry for the slow reply. I've been stuck on a long piece of work in another repo. Will check it out as soon as I get back to Tyrian. 👍

@a-khakimov
Copy link
Author

Hi @davesmith00000! Just wanted to check in regarding PR and see if you’ve had a chance to take a look. Thanks, and appreciate your time!

@a-khakimov a-khakimov changed the title Draft: Http request with BodyInit Http request with BodyInit Nov 6, 2024
@davesmith00000
Copy link
Member

Good morning @a-khakimov,
Just taking a look, thanks for the nudge. Can you do a quick rebase (or fork sync) against main please? It's just to pull in the CI config changes so we can run another build.

@@ -41,6 +43,7 @@ enum Body derives CanEqual:
* the content of the body
*/
case PlainText(contentType: String, body: String) extends Body
case File(contentType: String, body: BodyInit) extends Body
Copy link
Member

Choose a reason for hiding this comment

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

This might be ok, but I'm a bit worried about using BodyInit as a catch-all type. I am entirely open to counter arguments. 🙂

I haven't tried this so this is speculation, but from reading around I think we need something like this (though this seems cumbersome):

enum Body derives CanEqual:
  case Empty
  case PlainText(contentType: String, body: String)
  case File(contentType: String, body: org.scalajs.dom.File)
  case Blob(contentType: String, body: org.scalajs.dom.Blob)
  case FormData(contentType: String, body: org.scalajs.dom.FormData)
  case ArrayBuffer(contentType: String, body: org.scalajs.dom.ArrayBuffer)
  case TypedArray(contentType: String, body: org.scalajs.dom.TypedArray)
  case URLSearchParams(contentType: String, body: org.scalajs.dom.URLSearchParams)
  case ReadableStream(contentType: String, body: org.scalajs.dom.ReadableStream)

It also feels like we could do with an enum of content types, and then we could provide helper methods so that you could do things like:

def bodyImagePng(data: Blob): Body =
  Body.Blob(ContentType.imagePng, data)

Or something. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

A list of content types is a big 'ol list - maybe just types we support? I suspect for now you probably just want PlainText, File, and Blob to start.. dealing with application/json and application/xml are problems all on their own for another time 🙂

Copy link
Member

Choose a reason for hiding this comment

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

Possibly crossed wires (or I'm just wrong, wouldn't be the first time 😁 ), we're not talking mime-types, we're talking bodyinit types - I think that's the whole list ☝️.

References:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#parameters
https://fetch.spec.whatwg.org/#bodyinit-unions

An XMLHttpRequestBodyInit, which per the Fetch spec can be a Blob, an ArrayBuffer, a TypedArray, a DataView, a FormData, a URLSearchParams, or a string.

Not...sure...where I got ReadableStream from... 🤔

Does that change anything for you?

Copy link
Member

@davesmith00000 davesmith00000 left a comment

Choose a reason for hiding this comment

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

I'm approving this because I've been unintentionally sitting on it. It is an improvement that solves a problem, thank you for the work and I'm very sorry it's taken such a long time to approve.

I still think there's more to do here, but I've left a placeholder issue that points back to here for some future person to look at:
#290

I need to figure out how to rerun this build to make sure it's passing before I merge.

Thanks again!

Update: Can't figure out how to re-run it but I've run it locally and it seems ok.

@davesmith00000 davesmith00000 merged commit 9374a45 into PurpleKingdomGames:main Nov 21, 2024
1 check failed
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