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

[NP] Allow custom validations in HTTP Routes apart from @kbn/config-schema #51919

Merged
merged 44 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
93cc5db
[NP] Allow custom validations in HTTP Routes apart from @kbn/config-s…
afharo Nov 29, 2019
318f827
API docs
afharo Nov 29, 2019
1b3088a
Merge branch 'master' into np-decouple-configSchema
elasticmachine Dec 2, 2019
f9ea319
Allow validate function in the route handler (run-code validation)
afharo Dec 2, 2019
d28e0aa
Prefix RouteXXX + Params and Body Validation Aliases
afharo Dec 2, 2019
4fddfd1
Fix test broken by lodash
afharo Dec 2, 2019
472a60f
Update API docs
afharo Dec 2, 2019
b2e06db
Add default types for simpler manual declaration
afharo Dec 2, 2019
3996c2a
Add run-time validation of the RouteValidateSpec
afharo Dec 2, 2019
f5af513
Expose RouteValidationError instead of SchemaTypeError
afharo Dec 2, 2019
8d02bbe
Merge branch 'master' into np-decouple-configSchema
elasticmachine Dec 2, 2019
46c670f
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 3, 2019
db62d75
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 3, 2019
c949643
RouteValidator as a class to match config-schema interface
afharo Dec 4, 2019
1b2165c
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 4, 2019
336844e
Test for not-inline handler (need to check IRouter for #47047)
afharo Dec 4, 2019
bf71f8a
Add preValidation of the input for a safer custom validation
afharo Dec 4, 2019
ada74fe
Better types for RouteHandlers
afharo Dec 4, 2019
97d4cdb
[NP] Move route validation to RouteValidator wrapper
afharo Dec 6, 2019
4daabcb
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 6, 2019
091125a
Use the class only internally but maintain the same API
afharo Dec 6, 2019
9bb014a
[NP] Move route validation to RouteValidator wrapper
afharo Dec 6, 2019
dfdcccb
Merge branch 'master' into np-decouple-configSchema
elasticmachine Dec 7, 2019
b30e913
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 8, 2019
df9d33a
Fix types
afharo Dec 8, 2019
cccfe15
Ensure RouteValidator instance in KibanaRequest.from
afharo Dec 8, 2019
f99566d
Fix validator.tests (Buffer.from instead of new Buffer)
afharo Dec 8, 2019
d0ac3b9
Default precheck should allow null values
afharo Dec 9, 2019
be34522
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 9, 2019
67bd356
Merge branch 'np-decouple-configSchema' of github.com:afharo/kibana i…
afharo Dec 9, 2019
16ee910
Also allow undefined in preChecks
afharo Dec 9, 2019
83b9da6
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 9, 2019
6dc23dc
Merge branch 'master' into np-decouple-configSchema
elasticmachine Dec 16, 2019
6a6b0ab
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 17, 2019
c10723a
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 18, 2019
5c45228
MR feedback fixes
afharo Dec 18, 2019
fb0e6ad
Provide RouteValidationResolver to the validation function
afharo Dec 18, 2019
be46a67
Add functional tests
afharo Dec 18, 2019
ce57c16
Fix new functional tests
afharo Dec 18, 2019
3b930f3
Merge branch 'master' of github.com:elastic/kibana into np-decouple-c…
afharo Dec 18, 2019
e394259
Fix validator additional test
afharo Dec 18, 2019
1ece254
Fix test with new resolver
afharo Dec 18, 2019
7d6ea02
Remove unused import
afharo Dec 18, 2019
c8c7939
Rename ValidationResolver to ValidationResultFactory and change the i…
afharo Dec 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Wrap a router handler to catch and converts legacy boom errors to proper custom
<b>Signature:</b>

```typescript
handleLegacyErrors: <P extends ObjectType, Q extends ObjectType, B extends ObjectType>(handler: RequestHandler<P, Q, B>) => RequestHandler<P, Q, B>;
handleLegacyErrors: <P extends RouteValidateSpecs, Q extends RouteValidateSpecs, B extends RouteValidateSpecs>(handler: RequestHandler<P, Q, B>) => RequestHandler<P, Q, B>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface IRouter
| --- | --- | --- |
| [delete](./kibana-plugin-server.irouter.delete.md) | <code>RouteRegistrar&lt;'delete'&gt;</code> | Register a route handler for <code>DELETE</code> request. |
| [get](./kibana-plugin-server.irouter.get.md) | <code>RouteRegistrar&lt;'get'&gt;</code> | Register a route handler for <code>GET</code> request. |
| [handleLegacyErrors](./kibana-plugin-server.irouter.handlelegacyerrors.md) | <code>&lt;P extends ObjectType, Q extends ObjectType, B extends ObjectType&gt;(handler: RequestHandler&lt;P, Q, B&gt;) =&gt; RequestHandler&lt;P, Q, B&gt;</code> | Wrap a router handler to catch and converts legacy boom errors to proper custom errors. |
| [handleLegacyErrors](./kibana-plugin-server.irouter.handlelegacyerrors.md) | <code>&lt;P extends RouteValidateSpecs, Q extends RouteValidateSpecs, B extends RouteValidateSpecs&gt;(handler: RequestHandler&lt;P, Q, B&gt;) =&gt; RequestHandler&lt;P, Q, B&gt;</code> | Wrap a router handler to catch and converts legacy boom errors to proper custom errors. |
| [patch](./kibana-plugin-server.irouter.patch.md) | <code>RouteRegistrar&lt;'patch'&gt;</code> | Register a route handler for <code>PATCH</code> request. |
| [post](./kibana-plugin-server.irouter.post.md) | <code>RouteRegistrar&lt;'post'&gt;</code> | Register a route handler for <code>POST</code> request. |
| [put](./kibana-plugin-server.irouter.put.md) | <code>RouteRegistrar&lt;'put'&gt;</code> | Register a route handler for <code>PUT</code> request. |
Expand Down
3 changes: 3 additions & 0 deletions docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [ClusterClient](./kibana-plugin-server.clusterclient.md) | Represents an Elasticsearch cluster API client and allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>).<!-- -->See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->. |
| [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) | Helpers for working with errors returned from the Elasticsearch service.Since the internal data of errors are subject to change, consumers of the Elasticsearch service should always use these helpers to classify errors instead of checking error internals such as <code>body.error.header[WWW-Authenticate]</code> |
| [KibanaRequest](./kibana-plugin-server.kibanarequest.md) | Kibana specific abstraction for an incoming request. |
| [RouteValidationError](./kibana-plugin-server.routevalidationerror.md) | Error to return when the validation is not successful. |
| [RouteValidator](./kibana-plugin-server.routevalidator.md) | Custom validator class. If @<!-- -->kbn/config-schema is not a valid option in your plugin, you can use this class to define your own validation logic. |
| [SavedObjectsClient](./kibana-plugin-server.savedobjectsclient.md) | |
| [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | |
| [SavedObjectsRepository](./kibana-plugin-server.savedobjectsrepository.md) | |
Expand Down Expand Up @@ -188,6 +190,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [RouteContentType](./kibana-plugin-server.routecontenttype.md) | The set of supported parseable Content-Types |
| [RouteMethod](./kibana-plugin-server.routemethod.md) | The set of common HTTP methods supported by Kibana routing. |
| [RouteRegistrar](./kibana-plugin-server.routeregistrar.md) | Route handler common definition |
| [RouteValidateFunctionReturn](./kibana-plugin-server.routevalidatefunctionreturn.md) | Allowed returned format of the custom validate function |
| [SavedObjectAttribute](./kibana-plugin-server.savedobjectattribute.md) | Type definition for a Saved Object attribute value |
| [SavedObjectAttributeSingle](./kibana-plugin-server.savedobjectattributesingle.md) | Don't use this type, it's simply a helper type for [SavedObjectAttribute](./kibana-plugin-server.savedobjectattribute.md) |
| [SavedObjectsClientContract](./kibana-plugin-server.savedobjectsclientcontract.md) | Saved Objects is Kibana's data persisentence mechanism allowing plugins to use Elasticsearch for storing plugin state.<!-- -->\#\# SavedObjectsClient errors<!-- -->Since the SavedObjectsClient has its hands in everything we are a little paranoid about the way we present errors back to to application code. Ideally, all errors will be either:<!-- -->1. Caused by bad implementation (ie. undefined is not a function) and as such unpredictable 2. An error that has been classified and decorated appropriately by the decorators in [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md)<!-- -->Type 1 errors are inevitable, but since all expected/handle-able errors should be Type 2 the <code>isXYZError()</code> helpers exposed at <code>SavedObjectsErrorHelpers</code> should be used to understand and manage error responses from the <code>SavedObjectsClient</code>.<!-- -->Type 2 errors are decorated versions of the source error, so if the elasticsearch client threw an error it will be decorated based on its type. That means that rather than looking for <code>error.body.error.type</code> or doing substring checks on <code>error.body.error.reason</code>, just use the helpers to understand the meaning of the error:<!-- -->\`\`\`<!-- -->js if (SavedObjectsErrorHelpers.isNotFoundError(error)) { // handle 404 }<!-- -->if (SavedObjectsErrorHelpers.isNotAuthorizedError(error)) { // 401 handling should be automatic, but in case you wanted to know }<!-- -->// always rethrow the error unless you handle it throw error; \`\`\`<!-- -->\#\#\# 404s from missing index<!-- -->From the perspective of application code and APIs the SavedObjectsClient is a black box that persists objects. One of the internal details that users have no control over is that we use an elasticsearch index for persistance and that index might be missing.<!-- -->At the time of writing we are in the process of transitioning away from the operating assumption that the SavedObjects index is always available. Part of this transition is handling errors resulting from an index missing. These used to trigger a 500 error in most cases, and in others cause 404s with different error messages.<!-- -->From my (Spencer) perspective, a 404 from the SavedObjectsApi is a 404; The object the request/call was targeting could not be found. This is why \#14141 takes special care to ensure that 404 errors are generic and don't distinguish between index missing or document missing.<!-- -->\#\#\# 503s from missing index<!-- -->Unlike all other methods, create requests are supposed to succeed even when the Kibana index does not exist because it will be automatically created by elasticsearch. When that is not the case it is because Elasticsearch's <code>action.auto_create_index</code> setting prevents it from being created automatically so we throw a special 503 with the intention of informing the user that their Elasticsearch settings need to be updated.<!-- -->See [SavedObjectsClient](./kibana-plugin-server.savedobjectsclient.md) See [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A function executed when route path matched requested resource path. Request han
<b>Signature:</b>

```typescript
export declare type RequestHandler<P extends ObjectType, Q extends ObjectType, B extends ObjectType | Type<Buffer> | Type<Stream>, Method extends RouteMethod = any> = (context: RequestHandlerContext, request: KibanaRequest<TypeOf<P>, TypeOf<Q>, TypeOf<B>, Method>, response: KibanaResponseFactory) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;
export declare type RequestHandler<P extends RouteValidateSpecs = RouteValidator<unknown>, Q extends RouteValidateSpecs = RouteValidator<unknown>, B extends RouteValidateSpecs = RouteValidator<unknown>, Method extends RouteMethod = any> = (context: RequestHandlerContext, request: KibanaRequest<RouteValidatedType<P>, RouteValidatedType<Q>, RouteValidatedType<B>, Method>, response: KibanaResponseFactory) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;
```

## Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Route specific configuration.
<b>Signature:</b>

```typescript
export interface RouteConfig<P extends ObjectType, Q extends ObjectType, B extends ObjectType | Type<Buffer> | Type<Stream>, Method extends RouteMethod>
export interface RouteConfig<P extends RouteValidateSpecs, Q extends RouteValidateSpecs, B extends RouteValidateSpecs, Method extends RouteMethod>
```

## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Route handler common definition
<b>Signature:</b>

```typescript
export declare type RouteRegistrar<Method extends RouteMethod> = <P extends ObjectType, Q extends ObjectType, B extends ObjectType | Type<Buffer> | Type<Stream>>(route: RouteConfig<P, Q, B, Method>, handler: RequestHandler<P, Q, B, Method>) => void;
export declare type RouteRegistrar<Method extends RouteMethod> = <P extends RouteValidateSpecs, Q extends RouteValidateSpecs, B extends RouteValidateSpecs>(route: RouteConfig<P, Q, B, Method>, handler: RequestHandler<P, Q, B, Method>) => void;
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RouteSchemas contains the schemas for validating the different parts of a reques
<b>Signature:</b>

```typescript
export interface RouteSchemas<P extends ObjectType, Q extends ObjectType, B extends ObjectType | Type<Buffer> | Type<Stream>>
export interface RouteSchemas<P extends RouteValidateSpecs, Q extends RouteValidateSpecs, B extends RouteValidateSpecs>
```

## Properties
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidateFunctionReturn](./kibana-plugin-server.routevalidatefunctionreturn.md)

## RouteValidateFunctionReturn type

Allowed returned format of the custom validate function

<b>Signature:</b>

```typescript
export declare type RouteValidateFunctionReturn<T> = {
value: T;
error?: never;
} | {
value?: never;
error: RouteValidationError;
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidationError](./kibana-plugin-server.routevalidationerror.md) &gt; [(constructor)](./kibana-plugin-server.routevalidationerror._constructor_.md)

## RouteValidationError.(constructor)

Constructs a new instance of the `RouteValidationError` class

<b>Signature:</b>

```typescript
constructor(error: Error | string, path?: string[]);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| error | <code>Error &#124; string</code> | |
| path | <code>string[]</code> | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidationError](./kibana-plugin-server.routevalidationerror.md)

## RouteValidationError class

Error to return when the validation is not successful.

<b>Signature:</b>

```typescript
export declare class RouteValidationError extends SchemaTypeError
```

## Constructors

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(error, path)](./kibana-plugin-server.routevalidationerror._constructor_.md) | | Constructs a new instance of the <code>RouteValidationError</code> class |

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidator](./kibana-plugin-server.routevalidator.md) &gt; [(constructor)](./kibana-plugin-server.routevalidator._constructor_.md)

## RouteValidator.(constructor)

Constructs a new instance of the `RouteValidator` class

<b>Signature:</b>

```typescript
constructor(validationRule: (data: any) => RouteValidateFunctionReturn<T>);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| validationRule | <code>(data: any) =&gt; RouteValidateFunctionReturn&lt;T&gt;</code> | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidator](./kibana-plugin-server.routevalidator.md)

## RouteValidator class

Custom validator class. If @<!-- -->kbn/config-schema is not a valid option in your plugin, you can use this class to define your own validation logic.

<b>Signature:</b>

```typescript
export declare class RouteValidator<T>
```

## Constructors

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(validationRule)](./kibana-plugin-server.routevalidator._constructor_.md) | | Constructs a new instance of the <code>RouteValidator</code> class |

## Methods

| Method | Modifiers | Description |
| --- | --- | --- |
| [validate(data, namespace)](./kibana-plugin-server.routevalidator.validate.md) | | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidator](./kibana-plugin-server.routevalidator.md) &gt; [validate](./kibana-plugin-server.routevalidator.validate.md)

## RouteValidator.validate() method

<b>Signature:</b>

```typescript
validate(data: any, namespace?: string): T;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| data | <code>any</code> | |
| namespace | <code>string</code> | |

<b>Returns:</b>

`T`

1 change: 1 addition & 0 deletions packages/kbn-config-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {

export { ObjectType, TypeOf, Type };
export { ByteSizeValue } from './byte_size_value';
export { SchemaTypeError, ValidationError } from './errors';

function any(options?: TypeOptions<any>) {
return new AnyType(options);
Expand Down
97 changes: 96 additions & 1 deletion src/core/server/http/http_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ import supertest from 'supertest';

import { ByteSizeValue, schema } from '@kbn/config-schema';
import { HttpConfig } from './http_config';
import { Router } from './router';
import {
Router,
RouteValidationError,
RouteValidator,
KibanaRequest,
KibanaResponseFactory,
} from './router';
import { loggingServiceMock } from '../logging/logging_service.mock';
import { HttpServer } from './http_server';
import { Readable } from 'stream';
import { RequestHandlerContext } from 'kibana/server';

const cookieOptions = {
name: 'sid',
Expand Down Expand Up @@ -288,6 +295,94 @@ test('valid body', async () => {
});
});

test('valid body with validate function', async () => {
const router = new Router('/foo', logger, enhanceWithContext);

router.post(
{
path: '/',
validate: {
body: new RouteValidator(({ bar, baz } = {}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned with the amount of code that users have to write. If you want to declare a request handler separately it doesn't become simpler:

const handler: RequestHandler<never, never, RouteValidateSpecs<Stream>> = (context, req, res) => {
   req.body

What do you think if we always require users to specify types explicitly? Something like:

const validate = {
  query: schema.object({
    page: schema.number(),
  }),
  body: schema.stream(),
};
router.post( {
  path: '/',
  validate,
  // or another type helper that we provide
  <RequestTypeOf validate>(context, req, res) =>
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out!
I've revisited the type inference and I think I can get to the more sensible point:

const handler: RequestHandler<unknown, unknown, Stream> = (context, req, res) => {
  req.body // This is a Stream
}

RequestHandler is not (should not) be tied to the Config-Schema/RouteValidation types but to their outputs.

TS compiles. Running tests locally and, if all good, I'll push the changes :)

if (typeof bar === 'string' && typeof baz === 'number') {
return { value: { bar, baz } };
} else {
return { error: new RouteValidationError('Wrong payload', ['body']) };
}
}),
},
},
(context, req, res) => {
return res.ok({ body: req.body });
}
);

const { registerRouter, server: innerServer } = await server.setup(config);
registerRouter(router);

await server.start();

await supertest(innerServer.listener)
.post('/foo/')
.send({
bar: 'test',
baz: 123,
})
.expect(200)
.then(res => {
expect(res.body).toEqual({ bar: 'test', baz: 123 });
});
});

// https://github.com/elastic/kibana/issues/47047
test('not inline handler', async () => {
const router = new Router('/foo', logger, enhanceWithContext);

const handler = (
context: RequestHandlerContext,
req: KibanaRequest<unknown, unknown, { bar: string; baz: number }>,
res: KibanaResponseFactory
) => {
const body = {
bar: req.body.bar.toUpperCase(),
baz: req.body.baz.toString(),
};

return res.ok({ body });
};

router.post(
{
path: '/',
validate: {
body: new RouteValidator(({ bar, baz } = {}) => {
if (typeof bar === 'string' && typeof baz === 'number') {
return { value: { bar, baz } };
} else {
return { error: new RouteValidationError('Wrong payload', ['body']) };
}
}),
},
},
handler
);

const { registerRouter, server: innerServer } = await server.setup(config);
registerRouter(router);

await server.start();

await supertest(innerServer.listener)
.post('/foo/')
.send({
bar: 'test',
baz: 123,
})
.expect(200)
.then(res => {
expect(res.body).toEqual({ bar: 'TEST', baz: '123' });
});
});

test('invalid body', async () => {
const router = new Router('/foo', logger, enhanceWithContext);

Expand Down
3 changes: 3 additions & 0 deletions src/core/server/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export {
RouteConfigOptionsBody,
RouteContentType,
validBodyOutput,
RouteValidateFunctionReturn,
RouteValidator,
RouteValidationError,
} from './router';
export { BasePathProxyServer } from './base_path_proxy_server';
export { OnPreAuthHandler, OnPreAuthToolkit } from './lifecycle/on_pre_auth';
Expand Down
15 changes: 12 additions & 3 deletions src/core/server/http/router/error_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@
*/

import Boom from 'boom';
import { ObjectType, TypeOf } from '@kbn/config-schema';
import { KibanaRequest } from './request';
import { KibanaResponseFactory } from './response';
import { RequestHandler } from './router';
import { RequestHandlerContext } from '../../../server';
import { RouteMethod } from './route';
import { RouteValidatedType, RouteValidateSpecs } from './validator';

export const wrapErrors = <P extends ObjectType, Q extends ObjectType, B extends ObjectType>(
export const wrapErrors = <
P extends RouteValidateSpecs,
Q extends RouteValidateSpecs,
B extends RouteValidateSpecs
>(
handler: RequestHandler<P, Q, B, RouteMethod>
): RequestHandler<P, Q, B, RouteMethod> => {
return async (
context: RequestHandlerContext,
request: KibanaRequest<TypeOf<P>, TypeOf<Q>, TypeOf<B>, RouteMethod>,
request: KibanaRequest<
RouteValidatedType<P>,
RouteValidatedType<Q>,
RouteValidatedType<B>,
RouteMethod
>,
response: KibanaResponseFactory
) => {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/http/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ export {
} from './response';

export { IKibanaSocket } from './socket';

export { RouteValidateFunctionReturn, RouteValidator, RouteValidationError } from './validator';
Loading