import { Stream } from 'stream' ; import { ResponseHeaders } from './headers' ; public export type ResponseErrorAttributes = Record < string , any > ; public export type ResponseError = string | Error | { message : string | Error ; attributes ? : ResponseErrorAttributes ; } ; /**
 * A response data object, expected to returned as a result of  RequestHandler execution
 *  public export interface IOpenSearchDashboardsResponse < T extends HttpResponsePayload | ResponseError = any > { readonly status : number ; readonly payload ? : T ; readonly options : HttpResponseOptions ; } export function isOpenSearchDashboardsResponse ( response ) { return typeof response . status === 'number' && typeof response . options === 'object' ; } /**
 * A response data object, expected to returned as a result of  RequestHandler execution
 *  internal export class OpenSearchDashboardsResponse < T extends HttpResponsePayload | ResponseError = any > implements IOpenSearchDashboardsResponse < T > { constructor ( public readonly status , public readonly payload ? , public readonly options = { } ) { } } public export interface HttpResponseOptions { /** HTTP message to send to the client */ body ? : HttpResponsePayload ; /** HTTP Headers with additional information about response */ headers ? : ResponseHeaders ; } public export type HttpResponsePayload = undefined | string | Record < string , any > | Buffer | Stream ; public export interface CustomHttpResponseOptions < T extends HttpResponsePayload | ResponseError > { /** HTTP message to send to the client */ body ? : T ; /** HTTP Headers with additional information about response */ headers ? : ResponseHeaders ; statusCode : number ; } public export type RedirectResponseOptions = HttpResponseOptions & { headers : { location : string ; } ; } ; public export interface ErrorHttpResponseOptions { /** HTTP message to send to the client */ body ? : ResponseError ; /** HTTP Headers with additional information about response */ headers ? : ResponseHeaders ; } const successResponseFactory = { param options -  HttpResponseOptions configures HTTP response body & headers.
      ok : ( options = { } ) => new OpenSearchDashboardsResponse ( 200 , options . body , options ) , param options -  HttpResponseOptions configures HTTP response body & headers.
      accepted : ( options = { } ) => new OpenSearchDashboardsResponse ( 202 , options . body , options ) , param options -  HttpResponseOptions configures HTTP response body & headers.
      noContent : ( options = { } ) => new OpenSearchDashboardsResponse ( 204 , undefined , options ) , } ; const redirectionResponseFactory = { param options -  RedirectResponseOptions configures HTTP response body & headers.
     * Expects `location` header to be set.
      redirected : ( options ) => new OpenSearchDashboardsResponse ( 302 , options . body , options ) , } ; const errorResponseFactory = { param options -  HttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      badRequest : ( options = { } ) => new OpenSearchDashboardsResponse ( 400 , options . body || 'Bad Request' , options ) , param options -  HttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      unauthorized : ( options = { } ) => new OpenSearchDashboardsResponse ( 401 , options . body || 'Unauthorized' , options ) , param options -  HttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      forbidden : ( options = { } ) => new OpenSearchDashboardsResponse ( 403 , options . body || 'Forbidden' , options ) , param options -  HttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      notFound : ( options = { } ) => new OpenSearchDashboardsResponse ( 404 , options . body || 'Not Found' , options ) , param options -  HttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      conflict : ( options = { } ) => new OpenSearchDashboardsResponse ( 409 , options . body || 'Conflict' , options ) , param options -  HttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      internalError : ( options = { } ) => new OpenSearchDashboardsResponse ( 500 , options . body || 'Internal Error' , options ) , param options -  CustomHttpResponseOptions configures HTTP response headers, error message and other error details to pass to the client
      customError : ( options ) => { if ( ! options || ! options . statusCode ) { throw new Error ( `options.statusCode is expected to be set. given options: ${ options && options . statusCode }` ) ; } if ( options . statusCode < 400 || options . statusCode >= 600 ) { throw new Error ( `Unexpected Http status code. Expected from 400 to 599, but given: ${ options . statusCode }` ) ; } return new OpenSearchDashboardsResponse ( options . statusCode , options . body , options ) ; } , } ; /**
 * Set of helpers used to create `OpenSearchDashboardsResponse` to form HTTP response on an incoming request.
 * Should be returned as a result of  RequestHandler execution.
 *
 *  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
 * ```js
 * 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 .
 *
 * ```js
 * return response.ok({
 *   body: { id: '1' },
 *   headers: {
 *     'content-type': 'application/json'
 *   }
 * });
 * ```
 * 2. Redirection response. Redirection URL is configures via 'Location' header.
 * ```js
 * return response.redirected({
 *   body: 'The document has moved',
 *   headers: {
 *    location: '/new-url',
 *   },
 * });
 * ```
 * 3. 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.
 * ```js
 * 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
 *   });
 * }
 *
 * ```
 * 4. Custom response. `ResponseFactory` may not cover your use case, so you can use the `custom` function to customize the response.
 * ```js
 * return response.custom({
 *   body: 'ok',
 *   statusCode: 201,
 *   headers: {
 *     location: '/created-url'
 *   }
 * })
 * ```
 *  public export const opensearchDashboardsResponseFactory = { ... successResponseFactory , ... redirectionResponseFactory , ... errorResponseFactory , param options -  CustomHttpResponseOptions configures HTTP response parameters.
      custom : < T extends HttpResponsePayload | ResponseError > ( options ) => { if ( ! options || ! options . statusCode ) { throw new Error ( `options.statusCode is expected to be set. given options: ${ options && options . statusCode }` ) ; } const { statusCode : code , body , ... rest } = options ; return new OpenSearchDashboardsResponse ( code , body , rest ) ; } , } ; export const lifecycleResponseFactory = { ... redirectionResponseFactory , ... errorResponseFactory , } ; public export type OpenSearchDashboardsResponseFactory = typeof opensearchDashboardsResponseFactory ; public export type LifecycleResponseFactory = typeof lifecycleResponseFactory ;