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

Apply middleware per resource #92

Closed
Nevon opened this issue Mar 23, 2018 · 1 comment
Closed

Apply middleware per resource #92

Nevon opened this issue Mar 23, 2018 · 1 comment

Comments

@Nevon
Copy link
Collaborator

Nevon commented Mar 23, 2018

I would like to build a middleware that will deserialize my responses into the appropriate response object depending on the resource. To do that, I would need to be able to do something like:

const deserializeInto = require('./middleware/deserialization')
const User = require('./responses/user')

const client = forge({
  resources: {
    User: {
      all: { path: '/users', middleware: [deserializeInto(users => users.map(User.fromResponse))]},
      byId: { path: '/users/{id}', middleware: [deserializeInto(User.fromResponse)]},
    },
  }
})

To do this currently, I would need to apply a global middleware and switch on the request path, which feels very ugly and prone to breakage.

@Nevon
Copy link
Collaborator Author

Nevon commented Mar 23, 2018

Ah, nevermind, I'm a bit of an idiot. I didn't notice that the middleware actually receives the resourceName and resourceMethod, so it should be okay to selectively apply the middleware based on that.

@Nevon Nevon closed this as completed Mar 23, 2018
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

1 participant