-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[HTTP] Expose versioned router #153858
[HTTP] Expose versioned router #153858
Conversation
Pinging @elastic/kibana-core (Team:Core) |
… version via header and actually pass version via header
705b8eb
to
7b88ded
Compare
).resolves.toEqual( | ||
expect.objectContaining({ | ||
message: | ||
'Version expected at [get] [/my-path]. Please specify a version using the "elastic-api-version" header. Available versions are: "1,2"', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh so polite 😉
@@ -0,0 +1,3 @@ | |||
# @kbn/core-http-versioned-router-server-mocks | |||
|
|||
This package contains the mocks and test utils for core's server-side `http` service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
This package contains the mocks and test utils for core's server-side `http` service | |
This package contains the mocks and test utils for core's server-side `http` versioned router server. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
I added a couple of comments but nothing that blocks merging.
* Side Public License, v 1. | ||
*/ | ||
|
||
export function isValidRouteVersion(version: string): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation won't prevent duplicate versions from being defined. Should we consider adding an option to ensure that each version defined is unique or are we ok with leaving that up to consumers?
Ok, nevermind, we do
@@ -13,10 +13,19 @@ import { Method, VersionedRouterRoute } from './types'; | |||
|
|||
export class CoreVersionedRouter implements VersionedRouter { | |||
private readonly routes = new Set<CoreVersionedRoute>(); | |||
public static from({ router }: { router: IRouter }) { | |||
return new CoreVersionedRouter(router); | |||
public static from({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sneaky...I get where the inspiration comes from 😉
@@ -34,6 +35,7 @@ function createRouterMock({ routerPath = '' }: { routerPath?: string } = {}): Ro | |||
patch: jest.fn(), | |||
getRoutes: jest.fn(), | |||
handleLegacyErrors: jest.fn().mockImplementation((handler) => handler), | |||
versioned: createVersionedRouterMock(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the interest of reducing core domains packages footprint, I'd prefer we move the versioned router mock into the router server mocks now rather than piling on yet more tech debt.
private versionedRouter: undefined | VersionedRouter<Context> = undefined; | ||
public get versioned(): VersionedRouter<Context> { | ||
if (this.versionedRouter === undefined) { | ||
this.versionedRouter = CoreVersionedRouter.from({ router: this }); | ||
} | ||
return this.versionedRouter; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on recent discussions around architectural complexity specifically related to core's packages footprint, perhaps we should move the versioned router into the main core-http-router-server-internal
package.
* Side Public License, v 1. | ||
*/ | ||
|
||
export { RouteValidator } from './src/validator'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need another package just for the validator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This enabled us to use this same validation across the router and versioned router packages (otherwise we'd have a circular dep). Collapsing them into one removes this 🙌🏻
💚 Build Succeeded
Metrics [docs]Module Count
Public APIs missing comments
Public APIs missing exports
Page load bundle
Unknown metric groupsAPI count
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: cc @jloleysens |
Summary
Now that we merged #153543, this PR exposes the versioned router for teams to start using. The versioned router will be available on
IRouter
under a newversioned
property.Primary benefit of this approach is that plugin developers will not need to do anything other than "get" the
versioned
property to get a versioned router.Drawback is that this precludes us from passing in additional configuration, like a version, to scope the versioned router instance. For that we would need some kind of
createVersionedRouter({ version: ... })
. At this point it is not clear this is necessary, we could revisit this decision based on actual usage. Plugin developers could also do something like:In this way they could get many of the same benefits of a version-scoped version router, with the drawback that they need to pass this in for every route.
TODO
Future work