Api

Space.messaging.Api

Purpose

Define an interface for sending Commands, or to call Meteor.Methods if not using messaging architecture. Each method defined in a Space.messaging.Api is a Meteor.method, which if named the same as a Command becomes an endpoint the application's CommandBus delivers to. If not a Command, it can be used with Meteor.call or Meteor.applyas per normal.

Examples

// On server
My.Api = Space.messaging.Api.extend(My,'Api', {

  dependencies: {
    things: 'My.ThingsCollection'
  }

  methods() {
    return [{
      'My.Command': function (context, command) {
        if(context && !context.userId)
          throw new this.meteor.Error('You must be logged in to do this')
        this.commandBus.send(command)
      },
      'getThingsTotal': function (context, options) {
        if(context && !context.userId)
          throw new this.meteor.Error('You must be logged in to do this')
        return this.things.find(options).count()
      }
    }]
  }
});

Here you can see an example of both a Command and generic endpoint, with a simple validation example of checking the user is logged in.

Testing

myCommand = new MyCommand(…)
My.App.test(My.Api).given().send(myCommand).expect(myCommand)
My.App.test(My.Api).given().call(myCommand).expect(function({ //expectation }))