HTTP State Source

Provides a simple way of making HTTP requests.

var UsersAPI = Marty.createStateSource({
  type: 'http',
  createUser: function (user) {
    return this.post({ url: '/users', body: user }).then(function (res) {
      if (res.ok) {
        return res.json();
      }

      throw new Error('Failed to create user', res);
    });
  }
});
class UsersAPI extends Marty.HttpStateSource {
  createUser(user) {
    return this.post({ url: '/users', body: user }).then(res => {
      if (res.ok) {
        return res.json();
      }

      throw new Error('Failed to create user', res);
    });
  }
}

baseUrl

An (optional) base url to prepend to any urls.

request(options)

Starts an HTTP request with the given method and options. We use the fetch polyfill however you can override request() with your own implementation. The only requirement is it returns a Promise.

var UsersAPI = Marty.createStateSource({
  type: 'http',
  createUser: function (user) {
    return this.request({
      url: '/users',
      method: 'POST',
      body: { name: 'foo' },
      contentType: 'application/json'
    });
  }
});
class UsersAPI extends Marty.HttpStateSource {
  createUser(user) {
    return this.request({
      url: '/users',
      method: 'POST',
      body: { name: 'foo' }
    });
  }
}

Options

Name type default description
url string Url of resource
method string get http method
headers object {} http headers
contentType string application/json Content type of request
dataType string json The type of data that you're expecting back from the server. xml, json, script, or html

get(url)

Same as request({ method: 'GET', url: url })

get(options)

Same as request(_.extend(options, { method: 'GET'})

post(url)

Same as request({ method: 'POST', url: url })

post(options)

Same as request(_.extend(options, { method: 'POST'})

put(url)

Same as request({ method: 'PUT', url: url })

put(options)

Same as request(_.extend(options, { method: 'PUT'})

delete(url)

Same as request({ method: 'DELETE', url: url })

delete(options)

Same as request(_.extend(options, { method: 'DELETE'})

Hooks

Hooks allows you to make changes to requests before they are sent and as well as when responses are received. This can be useful when you want to do things like automatically converting all JSON responses to immutable objects.

Hooks are object literals which have 4 optional keys: id, before, after and priority. id is required if you wish to have multiple hooks registered. If before is present then it will be called with the request as its argument. If after is present then it will be called after the response is received with the response as its argument. Setting a priority allows you to alter in what order the hook is executed (The smaller the number, the earlier it will be executed).

var Marty = require('marty');

Marty.HttpStateSource.addHook({
  id: 'SomeHook',
  priority: 1,
  before(req) {
    req.headers['Foo'] = 'bar';
  },
  after(res) {
    return res.json();
  }
});

addHook

Registers the hook in the pipeline

var Marty = require('marty');

Marty.HttpStateSource.addHook({
  id: 'AnotherHook',
  priority: 1,
  before: function (req) {
    req.headers['Foo'] = 'bar';
  },
  after: function (res) {
    return res.json();
  }
});

removeHook

Removes the hook from the pipline.

var Marty = require('marty');
var ParseJSON = require('marty/http/hooks/parseJSON');

Marty.HttpStateSource.removeHook(ParseJSON);