tkh44 / holen

Declarative fetch for React
MIT License
150 stars 16 forks source link

Add body transform prop to handle body types other than json #1

Closed tkh44 closed 7 years ago

vesparny commented 7 years ago

Hi @tkh44. May I help on this one?

tkh44 commented 7 years ago

Yes!

vesparny commented 7 years ago

What do you exactly have in mind?

tkh44 commented 7 years ago

@vesparny I think I want to add a responseTransformer prop or something like that. The default would be:

(res) => res.json()

The only tricky part is I think we also need to pass in onError and onSuccess handlers to this transformer so that we can hook it into the existing success/error handling.

Maybe something like

(res, onSuccess, onError) => res.json().then(onSuccess).catch(onError)

What do you think?

tkh44 commented 7 years ago

I also wonder if it should be more generic so users can do more than just transform the request. Maybe the more generic version can live on a different prop.

vesparny commented 7 years ago

At the moment unfetch supports :

what about having an API like:

<Holen 
  body={JSON.stringify({ message: 'hello' })}
  method="POST"
  url="api.startup.com/users"
  type="json"
  transformResponse={(data) => {
   data.isDecorated = true
   return data
 }}
>
  {({data}) => <pre>{data.isDecorated}</pre>}
</Holen>
tkh44 commented 7 years ago

That looks great!

vesparny commented 7 years ago

Will work on it tomorrow :)

cheers

tkh44 commented 7 years ago

closed in #3