node-oauth2-provider icon indicating copy to clipboard operation
node-oauth2-provider copied to clipboard

Fix abuse of EventEmitter for callbacks

Open ammmir opened this issue 13 years ago • 5 comments

Maybe subclassing or delegation would be better...

ammmir avatar Sep 08 '11 07:09 ammmir

+1

timoxley avatar Jul 06 '12 09:07 timoxley

i'm not sure how serious i was about this... please provide some inspiration for a better api.

ammmir avatar Jul 06 '12 09:07 ammmir

Events make sense if are likely going to have multiple listeners, but in this case, you're only going to provide a single listener to those events (in-fact, because you're passing a callback, multiple listeners doesn't even make sense). Just provide overridable functions.

// roughly
var provider = OAuth2Provider.create({
  access_token: function() {

  },
  lookup_grant: function(client_id, client_secret, code, next) {

  },
  create_access_token: function(user_id, client_id, next) {

  },
  save_access_token: function(user_id, client_id, atok) {

  },
  remove_grant: function() {
      // etc
  }
})

timoxley avatar Jul 06 '12 10:07 timoxley

You could also do it via dependency injection - I always prefer composition over inheritance:

The provider would act as an api, and contain a token repository. This way you have two cohesive classes, one is public facing and the other is purely responsible for saving data. You could still emit the event for backwards compatibility, but I would have that done through a contained EventEmitter rather than base one.

oveddan avatar Sep 25 '12 03:09 oveddan

Working on this https://github.com/oveddan/node-oauth2-provider

oveddan avatar Sep 25 '12 03:09 oveddan