Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Future #1

Open
Anachron opened this issue Oct 18, 2014 · 4 comments
Open

Future #1

Anachron opened this issue Oct 18, 2014 · 4 comments

Comments

@Anachron
Copy link

Hi there, is this project still maintained? I kinda like it and I would like to know if you are still working on it.

@alancnet
Copy link
Owner

Thanks, Anachron! Yes, this project is still maintained. Would you like to contribute? Does it need something?

@Anachron
Copy link
Author

Yes, I would like to have something like rest.all for easy manipulation:

rest.all('/api/food', function(req, rest, action, url) {
  // action is based on HTTP header, get/put/post/delete
  switch( action ){
    case 'index': // /api/food
      rest.ok(records);
      break;

    case 'view': // /api/food/:id
      var record = records[req.params.id];
      if (record) rest.ok(record);
      else rest.notFound();
      break;

    case 'create': // /api/food
      records[req.params.id] = req.body;
      return rest.accepted(url + encodeURI(req.params.id));

    case 'update': // api/food/:id
      records.push(req.body);
      rest.created(url + (records.length - 1));
      break;

    case 'delete': // /api/food/:id
      delete records[req.params.id];
      rest.gone();
      break;
  }
});

Does it make sense? For me, this is more readable. Also I would only have to send the basic URL, the rest the application will handle.

@alancnet
Copy link
Owner

So in your sample, you route /api/food to the handler, but it wouldn't account for /api/food/5. You'll need to split them up. Also your actions aren't restful... They should be HTTP verbs. So how about this syntax?

rest.all('/api/food', function(req, rest) {
  switch( req.method ){
    case 'get': // /api/food
      rest.ok(records);
      break;

    case 'post': // /api/food
      records[req.params.id] = req.body;
      return rest.accepted(req.url + encodeURI(req.params.id));

    default:
      rest.notImplemented();
      break;
  }
});
rest.all('/api/food/:id', function(req, rest) {
  switch( req.method ){
    case 'get':
      var record = records[req.params.id];
      if (record) rest.ok(record);
      else rest.notFound();
      break;

    case 'put':
      records.push(req.body);
      rest.created(req.url + (records.length - 1));
      break;

    case 'delete':
      delete records[req.params.id];
      rest.gone();
      break;

    default:
      rest.notImplemented();
      break;
  }
});

@alancnet
Copy link
Owner

Actually you can have an optional parameter, so you could handle everything in a single handler

rest.all('/api/food/:id?', function(req, rest) {
  switch( req.method ){
    case 'get': // /api/food
      if (req.params.id) {
        var record = records[req.params.id];
        if (record) rest.ok(record);
        else rest.notFound();
      } else {
        rest.ok(records);
      }
      break;

    case 'post': // /api/food
      records[req.params.id] = req.body;
      return rest.accepted(req.url + encodeURI(req.params.id));

    case 'put':
      records.push(req.body);
      rest.created(req.url + (records.length - 1));
      break;

    case 'delete':
      delete records[req.params.id];
      rest.gone();
      break;

    default:
      rest.notImplemented();
      break;
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants