1.3.0 • Published 10 years ago

halster v1.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

halster

NPM version build status Test coverage Downloads

Extract and store hal previous and next links.

Installation

npm install halster

Usage

var halster = require('halster');

var links = halster({
  default: function() {
    return 'http://mysite.com/api';
  },
  previous: function(val) {
    return 'http://mysite.com/api?until=' + val;
  },
  next: function(val) {
    return 'http://mysite.com/api?from=' + val;
  },
  normalize: function(val) {
    return val.splice(13)[1];
  }
});

links.set({
  _links: {
    previous: {
      url: 'until=12345677'
    },
    next: {
      url: 'from=12345677'
    }
  }
});

links.get({previous: true});
// => 'http://mysite.com/api?until=12345677'

API

var links = halster()

Create a new halster instance. Takes an object of arguments. default is the default link to use if no previous or next were given. If previous or default are given the links are passed down to burl and handled there. The normalize link functions the same as in burl, converting urls into integers that can be compared to determine the newest / oldest link. Optionally you can also set invert: true to flip next and previous.

var halster = require('halster');

var links = halster({
  default: function() {
    return 'http://mysite.com/api';
  },
  previous: function(val) {
    return 'http://mysite.com/api?until=' + val;
  },
  next: function(val) {
    return 'http://mysite.com/api?from=' + val;
  },
  normalize: function(val) {
    return val.splice(13)[1];
  }
});

.set()

Pass in an object with a links / _links property and save the correct link within halster.

links.set({
  _links: {
    previous: {
      url: 'until=12345677'
    },
    next: {
      url: 'from=12345677'
    }
  }
});

.get()

Get an url. Can be passed an object with either next or previous set to true for the corresponding link. If no argument is provided the default link will be returned.

links.get({previous: true});
// => 'http://mysite.com/api?until=12345677'

links.get();
// => 'http://mysite.com/api'

See also

License

MIT

1.3.0

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago