import type { Context } from 'aws-lambda'; import Transaction from '../BaseEvent/Transaction.js'; /** * Represents an error response from an API. * @typedef {Object} ResponseErrorType * @property {string} err - The error message. * @property {string} [errCode] - The error code, if available. */ export type ResponseErrorType = { err: string; errCode?: string; }; /** * Represents a response object with various methods for building and manipulating the response. * @template BodyType - The type of the response body. */ export default class Response { /** * The private property that stores the status code. * @type {number} * @private */ private statusCode; /** * Private property representing the body of an object. * @type {any} * @private */ private body; /** * Indicates whether the object is currently piping out. * @type {boolean} */ private isPipingOut; /** * Private property that stores the headers as an object. */ private headers; /** * Determines whether streaming is enabled or not. * @returns {boolean} - True if streaming is enabled, false otherwise. */ readonly shouldStream: boolean; /** * Indicates whether the request body should be treated as raw data. * @type {boolean} */ readonly rawBody: boolean; /** * A boolean flag indicating whether to throw an error when encountering errors. * If set to true, any errors encountered will result in an exception being thrown. * If set to false, errors will be logged but the program will continue execution. */ readonly throwOnErrors: boolean; /** * Indicates whether the transaction ID is disabled. * @type {boolean} */ readonly disableTransactionID: boolean; /** * Constructs a new Response object with the given status code, body, and optional behavior. * @param {number} statusCode - The HTTP status code of the response. * @param {BodyType} body - The body of the response. * @param {Object} [optBehaviour] - Optional behavior configuration for the response. * @param {boolean} [optBehaviour.shouldStream] - Indicates whether the response should be streamed. * @param {boolean} [optBehaviour.rawBody] - Indicates whether the response body should be treated as raw data. * @param {boolean} [optBehaviour.throwOnErrors] - Indicates whether errors should be thrown for non-successful status codes. * @param {boolean} [optBehaviour */ constructor(statusCode: number, body: BodyType, optBehaviour?: { shouldStream?: boolean; rawBody?: boolean; throwOnErrors?: boolean; disableTransactionID?: boolean; } | undefined); /** * Get the status code of the response. * @returns {number} The status code. */ getCode(): number; /** * Get the body of the object. * @returns {BodyType} The body of the object. */ getBody(): BodyType; /** * Appends a key-value pair into the body object. * @param {string} key - The key to append. * @param {any} value - The value to append. * @returns None */ appendIntoBody(key: string, value: any): void; /** * Appends a header to the existing headers object. * @param {string} key - The key of the header. * @param {any} value - The value of the header. * @returns None */ appendHeader(key: string, value: any): void; /** * Builds the response for the given context and transaction. * @param {Context} context - The context object. * @param {Transaction} transaction - The transaction object. * @param {boolean} optDoNotCallContext - Optional flag to indicate whether to call the context or not. * @returns {Promise} - A promise that resolves when the response is built. */ build(context: Context, transaction: Transaction, optDoNotCallContext: boolean): Promise; /** * Private method that pipes the response to the given context. * @param {Context} context - The context object provided by AWS Lambda. * @returns None */ private pipe; /** * Private method that handles the raw context of a transaction. * @param {Context} context - The context object. * @param {Transaction} transaction - The transaction object. * @returns None */ private rawContext; /** * Generates a response object for a missing path parameter error. * @param {string} paramName - The name of the missing path parameter. * @returns {Response} - The response object with error details. */ static MissingParamResponse(paramName: string): Response; /** * Creates a response object for a missing query parameter error. * @param {string} paramName - The name of the missing query parameter. * @returns {Response} - The response object with error details. */ static MissingQueryResponse(paramName: string): Response; /** * Creates a BadRequestResponse object with the given parameters. * @param {string} [msg] - The error message. * @param {string} [errCode] - The error code. * @param {any} [optBody] - Optional additional body data. * @returns {Response} - The BadRequestResponse object. */ static BadRequestResponse(msg?: string, errCode?: string, optBody?: any): Response; /** * Creates a BadRequestResponse object with rollback option. * @param {string} msg - The error message. * @param {string} [errCode] - The error code. * @param {any} [optBody] - Optional body to include in the response. * @returns {Response} - The BadRequestResponse object. */ static BadRequestResponseWithRollback(msg: string, errCode?: string, optBody?: any): Response; /** * Creates an unauthorized response with the given error message and error code. * @param {string} msg - The error message. * @param {string} [errCode] - The error code (optional). * @returns {Response} - The unauthorized response. */ static UnauthorizedResponse(msg: string, errCode?: string): Response; /** * Creates a success response object with the given body. * @param {BodyType} body - The body of the response. * @returns {Response} - The success response object. */ static SuccessResponse(body: BodyType): Response; /** * Creates a redirect response with the specified URL. * @param {string} url - The URL to redirect to. * @returns {Response} - The redirect response. */ static RedirectResponse(url: string): Response; /** * Creates a success response with no content. * @returns {Response} A response object with a status code of 204 and no content. */ static SuccessNoContentResponse(): Response; /** * Creates a success response object with a streaming body and specified content type. * @param {any} stream - The stream object to be used as the response body. * @param {string} contentType - The content type of the response. * @returns {Response} - The success response object. */ static SuccessStreamResponse(stream: any, contentType: string): Response; /** * Creates a simple HTTP response with the given body and optional status code. * @param {BodyType} body - The body of the response. * @param {number} [optionalCode] - The optional status code of the response. Defaults to 200. * @returns {Response} - The created response object. */ static SimpleResponse(body: BodyType, optionalCode?: number): Response; }