koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

API versioning based on Accept header #30

Closed nilgradisnik closed 5 years ago

nilgradisnik commented 8 years ago

How would one go about building an API where the version is not part of an URL?

Other popular methods include using HTTP Accept header, custom request headers, etc.

It seems like a good way to achieve this would be by introducing condition to .route() options which can simply be a middeware function that returns a boolean. If the true it would continue to validate and execute handlers, if not it would simply skip to the next route(if any) with the same path and method.

This would give us an ability to inspect headers and execute the right validation and handlers based on the API version.

Thoughts?

paul42 commented 7 years ago

@nilgradisnik I think if you put validation onto the header you can just put the logic in your handler, if for example the header has a key of X-Accept-Version: 2 then you just put logic in your handler to call a different method. would that solve your use case?

aheckmann commented 5 years ago

Closing due to inactivity. Please reopen with a reproducible example if the issue persists.