kwhitley / itty-fetcher

An even simpler wrapper around native Fetch to strip boilerplate from your fetching code!
MIT License
99 stars 4 forks source link

FIX: defined headers should be honored *over* injected headers #17

Closed kwhitley closed 2 years ago

kwhitley commented 2 years ago

Expected Behavior

Actual Behavior

Use Case

I am attempting to send blob data through a fetcher instance. I can do this with native fetch, but not itty-fetcher:

const blob = new Blob(["This is some important text"], { type: "text/plain" });

fetch('https://ity.sh/create', { method: 'POST', body: blob })
danawoodman commented 2 years ago

Are you sure you can't override the content-type? I just added a test for it and passing { headers: { "Content-Type": "text/plain" } } and it works.

danawoodman commented 2 years ago

@kwhitley I've fixed the body issue in #18 as well as moved us to jsdom test env. I also added a test to verify the content-type header changing does work as intended

kwhitley commented 2 years ago

Thanks @danawoodman! Real quick on this, what would be the proposed way to send blob content? Currently, we attempt to JSON.stringify everything but FormData.

Currently in native fetch, you can do something like

fetch('/endpoint', {
  method: 'POST', 
  body: blob, // content-type is sent without explicitly defining
})

without having to explicitly define or override content-type... this allows you to easily pass files without extra steps. To support that in fetcher, we'd have to manually override the pre-embedded application/json header currently.

kwhitley commented 2 years ago

I'm leaning towards these behaviors:

Thoughts? I just don't want to be more limiting than native fetch through our assumptions, nor make folks jump through hoops to even replicate what they were able to do easily before.

Ideal interface

const body = new Blob(['something here'],  { type: 'text/plain' })

fetcher().post('/endpoint', body)
// would be equivalent to
fetch('/endpoint', { method: 'POST', body })
danawoodman commented 2 years ago

@kwhitley you can send arbitrary body content now, see the tests here: https://github.com/kwhitley/itty-fetcher/pull/18/files#diff-2aa640fd9240ec144ac5019315d5631742a2b12d957395a0cc6ff4a0d9cf3f02R135. My change means you can manually pass in whatever body you want and we don't stringify it.

If you want to send a diff type, I don't see passing a header as a big deal?