kibana/docs/development/core/server/kibana-plugin-server.requesthandler.md

1.5 KiB

Home > kibana-plugin-server > RequestHandler

RequestHandler type

A function executed when route path matched requested resource path. Request handler is expected to return a result of one of KibanaResponseFactory functions.

Signature:

export declare type RequestHandler<P extends ObjectType, Q extends ObjectType, B extends ObjectType> = (context: RequestHandlerContext, request: KibanaRequest<TypeOf<P>, TypeOf<Q>, TypeOf<B>>, response: KibanaResponseFactory) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;

Example

const router = httpSetup.createRouter();
// creates a route handler for GET request on 'my-app/path/{id}' path
router.get(
  {
    path: 'path/{id}',
    // defines a validation schema for a named segment of the route path
    validate: {
      params: schema.object({
        id: schema.string(),
      }),
    },
  },
  // function to execute to create a responses
  async (context, request, response) => {
    const data = await context.findObject(request.params.id);
    // creates a command to respond with 'not found' error
    if (!data) return response.notFound();
    // creates a command to send found data to the client
    return response.ok(data);
  }
);