Skip to content

Latest commit

 

History

History
118 lines (95 loc) · 3.97 KB

kibana-plugin-core-server.kibanaresponsefactory.md

File metadata and controls

118 lines (95 loc) · 3.97 KB

Home > kibana-plugin-core-server > kibanaResponseFactory

kibanaResponseFactory variable

Set of helpers used to create KibanaResponse to form HTTP response on an incoming request. Should be returned as a result of RequestHandler execution.

Signature:

kibanaResponseFactory: {
    custom: <T extends string | Record<string, any> | Buffer | Error | Stream | {
        message: string | Error;
        attributes?: Record<string, any> | undefined;
    } | undefined>(options: CustomHttpResponseOptions<T>) => KibanaResponse<T>;
    badRequest: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
    unauthorized: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
    forbidden: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
    notFound: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
    conflict: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
    internalError: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
    customError: (options: CustomHttpResponseOptions<ResponseError>) => KibanaResponse<ResponseError>;
    redirected: (options: RedirectResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
    ok: (options?: HttpResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
    accepted: (options?: HttpResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
    noContent: (options?: HttpResponseOptions) => KibanaResponse<undefined>;
}

Example

  1. Successful response. Supported types of response body are: - undefined, no content to send. - string, send text - JSON, send JSON object, HTTP server will throw if given object is not valid (has circular references, for example) - Stream send data stream - Buffer send binary stream
return response.ok();
return response.ok({ body: 'ack' });
return response.ok({ body: { id: '1' } });
return response.ok({ body: Buffer.from(...) });

const stream = new Stream.PassThrough();
fs.createReadStream('./file').pipe(stream);
return res.ok({ body: stream });

HTTP headers are configurable via response factory parameter options HttpResponseOptions.

return response.ok({
  body: { id: '1' },
  headers: {
    'content-type': 'application/json'
  }
});
  1. Redirection response. Redirection URL is configures via 'Location' header.
return response.redirected({
  body: 'The document has moved',
  headers: {
   location: '/new-url',
  },
});
  1. Error response. You may pass an error message to the client, where error message can be: - string send message text - Error send the message text of given Error object. - { message: string | Error, attributes: {data: Record<string, any>, ...} } - send message text and attach additional error data.
return response.unauthorized({
  body: 'User has no access to the requested resource.',
  headers: {
    'WWW-Authenticate': 'challenge',
  }
})
return response.badRequest();
return response.badRequest({ body: 'validation error' });

try {
  // ...
} catch(error){
  return response.badRequest({ body: error });
}

return response.badRequest({
 body:{
   message: 'validation error',
   attributes: {
     requestBody: request.body,
     failedFields: validationResult
   }
 }
});

try {
  // ...
} catch(error) {
  return response.badRequest({
    body: error
  });
}
  1. Custom response. ResponseFactory may not cover your use case, so you can use the custom function to customize the response.
return response.custom({
  body: 'ok',
  statusCode: 201,
  headers: {
    location: '/created-url'
  }
})