martyjs / marty

A Javascript library for state management in React applications
http://martyjs.org
MIT License
1.09k stars 76 forks source link

how to send a PATCH request? #123

Closed ycdesu closed 9 years ago

ycdesu commented 9 years ago

hello, I read http.js and found that it doesn't support PATCH method. I try to call this.request with option {method: "patch" ...} to send a PATCH request. The problem is, I don't know how to handle the promise rejection if an error occurred. Could you tell me where can I get enough information to achieve this? thanks.

oliverwoodings commented 9 years ago

Hi @ycavatars, the HttpStateSource uses the isomorphic-fetch polyfill for making the request. When you make a call to this.request it calls the fetch API and then handles the response for you (see code here). If the server responds with status 400 or greater, Marty will throw the response as an exception. Otherwise it will resolve normally. Therefore you can do the following:

this.request({ method: "patch" ...})
  .then(function (res) {
    console.log("everything went better than expected!");
  })
  .catch(function (res) {
    console.log("something foobar'd");
  });

Does that help?

ycdesu commented 9 years ago

yes, really thanks :)

oliverwoodings commented 9 years ago

I'll do a quick PR for you to add in the PATCH method as a sugar function like the other methods.