Skip to content
Consume any RESTful API with just a few lines of JavaScript
JavaScript Makefile
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
Makefile
README.md
index.js
package.json

README.md

Build Status

Important Notice

I'm no longer actively maintaining this project. If you are interested supporting it - ping me on twitter. The only thing that I will ask you is to not change the API drastically. If you are planning on doing that - better start a brand new project.

If you want me to transfer you only the name on npm, I'd be happy to only if the project does not have any downloads on npm lately. In case it's being downloaded, there are people that depend on it and might step up and start maintaining, so I will not transfer it to you, regardless if you want to release a new major version etc.

If you have any other questions, let me know.

Thanks!

Veselin

Sourcery

Sourcery is a framework for building RESTful API clients. It's heavily inspired by ActiveResource and behaves almost the same way. It's blazing fast and extremely simple to use.

Synopsis

Defining resources

var Resource = require('sourcery');

var Base = Resource.extend({
  host: 'http://example.com/api/v1', // base host
  ext:  'json',                      // optional, will include `.json` in the URLs
  root: true,                        // optional, will include root element
});

var Project = Base.extend({
  path: '/projects', // http://example.com/api/v1/projects
  name: 'Project',   // optional, this is the name for the root element
});

CRUD

Find one:

Project.first({ id: 1 }, function(err, project) {
  project.get('name');
  project.get('id');
});

Find many:

Project.all({ user_id: 1 }, function(err, projects) {
  projects[0].get('user_id');
  projects[1].get('user_id');
});

Create:

var project = new Project;
project.set('name', 'Top secret');

project.save(function(err, project) {
  console.log(project);
});
Project.create({ name: 'Secret' }, function(err, project) {
  console.log(project);
});

Update:

var project = new Project({ id: 1 });
project.set('name', 'New name');

project.save(function(err, project) {
  console.log(project);
});

Destroy:

Project.destroy(1, function(err) {
  // done;
});
var project = new Project({ id: 1 });

project.destroy(function(err) {
  // done
});

Include params:

Project
  .where('page', 1)
  .where('limit', 3)
  .all(function(err, projects) {
    console.log(projects);
  });

Nested resources

var Task = Base.extend({
  path: '/projects/:project_id/tasks',
});

Sourcery will replace the placeholders, in this example ":project_id", with the matching attribute.

Basic Auth

var BasicAuth = require('sourcery').BasicAuth;

var Base = Resource.extend({
  host: 'http://example.com/api/v1',
  auth: {
    type:  BasicAuth,
    user: 'replace-with-real-user',
    pass: 'replace-with-real-pass'
  }
});

Wait, but what about...

Please open issues if you have feature requests. Thanks!

Install

$ npm install sourcery

Requirements

  • Node.js >= 0.6.0

Tests

$ npm install
$ make test

Real world examples

TODO

  • More examples
  • Fake Engine
  • Custom endpoints
  • Schema
  • Associations
  • Validations
  • Test helpers
  • XML
  • Custom error classes
  • Resource.member()

License

MIT License

Copyright (C) 2012 Veselin Todorov (hi@vesln.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.