FuseJS/Rest

It would have been easier to use Fuse to something usefull if there was an easy to use function for communicating with REST based services. Supporting GET, POST, PUT and DELETE methods. And setting customer HTTP headers and default using JSON.

Fuse already supports both XMLHttpRequest (standard browser API) and fetch, which covers all of this.

For higher level functions you can use any JS library that wraps your desired REST api, but this will be API specific, like Parse.

Sure I could just wrap XMLHttpRequest to something more easy to use. I have tested XMLHttpRequest from Fuse so I know about it :slight_smile:

But writing code like this:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        console.log(xhr.responseText);
    }
}
xhr.open('GET', 'http://digi.no', true);
xhr.send(null);

isn’t as easy as an example with jQuery like this below or using $.ajax with a json structure:

$.get("http://digi.no", function(data) {
    console.log(data);
});

And the same goes for the other methods like POST, PUT and DELETE.

Maybe a Promise based solution like this:

var rest = require("FuseJS/Rest");
rest.get("http://digi.no").then(function (data) {
    console.log(data);
});

var rest = require("FuseJS/Rest");
var p1 = rest.post("http://senikk.com", {name: "Terje"});
p1.then(function (data) {
    console.log(data);
});

fetch() in Fuse does exactly that:

fetch(url).then(function(response) {


});

Read more here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

But fetch is only GET? It doesn’t say anything about init property in the docs https://www.fusetools.com/developers/guides/fusejs/fetch the page you refered to have:

fetch(url, { method: 'PUT' }).then(function(response) {
});

fetch(url, {
    method: 'post',
    headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"},
    body: 'foo=bar&lorem=ipsum'
  }).then(function(response) {}

Ok so its just the Fuse documentation that isn’t good enough. This makes it easy to make some functions for get/put/post/delete with predefined init :slight_smile:

You are looking at Fetch with a capital f not fetch. Fetch is returning an Observable. We are going to rename it to prevent confusion with fetch.

Aha I see, didn’t find any docs about fetch with lowercase f. Does it exist a page with all methods available in Fuse?

I’m so glad that I stumbed across this post; I also believed that this functionality didn’t exist, and I’m super pleased that it does.

Terje: we don’t have documentation for fetch (lowercase) because it’s a standard API. The link Anders Lassen posted covers its usage thoroughly :slight_smile:

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Michael: Good to hear :smiley:

Terje Pedersen wrote:

Sure I could just wrap XMLHttpRequest to something more easy to use. I have tested XMLHttpRequest from Fuse so I know about it :slight_smile:

But writing code like this:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        console.log(xhr.responseText);
    }
}
xhr.open('GET', 'http://digi.no', true);
xhr.send(null);

isn’t as easy as an example with jQuery like this below or using $.ajax with a json structure:

$.get("http://digi.no", function(data) {
    console.log(data);
});

And the same goes for the other methods like POST, PUT and DELETE.

Maybe a Promise based solution like this:

var rest = require("FuseJS/Rest");
rest.get("http://digi.no").then(function (data) {
    console.log(data);
});

var rest = require("FuseJS/Rest");
var p1 = rest.post("http://senikk.com", {name: "Terje"});
p1.then(function (data) {
    console.log(data);
});

Hello Terje Pedersen, i tried this in my App and with my own Server but i get the “modul not found: FuseJS/Rest” error. Can you help me to solve this problem?

maxresch@me.com wrote:

Terje Pedersen wrote:

Sure I could just wrap XMLHttpRequest to something more easy to use. I have tested XMLHttpRequest from Fuse so I know about it :slight_smile:

But writing code like this:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        console.log(xhr.responseText);
    }
}
xhr.open('GET', 'http://digi.no', true);
xhr.send(null);

isn’t as easy as an example with jQuery like this below or using $.ajax with a json structure:

$.get("http://digi.no", function(data) {
    console.log(data);
});

And the same goes for the other methods like POST, PUT and DELETE.

Maybe a Promise based solution like this:

var rest = require("FuseJS/Rest");
rest.get("http://digi.no").then(function (data) {
    console.log(data);
});

var rest = require("FuseJS/Rest");
var p1 = rest.post("http://senikk.com", {name: "Terje"});
p1.then(function (data) {
    console.log(data);
});

Hello Terje Pedersen, i tried this in my App and with my own Server but i get the “modul not found: FuseJS/Rest” error. Can you help me to solve this problem?

There is no FuseJS/Rest, this was just a proposal. So no, this is not expected to work.