Skip to content

Commit

Permalink
Update API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Dec 2, 2019
1 parent 4fddfd1 commit 472a60f
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 33 deletions.
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 RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams>(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 RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams&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
6 changes: 4 additions & 2 deletions docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,17 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [ResponseError](./kibana-plugin-server.responseerror.md) | Error message and optional data send to the client in case of error. |
| [ResponseErrorAttributes](./kibana-plugin-server.responseerrorattributes.md) | Additional data to provide error details. |
| [ResponseHeaders](./kibana-plugin-server.responseheaders.md) | Http response headers to set. |
| [RouteBodyValidationParams](./kibana-plugin-server.routebodyvalidationparams.md) | Similar to the URL and query params, the body payload for a route are allowed to be validated via <code>schema.object(...)</code> (from @<!-- -->kbn/config-schema) or a <code>RouteValidateFunction</code> that returns the custom-validated value (similarly to the URL and query params). But, on top of that, when the <code>body</code> options <code>parse: false</code> or <code>output: 'stream'</code> are used, the <code>@kbn/config-schema</code> options to validate it are <code>schema.buffer(...)</code> or <code>schema.stream(...)</code>, respectively. |
| [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 |
| [RouteURLValidationParams](./kibana-plugin-server.routeurlvalidationparams.md) | URL params and query properties are allowed to be validated via <code>schema.object(...)</code> (from @<!-- -->kbn/config-schema) or a <code>RouteValidateFunction</code> that returns the custom-validated value. |
| [RouteValidateFunction](./kibana-plugin-server.routevalidatefunction.md) | Custom validate function (only if @<!-- -->kbn/config-schema is not a valid option in your plugin) |
| [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) |
| [SavedObjectsClientFactory](./kibana-plugin-server.savedobjectsclientfactory.md) | Describes the factory used to create instances of the Saved Objects Client. |
| [SavedObjectsClientWrapperFactory](./kibana-plugin-server.savedobjectsclientwrapperfactory.md) | Describes the factory used to create instances of Saved Objects Client Wrappers. |
| [UiSettingsType](./kibana-plugin-server.uisettingstype.md) | UI element type to represent the settings. |
| [ValidateFunction](./kibana-plugin-server.validatefunction.md) | Custom validate function (only if @<!-- -->kbn/config-schema is not a valid option in your plugin) |
| [ValidateFunctionReturn](./kibana-plugin-server.validatefunctionreturn.md) | Allowed returned format of the custom validate function |

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 | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>, Method extends RouteMethod = any> = (context: RequestHandlerContext, request: KibanaRequest<ValidatedType<P>, ValidatedType<Q>, ValidatedType<B>, Method>, response: KibanaResponseFactory) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;
export declare type RequestHandler<P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams, 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
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

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

## RouteBodyValidationParams type

Similar to the URL and query params, the body payload for a route are allowed to be validated via `schema.object(...)` (from @<!-- -->kbn/config-schema) or a `RouteValidateFunction` that returns the custom-validated value (similarly to the URL and query params). But, on top of that, when the `body` options `parse: false` or `output: 'stream'` are used, the `@kbn/config-schema` options to validate it are `schema.buffer(...)` or `schema.stream(...)`<!-- -->, respectively.

<b>Signature:</b>

```typescript
export declare type RouteBodyValidationParams = RouteURLValidationParams | Type<Buffer> | Type<Stream>;
```
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 | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>, Method extends RouteMethod>
export interface RouteConfig<P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams, 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 | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>>(route: RouteConfig<P, Q, B, Method>, handler: RequestHandler<P, Q, B, Method>) => void;
export declare type RouteRegistrar<Method extends RouteMethod> = <P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams>(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 | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>>
export interface RouteSchemas<P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams>
```

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

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

## RouteURLValidationParams type

URL params and query properties are allowed to be validated via `schema.object(...)` (from @<!-- -->kbn/config-schema) or a `RouteValidateFunction` that returns the custom-validated value.

<b>Signature:</b>

```typescript
export declare type RouteURLValidationParams = ObjectType | RouteValidateFunction<unknown>;
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ValidateFunction](./kibana-plugin-server.validatefunction.md)
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RouteValidateFunction](./kibana-plugin-server.routevalidatefunction.md)

## ValidateFunction type
## RouteValidateFunction type

Custom validate function (only if @<!-- -->kbn/config-schema is not a valid option in your plugin)

<b>Signature:</b>

```typescript
export declare type ValidateFunction<T> = (data: any) => ValidateFunctionReturn<T>;
export declare type RouteValidateFunction<T> = (data: any) => RouteValidateFunctionReturn<T>;
```
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

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

## ValidateFunctionReturn type
## RouteValidateFunctionReturn type

Allowed returned format of the custom validate function

<b>Signature:</b>

```typescript
export declare type ValidateFunctionReturn<T> = {
export declare type RouteValidateFunctionReturn<T> = {
value: T;
error?: undefined;
} | {
Expand Down
44 changes: 25 additions & 19 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ export interface IRouter {
//
// @internal
getRoutes: () => RouterRoute[];
handleLegacyErrors: <P extends ObjectType, Q extends ObjectType, B extends ObjectType>(handler: RequestHandler<P, Q, B>) => RequestHandler<P, Q, B>;
handleLegacyErrors: <P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams>(handler: RequestHandler<P, Q, B>) => RequestHandler<P, Q, B>;
patch: RouteRegistrar<'patch'>;
post: RouteRegistrar<'post'>;
put: RouteRegistrar<'put'>;
Expand Down Expand Up @@ -760,10 +760,10 @@ export class KibanaRequest<Params = unknown, Query = unknown, Body = unknown, Me
constructor(request: Request, params: Params, query: Query, body: Body, withoutSecretHeaders: boolean);
// (undocumented)
readonly body: Body;
// Warning: (ae-forgotten-export) The symbol "ValidatedType" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "RouteValidatedType" needs to be exported by the entry point index.d.ts
//
// @internal
static from<P extends ObjectType | ValidateFunction<any>, Q extends ObjectType | ValidateFunction<any>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<any>>(req: Request, routeSchemas?: RouteSchemas<P, Q, B>, withoutSecretHeaders?: boolean): KibanaRequest<ValidatedType<P>, ValidatedType<Q>, ValidatedType<B>, any>;
static from<P extends ObjectType | RouteValidateFunction<any>, Q extends ObjectType | RouteValidateFunction<any>, B extends ObjectType | Type<Buffer> | Type<Stream> | RouteValidateFunction<any>>(req: Request, routeSchemas?: RouteSchemas<P, Q, B>, withoutSecretHeaders?: boolean): KibanaRequest<RouteValidatedType<P>, RouteValidatedType<Q>, RouteValidatedType<B>, any>;
readonly headers: Headers;
// (undocumented)
readonly params: Params;
Expand Down Expand Up @@ -1054,7 +1054,7 @@ export type RedirectResponseOptions = HttpResponseOptions & {
};

// @public
export type RequestHandler<P extends ObjectType | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>, Method extends RouteMethod = any> = (context: RequestHandlerContext, request: KibanaRequest<ValidatedType<P>, ValidatedType<Q>, ValidatedType<B>, Method>, response: KibanaResponseFactory) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;
export type RequestHandler<P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams, Method extends RouteMethod = any> = (context: RequestHandlerContext, request: KibanaRequest<RouteValidatedType<P>, RouteValidatedType<Q>, RouteValidatedType<B>, Method>, response: KibanaResponseFactory) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;

// @public
export interface RequestHandlerContext {
Expand Down Expand Up @@ -1096,7 +1096,10 @@ export type ResponseHeaders = {
};

// @public
export interface RouteConfig<P extends ObjectType | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>, Method extends RouteMethod> {
export type RouteBodyValidationParams = RouteURLValidationParams | Type<Buffer> | Type<Stream>;

// @public
export interface RouteConfig<P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams, Method extends RouteMethod> {
options?: RouteConfigOptions<Method>;
path: string;
validate: RouteSchemas<P, Q, B> | false;
Expand Down Expand Up @@ -1124,10 +1127,10 @@ export type RouteContentType = 'application/json' | 'application/*+json' | 'appl
export type RouteMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options';

// @public
export type RouteRegistrar<Method extends RouteMethod> = <P extends ObjectType | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>>(route: RouteConfig<P, Q, B, Method>, handler: RequestHandler<P, Q, B, Method>) => void;
export type RouteRegistrar<Method extends RouteMethod> = <P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams>(route: RouteConfig<P, Q, B, Method>, handler: RequestHandler<P, Q, B, Method>) => void;

// @public
export interface RouteSchemas<P extends ObjectType | ValidateFunction<unknown>, Q extends ObjectType | ValidateFunction<unknown>, B extends ObjectType | Type<Buffer> | Type<Stream> | ValidateFunction<unknown>> {
export interface RouteSchemas<P extends RouteURLValidationParams, Q extends RouteURLValidationParams, B extends RouteBodyValidationParams> {
// (undocumented)
body?: B;
// (undocumented)
Expand All @@ -1136,6 +1139,21 @@ export interface RouteSchemas<P extends ObjectType | ValidateFunction<unknown>,
query?: Q;
}

// @public
export type RouteURLValidationParams = ObjectType | RouteValidateFunction<unknown>;

// @public
export type RouteValidateFunction<T> = (data: any) => RouteValidateFunctionReturn<T>;

// @public
export type RouteValidateFunctionReturn<T> = {
value: T;
error?: undefined;
} | {
value?: undefined;
error: SchemaTypeError;
};

// @public (undocumented)
export interface SavedObject<T extends SavedObjectAttributes = any> {
attributes: T;
Expand Down Expand Up @@ -1722,18 +1740,6 @@ export interface UserProvidedValues<T = any> {
userValue?: T;
}

// @public
export type ValidateFunction<T> = (data: any) => ValidateFunctionReturn<T>;

// @public
export type ValidateFunctionReturn<T> = {
value: T;
error?: undefined;
} | {
value?: undefined;
error: SchemaTypeError;
};

// @public
export const validBodyOutput: readonly ["data", "stream"];

Expand Down

0 comments on commit 472a60f

Please sign in to comment.