Skip to content

Response Groups #15

@NickGlenn

Description

@NickGlenn

Here's the README section I put together to help describe the requirements of this task.


Response Groups

Sometimes you may want to run different assertions or return different fixture data based on the input given in the request. For this, you can create a "response group" by using RESTfool's group() method.

The group() method takes an object containing different "trigger" names and their criteria. For now, the criteria is simply a function that returns true if satisfied. If no trigger is matched, then the "default" trigger will be used instead.

var getPosts = restfool.group({

  // will attempt to match this first...
  sortByTitleAsc: function (req) {
    return (
      req.query.sort === 'title' &&
      req.query.order === 'asc'
    )
  },

  // otherwise, this criteria will be checked...
  sortByTitleDesc: function (req) {
    return (
      req.query.sort === 'title' &&
      req.query.order === 'desc'
    )
  }

  // default will be passed if neither of the above matched...

})

// add the group to your router/server
server.get('/posts', getPosts)

Once you have a group, you can begin adding middleware for the different triggers using the on() method. You can define multiple middleware per trigger that will be called in the order that they are added.

Note: You can also use the onDefault method as a shortcut for on('default', ...).

var _ = require('lodash')
var fixture = [ ... ]

getPosts.on('sortByTitleAsc', function (req, res, next) {
  res.send(_.sortByOrder(fixture, 'user', 'asc'))
})

getPosts.on('sortByTitleDesc', function (req, res, next) {
  res.send(_.sortByOrder(fixture, 'user', 'desc'))
})

getPosts.onDefault(function (req, res, next) {
  res.send(fixture)
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions