-
Notifications
You must be signed in to change notification settings - Fork 778
API for fetch-effect #877
Description
In my humble opinion, there needs to be an official fetch effect (or http, or ajax or whatever) for the final release of Hyperapp 2. Otherwise, it will be the first thing people ask for, and telling them to make their own would be inconsistent with the message that "you should not need to write your own effects most of the time".
The question is only what the API should be. I opened this issue to have that discussion, as it isn't as easy as one might think at first.
My first suggestion would be something like this:
Like:
Fetch({
url: ...
// the URL in a native fetch(url) call, mandatory
options: {...},
//the options in a fetch(url, options) call.
// Optional, default: undefined or {},
onResponse: ... ,
// fetch(...).then(resp => dispatch(props.onResponse, resp)
// Optional, default: no dispatch
onFailure: ...,
//fetch(...).catch(err => dispatch(props.onFailure, err)
// Optional, default: no dispatch
bodyType: ...
//only relevant if using onBody callback
// can be 'text', 'json', 'blob', 'formData', 'arrayBuffer'
// Optional, default 'text',
onBody: ...
// fetch(...)
// .then(response => response[props.bodyType]())
// .then(body => dispatch(props.onBody, body)
// Optional, default: no dispatch
onErrorParsingBody: ...
// fetch(...)
// .then(response => response[props.bodyType]())
// .catch(error => dispatch(props.onErrorParsingBody, error)
// Optional. Default: no dispatch
})The thing that concerns me with this, is that fetch will resolve for any response from the remote. Including 404's et c. That means you'll get onBody callbacks even when there is no body, or the body is not of the bodyType you specified (in which case onErrorParsingBody will be dispatched).
So I was thinking, we could limit the body-parsing routine to be done only for 200 OK responses, and only if the request was using the GET method. But perhaps that's to restrictive?
If we limit body parsing to GET requests with 200 OK responses, then we'll need another option: onNotOK (maybe?) for the case when we got a response but decided not to parse the body. Or is that getting too complicated?