-
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
[Versioned HTTP] Add response runtime and type-level validation #153011
[Versioned HTTP] Add response runtime and type-level validation #153011
Conversation
…nd TS type checking
@@ -27,14 +27,18 @@ export interface KibanaSuccessResponseFactory { | |||
* Status code: `200`. | |||
* @param options - {@link HttpResponseOptions} configures HTTP response body & headers. | |||
*/ | |||
ok(options?: HttpResponseOptions): IKibanaResponse; | |||
ok<T extends HttpResponsePayload = HttpResponsePayload>( |
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.
Needed for properly type checking responses. Assuming we only want this for ok
. But I can easily see this also being checkable for non-OK responses like 400
.
interface FullValidationConfig<P, Q, B, R> { | ||
/** Validation to run against route inputs: params, query and body */ | ||
in?: InValidation<P, Q, B>; | ||
/** Validation to run against route output */ | ||
out?: Type<R>; | ||
} |
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.
Adding something like this gives us a few new capabilities:
- TS checking enforcing shape of return type
- Runtime checks against the shape of return type
- A clearer path to true HTTP API introspection since we have the types for the return value of the endpoint...
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.
I like where we're heading here. I left a few nits as naming conventions.
packages/core/versioning/core-version-http-server/src/version_http_toolkit.ts
Outdated
Show resolved
Hide resolved
packages/core/versioning/core-version-http-server/src/version_http_toolkit.ts
Outdated
Show resolved
Hide resolved
packages/core/versioning/core-version-http-server/src/version_http_toolkit.ts
Outdated
Show resolved
Hide resolved
src/plugins/data_views/server/rest_api_routes/runtime_fields/response_formatter.ts
Show resolved
Hide resolved
x-pack/test/plugin_api_perf/plugins/task_manager_performance/server/init_routes.ts
Show resolved
Hide resolved
packages/core/versioning/core-version-http-server/src/version_http_toolkit.ts
Outdated
Show resolved
Hide resolved
Pinging @elastic/kibana-core (Team:Core) |
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.
Response Ops changes LGTM
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.
LGTM
opts: AddVersionOpts<P, Q, B>, | ||
handler: RequestHandler<P, Q, B, Ctx> | ||
addVersion<P, Q, B, R>( | ||
options: AddVersionOpts<P, Q, B, R>, |
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.
👍
* interface Foo { bar?: string } | ||
* const foo: WithRequiredProperty<Foo, 'bar'> = { bar: 'baz' } | ||
*/ | ||
export type WithRequiredProperty<Type, Key extends keyof Type> = Omit<Type, Key> & { |
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.
We'll need to add a test if the type starts getting used a lot.
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]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 @rudolf |
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.
response_formatter.ts
explicit return type change LGTM!
Summary
This PR restructures the
validation
object on the.addVersion
method by:in
object for thebody
,params
andquery
validationsout
so that we can have both runtime and TS type checking our responsesTo reviewers: easiest way to interpret these changes is to read the
example.ts
file.