import { Encoding } from 'node:crypto'; import { HttpJsonConfig } from './options/HttpConfig.js'; import { IncomingHttpEvent } from './IncomingHttpEvent.js'; import { CookieCollection } from './cookies/CookieCollection.js'; import { HeadersType, IOutgoingHttpResponse, CookieOptions } from './declarations.js'; import { IBlueprint, OutgoingResponse, OutgoingResponseOptions, IContainer } from '@stone-js/core'; /** * Options for creating an Outgoing HTTP Response. */ export interface OutgoingHttpResponseOptions extends OutgoingResponseOptions { headers?: HeadersType; } /** * Class representing an Outgoing HTTP Response. * Extends the OutgoingResponse class and provides additional features * such as setting headers, cookies, and interacting with IncomingHttpEvent. */ export declare class OutgoingHttpResponse extends OutgoingResponse implements IOutgoingHttpResponse { static OUTGOING_HTTP_RESPONSE: string; protected _charset?: Encoding; protected _formats?: Record unknown>; protected _incomingEventResolver?: () => IncomingHttpEvent; protected _blueprintResolver?: () => IBlueprint | undefined; protected readonly _headers: Headers; protected readonly _cookieCollection: CookieCollection; /** * Create an instance of OutgoingHttpResponse. * * @param options - Options for the outgoing HTTP response. * @returns A new instance of OutgoingHttpResponse. */ static create(options: OutgoingHttpResponseOptions): T; /** * Constructor for OutgoingHttpResponse. * Initializes headers and cookies based on the provided options. * * @param options - Options for the outgoing HTTP response. */ constructor(options: OutgoingHttpResponseOptions); /** * Get the HTTP status code. * * @returns The HTTP status code. */ get status(): number | undefined; /** * Get the headers of the response. * * @returns The headers of the response as a Headers object. */ get headers(): Headers; /** * Get the character set encoding. * Defaults to 'utf-8' if not explicitly set. * * @returns The character set encoding. */ get charset(): Encoding; /** * Get the ETag of the response. * * @returns The value of the ETag header, if present. */ get etag(): string | undefined; /** * Get the Vary header as an array of values. * * @returns The Vary header values split by comma, or undefined if not present. */ get vary(): string[] | undefined; /** * Get the Last-Modified date of the response. * * @returns The value of the Last-Modified header, if present. */ get lastModified(): string | undefined; /** * Get the associated IncomingHttpEvent. * * @throws InternalServerError if the IncomingHttpEvent resolver is not set. * @returns The associated IncomingHttpEvent. */ get incomingEvent(): IncomingHttpEvent; /** * Get the blueprint associated with the response. * * @returns The blueprint or undefined if not set. */ get blueprint(): IBlueprint | undefined; /** * Get the regular expression for matching charset in content type. * * @protected * @returns The regular expression for matching charset in content type. */ protected get charsetRegExp(): RegExp; /** * Set multiple headers for the response. * * @param values - A key-value pair of headers to be set. * @returns The current instance of OutgoingHttpResponse for chaining. */ setHeaders(values: HeadersType): this; /** * Set a single header for the response. * If the header is "Content-Type," ensures charset is set appropriately. * * @param key - The header name. * @param value - The value of the header. * @returns The current instance of OutgoingHttpResponse for chaining. */ setHeader(key: string, value: string | string[]): this; /** * Append a value to an existing header or create a new header. * * @param key - The header name. * @param value - The value to append. * @returns The current instance of OutgoingHttpResponse for chaining. */ appendHeader(key: string, value: string): this; /** * Get a header value. * * @param name - The header name. * @returns The header value or the fallback value. */ getHeader(name: string): TReturn | undefined; /** * Get a header value. * * @param name - The header name. * @param fallback - A fallback value if the header is not found. * @returns The header value or the fallback value. */ getHeader(name: string, fallback: TReturn): TReturn; /** * Get all header names. * * @returns An array of all header names. */ getHeaderNames(): string[]; /** * Check if a specific header exists. * * @param key - The header name to check. * @returns True if the header exists, false otherwise. */ hasHeader(key: string): boolean; /** * Remove headers from the response. * * @param key - The header or headers to be removed. * @returns The current instance of OutgoingHttpResponse for chaining. */ removeHeader(key: string | string[]): this; /** * Set the HTTP status code of the response. * Also sets a default status message if none is provided. * * @param code - The HTTP status code. * @param text - Optional status message. * @returns The current instance of OutgoingHttpResponse for chaining. * @throws InternalServerError if the status code is invalid. */ setStatus(code: number, text?: string): this; /** * Set the response content. * If the content should be JSON, it will be converted appropriately. * * @param value - The content to set. * @param options - The JSON options. * @returns The current instance of OutgoingHttpResponse for chaining. */ setContent(value: unknown, options?: Partial): this; /** * Set a cookie for the response. * * @param name - The name of the cookie. * @param value - The value of the cookie. * @param options - Optional settings for the cookie. * @returns The current instance of OutgoingHttpResponse for chaining. */ setCookie(name: string, value: unknown, options?: CookieOptions): this; /** * Clear a specific cookie from the response. * * @param name - The name of the cookie to be cleared. * @param options - Optional settings for the cookie. * @param force - Whether to force the removal of the cookie, even if it doesn't exist. * @returns The current instance of OutgoingHttpResponse for chaining. */ clearCookie(name: string, options?: CookieOptions, force?: boolean): this; /** * Clear all cookies from the response. * * @param force - Whether to force the removal of all cookies. * @returns The current instance of OutgoingHttpResponse for chaining. */ clearCookies(force?: boolean): this; /** * Secure all cookies by setting the "Secure" attribute. * * @param value - Whether to set or unset the "Secure" attribute for cookies. * @returns The current instance of OutgoingHttpResponse for chaining. */ secureCookies(value?: boolean): this; /** * Set the character set for the response. * * @param value - The character encoding to use. * @returns The current instance of OutgoingHttpResponse for chaining. */ setCharset(value: string): this; /** * Set the content type of the response. * * @param value - The MIME type for the response. * @returns The current instance of OutgoingHttpResponse for chaining. * @throws InternalServerError if the provided MIME type is invalid. */ setContentType(value: string): this; /** * Set the content type by file extension. * * @param value - The file extension. * @returns The current instance of OutgoingHttpResponse for chaining. */ setType(value: string): this; /** * Set link headers for the response. * * @param links - An object representing links to set. * @returns The current instance of OutgoingHttpResponse for chaining. */ setLinks(links: Record): this; /** * Handles content negotiation based on the `Accept` header of the incoming request. * * @param formats - An object where keys are MIME types and values are functions that return the content for that MIME type. * @returns The current instance of OutgoingHttpResponse for chaining. */ format(formats: Record unknown>): this; /** * Add a field to the Vary header. * * @param field - The field to add to the Vary header. * @returns The current instance of OutgoingHttpResponse for chaining. */ addVary(field: string | string[]): this; /** * Set the ETag for the response. * * @param etag - The ETag value to set. * @param weak - Whether the ETag should be marked as weak. * @returns The current instance of OutgoingHttpResponse for chaining. */ setEtag(etag?: string, weak?: boolean): this; /** * Set the Last-Modified header for the response. * * @param date - The date to set as the Last-Modified header. * @returns The current instance of OutgoingHttpResponse for chaining. */ setLastModified(date?: Date): this; /** * Set the resolver for the incoming HTTP event. * * @param resolver - A function that returns the incoming HTTP event. * @returns The current instance of OutgoingHttpResponse for chaining. */ setIncomingEventResolver(resolver: () => IncomingHttpEvent): this; /** * Set the resolver for the blueprint. * * @param resolver - A function that returns the blueprint. * @returns The current instance of OutgoingHttpResponse for chaining. */ setBlueprintResolver(resolver: () => IBlueprint | undefined): this; /** * Check if the status code falls within the specified range. * * @param start - The starting value of the range (inclusive). * @param end - The ending value of the range (exclusive). * @returns True if the status code is within the specified range, otherwise false. */ isInStatusRange(start: number, end: number): boolean; /** * Check if the status code is invalid. * * @returns True if the status code is invalid, otherwise false. */ isInvalid(): boolean; /** * Check if the status code represents an informational response (1xx). * * @returns True if the status code is informational, otherwise false. */ is1xx(): boolean; /** * Check if the status code represents a successful response (2xx). * * @returns True if the status code is successful, otherwise false. */ is2xx(): boolean; /** * Check if the status code represents a redirection response (3xx). * * @returns True if the status code is a redirection, otherwise false. */ is3xx(): boolean; /** * Check if the status code represents a client error response (4xx). * * @returns True if the status code is a client error, otherwise false. */ is4xx(): boolean; /** * Check if the status code represents a server error response (5xx). * * @returns True if the status code is a server error, otherwise false. */ is5xx(): boolean; /** * Check if the status code is not an error (i.e., not 4xx or 5xx). * * @returns True if the status code is not an error, otherwise false. */ isNotError(): boolean; /** * Check if the status code is an error (i.e., 4xx or 5xx). * * @returns True if the status code is an error, otherwise false. */ isError(): boolean; /** * Check if the status code is 200 (OK). * * @returns True if the status code is 200, otherwise false. */ isOk(): boolean; /** * Check if the status code is 205 (Reset Content). * * @returns True if the status code is 205, otherwise false. */ isResetContent(): boolean; /** * Check if the response is empty. * * @returns True if the status code indicates an empty response, otherwise false. */ isEmpty(): boolean; /** * Check if the response is a redirect. * * @param location - The optional location to check for redirection. * @returns True if the status code indicates a redirect, otherwise false. */ isRedirect(location?: string): boolean; /** * Check if the status code is 301 (Moved Permanently). * * @returns True if the status code is 301, otherwise false. */ isMovedPermanently(): boolean; /** * Check if the status code is 401 (Unauthorized). * * @returns True if the status code is 401, otherwise false. */ isUnauthorized(): boolean; /** * Check if the status code is 403 (Forbidden). * * @returns True if the status code is 403, otherwise false. */ isForbidden(): boolean; /** * Check if the status code is 404 (Not Found). * * @returns True if the status code is 404, otherwise false. */ isNotFound(): boolean; /** * Check if the response is validateable. * * @returns True if the response has Last-Modified or ETag headers, otherwise false. */ isValidateable(): boolean; /** * Prepare the response before sending. * * @param event - The incoming HTTP event. * @param container - The service container. * @returns The current instance of the response for chaining. */ prepare(event: IncomingHttpEvent, container?: IContainer): this | Promise; /** * Handles content negotiation based on the `Accept` header of the incoming request. * * @returns The current instance of OutgoingHttpResponse for chaining. */ protected handleContentNegotiation(): this; /** * Set the content type if it's not already set. * * @returns The current instance of the response for chaining. */ protected setContentTypeIfNeeded(): this; /** * Handle cache headers like ETag and Last-Modified. * * @returns The current instance of the response for chaining. */ protected handleCacheHeaders(): this; /** * Prepare content-related headers such as Content-Length and ETag. * * @returns The current instance of the response for chaining. */ protected prepareContentHeaders(): this; /** * Set content headers such as Content-Length and ETag. * * @returns The current instance of the response for chaining. */ protected setContentHeaders(): this; /** * Calculate the content length. * * @returns The content length. */ protected calculateContentLength(): number; /** * Ensure that the "Content-Type" header has a charset specified. * * @param value - The "Content-Type" header value. * @returns The current instance of OutgoingHttpResponse for chaining. */ protected ensureCharset(value: string): this; /** * Determine if the content should be serialized as JSON. * * @param content - The content to check. * @returns True if the content should be serialized as JSON, otherwise false. */ protected shouldBeJson(content: unknown): boolean; /** * Convert the given content to a JSON string. * * @param content - The content to convert. * @param options - Options to customize the serialization process. * @returns A JSON string representation of the content. * @throws InternalServerError if the content cannot be converted to JSON. */ protected morphToJson(content: unknown, options?: Partial): string; /** * Prepare cookies by setting the appropriate headers. * * @returns The current instance of the response for chaining. */ protected prepareCookies(): this; /** * Generate a default ETag for the given content. * * @param content - The content to generate an ETag for. * @param encoding - The encoding to use. * @returns The generated ETag as a base64 string. */ protected defaultEtagFn(content: string, encoding: Encoding): string; /** * Get the hashed content using the specified encoding. * * @param content - The content to hash. * @param encoding - The encoding to use for hashing. * @returns The hashed content as a hexadecimal string. */ protected getHashedContent(content: string, encoding: Encoding): string; /** * Convert the given value to a JSON string with optional escaping. * * @param value - The value to convert. * @param replacer - A function or array that alters the behavior of the stringification process. * @param spaces - The number of spaces to use for pretty-printing the JSON string. * @param escape - Whether to escape special characters. * @returns The JSON string representation of the value. */ protected stringify(value: unknown, replacer?: HttpJsonConfig['replacer'], spaces?: string, escape?: boolean): string; }